// img replacement code

function enforceDropShip() {
	if (document.cartForm.drpOpt==null)
		return true;
	if (document.cartForm.drpOpt[0].checked) {
		if (	document.orderForm.dropShipName.value.length == 0 ||
			document.orderForm.dropShipAddress.value.length == 0 ||
			document.orderForm.dropShipCity.value.length == 0 ||
			document.orderForm.dropShipState.value.length == 0 ||
			document.orderForm.dropShipZip.value.length == 0
		) {
			alert("To use the Drop Ship function you must supply the customer's complete destination address. If this is a special order or an order in which you don't know the customer's destination address yet, please select 'No' under Drop Ship and include your instructions in the Shipping Requests box below.");
			return false;
		}
	}
	return true;
}

function visFlip(flipSwitch) { 
	var tr = document.getElementById('dropInfo'); 
	if(!flipSwitch) { tr.style.display = "none"; }
	else { tr.style.display = "inline"; }
	}
	
var imgReplace = {
	switchImg : function(path,url,prodID,alt) {
		imgHold = document.getElementById('img'+prodID);
		if(imgHold.getAttributeNode('src').value == web_pathname+"img/productImages/primary/"+url) {return;}
		aHold = document.getElementById('a'+prodID);
		imgHold.removeAttribute('src');	
		imgHold.removeAttribute('alt');	
		imgHold.setAttribute('src',web_pathname+'img/productImages/primary/'+url);
		imgHold.setAttribute('alt',alt);
		aHold.removeAttribute('href');	
		if(alt === "") {return;}
		aHold.setAttribute('href',path+'images/imageResizer.php?image_type=PRODPRIM&fileName='+url+'&maxWidth=600');
	}
};

//use to create a popup window, requires URL and name parameters

function popupWindow(url,name,w,h)
{
	popupWin = window.open(url,name,'scrollbars,resizable,toolbar,width='+w+',height='+h+',left=50,top=50');
	popupWin.focus();
}

function disableEnterKey(e)
{
     var key;     
     if(window.event) {
          key = window.event.keyCode; //IE
     } else {
          key = e.which; //firefox     
     }
     return (key != 13);
}

function showTab(id, entityType, tabid, setfocus)
{
  var tabs;
  var nc="#6C6F74";
  var nb="#e0e0e0";
  var ac="#e8e8e8";
  var ab="#6c6f74";

  if(entityType == "product") {
    tabs = new Array("features", "images", "accessories", "connectors", "instructions");

  } else if(entityType == "vehicle") {
    tabs = new Array("socketKits", "products", "images", "connectors", "eec");

  } else if(entityType == "appliance") {
    tabs = new Array("cables", "images", "connectors", "description");
  }/* else if(entityType == "SKTKIT") {
    tabs = new Array("make", "vehicleCategory", "productSubcategory");
  } else if(entityType == "video") {
    tabs = new Array("install", "inuse", "vehicle","appliance");
  }*/

  for(var loop = 0; loop < tabs.length; loop++)
  {
    if(tabs[loop] != id) {
//      $(tabs[loop])./*hide();*/setAttribute('style','position:absolute;left:-1800px;width:1em;height: 1em;');
      $(tabs[loop])./*hide();*/className = "boxarea";
    }
    var tabel="anc"+loop;
    $(tabel).style.color=nc;
    $(tabel).style.background=nb;
  }
  $(tabid).style.color=ac;
  $(tabid).style.background=ab;
  // /*$(id).show();*/ $(id).setAttribute('style','position:relative;left:0px;');
  /*$(id).show();*/ $(id).className = "boxarea boxshow";
  if (setfocus)
	  $(tabid).focus();
}

function jumpToLogin()
{
  var form = $("loginForm");

  form.email_addr.style.background = "#ffffcc"; 
  form.password_text.style.background = "#ffffcc"; 
  window.scrollTo(0,0);
  form.email_addr.focus();
}

//make this server-side
function showLargeImage(fileName, entityName)
{
  var win = window.open("", "Enlarge", "width=430, height=430");
  if(window.focus)
    win.focus();

  win.document.write("<html>");
  win.document.write("<title>" + entityName + "</title>");
  win.document.write("<img src='" + fileName + "' alt=\"\" onclick='window.close();'/>");
  win.document.write("<br/>");
  win.document.write("Click the image to close.");
  win.document.write("</html>");
  win.document.close();
}

function dolightbox(elem) {
	document.getElementById(elem).style.display='block';
	document.getElementById('fade').style.display='block';
}


function dolightboxContent(elem,srcDiv) {
	platform = navigator.platform;
	islinux = platform.match(/linux/i);
	if (islinux && document.getElementById('hmflash')!=null)
		document.getElementById('hmflash').style.visibility='hidden';
	//document.getElementById('hmflash').style.margin='0 980 0 0';
	if (srcDiv.length>0) {
		var content=$(srcDiv).innerHTML;
		$(elem).update(content);
	}

	document.getElementById('light').style.display='block';
	document.getElementById('fade').style.display='block';
}

function closelightbox(elem) {
	document.getElementById(elem).style.display='none';
	document.getElementById('fade').style.display='none';
	if (islinux && document.getElementById('hmflash')!=null)
		document.getElementById('hmflash').style.visibility='visible';
	//document.getElementById('hmflash').style.margin='0 0 0 0';
}

// expand div

function expandBox(name)
{
	$(name).setStyle({height: '300px'});
}

function video_load(vidfile) {
	var vidpath="video/";
	var flash_data='<object id="flashcontent" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" width="424" height="359"> <param name="movie" value="'+vidpath+vidfile+'" />'+'<param name="wmode" value="opaque" /> <!--[if !IE]>--> <object type="application/x-shockwave-flash" data="'+vidpath+vidfile+'" width="424" height="359"> <param name="wmode" value="opaque" />'+'<!--<![endif]--> <p>Please update your Flash Player</p> <!--[if !IE]>--> </object> <!--<![endif]--> </object>';
	$('video_container').update(flash_data);
}

function video_play(vidnum) {
	var divname="video"+vidnum;
	$('video_description').update($(divname).innerHTML);
	switch (vidnum) {
		case 1: video_load("Powerbar+ Install.swf");
		break;
		case 2: video_load("Powermount Install.swf");
		break;
		case 3: video_load("101 Kit Install_Honda.swf");
		break;
		case 4: video_load("Front Panel Kit Install_Concours.swf");
		break;
		case 5: video_load("Panel Kit Install_M109 Suzuki.swf");
		break;
		case 6: video_load("Panel Kit In Use_Honda 750 VT.swf");
		break;
		case 7: video_load("Wired For Life.swf");
		break;
		case 8: video_load("Powerbar +.swf");
		break;
		case 9: video_load("Metric Cruiser Kits.swf");
		break;
		case 10: video_load("Liner the Movie.swf");
		break;
	}
}

function isEnter(e) {
	var characterCode;

/*
	if(e && e.which){
		e = e;
		characterCode = e.which;
	} else {
		e = event;
		characterCode = e.keyCode;
	}
*/
	if (!e) var e = window.event;
	if (e.keyCode) characterCode = e.keyCode;
	else if (e.which) characterCode = e.which;


	if(characterCode == 13){
		return true;
	} else {
		return false;
	}

}

function loadPanel(pan1,pan2) {
	showOverlay();
	copyPanel(pan1,pan2);
}

function copyPanel(pan1,pan2) {
	var content=$(pan2).innerHTML;
	$(pan1).update(content);
	hideOverlay();
}

function swapPanels(pan1,pan2,height) {
	$(pan1).hide();
	$(pan1).setStyle('height: 0px;');
	$(pan2).setStyle('height: '+height+'px;');
	$(pan2).show();
}

function submitsubscribe() {
	document.contactForm.antispam_key.value="subscribeme";
	document.contactForm.submit();
}

function submitwithkey() {
	document.contactForm.antispam_key.value="999";
	document.contactForm.submit();
}

function showErrors() {
	var f=$$('#addressesForm .error_message');
	for(i=0; i<f.length; i++){
		f[i].setStyle({
			'display' : 'inline'
		});
	}

}

function lockAddressesForm() {
	var fgc="#a0a0a0";
	var bgc="#f8f8f8";

	var f=$$('#addressesForm .add_input input,select');
	for(i=0; i<f.length; i++){
		f[i].setStyle({
			'color' : "#606060",
			'background' : bgc,
			'border' : '0px'
		});
	}
	f=$$('#addressesForm .add_label');
	for(i=0; i<f.length; i++){
		f[i].setStyle({
			'color' : fgc,
			'background' : bgc
		});
	}

	$('addressesForm').setStyle({
              'color' : fgc,
              'background' : bgc
        });
	$('addressesForm').disable();
}

function include_google_analytics() {
	var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
	document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
}

function set_google_tracker(tracker) {
	if (typeof _gat=="undefined") {
		return;
	} else {
		var pageTracker = _gat._getTracker(tracker);
		pageTracker._trackPageview();
	}
}


function credcard_jump(fld,jump) {
        if (fld.value.length==4)
                $(jump).focus();
}

// autocomplete - quickadd

var cursel = 0;
var isOpera = navigator.userAgent.indexOf("Opera") > -1;
var isIE = navigator.userAgent.indexOf("MSIE") > 1 && !isOpera;
var isMoz = navigator.userAgent.indexOf("Mozilla/5.") == 0 && !isOpera;

function pickitem(a) {
	if (cursel<1) cursel=1;
	if (a=='up') document.quickform.quickinput.value=items[--cursel];
	if (cursel>items.length-2) cursel=items.length-2;
	if (a=='down') document.quickform.quickinput.value=items[++cursel];
	//if (a=='fname') document.quickform.first_name.focus();
	if (a=='left') {
		document.quickform.quantity1.focus();
		document.quickform.quantity1.select();
	}
	return a;
}


function textboxSelect (oTextbox, iStart, iEnd) {

   switch(arguments.length) {
	case 1:
	   oTextbox.select();
	   break;

	case 2:
	   iEnd = oTextbox.value.length;
	   /* falls through */

	case 3:
	   if (isIE) {
		var oRange = oTextbox.createTextRange();
		oRange.moveStart("character", iStart);
		oRange.moveEnd("character", -oTextbox.value.length + iEnd);
		oRange.select();
	   } else if (isMoz){
		oTextbox.setSelectionRange(iStart, iEnd);
	   }
   }
   oTextbox.focus();
}

function textboxReplaceSelect (oTextbox, sText) {
   if (isIE) {
	var oRange = document.selection.createRange();
	oRange.text = sText;
	oRange.collapse(true);
	oRange.select();
   } else if (isMoz) {
	var iStart = oTextbox.selectionStart;
	oTextbox.value = oTextbox.value.substring(0, iStart) + sText + 
			oTextbox.value.substring(
			oTextbox.selectionEnd, oTextbox.value.length);
	oTextbox.setSelectionRange(
		iStart + sText.length, iStart + sText.length);
   }
   oTextbox.focus();
}

function autocompleteMatch (sText) {
   for (var i=0; i < items.length; i++) {
	if ((items[i].toLowerCase()).indexOf(sText.toLowerCase()) == 0) {
	   cursel=i;
	   return items[i];
	}
   }
   return null;
}

function autocompleteAN(oTextbox, oEvent) {
   switch (oEvent.keyCode) {
	case 38: return "up";//up arrow
	case 40: return "down";//down arrow
	case 37: //return "left";//left arrow
	case 39: //return "right";//right arrow
	case 33: //page up
	case 34: //return "fname";//page down  
	case 36: //home
	case 35: //end		  
	case 13: //enter
	case 9:  //tab  
	case 27: //esc
	case 16: //shift  
	case 17: //ctrl
	case 18: //alt  
	case 20: //caps lock
	case 8: //backspace  
	case 46: //delete
	return true;
	break;

	default:
	textboxReplaceSelect(oTextbox, String.fromCharCode(
			isIE ? oEvent.keyCode : oEvent.charCode));
	var iLen = oTextbox.value.length;
	var sMatch = autocompleteMatch(oTextbox.value);
	if (sMatch != null) {
		oTextbox.value = sMatch;
		textboxSelect(oTextbox, iLen, oTextbox.value.length);
	}
	return false;
   }
}

