
function setBigButtons() {
	var sPath = window.location.pathname;
	var sPage = sPath.substring(sPath);

	if (sPage == "\/quotes\/" || sPage == "\/quotes\/easy" || sPage == "\/quotes\/advanced" || sPage == "\/quotes\/show" || sPage == "\/quotes\/thanks"){
		elem = document.getElementById("printquoteRef");
		elem.style.backgroundPosition = "0 -56px";
	}
	if (sPage == "\/shop\/"){
		elem = document.getElementById("priceguideRef");
		elem.style.backgroundPosition = "0 -56px";
	}
	if (sPage == "\/about\/five_ways" || sPage == "\/about\/call_us" || sPage == "\/about\/fax_us" || sPage == "\/about\/walk_in" || sPage == "\/about\/purchase_order"){
		elem = document.getElementById("5waysRef");
		elem.style.backgroundPosition = "0 -56px";
	}

}

function echeck(str) {

		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)
		if (str.indexOf(at)==-1){
		   alert("Invalid email address")
		   return false
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid email address")
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid email address")
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid email address")
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid email address")
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid email address")
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid email address")
		    return false
		 }

 		 return true					
	}

function ValidateForm(){
	var emailID=document.sendInvite.email
	
	if ((emailID.value==null)||(emailID.value=="")){
		alert("Please Enter your Email Address")
		emailID.focus()
		return false
	}
	if (echeck(emailID.value)==false){
		emailID.value=""
		emailID.focus()
		return false
	}
	return true
 }

/*
var prevHeight = 0;
setTimeout("var test = document.getElementById('subnav' + sideNavSelected);var prevHeight = test.clientHeight; alert(prevHeight)", 1.5);
*/

/* dependent on variable sideNavSelected, set during page load */


var boxClosing = false;
/* dependent on variable openInfoBox, set during page load */


// debug helper
if(typeof(console) !== "undefined"){
	debug = console.log;
}

function infobox(i)
{
    if (!boxClosing)
    {
        boxClosing = true;

        if(!browser.isIE6x){
        setTimeout("boxClosing = false", 400);
        }
        else{
        boxClosing = false;
        }


        var elem1 = "infobox-img" + openInfoBox;

        var oldText = document.getElementById("infoboxText" + openInfoBox);
        var imgSrc = oldText.src;

        imgSrc = imgSrc.substring(0, imgSrc.lastIndexOf("_selected.gif")) + ".gif";
        oldText.src = imgSrc;

        var newText = document.getElementById("infoboxText" + i);

        imgSrc = newText.src;
        imgSrc = imgSrc.substring(0, imgSrc.lastIndexOf(".gif")) + "_selected.gif";
        newText.src = imgSrc;

        var attributes = {
            height: { to: 0 }
        };

		if(!browser.isIE6x){
	        var anim1 = new YAHOO.util.Anim(elem1, attributes, .2, YAHOO.util.Easing.easeOut);
	        anim1.animate();
		}
		else{
			jQuery('#'+elem1+'').css('display','none');
		}


        var elem = "infobox-img" + i;
        attributes = {
            height: { to: 234 }
        };

   		if(!browser.isIE6x){
        var anim2 = new YAHOO.util.Anim(elem, attributes, .6, YAHOO.util.Easing.easeIn);
        anim2.animate();
        }
        else{
			jQuery('#'+elem+'').css('display','block');
        }

        openInfoBox = i;

    }
}
function fivewaybox(i)
{
    if (!boxClosing)
    {
        boxClosing = true;
        setTimeout("boxClosing = false;", 600);

        var elem1 = "infobox-img" + openInfoBox;

        var oldText = document.getElementById("infoboxText" + openInfoBox);
        var imgSrc = oldText.src;
        imgSrc = imgSrc.substring(0, imgSrc.lastIndexOf("selected.gif")) + ".gif";
        oldText.src = imgSrc;

        var newText = document.getElementById("infoboxText" + i);
        imgSrc = newText.src;
        imgSrc = imgSrc.substring(0, imgSrc.lastIndexOf(".gif")) + "selected.gif";
        newText.src = imgSrc;

        var attributes = {
            height: { to: 0 }
        };
		if(!browser.isIE6x){
	        var anim1 = new YAHOO.util.Anim(elem1, attributes, .2, YAHOO.util.Easing.easeOut);
	        anim1.animate();
		}
		else{
			/* Fix IE */
	    	jQuery(elem1).css('display','none');
		}

        var elem = "infobox-img" + i;
        attributes = {
            height: { to: 231 }
        };
   		if(!browser.isIE6x){
        var anim2 = new YAHOO.util.Anim(elem, attributes, .6, YAHOO.util.Easing.easeIn);
        anim2.animate();
        }
        else{
			jQuery(elem1).css('display','none');
        }

        openInfoBox = i;
    }
}

function BrowserDetect() {
   var ua = navigator.userAgent.toLowerCase();

   // browser engine name
   this.isGecko       = (ua.indexOf('gecko') != -1 && ua.indexOf('safari') == -1);
   this.isAppleWebKit = (ua.indexOf('applewebkit') != -1);

   // browser name
   this.isKonqueror   = (ua.indexOf('konqueror') != -1);
   this.isSafari      = (ua.indexOf('safari') != - 1);
   this.isOmniweb     = (ua.indexOf('omniweb') != - 1);
   this.isOpera       = (ua.indexOf('opera') != -1);
   this.isIcab        = (ua.indexOf('icab') != -1);
   this.isAol         = (ua.indexOf('aol') != -1);
   this.isIE          = (ua.indexOf('msie') != -1 && !this.isOpera && (ua.indexOf('webtv') == -1) );
   this.isMozilla     = (this.isGecko && ua.indexOf('gecko/') + 14 == ua.length);
   this.isFirefox     = (ua.indexOf('firefox/') != -1 || ua.indexOf('firebird/') != -1);
   this.isNS          = ( (this.isGecko) ? (ua.indexOf('netscape') != -1) : ( (ua.indexOf('mozilla') != -1) && !this.isOpera && !this.isSafari && (ua.indexOf('spoofer') == -1) && (ua.indexOf('compatible') == -1) && (ua.indexOf('webtv') == -1) && (ua.indexOf('hotjava') == -1) ) );

   // spoofing and compatible browsers
   this.isIECompatible = ( (ua.indexOf('msie') != -1) && !this.isIE);
   this.isNSCompatible = ( (ua.indexOf('mozilla') != -1) && !this.isNS && !this.isMozilla);

   // rendering engine versions
   this.geckoVersion = ( (this.isGecko) ? ua.substring( (ua.lastIndexOf('gecko/') + 6), (ua.lastIndexOf('gecko/') + 14) ) : -1 );
   this.equivalentMozilla = ( (this.isGecko) ? parseFloat( ua.substring( ua.indexOf('rv:') + 3 ) ) : -1 );
   this.appleWebKitVersion = ( (this.isAppleWebKit) ? parseFloat( ua.substring( ua.indexOf('applewebkit/') + 12) ) : -1 );

   // browser version
   this.versionMinor = parseFloat(navigator.appVersion);

   // correct version number
   if (this.isGecko && !this.isMozilla) {
      this.versionMinor = parseFloat( ua.substring( ua.indexOf('/', ua.indexOf('gecko/') + 6) + 1 ) );
   }
   else if (this.isMozilla) {
      this.versionMinor = parseFloat( ua.substring( ua.indexOf('rv:') + 3 ) );
   }
   else if (this.isIE && this.versionMinor >= 4) {
      this.versionMinor = parseFloat( ua.substring( ua.indexOf('msie ') + 5 ) );
   }
   else if (this.isKonqueror) {
      this.versionMinor = parseFloat( ua.substring( ua.indexOf('konqueror/') + 10 ) );
   }
   else if (this.isSafari) {
      this.versionMinor = parseFloat( ua.substring( ua.lastIndexOf('safari/') + 7 ) );
   }
   else if (this.isOmniweb) {
      this.versionMinor = parseFloat( ua.substring( ua.lastIndexOf('omniweb/') + 8 ) );
   }
   else if (this.isOpera) {
      this.versionMinor = parseFloat( ua.substring( ua.indexOf('opera') + 6 ) );
   }
   else if (this.isIcab) {
      this.versionMinor = parseFloat( ua.substring( ua.indexOf('icab') + 5 ) );
   }

   this.versionMajor = parseInt(this.versionMinor);

   // dom support
   this.isDOM1 = (document.getElementById);
   this.isDOM2Event = (document.addEventListener && document.removeEventListener);

   // css compatibility mode
   this.mode = document.compatMode ? document.compatMode : 'BackCompat';

   // platform
   this.isWin    = (ua.indexOf('win') != -1);
   this.isWin32  = (this.isWin && ( ua.indexOf('95') != -1 || ua.indexOf('98') != -1 || ua.indexOf('nt') != -1 || ua.indexOf('win32') != -1 || ua.indexOf('32bit') != -1 || ua.indexOf('xp') != -1) );
   this.isMac    = (ua.indexOf('mac') != -1);
   this.isUnix   = (ua.indexOf('unix') != -1 || ua.indexOf('sunos') != -1 || ua.indexOf('bsd') != -1 || ua.indexOf('x11') != -1)
   this.isLinux  = (ua.indexOf('linux') != -1);

   // specific browser shortcuts
   this.isNS4x = (this.isNS && this.versionMajor == 4);
   this.isNS40x = (this.isNS4x && this.versionMinor < 4.5);
   this.isNS47x = (this.isNS4x && this.versionMinor >= 4.7);
   this.isNS4up = (this.isNS && this.versionMinor >= 4);
   this.isNS6x = (this.isNS && this.versionMajor == 6);
   this.isNS6up = (this.isNS && this.versionMajor >= 6);
   this.isNS7x = (this.isNS && this.versionMajor == 7);
   this.isNS7up = (this.isNS && this.versionMajor >= 7);

   this.isIE4x = (this.isIE && this.versionMajor == 4);
   this.isIE4up = (this.isIE && this.versionMajor >= 4);
   this.isIE5x = (this.isIE && this.versionMajor == 5);
   this.isIE55 = (this.isIE && this.versionMinor == 5.5);
   this.isIE5up = (this.isIE && this.versionMajor >= 5);
   this.isIE6x = (this.isIE && this.versionMajor == 6);
   this.isIE6up = (this.isIE && this.versionMajor >= 6);

   this.isIE4xMac = (this.isIE4x && this.isMac);
}

var browser = new BrowserDetect();

function setButtons(container) {
	elementRef = 'a.var-button'
	if (container) {
		elementRef = container + ' a.var-button'
	}


	if (!jQuery(elementRef + " > span").text()){
		jQuery(elementRef).wrapInner('<span></span>').addClass('var-button-replace');
	}

}


function overlaySetup() {
	var flashSource = 'SiFR/myriad-pro.swf';
	sIFR.replaceElement(named({sSelector:"div.overlay h2", sFlashSrc:flashSource, sColor:"#00246B",sBgColor:"#FFFFFF", sFlashVars:"textalign=left&offsetTop=0"}));
	sIFR.replaceElement(named({sSelector:"div.overlay ul#overlay-process li.selected span", sFlashSrc:flashSource, sColor:"#B50029",sLinkColor:"#B50029",sBgColor:"#FFFFFF", sFlashVars:"offsetTop=0"}));
	sIFR.replaceElement(named({sSelector:"div.overlay ul#overlay-process li span", sFlashSrc:flashSource, sColor:"#737173",sLinkColor:"#737173",sBgColor:"#FFFFFF", sFlashVars:"offsetTop=0"}));
	alert('test/257');
	jQuery('div.overlay a[rel*=facebox]').facebox();
}

jQuery(document).ready(function(){
	setButtons();
	if (typeof(jQuery.facebox) == 'function' && jQuery('#facebox .content').length == 0) {
		jQuery('a[rel*=facebox]').facebox()
	}

});

function switchUploaderState(toState) {
	if(toState == 'fail') {
		jQuery('div#uploader-uploading').removeClass('current');
		jQuery('div#uploader-failed').addClass('current');
	}
	else {
		jQuery('div#uploader-failed').removeClass('current');
		jQuery('div#uploader-uploading').addClass('current');
		if(toState == 'complete') {
			jQuery('div#uploader-uploading').removeClass('transfer-in-progress');
			jQuery('div#uploader-uploading').addClass('transfer-complete');
		}
		else {//must be in progress
			jQuery('div#uploader-uploading').removeClass('transfer-complete');
			jQuery('div#uploader-uploading').addClass('transfer-in-progress');
		}
	}
}


function loadgreyacc(newItemID){

	hidegreycc(itemSelected);
	showgreycc(newItemID);

	this.itemSelected = newItemID;

}

function showgreycc(id){
	var currentRadio = document.getElementById(id + '-radio');
    var arrowImg = document.getElementById(id + '-arrow');

	currentRadio.checked = true;

	arrowImg.src = "/images/arrowGrey_selected.gif";

	var currentBody = document.getElementById(id);

	currentBody.style.height = "auto";
	var height = currentBody.clientHeight;
	currentBody.style.height = "0";

	var attributes = {
	    height: { to: height }
	};
	var anim = new YAHOO.util.Anim(id, attributes, .5, YAHOO.util.Easing.easeOut);
	anim.animate();
}

function hidegreycc(id){

    var arrowImg = document.getElementById(id + '-arrow');
	arrowImg.src = "/images/arrowGrey_deselected.gif";

	var attributes = {
	    height: { to: 0 }
	};
	var anim = new YAHOO.util.Anim(id, attributes, .5, YAHOO.util.Easing.easeOut);
	anim.animate();
}

function load_checkbox_animatedDiv(newItemID){

	var currentRadio = document.getElementById(newItemID + '-radio');
	if(currentRadio.checked == true){
	show_checkbox_animatedDiv(newItemID);
	}
	else{
	hide_checkbox_animatedDiv(newItemID);
	}

return true;
}

function load_checkbox_animatedDivv2(e,newItemID){

	YAHOO.util.Event.preventDefault(e);

	var currentRadio = document.getElementById(newItemID + '-radio');
	if(currentRadio.checked == true){
	show_checkbox_animatedDiv(newItemID);
	}
	else{
	hide_checkbox_animatedDiv(newItemID);
	}

return true;
}


function load_checkbox_animatedDiv_img(newItemID){
	var currentRadio = document.getElementById(newItemID + '-radio');
	var arrowImg = document.getElementById(newItemID + '-arrow');

	//arrowImg.src = "/images/arrowGrey_selected.gif"; // /images/arrowGrey_deselected.gif
	var imgSrc = arrowImg.src;
	var imgSrc = imgSrc.substring(imgSrc.lastIndexOf("_selected.gif"), imgSrc.length);

	if(imgSrc !== "_selected.gif"){
		show_checkbox_animatedDiv(newItemID);
		currentRadio.checked = true;
	}
	else{
		hide_checkbox_animatedDiv(newItemID);
		currentRadio.checked = false;
	}
	return false;
}


function show_checkbox_animatedDiv(id){
	var currentRadio = document.getElementById(id + '-radio');
    var arrowImg = document.getElementById(id + '-arrow');

	arrowImg.src = "/images/arrowGrey_selected.gif";

	var currentBody = document.getElementById(id);

	currentBody.style.height = "auto";
	var height = currentBody.clientHeight;
	currentBody.style.height = "0";

	var attributes = {
	    height: { to: height }
	};

	var anim = new YAHOO.util.Anim(id, attributes, .5, YAHOO.util.Easing.easeOut);
	anim.animate();


	return true;
}

function hide_checkbox_animatedDiv(id){

    var arrowImg = document.getElementById(id + '-arrow');
	arrowImg.src = "/images/arrowGrey_deselected.gif";

	var attributes = {
	    height: { to: 0 }
	};

	var anim = new YAHOO.util.Anim(id, attributes, .5, YAHOO.util.Easing.easeOut);
	anim.animate();
	return true;
}


/* Replacement for infobox */

function infobox2_in(object){

	var old_object_num = object_num;
	var text_image_src = jQuery(object).attr('src');
	var text_image_id = jQuery(object).attr('id');
	var object_num = text_image_id.substring(text_image_id.length,text_image_id.length - 1);
	var text_image_src_new = text_image_src.substring(0, text_image_src.lastIndexOf(".gif")) + "_selected.gif";
	jQuery('#infoboxDiv' + object_num + '').fadeIn('slow');
	jQuery(object).attr('src',text_image_src_new);
	finished = object_num;

}

function infobox2_out(object){
	var text_image_src = jQuery(object).attr('src');
	var text_image_id = jQuery(object).attr('id');
	var object_num = text_image_id.substring(text_image_id.length,text_image_id.length - 1);
	var text_image_src_new = text_image_src.substring(0, text_image_src.lastIndexOf("_selected.gif")) + ".gif";

	if(finished !== object_num){
	jQuery('#infoboxDiv' + object_num + '').fadeOut('fast');
	}
	else{
	jQuery('#infoboxDiv' + object_num + '').hide();
	}
	jQuery(object).attr('src',text_image_src_new);

}


function sideNav(id)
{
    if (sideNavSelected != id)
    {
        var subnavBefore = document.getElementById("subnav" + sideNavSelected);

        var subnav = document.getElementById("subnav" + id);

        var origheight = subnav.clientHeight;

       	subnav.style.position = "static";

        subnav.style.height = "auto";
      var height = subnav.clientHeight;
        subnav.style.height = height + "px";

        var attributes = {
            height: { to: 0 }
        };

        var anim = new YAHOO.util.Anim("subnav" + sideNavSelected, attributes, .5, YAHOO.util.Easing.easeOut);
        anim.animate();


        if(browser.isIE)
        {
            subnavBefore.style.marginTop = "-14px";
        }

        setTimeout("closeSidenav(" + sideNavSelected + "," + height + ");", 0);


	    var height = subnav.clientHeight;
	    subnav.style.height = "0";

	    subnav.style.visibility = "visible";


        var attributes = {
            height: { to: height }
        };

        var anim1 = new YAHOO.util.Anim("subnav" + id, attributes, .5, YAHOO.util.Easing.easeIn);
        anim1.animate();

       // subnav.parentNode.parentNode.parentNode.className = "sidenav1selected";
        if(browser.isIE)
        {
            subnav.previousSibling.className = "sidenav1selected";
        }
        else
        {
            subnav.previousSibling.previousSibling.className = "sidenav1selected";
        }
        var arrow = document.getElementById("nav-" + id);
        arrow.src = "/images/nav-rs.gif";

        if(browser.isIE)
        {
            subnav.style.marginTop = "0px";
        }

        sideNavSelected = id;
    }

}
function closeSidenav(sideNavSelected, origheight)
{
    var subnavBefore = document.getElementById("subnav" + sideNavSelected);
    //subnavBefore.style.position = 'absolute';
    /*if(browser.isIE)
    {
        subnavBefore.style.display = "none";
    }
    */
    /*
    subnavBefore.style.position = "absolute";
    subnavBefore.style.visibility = "hidden";
    /*subnavBefore.style.height = origheight + "px";
    */

    if(browser.isIE)
    {
        subnavBefore.previousSibling.className = "sidenav1";
        /*
        subnavBefore.style.height = "auto";
        subnavBefore.style.visibility = "hidden";
        */
        //alert("height is " +  subnavBefore.style.height );
    }
    else
    {
        subnavBefore.previousSibling.previousSibling.className = "sidenav1";
    }
    var arrow = document.getElementById("nav-" + sideNavSelected);
    arrow.src = "/images/nav-r.gif";

}

function navDown(id)
{
     if(id != sideNavSelected)
     {
         var img = document.getElementById("nav-" + id);
         img.src = "/images/nav-d.gif";
     }
}
function navIn(id)
{
//    if(id != sideNavSelected)
//    {
//        var img = document.getElementById("nav-" + id);
//        img.src = "/images/nav-r.gif";
//    }
}


/*
	Replaces <input type=file> with a custom styled element.
	only replaces when inside of a element with the class of fileinputs and called to initiate after the DOM has loaded.
	Only when InputFileReplace is called
*/

  var W3CDOM = (document.createElement && document.getElementsByTagName);

	function InputFileReplace() {
		if (!W3CDOM) return;
		var fakeFileUpload = document.createElement('div');
		fakeFileUpload.className = 'fakefile';
		fakeFileUpload.appendChild(document.createElement('input'));
		var image = document.createElement('img');
		image.src='/images/attach.gif';
		fakeFileUpload.appendChild(image);
		var x = document.getElementsByTagName('input');
		for (var i=0;i<x.length;i++) {
			if (x[i].type != 'file') continue;
			if (x[i].getAttribute('noscript')) continue;
			if (x[i].parentNode.className != 'fileinputs') continue;
			x[i].className = 'file hidden';
			var clone = fakeFileUpload.cloneNode(true);
			x[i].parentNode.appendChild(clone);
			x[i].relatedElement = clone.getElementsByTagName('input')[0];
			if (x[i].value)
				x[i].onchange();
			x[i].onchange = x[i].onmouseout = function () {
				this.relatedElement.value = this.value;
			}
		}
	}


window.onload = function () {
    jQuery('.cc-payment-content').css('height','auto');
}



/* START JAVASCRIPT TO LOAD WHEN OVERLAYS ARE FINISHED LOADING (NEW DYNAMIC CONTENT) */
jQuery.fn.onOverlayReady = function(){


/*
 * More flexable version of main "content slider", use this one from now on
 * when applying this effect and style... make copy of markup and apply
 * contentSlider() via jQuery function.
*/

	// Main contentSlider initialization function
	jQuery.fn.contentSlider = function(){
		return this.each(function(){ // For applying to multiple jQuery objects
			var jQuerythis = jQuery(this); // Shortcut to jQuery's this instead of javascript this

			jQuerythis.click(function(){

				// Setup readable / frequently used variables

				var baseReference = '.contentSliderItem:first';
				var headingRadio = jQuery(jQuerythis).parents(baseReference).find('.heading input[type=radio]')[0]; // Javascript object vs jQuery Ie. [0]
				var allContentItems = jQuery(jQuerythis).parents('.contentSlider:first').find('.contentSliderItem a'); // Grab all items (for event use later)
				if(jQuerythis.is('input[type=radio]')){var fromRadio = true;}  // If the function called from a radio
				if(jQuerythis.is('a[href]')){var fromLink = true;}  // If the function called from a link

				if(fromRadio){
					allContentItems.contentSliderClose();
					jQuerythis.contentSliderOpen();
				}

				if(fromLink){
					if(headingRadio.checked == true){

						if(jQuery(jQuerythis).parents(baseReference)[0].contentSliderOpened){
							jQuery(jQuerythis).parents(baseReference).find('input[type=radio]').contentSliderClose();
						}
						else{
							jQuery(jQuerythis).parents(baseReference).find('input[type=radio]').contentSliderOpen();
						}

					}
					else{
						allContentItems.contentSliderClose();
						jQuerythis.contentSliderOpen();
						headingRadio.checked = true;
					}
				}

				if(fromLink){ // Radio must perform default browser actions (or it won't be slected)
					return false;
				}

			});

		});
	}

	// Open a contentSlider Item
	jQuery.fn.contentSliderOpen = function(){
		return this.each(function(){
			var jQuerythis = jQuery(this); //Shortcut to jQuery's this instead of javascript this
			var baseReference = '.contentSliderItem:first';
			jQuery(jQuerythis).parents(baseReference).find('.content').slideDown("slow");
			jQuery(jQuerythis).parents(baseReference).find('.heading .arrow a img').attr('src', '/images/arrowgrey_selected.gif');
			jQuerythis.changeButtonStates(); // Change button text / link relating to section
			jQuery(jQuerythis).parents(baseReference)[0].contentSliderOpened = true;
			console.log(jQuery(jQuerythis).parents(baseReference),jQuery(jQuerythis).parents(baseReference)[0]);
		});
	}

	// Close a contentSlider Item
	jQuery.fn.contentSliderClose = function(){
		return this.each(function(){
			var jQuerythis = jQuery(this); //Shortcut to jQuery's this instead of javascript this
			var baseReference = '.contentSliderItem:first';
			jQuery(jQuerythis).parents(baseReference).find('.content').slideUp("slow");
			jQuery(jQuerythis).parents(baseReference).find('.heading .arrow a img').attr('src', '/images/arrowgrey_deselected.gif');
			jQuery(jQuerythis).parents(baseReference)[0].contentSliderOpened = false;
			console.log(jQuery(jQuerythis).parents(baseReference),jQuery(jQuerythis).parents(baseReference)[0]);
		});
	}



	// Initializes contentSlider on specified elements
	jQuery('.contentSliderItem .heading ').find('input[type=radio], h2.headingItem a, .arrow a').contentSlider();
	jQuery('.contentSliderItem .heading ').find('input[type=radio]:not(:checked)').contentSliderClose();


/* Change button text / link relating to section (Specific to overlay) */
	jQuery.fn.changeButtonStates = function(){
		alert('test/699');
			jQuery('div.overlay a[rel*=facebox]').facebox();

			if(jQuery(this).parents('.contentSliderItem').hasClass('chooseShippingAddress')){
				jQuery('.overlayFooter a.var-button').unbind();
				jQuery('.overlayFooter a.var-button').livequery('click',function(){
					jQuery.facebox.close();
				});
				jQuery('.overlayFooter a.var-button span').html('CONFIRM SHIPPING ADDRESS &raquo;');
			}

			if(jQuery(this).parents('.contentSliderItem').hasClass('orEnterANewShippingAddress')){
				jQuery('.overlayFooter a.var-button').unbind();
				jQuery('.overlayFooter a.var-button').livequery('click',function(){
					jQuery.facebox.close();
				});
				jQuery('.overlayFooter a.var-button span').html('CONFIRM SHIPPING ADDRESS &raquo;');
			}

			if(jQuery(this).parents('.contentSliderItem').hasClass('willCall')){
				jQuery('.overlayFooter a.var-button').unbind();
				jQuery('.overlayFooter a.var-button').livequery('click',function(){
					jQuery.facebox.close();
				});
				jQuery('.overlayFooter a.var-button span').html('CLOSE AND CONTINUE &raquo;');
			}

			if(jQuery(this).parents('.contentSliderItem').hasClass('needAssistance')){
				jQuery('.overlayFooter a.var-button').unbind();
				jQuery('.overlayFooter a.var-button').livequery('click',function(){
					jQuery.facebox.close();
				});
				jQuery('.overlayFooter a.var-button span').html('CLOSE AND CONTINUE &raquo;');
			}

	};

/*
 * Custom editable text functions (specific to shipping overlay)
*/

	jQuery.fn.showInputs = function(){
		jQuery('.chooseShippingAddressTable tbody tr td.input span.text').show();
		jQuery('.chooseShippingAddressTable tbody tr td.input input').hide();
		var baseReference = jQuery(this).parents('tr:first');
		jQuery(baseReference).find('.input span.text').hide();
		jQuery(baseReference).find('.input   input').each(function(){
			var newValue = jQuery(this).parents('.input').find('span.text').text();
			jQuery(this).val(newValue);
		});
		jQuery(baseReference).find('.input input').show();
	}

	jQuery.fn.hideInputs = function(baseReference){
		window.TMPbaseReference = baseReference;
			jQuery(baseReference).find('.input span.text').each(function(){
				var newValue = jQuery(this).parents('.input').find('input').val();
				jQuery(this).text(newValue);
			});
		 jQuery(TMPbaseReference).find('.input input').hide();
		 jQuery(TMPbaseReference).find('.input span.text').show();

	}

	// Shows and Displays editable text input and then keeps the information updated in sync
	jQuery('.chooseShippingAddress table:first td.options a.edit').livequery('click',function(){
		if(jQuery(this).hasClass('editing')){
			jQuery(this).text('Edit');
			baseReference = jQuery(this).parents('tr:first');
			jQuery(this).hideInputs(baseReference);
		}
		else{
			jQuery('.chooseShippingAddressTable tbody tr td.options a.edit').text('Edit');
			jQuery('.chooseShippingAddressTable tbody tr td.options a.edit').removeClass('editing');
			jQuery(this).text('Save');
			jQuery(this).addClass('editing');
			jQuery(this).showInputs(); // Not a save problem D Lopez
			baseReference = jQuery(this).parents('tr:first');
		}
		return false;
	});

	jQuery('.shippingOptions .shipEntireProductToOneLocation .answer input[type=radio]').livequery('click',function(){
		var answerValue = jQuery(this).val();
			if(answerValue == "yes"){
				jQuery(this).parents('.shippingOptions:first').find('.multipleLocation').hide();
			}
			if(answerValue == "no"){
				jQuery(this).parents('.shippingOptions:first').find('.multipleLocation').show();
			}
		this.checked = true;
		return true;
	});


// Highlights (changes from grey/black) the selected shipping option
	jQuery('table.shippingType .choose input[type=radio]').livequery('click',function(){
		jQuery('table.shippingType tbody tr').attr('class','');
		jQuery(this).parents('tr:first').attr('class','highlighted');
	});


}

/* END JAVASCRIPT TO LOAD WHEN OVERLAYS ARE FINISHED LOADING (NEW DYNAMIC CONTENT) */

function setCompanyAddressInMyPaymentPage()

{

	if (tmp = document.getElementById("credit_card_cc_address"))

		tmp.value = document.getElementById("company_address_address").value;



	if (tmp = document.getElementById("credit_card_cc_city"))

		tmp.value = document.getElementById("company_address_city").value;



	if (tmp = document.getElementById("credit_card_state"))

		tmp.value = document.getElementById("company_address_state").value;



	if (tmp = document.getElementById("credit_card_cc_zipcode"))

		tmp.value = document.getElementById("company_address_zipcode").value;



	if (tmp = document.getElementById("credit_card_country"))

		tmp.value = document.getElementById("company_address_country").value;

}



function setCompanyAddressInMyShippingPage()
{

	if (tmp = document.getElementById("shipping_my_address"))

		tmp.value = document.getElementById("company_address_address").value;



	if (tmp = document.getElementById("shipping_my_city"))

		tmp.value = document.getElementById("company_address_city").value;



	if (tmp = document.getElementById("shipping_my_state"))

		tmp.value = document.getElementById("company_address_state").value;



	if (tmp = document.getElementById("shipping_my_zipcode"))

		tmp.value = document.getElementById("company_address_zipcode").value;



	if (tmp = document.getElementById("shipping_my_country"))

		tmp.value = document.getElementById("company_address_country").value;

}

function setCompanyAddressInMyShippingPageForAjax(id)
{
	if (tmp = document.getElementById("company_address_address")) jQuery(":input[edit_name='address_" + id + "']").val(tmp.value);
	if (tmp = document.getElementById("company_address_city")) jQuery(":input[edit_name='city_" + id + "']").val(tmp.value);
	if (tmp = document.getElementById("company_address_state")) jQuery(":input[edit_name='state_" + id + "']").val(tmp.value);
	if (tmp = document.getElementById("company_address_zipcode")) jQuery(":input[edit_name='zipcode_" + id + "']").val(tmp.value);
	if (tmp = document.getElementById("company_address_country")) jQuery(":input[edit_name='country_" + id + "']").val(tmp.value);

}

function shipping_method_form_check(f)
{
	var out = false;

	for(i = 0; i < f.shippingType.length; i ++)
	{
		if (f.shippingType[i].checked) out = true;
	}

	if (!out) alert("Please provide your shipping method for this item.");

	return out;
}

		jQuery(document).ready(function() {
			jQuery("div.notice").fadeOut(3000);
		});

jQuery(document).ready(function(){

p_option1_open = 0
p_option2_open = 0
p_option3_open = 0
p_option4_open = 0


jQuery("#cart-left-column .payment-tab1").click(function(){
	if (p_option1_open == 0){
		jQuery(this).css({backgroundImage:"url(/images/faq_arrow_down.png)"});
		jQuery("#payment-option1").slideToggle(600);
		if (p_option2_open) {jQuery("#payment-option2").slideToggle(600);p_option2_open = 0;}
		if (p_option3_open) {jQuery("#payment-option3").slideToggle(600);p_option3_open = 0;}
		if (p_option4_open) {jQuery("#payment-option4").slideToggle(600);p_option4_open = 0;}
		p_option1_open = 1;
	}
	else {
		jQuery(this).css({backgroundImage:"url(/images/faq_arrow_up.png)"});
		jQuery("#payment-option1").slideToggle(600);
		p_option1_open = 0;
	}
});

jQuery("#cart-left-column .payment-tab2").click(function(){
	if (p_option2_open == 0){
		jQuery(this).css({backgroundImage:"url(/images/faq_arrow_down.png)"});
		jQuery("#payment-option2").slideToggle(600);
		if (p_option1_open) {jQuery("#payment-option1").slideToggle(600);p_option1_open = 0;}
		if (p_option3_open) {jQuery("#payment-option3").slideToggle(600);p_option3_open = 0;}
		if (p_option4_open) {jQuery("#payment-option4").slideToggle(600);p_option4_open = 0;}
		p_option2_open = 1;
	}
	else {
		jQuery(this).css({backgroundImage:"url(/images/faq_arrow_up.png)"});
		jQuery("#payment-option2").slideToggle(600);
		p_option2_open = 0;
	}
});


jQuery("#cart-left-column .payment-tab3").click(function(){
	if (p_option3_open == 0){
		jQuery(this).css({backgroundImage:"url(/images/faq_arrow_down.png)"});
		jQuery("#payment-option3").slideToggle(600);
		if (p_option1_open) {jQuery("#payment-option1").slideToggle(600);p_option1_open = 0;}
		if (p_option2_open) {jQuery("#payment-option2").slideToggle(600);p_option2_open = 0;}
		if (p_option4_open) {jQuery("#payment-option4").slideToggle(600);p_option4_open = 0;}
		p_option3_open = 1;
	}
	else {
		jQuery(this).css({backgroundImage:"url(/images/faq_arrow_up.png)"});
		jQuery("#payment-option3").slideToggle(600);
		p_option3_open = 0;
	}
});

jQuery("#cart-left-column .payment-tab4").click(function(){
	if (p_option4_open == 0){
		jQuery(this).css({backgroundImage:"url(/images/faq_arrow_down.png)"});
		jQuery("#payment-option4").slideToggle(600);
		if (p_option1_open) {jQuery("#payment-option1").slideToggle(600);p_option1_open = 0;}
		if (p_option2_open) {jQuery("#payment-option2").slideToggle(600);p_option2_open = 0;}
		if (p_option3_open) {jQuery("#payment-option3").slideToggle(600);p_option3_open = 0;}
		p_option4_open = 1;
	}
	else {
		jQuery(this).css({backgroundImage:"url(/images/faq_arrow_up.png)"});
		jQuery("#payment-option4").slideToggle(600);
		p_option4_open = 0;
	}
});

myjobs_open = 0
myquotes_open = 0
mymessages_open = 0
mypayments_open = 0

jQuery(".myjobs-tab").click(function(){
	if (myjobs_open == 0){
		jQuery(this).css({backgroundImage:"url(/images/myjobs_down.png)"});
		jQuery("#myjobs-main").slideToggle(600);
		myjobs_open = 1;
	}
	else {
		jQuery(this).css({backgroundImage:"url(/images/myjobs_up.png)"});
		jQuery("#myjobs-main").slideToggle(600);
		myjobs_open = 0;
	}
});

jQuery(".myquotes-tab").click(function(){
	if (myquotes_open == 0){
		jQuery(this).css({backgroundImage:"url(/images/myquotes_down.png)"});
		jQuery("#myquotes-main").slideToggle(600);
		myquotes_open = 1;
	}
	else {
		jQuery(this).css({backgroundImage:"url(/images/myquotes_up.png)"});
		jQuery("#myquotes-main").slideToggle(600);
		myquotes_open = 0;
	}
});

jQuery(".mymessages-tab").click(function(){
	if (mymessages_open == 0){
		jQuery(this).css({backgroundImage:"url(/images/mymessages_down.png)"});
		jQuery("#mymessages-main").slideToggle(600);
		mymessages_open = 1;
	}
	else {
		jQuery(this).css({backgroundImage:"url(/images/mymessages_up.png)"});
		jQuery("#mymessages-main").slideToggle(600);
		mymessages_open = 0;
	}
});

jQuery(".mypayments-tab").click(function(){
	if (mypayments_open == 0){
		jQuery(this).css({backgroundImage:"url(/images/mypayments_down.png)"});
		jQuery("#mypayments-main").slideToggle(600);
		mypayments_open = 1;
	}
	else {
		jQuery(this).css({backgroundImage:"url(/images/mypayments_up.png)"});
		jQuery("#mypayments-main").slideToggle(600);
		mypayments_open = 0;
	}
});


});


var isNN = (navigator.appName.indexOf("Netscape")!=-1);
function autoTab(input,len, e) {
	var keyCode = (isNN) ? e.which : e.keyCode;
	var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
	if(input.value.length >= len && !containsElement(filter,keyCode)) {
		input.value = input.value.slice(0, len);
		input.form[(getIndex(input)+1) % input.form.length].focus();
}
	function containsElement(arr, ele) {
		var found = false, index = 0;
		while(!found && index < arr.length)
		if(arr[index] == ele)
		found = true;
		else
		index++;
		return found;
	}
	function getIndex(input) {
		var index = -1, i = 0, found = false;
		while (i < input.form.length && index == -1)
		if (input.form[i] == input)index = i;
		else i++;
		return index;
	}
	return true;
}

var last_shipping_edit_field = '';
function openShippingEditField(id){
	if (last_shipping_edit_field != '') jQuery(last_shipping_edit_field).parent().parent().hide();
	jQuery.ajax({
		type: "GET",
		url: "/account/get_shipping_edit_field",
		data: "id=" + id,
		success: function (html) {
			document.getElementById('shipping_edit_field_' + id).innerHTML = html;
			jQuery('#shipping_edit_field_' + id).parent().parent().show();
			last_shipping_edit_field = '#shipping_edit_field_' + id;
		}
	});
}

function update_shipping_info(shipping_id) {
	shipping_purpose = jQuery(":input[edit_name='shipping_purpose_" + shipping_id + "']:checked").val();
	reference_name = jQuery(":input[edit_name='reference_name_" + shipping_id + "']").val();
	contact_name = jQuery(":input[edit_name='contact_name_" + shipping_id + "']").val();
	ph_ac = jQuery(":input[edit_name='ph_ac_" + shipping_id + "']").val();
	ph_exc = jQuery(":input[edit_name='ph_exc_" + shipping_id + "']").val();
	ph_num = jQuery(":input[edit_name='ph_num_" + shipping_id + "']").val();
	ph_ext = jQuery(":input[edit_name='ph_ext_" + shipping_id + "']").val();
	address = jQuery(":input[edit_name='address_" + shipping_id + "']").val();
	city = jQuery(":input[edit_name='city_" + shipping_id + "']").val();
	state = jQuery(":input[edit_name='state_" + shipping_id + "']").val();
	zipcode = jQuery(":input[edit_name='zipcode_" + shipping_id + "']").val();
	country = jQuery(":input[edit_name='country_" + shipping_id + "']").val();
	preferred_shipping = jQuery(":input[edit_name='preferred_shipping_" + shipping_id + "']").val();

	tr = jQuery('#shipping_edit_field_' + shipping_id).parent().parent();
	tr.hide();

	refe = jQuery('#refe_' + shipping_id);
	cont = jQuery('#cont_' + shipping_id);
	addr = jQuery('#addr_' + shipping_id);

	jQuery.ajax({
		type: "POST",
		url: "/account/ajax_update_shipping",
		data: "shipping_id=" + shipping_id
			+ "&shipping_purpose=" + shipping_purpose
			+ "&reference_name=" + reference_name
			+ "&contact_name=" + contact_name
			+ "&ph_ac=" + ph_ac
			+ "&ph_exc=" + ph_exc
			+ "&ph_num=" + ph_num
			+ "&ph_ext=" + ph_ext
			+ "&address=" + address
			+ "&city=" + city
			+ "&state=" + state
			+ "&zipcode=" + zipcode
			+ "&country=" + country
			+ "&preferred_shipping=" + preferred_shipping,
		success: function(msg) {
			if (msg == '0') {
				alert('Editing has failed. Please check all required fields.')
			} else {
				refe.fadeOut('slow', function () {refe.text(reference_name).fadeIn('slow');});
				cont.fadeOut('slow', function () {cont.text(contact_name).fadeIn('slow');});
				addr.fadeOut('slow', function () {addr.html(msg).fadeIn('slow');});
			}
		}
	});
}

var last_contact_edit_field = '';
function openContactEditField(id){
	if (last_contact_edit_field != '') jQuery(last_contact_edit_field).parent().parent().hide();
	jQuery.ajax({
		type: "GET",
		url: "/account/get_contact_edit_field",
		data: "id=" + id,
		success: function (html) {
			document.getElementById('contact_edit_field_' + id).innerHTML = html;
			jQuery('#contact_edit_field_' + id).parent().parent().show();
			last_contact_edit_field = '#contact_edit_field_' + id;
		}
	});
}

function update_contact_info(contact_id) {
	if (contact_id == 'p') {
		first_name = jQuery(":input[edit_name='first_name_p']").val();
		last_name = jQuery(":input[edit_name='last_name_p']").val();
		title = jQuery(":input[edit_name='title_p']").val();
		accounting_email = jQuery(":input[edit_name='accounting_email_p']").val();
	} else {
		order_permission = (jQuery(":input[edit_name='order_permission_" + contact_id + "']:checked").length)? 1: 0;
		contact_name = jQuery(":input[edit_name='contact_name_" + contact_id + "']").val();
		contact_type = jQuery(":input[edit_name='contact_type_" + contact_id + "']").val();
	}

	title_other = jQuery(":input[edit_name='title_other_" + contact_id + "']").val();
	work_email = jQuery(":input[edit_name='work_email_" + contact_id + "']").val();
	current_password = jQuery(":input[edit_name='current_password_" + contact_id + "']").val();
	password = jQuery(":input[edit_name='password_" + contact_id + "']").val();
	password_confirmation = jQuery(":input[edit_name='password_confirmation_" + contact_id + "']").val();
	ph1_ac = jQuery(":input[edit_name='ph1_ac_" + contact_id + "']").val();
	ph1_exc = jQuery(":input[edit_name='ph1_exc_" + contact_id + "']").val();
	ph1_num = jQuery(":input[edit_name='ph1_num_" + contact_id + "']").val();
	ph1_ext = jQuery(":input[edit_name='ph1_ext_" + contact_id + "']").val();
	ph2_ac = jQuery(":input[edit_name='ph2_ac_" + contact_id + "']").val();
	ph2_exc = jQuery(":input[edit_name='ph2_exc_" + contact_id + "']").val();
	ph2_num = jQuery(":input[edit_name='ph2_num_" + contact_id + "']").val();
	ph2_ext = jQuery(":input[edit_name='ph2_ext_" + contact_id + "']").val();
	ph3_ac = jQuery(":input[edit_name='ph3_ac_" + contact_id + "']").val();
	ph3_exc = jQuery(":input[edit_name='ph3_exc_" + contact_id + "']").val();
	ph3_num = jQuery(":input[edit_name='ph3_num_" + contact_id + "']").val();
	ph3_description = jQuery(":input[edit_name='ph3_description_" + contact_id + "']").val();
	ph4_ac = jQuery(":input[edit_name='ph4_ac_" + contact_id + "']").val();
	ph4_exc = jQuery(":input[edit_name='ph4_exc_" + contact_id + "']").val();
	ph4_num = jQuery(":input[edit_name='ph4_num_" + contact_id + "']").val();
	ph5_ac = jQuery(":input[edit_name='ph5_ac_" + contact_id + "']").val();
	ph5_exc = jQuery(":input[edit_name='ph5_exc_" + contact_id + "']").val();
	ph5_num = jQuery(":input[edit_name='ph5_num_" + contact_id + "']").val();

	tr = jQuery('#contact_edit_field_' + contact_id).parent().parent();
	tr.hide();

	cont = jQuery('#cont_' + contact_id);
	titl = jQuery('#titl_' + contact_id);
	emai = jQuery('#emai_' + contact_id);

	data = "contact_id=" + contact_id;
	if (contact_id == 'p') {
		data += "&first_name=" + first_name;
		data += "&last_name=" + last_name;
		data += "&title=" + title;
		data += "&accounting_email=" + accounting_email;
	} else {
		data += "&order_permission=" + order_permission;
		data += "&contact_name=" + contact_name;
		data += "&contact_type=" + contact_type;
	}

	data += "&title_other=" + title_other;
	data += "&work_email=" + encodeURL(work_email);
	data += "&current_password=" + current_password;
	data += "&password=" + password;
	data += "&password_confirmation=" + password_confirmation;
	data += "&ph1_ac=" + ph1_ac;
	data += "&ph1_exc=" + ph1_exc;
	data += "&ph1_num=" + ph1_num;
	data += "&ph1_ext=" + ph1_ext;
	data += "&ph2_ac=" + ph2_ac;
	data += "&ph2_exc=" + ph2_exc;
	data += "&ph2_num=" + ph2_num;
	data += "&ph2_ext=" + ph2_ext;
	data += "&ph3_ac=" + ph3_ac;
	data += "&ph3_exc=" + ph3_exc;
	data += "&ph3_num=" + ph3_num;
	data += "&ph3_description=" + ph3_description;
	data += "&ph4_ac=" + ph4_ac;
	data += "&ph4_exc=" + ph4_exc;
	data += "&ph4_num=" + ph4_num;
	data += "&ph5_ac=" + ph5_ac;
	data += "&ph5_exc=" + ph5_exc;
	data += "&ph5_num=" + ph5_num;

	jQuery.ajax({
		type: "POST",
		url: "/account/ajax_update_contact",
		data: data,
		success: function (msg) {
			if (msg == '0') {
				alert('Editing has failed. Please check all required fields.')
			} else {
				if (contact_id == 'p') {
					name = last_name + ', ' + first_name + '<br />(Primary Contact)';
				} else if (order_permission == 1) {
					name = '<span class="asterisk">* </span>' + contact_name;
				} else {
					name = contact_name;
				}

				cont.fadeOut('slow', function () {cont.html(name).fadeIn('slow');});
				titl.fadeOut('slow', function () {titl.text(msg).fadeIn('slow');});
				emai.fadeOut('slow', function () {emai.html(work_email).fadeIn('slow');});

			}
		}
	});
}

function encodeURL(str) {
  var character = '';
  var unicode   = '';
  var string    = '';
  var i         = 0;

  for (i = 0; i < str.length; i++) {
    character = str.charAt(i);
    unicode   = str.charCodeAt(i);

    if (character == ' ') {
      string += '+';
    } else {
      if (unicode == 0x2a || unicode == 0x2d || unicode == 0x2e || unicode == 0x5f || ((unicode >= 0x30) && (unicode <= 0x39)) || ((unicode >= 0x41) && (unicode <= 0x5a)) || ((unicode >= 0x61) && (unicode <= 0x7a))) {
        string = string + character;
      } else {
        if ((unicode >= 0x0) && (unicode <= 0x7f)) {
          character   = '0' + unicode.toString(16);
          string += '%' + character.substr(character.length - 2);
        } else if (unicode > 0x1fffff) {
          string += '%' + (oxf0 + ((unicode & 0x1c0000) >> 18)).toString(16);
          string += '%' + (0x80 + ((unicode & 0x3f000) >> 12)).toString(16);
          string += '%' + (0x80 + ((unicode & 0xfc0) >> 6)).toString(16);
          string += '%' + (0x80 + (unicode & 0x3f)).toString(16);
        } else if (unicode > 0x7ff) {
          string += '%' + (0xe0 + ((unicode & 0xf000) >> 12)).toString(16);
          string += '%' + (0x80 + ((unicode & 0xfc0) >> 6)).toString(16);
          string += '%' + (0x80 + (unicode & 0x3f)).toString(16);
        } else {
          string += '%' + (0xc0 + ((unicode & 0x7c0) >> 6)).toString(16);
          string += '%' + (0x80 + (unicode & 0x3f)).toString(16);
        }
      }
    }
  }

  return string;
}


