function _$(elm) { return document.getElementById(elm); }

/************* On Product Page, Opens up Large Image **********/
	function ProductPopUp() {
		$("product-detail-popup").style.display="block";
		$("black").style.height=$("doc2").scrollHeight + "px";
		$("black").style.display="block";
		IE6_IframeCtrl('OVER', $("product-detail-popup"))		
		//var tmpVar = (($("doc2").scrollHeight)-750)/2;
		//$("product-detail-popup").style.top=tmpVar + "px";
	}
	function closePopUpWindow() {
		$("product-detail-popup").style.display="none";
		$("black").style.display="none";
		IE6_IframeCtrl('OUT', $("product-detail-popup"))
	}
/*************************************************************/

/***********************  Swap Image *************************/
//	function swapImage(DivID,whichAtt,colorName,img,src,lgimg,newMargin){
//		DivIDgen = DivID.substring(0,DivID.length-1);
//		var el=$(img);el.src=src;
//		if(lgimg)el.setAttribute('largeImg',lgimg);
//		el.style.margin=newMargin;
//		if(document.getElementById('AvColorsCol'))document.getElementById('AvColorsCol').innerHTML=" ";
//		if(document.getElementById('AdViewsCol'))document.getElementById('AdViewsCol').innerHTML=" ";
//		if(document.getElementById('AcPopUp'))document.getElementById('AcPopUp').innerHTML=" ";
//		if(document.getElementById('AvPopUp'))document.getElementById('AvPopUp').innerHTML=" ";		
//		
//		if(whichAtt=="MainColor")document.getElementById('AvColorsCol').innerHTML=" "+colorName;
//		if(whichAtt=="MainAV")document.getElementById('AdViewsCol').innerHTML=" "+colorName;
//		if(whichAtt=="ACPopUp")document.getElementById('AcPopUp').innerHTML=" "+colorName;
//		if(whichAtt=="AvPopUp")document.getElementById('AvPopUp').innerHTML=" "+colorName;
//			
//		for(i=0; i<totalProdThumbs; i++) {
//			var blab=(DivIDgen+i);
//			if($(blab)!=null){
//				$(blab).style.border="1px solid #8F8580";
//			}
//		}
//		$(DivID).style.border="1px solid #c92127";
//		
//		}
	function open_win(urlpath,heightwidth){
		window.open(urlpath,null,heightwidth);
		}
		
/************************************************************/

/************ Update Sku Info on Prod Page ******************/
	function displaySkuInfo(selectBox, infoArray, PriceSpan) {
		if(selectBox.selectedIndex!=0) {
			var info = infoArray[selectBox.options[selectBox.selectedIndex].value];
			var strkAmt = "";
			if (eval(info.price) < eval(info.defprice)) {
				strkAmt = '<span class="strikethrough">$'+info.defprice+'</span>';
			}
			document.getElementById(PriceSpan).innerHTML = strkAmt + "<span class='ProdPagePrice'>$" + info.price + "</span>&nbsp;&nbsp;&nbsp;&nbsp;item # " + info.prodCode;
			if(PriceSpan=="DisplayPrice"){
				tmpStock = "Availability: ";
				tmpAvail = info.Availability;
				if (info.Availability!="INVAVL"){
					tmpStock = "Estimated Availability: ";
				}
				if (info.Availability!="INVAVL"){
					tmpAvail = "Uncertain - Get Notified Below";
					if(document.getElementById("AddButtonDiv")){
						document.getElementById("AddButtonDiv").style.display="none";
					}
					if(document.getElementById("ProdBtnRowKit")){
						document.getElementById("ProdBtnRowKit").style.display="none";
					}
					if(document.getElementById("ProdBtnRowWList")){
						document.getElementById("ProdBtnRowWList").style.display="none";
					}
					if(document.getElementById("ProdBtnRowMyBuy")){
						document.getElementById("ProdBtnRowMyBuy").style.display="none";
					}
					if(document.getElementById("AddToCompleteBtn")){
						document.getElementById("AddToCompleteBtn").style.display="none";
					}
					if(document.getElementById("frmNotifyMe")){
						document.getElementById("frmNotifyMe").style.display="block";
					}
				}
				if (info.Availability=="INVAVL"){
					tmpAvail = "<span style='color:green;'>In Stock</span>";
					if(document.getElementById("AddButtonDiv")){
						document.getElementById("AddButtonDiv").style.display="block";
					}
					if(document.getElementById("frmNotifyMe")){
						document.getElementById("frmNotifyMe").style.display="none";
					}
					if(document.getElementById("AddToCompleteBtn")){
						document.getElementById("AddToCompleteBtn").style.display="inline";
					}
					if(document.getElementById("ProdBtnRowKit")){
						document.getElementById("ProdBtnRowKit").style.display="block";
					}
					if(document.getElementById("ProdBtnRowWList")){
						document.getElementById("ProdBtnRowWList").style.display="block";
					}
					if(document.getElementById("ProdBtnRowMyBuy")){
						document.getElementById("ProdBtnRowMyBuy").style.display="block";
					}
				}
				if(document.getElementById('smallTextProdShipping')){
					document.getElementById('smallTextProdShipping').innerHTML = "<p><strong> " + tmpStock + "</strong>" + tmpAvail + "</p>"
				}
			}
		}
	}
/************************************************************/

/****************** Additiona View Selector *******************/

	function AddlViewThumb_Ctrl(prodID, ctrlName, ctrlData) {
		var arrAddlViewGroups = eval("arrAddlViewGroups_"+ctrlName+"_"+prodID); 
		var arrAddlViewGroupData = eval("arrAddlViewGroupsData_"+ctrlName+"_"+prodID);
		for(i=0; i<arrAddlViewGroups.length; i++) {
			var oDiv = _$(arrAddlViewGroups[i].AVGroupID);
			if(oDiv){
				oDiv.className = oDiv.className.replace(" groupselect","");
				oDiv.className = oDiv.className.replace("groupselect","");
			}
		}
		var data=arrAddlViewGroupData["AV"+ctrlData];
		if(!data) return;
		var targetGroup = _$(data.AVGroupID);
		targetGroup.className+=" groupselect";
		//Select first Thumb in group
		var activeEl = _$("ThAV_"+ctrlName+"_"+ctrlData); var targetEl = _$(ctrlName+prodID);
		selectThumb(activeEl)
		swapImg(targetEl,data.ImgUrl)
	}
	
/************************************************************/

/*************************IMG PReload*************************/

function preload_HiddenImgs() {
	var arrObj = arrImgPreLoad.split(',');
	var execString =""
	for(i=1;i<arrObj.length;i++){
		var img = new Image(); img.src=arrObj[i];
	}
}



/************************************************************/

/***********************  SKU Control *************************/

	function swatchSelCtrl_onclick(active,ctrlSet,ctrlName,ctrlData,dataArr) {
		var data = dataArr[ctrlData];
		var activeEl = _$(active);
		var targetEl = _$(ctrlName+data.ProdID); 
		swapImg(targetEl,data.ImgUrl);
		if(!data.IsProdLinkSel)AddlViewThumb_Ctrl(data.ProdID, ctrlName, ctrlData);
		if(data.IsPopUp) {
			selectThumb(activeEl);
			UpdateHdrLabels(activeEl,data.ProdID,ctrlName,data.Value,'Color')
		}
		if(data.IsSkuSel) SelectSku(active,data.ProdID,ctrlName,'Color',data.AttOptID,data.Value);
		if(data.IsProdLinkSel) {
			selectThumb(activeEl);
			setLink('lnkLstItm-'+data.ProdID, data.ProdURL+'?cs='+data.AttOptID);
		}
	}

	function addlViewSelCtrl_onclick(active,target,ctrlData,dataArr){
		var targetEl = _$(target); var activeEl = _$(active);
		swapImg(targetEl,dataArr[ctrlData].ImgUrl);
		selectThumb(activeEl);
	}
	
	function selectThumb(activeEl) {
		if(!activeEl)return;
		var Thumbs = document.getElementsByName(activeEl.name);
		for(i=0; i<Thumbs.length; i++) {
			Thumbs[i].className = Thumbs[i].className.replace("selected ","");
			Thumbs[i].className = Thumbs[i].className.replace("selected","");
		}
		activeEl.className="selected " + activeEl.className;
	}

	function swapImg(targetEl,bgImgUrl) {
		if(targetEl){
			targetEl.style.backgroundImage='url("'+bgImgUrl+'")';
		}
	}
	
	function setLink(elementName, newHref){
		var oProdLinks = document.getElementsByName(elementName)
		if(oProdLinks.length>0){
			for(i=0;i<oProdLinks.length;i++){
				oProdLinks[i].onclick = function(){
						window.location.href = newHref;
				};
				oProdLinks[i].href = newHref;
			}
		}
	}

	function SelectSku(active, prodID, ctrlName, attName, attOptID, attValue) {
		var arrSku = eval("arrSku_"+prodID);
		var activeEl = _$(active);
		var SelSkuID = _$("hdnSelSkuID"+prodID);
		var SelSkuColor = _$("hdnSkuColor"+prodID);
		var SelSkuSize = _$("hdnSkuSize"+prodID);
		var ThumbsSize = document.getElementsByName("ThSize_"+ctrlName+"_"+prodID);
		//Clear SelSKUID
		SelSkuID.value="";
		switch(attName) {
			case "Color": {
				SelSkuColor.value = attOptID;
				ColorSizeControl(activeEl, arrSku, ThumbsSize, prodID, ctrlName, attOptID);
				break;
			}
			case "Size":{
				SelSkuSize.value = attOptID;
				break;
			}
		}
		//Now capture sku if both Color and Size have values
		if(!(SkuCapture(SelSkuID,SelSkuColor,SelSkuSize,arrSku,prodID))&&(ThumbsSize.length==0||SelSkuSize.value!="")) {
			StatusControl("INVDSC",false,"",prodID);
			//_$("prodQtyMsg_"+prodID).innerHTML="&nbsp;&nbsp;&nbsp;Not Available.";
			//_$("qty"+prodID).disabled=true;
			//_$("qty"+prodID).style.backgroundColor="#EEAAAA";
		}
		selectThumb(activeEl);
		UpdateHdrLabels(activeEl,prodID,ctrlName,attValue,attName);
	}
	
	function UpdateHdrLabels(activeEl,prodID,ctrlName,attValue,attName) {
		if(attValue=='')attValue=activeEl.innerHTML;
		//Update Labels
		if(_$("sel"+attName+"_"+ctrlName+"_"+prodID)) {
			_$("sel"+attName+"_"+ctrlName+"_"+prodID).innerHTML = attValue;
		}
	}	
	
	function SkuCapture(SelSkuID,SelSkuColor,SelSkuSize,arrSku,prodID) {
		//Now capture sku if both Color and Size have values
		var bSkuExists = false;
		for(i=0; i<arrSku.length; i++){
			if((SelSkuColor==null||arrSku[i].colorOptID == SelSkuColor.value) && (SelSkuSize==null||arrSku[i].sizeOptID == SelSkuSize.value)){
				SelSkuID.value=arrSku[i].skuID;
				bSkuExists=true;
				StatusControl(arrSku[i].status,arrSku[i].allowNotifyMe,arrSku[i].skuID,prodID);
				break;
			}
		}
		return bSkuExists;
	}	

	function StatusControl(status,allowNotifyMe,skuID,prodID) {
		var tmpStock = ""; var tmpAvail = "";
		switch(status) {
			case "INVUVL": {
//				if(_$("btnAddToCart_"+prodID)){
//					_$("btnAddToCart_"+prodID).style.display="none";
//					_$("btnAddToCartDis_"+prodID).style.display="inline";
//				}
//				if(_$("btnAddToWish_"+prodID)){
//					_$("btnAddToWish_"+prodID).style.display="inline";
//					_$("btnAddToWishDis_"+prodID).style.display="none";
//				}
//				if(_$("btnTellFriend_"+prodID)){
//					_$("btnTellFriend_"+prodID).style.display="inline";
//					_$("btnTellFriendDis_"+prodID).style.display="none";
//				}				
//				_$("prodQtyMsg_"+prodID).innerHTML="&nbsp;&nbsp;&nbsp;Out of stock.";
//				_$("qty"+prodID).disabled=true;
//				_$("qty"+prodID).style.backgroundColor="#EEAAAA";
				if(_$("AddButtonDiv")){
					_$("AddButtonDiv").style.display="none";
				}
				if(_$("ProdBtnRowKit")){
					_$("ProdBtnRowKit").style.display="none";
				}
				if(_$("ProdBtnRowWList")){
					_$("ProdBtnRowWList").style.display="none";
				}
				if(_$("ProdBtnRowMyBuy")){
					_$("ProdBtnRowMyBuy").style.display="none";
				}				
				if(_$("AddToCompleteBtn")){
					_$("AddToCompleteBtn").style.display="none";
				}
				tmpAvail = "<strong> Estimated Availability:</strong> Uncertain - Get Notified Below";
				if(allowNotifyMe)NotifyMeControl(skuID,"block")
				break;
			} case "INVDSC": {
//				if(_$("btnAddToCart_"+prodID)){
//					_$("btnAddToCart_"+prodID).style.display="none";
//					_$("btnAddToCartDis_"+prodID).style.display="inline";
//				}
//				if(_$("btnAddToWish_"+prodID)){
//					_$("btnAddToWish_"+prodID).style.display="none";
//					_$("btnAddToWishDis_"+prodID).style.display="inline";
//				}
//				if(_$("btnTellFriend_"+prodID)){
//					_$("btnTellFriend_"+prodID).style.display="none";
//					_$("btnTellFriendDis_"+prodID).style.display="inline";
//				}				
//				_$("prodQtyMsg_"+prodID).innerHTML="&nbsp;&nbsp;&nbsp;Not Available.";
//				_$("qty"+prodID).disabled=true;
//				_$("qty"+prodID).style.backgroundColor="#EEAAAA";
				if(_$("AddButtonDiv")){
					_$("AddButtonDiv").style.display="none";
				}
				if(_$("ProdBtnRowKit")){
					_$("ProdBtnRowKit").style.display="none";
				}
				if(_$("ProdBtnRowWList")){
					_$("ProdBtnRowWList").style.display="none";
				}
				if(_$("ProdBtnRowMyBuy")){
					_$("ProdBtnRowMyBuy").style.display="none";
				}				
				if(_$("AddToCompleteBtn")){
					_$("AddToCompleteBtn").style.display="none";
				}
				tmpAvail = "<font style='color: #C92127;'>This color/size is out of stock and discontinued. We will not be receiving more.</font>";
				NotifyMeControl("","none");
				break;
			} default: {
//				if(_$("btnAddToCart_"+prodID)){
//					_$("btnAddToCart_"+prodID).style.display="inline";
//					_$("btnAddToCartDis_"+prodID).style.display="none";
//				}
//				if(_$("btnAddToWish_"+prodID)){
//					_$("btnAddToWish_"+prodID).style.display="inline";
//					_$("btnAddToWishDis_"+prodID).style.display="none";
//				}
//				if(_$("btnTellFriend_"+prodID)){
//					_$("btnTellFriend_"+prodID).style.display="inline";
//					_$("btnTellFriendDis_"+prodID).style.display="none";
//				}				
//				_$("prodQtyMsg_"+prodID).innerHTML="";
//				_$("qty"+prodID).disabled=false;
//				_$("qty"+prodID).style.backgroundColor="#FFFCF3";
				if(_$("AddButtonDiv")){
					_$("AddButtonDiv").style.display="block";
				}
				if(_$("ProdBtnRowKit")){
					_$("ProdBtnRowKit").style.display="block";
				}
				if(_$("ProdBtnRowWList")){
					_$("ProdBtnRowWList").style.display="block";
				}
				if(_$("ProdBtnRowMyBuy")){
					_$("ProdBtnRowMyBuy").style.display="block";
				}				
				if(_$("AddToCompleteBtn")){
					_$("AddToCompleteBtn").style.display="inline";
				}
				tmpAvail = "<strong>Availability: </strong><span style='color:green;'>In Stock</span>";
				NotifyMeControl("","none")
				break;
			}
		}
		if(_$("smallTextProdShipping")){
			_$("smallTextProdShipping").innerHTML = tmpAvail;
		}
	}
	
	function NotifyMeControl(skuID, dispStyle) {
		if(_$("frmNotifyMe")){
			_$("formSkuID").value=skuID;
			_$("frmNotifyMe").style.display=dispStyle;
		}
	}
	
	function ColorSizeControl(activeEl, arrSku, ThumbsSize, prodID, ctrlName, attOptID) {
		var ThumbsSize = document.getElementsByName("ThSize_"+ctrlName+"_"+prodID);
		var oSizeObj=null
		if(ThumbsSize.length>0) {
			//Clean up Sizes... make all discontinued by default
			for(i=0; i<ThumbsSize.length; i++) {
				oSizeObj = ThumbsSize[i];
				oSizeObj.className = oSizeObj.className.replace(" discontinued","");
				oSizeObj.className = oSizeObj.className.replace("discontinued","");
				oSizeObj.className = oSizeObj.className.replace(" notavail","");
				oSizeObj.className = oSizeObj.className.replace("notavail","");
				oSizeObj.className+=" discontinued";
			}
			//Now change the class "status" for those that need it 
			for(i=0; i<arrSku.length; i++) {
				if(arrSku[i].colorOptID == attOptID) {
					oSizeObj = _$("ThSize_"+ctrlName+"_"+prodID+"_"+arrSku[i].sizeOptID);
					switch(arrSku[i].status) {
						case "INVAVL": {
							oSizeObj.className = oSizeObj.className.replace(" discontinued","");
							oSizeObj.className = oSizeObj.className.replace("discontinued","");
							break;
						}
						case "INVUVL": {
							oSizeObj.className = oSizeObj.className.replace(" discontinued","");
							oSizeObj.className = oSizeObj.className.replace("discontinued","");
							oSizeObj.className+=" notavail";
							break;
						}
						default: {
							break;
						}
					}
				}
			}
		}
	}
	
/************************************************************/

/****************** Payment Type Selector *******************/

function swap_PayMethods(selValue) {
	hide_PayMethods();
	document.getElementById("type"+selValue).style.display="block";
}

function hide_PayMethods(){
	if(document.getElementById("typePMTCCD"))document.getElementById("typePMTCCD").style.display="none";
	if(document.getElementById("typePMTPOR"))document.getElementById("typePMTPOR").style.display="none";
	if(document.getElementById("typePMTWUN"))document.getElementById("typePMTWUN").style.display="none";
	if(document.getElementById("typePMTBML"))document.getElementById("typePMTBML").style.display="none";
	if(document.getElementById("typePMTPPL"))document.getElementById("typePMTPPL").style.display="none";
	if(document.getElementById("typePMTGVX"))document.getElementById("typePMTGVX").style.display="none";
	if(document.getElementById("typePMTMCR"))document.getElementById("typePMTMCR").style.display="none";
	if(document.getElementById("typePMTSGC"))document.getElementById("typePMTSGC").style.display="none";
	if(document.getElementById("typePMTCOD"))document.getElementById("typePMTCOD").style.display="none";
	if(document.getElementById("typePMTMOR"))document.getElementById("typePMTMOR").style.display="none";
}		

/************************************************************/
function validate_required(field,alerttxt) {
	with (field) {
		if (name=="hdnSelSkuID") return true;
		if (value==null||value=="") {
			alert(alerttxt);
			return false;
		} else {
			return true;
		}
	}
}

function validate_skuforms(thisform) {
	switch (thisform.name){
		case 'productSkuForm': {
			with (thisform) {
				for(i=0; i<elements.length; i++) {
					if (!validate_required(elements[i],"Error! Select a Size/Color")) {
						elements[i].focus();
						return false;	break;
					}
				}
			}
			return true; break;
		} case 'productBundleForm': {
			with (thisform) {
				for(i=0; i<elements.length; i++) {
					if (!validate_required(elements[i],"Error! Select a Size/Color")) {
						elements[i].focus();
						return false;	break;
					}
				}
			}
			return true; break;
		} default: {
			return true; break;
		}
	}
}

function displayProvinceDropDown(country_list,province_list,list) { 
	var country=""; 
	var pre_selected = list.value; country=country_list.value; 
	var provinces=null;
	for ( var i = 0; i < province_list.length; i++ ) { 
		if ( province_list[i].name==country ) { 
			provinces = province_list[i].provinces; break; 
		} 
	}
	list.innerHTML=""; 
	if (provinces==null || provinces.length == 0 ) { 
		list.style.visibility="hidden"; 
		$("state_label").style.visibility="hidden"; 
	} else { 
		list.style.visibility="visible"; 
		$("state_label").style.visibility="visible"; 
	}
	var o = document.createElement("option"); 
	o.innerHTML="Please Select A State/Province"; 
	list.appendChild(o);
	for ( var i = 0; i < provinces.length; i++ ) { 
		var province = provinces[i]; 
		var o = document.createElement("option"); 
		o.setAttribute("value", province.id); 
		o.innerHTML=province.name; 
		if ( province.id==pre_selected ) {
			o.setAttribute("selected", "selected"); 
		}
		list.appendChild(o); 
	} 
}

function CheckRightSku()
{
	if(document.getElementById('SKUSELECTION')){
		document.getElementById('formSkuID').value=document.getElementById('SKUSELECTION').value;
	}
}

function popup(mylink, windowname) {
	if (!window.focus)return true;
	var href;
	if (typeof(mylink) == 'string')
		href=mylink;
	else
		href=mylink.href;
		window.open(href, null, 'width=400,height=500,scrollbars=yes');
	return false;
}

function callSubmit() {
	 document.frmApplicationTermsCond.submit();
}