var refMoreCount = 0
var xmlhttp
var bolIsReady = false
var objSample
var arrSample = new Array()
var dvCartID = null

function ValidateReferee(){
	var term = document.getElementById("chkTerms")
	//alert('term checked: ' + term.checked)
	if(term.checked){
		document.getElementById("dvChkTerm").style.visibility = 'hidden'
		return true
	}
	else{
		document.getElementById("dvChkTerm").style.visibility = 'visible'
		return false
	}
	
}

function CheckReferTerm(id){
	var cid = id.substring(0,id.lastIndexOf("_")+1) + 'chkReferTerm'
	if(document.getElementById(cid).checked){
		document.getElementById('dvRefMsg').style.visibility = "hidden"
		document.getElementById(id).style.visibility = "hidden"
		return true
	}
	else{
		document.getElementById('dvRefMsg').style.visibility = "visible"
		return false
	}
		
}

function ShowMoreRefer(){
	if(document.getElementById('txtRefMore').value != '') refMoreCount = parseInt(document.getElementById('txtRefMore').value)
	if(refMoreCount < 9) refMoreCount = refMoreCount + 1
	var idx = 0
	var refID = ''
	var dv
	for(idx = 1; idx <= refMoreCount; idx ++){
		refID = 'dvRefMore' + idx;
		dv = document.getElementById(refID)
		if(dv != null) dv.style.display = 'inline'
	}
	//document.getElementById('dvRefMore1').style.display = 'inline'
	//document.getElementById('txtRefMore').value = 'true'
	document.getElementById('txtRefMore').value = refMoreCount
	//alert(document.getElementById('txtRefMore').value)
	
}

function OpenRefEmail(){
	var win = window.open("ReferralEmail.aspx", "viewRefEmail", 'width=560,height=550,menubar=no,toolbar=no,resizable=yes,scrollbars=no,status=no,directories=no,alwaysRaised=yes');
}

function ShowButton(id){
	var cid = id.substring(0,id.lastIndexOf("_")+1) + 'ibtnSubmitReferCode'
	var lid = id.substring(0,id.lastIndexOf("_")+1) + 'lblSubmit'
	document.getElementById(cid).style.visibility = "visible" 
	document.getElementById(cid).style.display = "inline" 

	if(document.getElementById(lid) != null)
		document.getElementById(lid).style.visibility = "hidden" 	
	//alert(document.getElementById(cid))
	
}

function ProcessRefCode(checked){

	if(checked){ 
		document.getElementById('dvRefCode').style.visibility = "visible"
		document.getElementById('Cart_ReferralCode1_ibtnSubmitReferCode').style.visibility = "visible"
		document.getElementById('dvRefCode').style.display = "block"
		document.getElementById('Cart_ReferralCode1_ibtnSubmitReferCode').style.display = "block"
	}
	else{
		document.getElementById('dvRefCode').style.visibility = "hidden"
		document.getElementById('Cart_ReferralCode1_ibtnSubmitReferCode').style.visibility = "hidden"
		document.getElementById('dvRefCode').style.display = "none"
		document.getElementById('Cart_ReferralCode1_ibtnSubmitReferCode').style.display = "none"

	}
	
}

function ReferCodeChange(e,id){
//debugger
	if(event.keyCode == "13"){
		ShowButton(id)
		return false
	}
	else if(e.which == "13"){
		ShowButton(id)
		return false
	}
	else
		return true
}

function TrickEnterKey(e){
	if((event.keyCode >=48 && event.keyCode <= 57) || event.keyCode == 46)
		return true
	else if(event.keyCode == 13){
		/*var len = document.links.length
		var idx = 0
		for(idx = 0; idx < len; idx++){
			if(document.links[idx].href.indexOf("#update") > -1){
				document.links[idx].click()
				return false
			}
		}*/
		//document.forms[0].submit()
		return false
	}
	else if(e.which == 13){
		return false
	}
	else
		return false
}
function CheckReferTermRer(){
	if(document.getElementById("chkTerms").checked){
		document.getElementById('dvChkTerm').style.visibility = "hidden"
		return true
	}
	else{
		document.getElementById('dvChkTerm').style.visibility = "visible"
		return false
	}
}
function SubmitForm(){
	if(document.getElementById("chkTerms").checked){
		document.getElementById('txtSubmit').value = 'true'
		document.forms[0].submit()
	}
	else
		document.getElementById('dvChkTerm').style.visibility = "visible"
}

function DisableEnterKey(e){
	if(event.keyCode == 13)
		return false
	else if(e.which == 13)
		return false
	else
		return true
}

/*********  for shopping cart qty and coverage change, calculate subtotal and product total ***************/
//document.addEventListener("keypress", KeyPressEvent, true);

function numbersonlyEnter(e, func, obj)
{
//debugger
	var key;
	var keychar;
	if (window.event)
		key = window.event.keyCode;
	else if (e){
			key = e.which;
		}
		else
			return true;
	keychar = String.fromCharCode(key);
	keychar = keychar.toLowerCase();
	//alert('key: ' + key + ', keychar: ' + keychar)
	//Enter key
	if(key == 13){
		//alert(func)
		eval(func + "(" + obj.value + ",'" + obj.id + "')")
		
		return false
	}
	// control keys
	if ((key==null) || (key==0) || (key==8) || (key==9) || (key==27) )
		return true;
	// numbers
	else if ((("0123456789.").indexOf(keychar) > -1))
		return true;
	else
		return false;
}

//shopping cart 
function CalculateTotal(qty, id){

	var prefix = id.substring(0, id.lastIndexOf("_") + 1)
	var noShipping = (document.getElementById(prefix.substring(0,prefix.indexOf("ItemRptr")) + 'lblMShipping').innerHTML == '--')
	var minQty = parseInt(document.getElementById(prefix + 'MiniQuantity').innerHTML)
	var subtotal = document.getElementById(id.substring(0, id.lastIndexOf("_") + 1) + 'itemsubtotal')
	var unitPrice = formatCurrency(document.getElementById(prefix + 'lblprice').innerHTML)
	var pupsu = parseFloat(document.getElementById(prefix + 'PriceUnitPerSellUnit').innerHTML)
	var itemID = document.getElementById(prefix + 'ItemID').innerHTML
	var sMinQty = parseFloat(document.getElementById(prefix + 'lblSMinQty').innerHTML)
	var sMinDollar = parseFloat(document.getElementById(prefix + 'lblSMinDollar').innerHTML)
	var realQty = qty
	var cov = 0
	var stotal = 0
	
	//debugger
	if(noShipping){
		unitPrice = unitPrice.replace('$', '')
		unitPrice = unitPrice.replace(',', '')
		//if qty is less the minQty, use minQty instead
		if(qty < parseInt(minQty)){
			realQty = minQty
			document.getElementById(id).value = minQty
		}

		UpdateQty(itemID, realQty)
		//alert('finish update qty , ' + document.getElementById(prefix + 'SupplierMiniOrderRow'))
		if(document.getElementById(prefix + 'SupplierMiniOrderRow'))
			document.forms[0].submit();
		else{
			//alert('realQty:' + realQty + " unitPrice:" + parseFloat(unitPrice))
			//alert("unitprice: " + unitPrice + ", subtotal: " + subtotal.innerHTML)
			stotal = formatCurrency(parseFloat(unitPrice) * realQty * pupsu)
			subtotal.innerHTML = '$' + stotal
			if(document.getElementById(prefix + 'txtCoverage') != null){
				cov = formatCurrency(realQty * pupsu)
				document.getElementById(prefix + 'txtCoverage').value = cov
			}
			//check for manuf min qty and dollar
	//alert('sMinQty: ' + sMinQty + ', sMinDollar: ' + sMinDollar + ', stotal: ' + stotal)
			if(sMinQty > 0)
				if(cov > 0 && cov < sMinQty)
					document.forms[0].submit();
					
			if(sMinDollar > 0 && stotal < sMinDollar)
				document.forms[0].submit();
		}	
	}
	else{
		//shipping price exists, submit form to clear the shipping
		document.forms[0].submit();
	}
	
	CalculateShoppingCartTotal()
}

function CalculateTotal2(cov, id){
	//alert("cov: " + cov + ",id: " + document.getElementById(id).value)
	var prefix = id.substring(0, id.lastIndexOf("_") + 1)
	var noShipping = (document.getElementById(prefix.substring(0,prefix.indexOf("ItemRptr")) + 'lblMShipping').innerHTML == '--')
	var minQty = parseInt(document.getElementById(prefix + 'MiniQuantity').innerHTML)
	var subtotal = document.getElementById(id.substring(0, id.lastIndexOf("_") + 1) + 'itemsubtotal')
	var unitPrice = formatCurrency(document.getElementById(prefix + 'lblprice').innerHTML)
	var pupsu = parseFloat(document.getElementById(prefix + 'PriceUnitPerSellUnit').innerHTML)
	var itemID = document.getElementById(prefix + 'ItemID').innerHTML
	var sMinQty = parseFloat(document.getElementById(prefix + 'lblSMinQty').innerHTML)
	var sMinDollar = parseFloat(document.getElementById(prefix + 'lblSMinDollar').innerHTML)
	var qty = 0
	//var realQty = qty

	qty = Math.floor(cov/pupsu)
	if(qty < (cov/pupsu))
		qty = qty + 1
	
	if(qty < minQty)
		qty = minQty

	subtotal.innerHTML = '$' + formatCurrency(unitPrice * pupsu * qty)
	document.getElementById(id).value = formatCurrency(qty * pupsu)
	document.getElementById(prefix + 'txtQuantity').value = qty
	
	if(document.getElementById(prefix + 'SupplierMiniOrderRow'))
			document.forms[0].submit();
	else{
	//alert("sMinQty: " + sMinQty + ", sMinDollar: " + sMinDollar)
			if(sMinQty > 0)
				if(cov > 0 && cov < sMinQty)
					document.forms[0].submit();
					
			stotal = formatCurrency(parseFloat(unitPrice) * qty * pupsu)		
			if(sMinDollar > 0 && stotal < sMinDollar)
				document.forms[0].submit();
	}
	
	//update grand total
	CalculateShoppingCartTotal()

}

function CalculateShoppingCartTotal(){
	var mprefix, count, idx, id, msubtotal
	//debugger
	if(document.all){
		//IE
		mprefix = ''
		total = 0
		count = document.all.length
		for(idx = 0; idx < count; idx++){
			if(document.all[idx].tagName == 'SPAN'){
				id = document.all[idx].id
				//alert(id)
				if(id.indexOf('itemsubtotal') > -1){
					if(mprefix != id.substring(0, id.indexOf('ItemRptr'))) {
						if(mprefix != ''){
							//update lblMSubTotal
							document.getElementById(mprefix + 'lblMSubTotal').innerHTML = '$' + formatCurrency(total)
						}
												
						total = parseFloat(formatCurrency(document.all[idx].innerHTML))
						mprefix = id.substring(0, id.indexOf('ItemRptr'))
						
					}
					else
						total = total + parseFloat(formatCurrency(document.all[idx].innerHTML))
				}
			}
		}
		//udpate last lblMSubTotal
		document.getElementById(mprefix + 'lblMSubTotal').innerHTML = '$' + formatCurrency(total)		
	}
	else{
		//FireFox
		var iprefix
		count = document.forms[0].elements.length
		mprefix = ''
		total = 0
		iprefix = ''
		for(i = 0; i < count; i++){
			id = document.forms[0].elements[i].id
			if(id.indexOf('txtQuantity') > -1){
				//alert('mprefix: ' + mprefix + ', id:' + id)
				if(mprefix != id.substring(0, id.indexOf('ItemRptr'))){
				//alert('mprefix changed, mprefix: ' + mprefix + ', total: ' + total)
					
					if(mprefix != ''){
						
						//alert(document.getElementById(mprefix.substring(0,mprefix.indexOf('ItemRptr')) + 'lblMSubTotal'))
						document.getElementById(mprefix + 'lblMSubTotal').innerHTML = '$' + formatCurrency(total)
					}
					
					mprefix = id.substring(0, id.indexOf('ItemRptr'))
					iprefix = id.substring(0, id.indexOf('txtQuantity'))
					total = parseFloat(formatCurrency(document.getElementById(iprefix + 'itemsubtotal').innerHTML))
					//alert('total: ' + total + ', mprefix: ' + mprefix)
				}
				else{
					//alert('1 total: ' + total)
					iprefix = id.substring(0, id.indexOf('txtQuantity'))
					total = total + parseFloat(formatCurrency(document.getElementById(iprefix + 'itemsubtotal').innerHTML))
					//alert('2 total: ' + total)
				}
			}
		}
		//udpate last lblMSubTotal
		document.getElementById(mprefix.substring(0,mprefix.indexOf('ItemRptr')) + 'lblMSubTotal').innerHTML = '$' + formatCurrency(total)
		
	}
	
}


function CalculateTotalPDP(qty, id){
//debugger
	
	var prefix = id.substring(0, id.lastIndexOf("_") + 1 )
	var itemtotal = document.getElementById(prefix + 'ItemSubtotal')
	var minOrder = parseFloat(document.getElementById(prefix + 'lblMiniOrder').innerHTML)
	var pupsu = 1
	var pprice = document.getElementById(prefix + 'PPrice').innerHTML
	var unitPrice = parseFloat(formatCurrency(pprice.substring(1, pprice.lastIndexOf("/"))))
	
	if(document.getElementById(prefix + 'lblPUPSU'))
		pupsu = parseFloat(document.getElementById(prefix + 'lblPUPSU').innerHTML)
	if(qty < minOrder)
		qty = minOrder
		
//	alert("prefix: " + prefix + ",unitPrice: " + unitPrice + ", pupsu : " + pupsu + ", qty: " + qty)
	itemtotal.innerHTML = '<b>$' + formatCurrency(unitPrice * pupsu * qty) + '</b>'
	document.getElementById(id).value = qty
	if(document.getElementById(prefix + 'ItemCoverage') != null)
		document.getElementById(prefix + 'ItemCoverage').value = formatCurrency(qty * pupsu)
	//update grand total
	CalculateProductTotal()
	//Call to webservice to set quontity in session 
	//used for optimost option A (pguo-stgol)
	SetItemQty(qty)
}




function CalculateTotalPDP2(cov, id){
	var prefix = id.substring(0, id.lastIndexOf("_") + 1 )
	var itemtotal = document.getElementById(prefix + 'ItemSubtotal')
	var minOrder = parseFloat(document.getElementById(prefix + 'lblMiniOrder').innerHTML)
	var pupsu = parseFloat(document.getElementById(prefix + 'lblPUPSU').innerHTML)
	var pprice = document.getElementById(prefix + 'PPrice').innerHTML
	var unitPrice = parseFloat(formatCurrency(pprice.substring(1, pprice.lastIndexOf("/"))))
	var qty = 0
	
	
	qty = Math.floor(cov/pupsu)
	if(qty < (cov/pupsu))
		qty = qty + 1
	
	if(qty < minOrder)
		qty = minOrder

	itemtotal.innerHTML = '$' + formatCurrency(unitPrice * pupsu * qty)
	document.getElementById(id).value = formatCurrency(qty * pupsu)
	document.getElementById(prefix + 'ItemQuantity').value = qty
	
	//update grand total
	CalculateProductTotal()
    //Call to webservice to set coverage in session 
	//used for optimost option A (pguo-stgol)
	SetItemQty(qty)

}

function CalculateTotalPDPAcc(qty, id){
	//debugger
	var prefix = id.substring(0, id.lastIndexOf("_") + 1 )
	if(qty == "" || qty == "0"){
		//clear qty, subtotal
		if(document.getElementById(prefix + 'ItemCoverage') != null)
			document.getElementById(prefix + 'ItemCoverage').value = ''

		document.getElementById(prefix + 'ItemSubtotal').innerHTML = "$0.00"
		//CalculateProductTotal()		
	}
	else{
		var itemtotal = document.getElementById(prefix + 'ItemSubtotal')
		var minOrder = parseFloat(document.getElementById(prefix + 'ItemMinimum').innerHTML)
		var pupsu = parseFloat(document.getElementById(prefix + 'ItemPUPSU').innerHTML)
		var pprice = document.getElementById(prefix + 'ItemPrice').innerHTML
		var unitPrice = parseFloat(formatCurrency(pprice.substring(1)))
		
		if(qty < minOrder)
			qty = minOrder
			
	//	alert("prefix: " + prefix + ",unitPrice: " + unitPrice + ", pupsu : " + pupsu + ", qty: " + qty)
		itemtotal.innerHTML = '$' + formatCurrency(unitPrice * pupsu * qty)
		document.getElementById(id).value = qty
		if(document.getElementById(prefix + 'ItemCoverage') != null)
			document.getElementById(prefix + 'ItemCoverage').value = formatCurrency(qty * pupsu)
		
	}
	
	//update grand total
	CalculateProductTotal()
}

function CalculateTotalAcc2(cov, id){
	var prefix = id.substring(0, id.lastIndexOf("_") + 1 )
	if(cov == "" || cov == "0"){
	//alert(document.getElementById(prefix + 'ItemSubtotal'))
		document.getElementById(prefix + 'ItemSubtotal').innerHTML = "$0.00"
		document.getElementById(prefix + 'ItemQuantity').value = ''
	}
	else{
		var itemtotal = document.getElementById(prefix + 'ItemSubtotal')
		var minOrder = parseFloat(document.getElementById(prefix + 'ItemMinimum').innerHTML)
		var pupsu = parseFloat(document.getElementById(prefix + 'ItemPUPSU').innerHTML)
		var pprice = document.getElementById(prefix + 'ItemPrice').innerHTML
		var unitPrice = parseFloat(formatCurrency(pprice.substring(1)))
		var qty = 0
		
		qty = Math.floor(cov/pupsu)
		if(qty < (cov/pupsu))
			qty = qty + 1
		
		if(qty < minOrder)
			qty = minOrder

		itemtotal.innerHTML = '$' + formatCurrency(unitPrice * pupsu * qty)
		document.getElementById(id).value = formatCurrency(qty * pupsu)
		document.getElementById(prefix + 'ItemQuantity').value = qty
		
		//alert(cov + ',id: ' + id + ',minOrder: ' + minOrder)
	}
	
	//update grand total
	CalculateProductTotal()
}


function UpdateQty(itemID, qty){
//debugger
	var url = path + "/Services/WSShoppingCart.asmx/UpdateCartItemQty?itemID=" + itemID + "&qty=" + qty
	//alert(url)
	CreateXMLHTTP(url, UpdateQtyChanged)
	
}

function CreateXMLHTTP(url, changeEventHandler){
	var request = null;
    //Create XmlHttpxmlHttp object
    var ua = navigator.userAgent.toLowerCase();
    if (window.XMLHttpRequest) //IE7, Safari, Mozilla
    {
   // debugger
		if(xmlhttp == null || bolIsReady){
			//xmlhttp=new XMLHttpRequest();
			SendXMLHTTP(url, changeEventHandler);
		}
		else {
			//window.setTimeout('SendXMLHTTP("' + url + '", ' + changeEventHandler + ')', 2000)
			window.setTimeout('CreateXMLHTTP("' + url + '", ' + changeEventHandler + ')', 2000)
		}
				
      /* try{
			bolIsReady = false;
			xmlhttp.onreadystatechange=changeEventHandler;
			xmlhttp.open("GET",url,true);
			xmlhttp.setRequestHeader("myVal","BuildDirect");
			xmlhttp.send(null);
       }
       catch(ex){
			//alert(ex)
       }*/
    
    }
    else //IE5, IE6
    {
		if(xmlhttp == null || bolIsReady){
				
			SendXMLHttpIEOld(url, changeEventHandler);
		}
		else
			window.setTimeout('CreateXMLHTTP("' + url + '", ' + changeEventHandler + ')', 2000)
			
			/*if (ua.indexOf('msie 5') == -1)
				xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
			else
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    
		if(xmlhttp)
		{
			try
			{
				bolIsReady = false
				xmlhttp.onreadystatechange=changeEventHandler;
				xmlhttp.open("GET",url,false);
				xmlhttp.send();
			}
			catch(e){
				//alert(e)
			} 
		}*/
    }    
}

function SendXMLHTTP(url, changeEventHandler){
 try{
 //debugger
			xmlhttp=new XMLHttpRequest();
			bolIsReady = false;
			xmlhttp.onreadystatechange=changeEventHandler;
			xmlhttp.open("GET",url,true);
			xmlhttp.setRequestHeader("myVal","BuildDirect");
			xmlhttp.send(null);
       }
       catch(ex){
			//alert(ex)
       }
}

function SendXMLHttpIEOld(url, changeEventHandler){
	if (window.ActiveXObject){
		try{
			xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e){
			try{
				xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
				}
			catch(e){
				
			}
		}
	}

     
	if(xmlhttp)
	{
			try
			{
				//alert('SendXMLHttpIEOld url: ' + url)

				bolIsReady = false
				xmlhttp.onreadystatechange = changeEventHandler;
				xmlhttp.open("GET",url,true);
				xmlhttp.send();

			}
			catch(e){
				alert(e)
			} 
	}
}

function CreateXMLIEOld(){
	if (ua.indexOf('msie 5') == -1)
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	else
		xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}

function UpdateQtyChanged(){
	if(xmlhttp.readyState==4)
    {
		bolIsReady = true
		if(xmlhttp.status==200){
           var myxml = xmlhttp.responseXML;
           var mytext = xmlhttp.responseText;
           
        }
    }
}

function CalculateProductTotal(){
	var i, count,pprice, qty
	var total = 0
	var pupsu
	var prefix, id
	
	//IE
	if(document.all){
		count = document.all.length
		for(i = 0; i < count; i++){
			if(document.all[i].tagName == 'SPAN')
				if(document.all[i].id.indexOf('ItemSubtotal') > -1){
					//alert(document.all[i].innerHTML)
					stotal = document.all[i].innerHTML.substring(document.all[i].innerHTML.indexOf("$"),document.all[i].innerHTML.indexOf("</")) 
					if(stotal == '') stotal = document.all[i].innerHTML.substring(1)
					//alert(stotal)
					total = total + parseFloat(formatCurrency(stotal))
					//alert(total)
				}
		}
	}
	else{
		var itemTotal = document.getElementById('SelectedProductItemDetail_ItemSubtotal').innerHTML
		total = parseFloat(formatCurrency(itemTotal.substring(itemTotal.indexOf("$") + 1, itemTotal.lastIndexOf("</"))))
		//alert('itemTotal: ' + itemTotal + ', total: ' + total)
		count = document.forms[0].elements.length
		for(i = 0; i < count; i++){
			id = document.forms[0].elements[i].id
			if(id.indexOf('ItemQuantity') > -1){
				prefix = id.substring(0, id.lastIndexOf("_") + 1 )
				if(document.getElementById(prefix + 'ItemPrice')){
					pprice = document.getElementById(prefix + 'ItemPrice').innerHTML
					pupsu = parseFloat(document.getElementById(prefix + 'ItemPUPSU').innerHTML)
					qty = parseFloat(formatCurrency(document.getElementById(id).value))
					//alert('subtotal: ' + qty * pupsu * parseFloat(pprice.substring(1)))
					total = total + qty * pupsu * parseFloat(pprice.substring(1))
				}
			}
		}
	}
	
	//alert('total: ' + total)
	if(total > 0)	
		if(document.getElementById('GrandTotal') != null)
			document.getElementById('GrandTotal').innerHTML = '$' + formatCurrency(total)
}

function formatCurrency(num) 
{
	var isNegative = false;
	num = num.toString().replace(/\$|\,/g,'');
	if(isNaN(num)) {num = new Number(0)} else { num = new Number(num); };
	if (num < 0) 
	{
		isNegative = true;
		num = num.toString().replace('-','');
	}
	cents = Math.floor((num*100+0.5)%100); 
	num = Math.floor(num).toString();
	if(cents < 10) cents = "0" + cents; 
	if (isNegative) 
		return ('-' + num + '.' + cents); 
	else
		return (num + '.' + cents);
}


/*********  end ****************/

//OPTIMOST 
function SetItemQty(qty){
 //Call to webservice to set quontity or coverage in session 
//optimost option A (pguo-stgol)
//debugger
	var url = path + "/Services/WSProduct.asmx/SetQty?qty=" + qty
	CreateXMLHTTP(url, SetQtyChanged)

}

function SetQtyChanged(){
if(xmlhttp.readyState==4)
    {
		bolIsReady = true
		if(xmlhttp.status==200){
           var myxml = xmlhttp.responseXML;
           var mytext = xmlhttp.responseText;
           
        }
    }
}

//End OPTIMOST 


//**************** one click sample functions ***************
function AddToCartClick(configID,supplierID,categoryID,orderTypeID,deliveryType, obj){
	//alert('add to sample clicked! configID:'+configID + ',supplierID:' + supplierID + ',categoryID: ' + categoryID + ',orderTypeID: ' + orderTypeID + ',DeliveryType: ' + deliveryType )
	//alert(obj + ',src: ' + obj.src)
	//debugger
	objSample = obj
	var src = obj.src
	var loadCart = false
	if(src.indexOf("add_sample.gif") > -1){
		//alert('path:' + path)
		if(sampleCount < maxSampleCount){
			sampleCount = sampleCount + 1
	
			arrSample.push(obj.id)
			//alert('arrSample.push: ' + arrSample.length + ',ID: ' + obj.id)
			obj.src = src.substring(0, src.lastIndexOf("/")+1) + "ajax-loader2.gif"//"sample_added.gif"
			
			//if(screen.width>=1200 && window.location.href.toLowerCase().indexOf("result") > -1)
			if(screen.width>=1200 && getCookieValueByName('SiteRefferUrl').toLowerCase().indexOf("result") > -1)
				loadCart = true
			else
				loadCart = false
				
			CreateXMLHTTP(path + "/Services/OneClickSample.aspx?configId=" + configID + "&supplierID=" + supplierID + 
				          "&deliveryType=" + deliveryType + "&orderTypeId=" + orderTypeID + "&categoryId=" + 
				          categoryID + "&ran="  + Math.random() + "&loadCart=" + loadCart,
				           AddSampleChangeEvent)
		}
		else
			alert("Sorry, you cannot order more than 40 samples at a time. If you require more samples, please contact a customer service representative")
	}
	else if(src.indexOf("sample_added.gif") > -1){
		//sample already add to cart, do redirect to cart page
		window.location.href = cartPath + '/Order/Cart.aspx'
	}
	else if(src.indexOf("please_call.gif") > -1){
		//please call displayed, redirect to contact us page
		window.location.href = cartPath + '/ContactUs.aspx'
	}

}


function AddSampleChangeEvent(){
	if(xmlhttp.readyState==4){
		bolIsReady = true
		if(xmlhttp.status==200){
			var myxml = xmlhttp.responseXML;
			var mytext = xmlhttp.responseText;
						
			//if( mytext == 'true' || mytext == 'True'){
			if( mytext == "false"){
				//error when adding sample or binding mini shopping cart
				alert('Sorry, we had a problem adding this sample to your cart. Please click your browser refresh button and then try again')
				ClearSampleImage();
			}
			else {
				//successful add sample
				//alert(mytext)
				SwapSampleImage();
				if(mytext != "true")
				    LoadMiniCart(mytext);
			}
		}
		else{
			alert('Sorry, we had a problem adding this sample to your cart. Please click your browser refresh button and then try again')
			ClearSampleImage();
		}
		
		xmlhttp = null;
		
	}
}

function LoadMiniCart(text){
	if(text != ""){
		if(dvCartID != null){
			var dvCart = document.getElementById(dvCartID)//flyoutright
			//alert('dvCart:' + dvCart)
			if(dvCart){
			//debuggers
				if (screen.width>=1200)
				{
					dvCart.style.visibility = 'visible'
					dvCart.innerHTML = text
			
					// setting Mini Shopping Cart position for Firefox	
					//	if(!document.all){
					//		var left=document.body.clientWidth
					//		if(left > 780)
					//			left = (left - 780)/2 + 785
					//		else
					//			left = 785
					//		document.getElementById("cart_container").style.left= left + "px"
						
					//	}
					
					if (!document.all)
					{
						document.getElementById("cart_container").className = 'minicart'
						document.getElementById("page_body").className = 'container_sc'
						document.getElementById("page_left").className = 'container_blank'
						document.getElementById("page_main").className = 'container_right'
					}
					else
						document.getElementById("cart_container").className = 'minicart_ie'	
						
					//document.getElementById("result_container").className = 'container_sc'
					//document.getElementById("result_left").className = 'container_blank'
					//document.getElementById("result_right").className = 'container_right'
					
				}
				else
				{
				//if (screen.width<=800)
					dvCart.style.visibility = 'hidden'
					document.getElementById("cart_container").className = 'minicart_hidden'
				}
			}
		}
	}
	
}

function SwapSampleImage(){
	var mySample = objSample
	var src = objSample.src
	if(arrSample.length > 0){
		mySample = document.getElementById(arrSample.shift())
		if(mySample)
			src = mySample.src
	}
	
	mySample.src = src.substring(0, src.lastIndexOf("/")+1) + "sample_added.gif"
	//alert('arrSample.length: ' + arrSample.length + ',mySample.src: ' + mySample.src + ',ID: ' + mySample.id)
}

function ClearSampleImage(){
	if(arrSample.length > 0){
		var len = arrSample.length 
		var idx
		for(idx = 0; idx < len; idx++){
			mySample = document.getElementById(arrSample.shift())
			if(mySample){
				src = mySample.src
				mySample.src = src.substring(0, src.lastIndexOf("/")+1) + "add_sample.gif"
			}
		}
	}
}

function displayEnlargeImage(enlargeDiv, e)
{
	var largeDv = document.getElementById(enlargeDiv)
	largeDv.style.display ="block"
}
function closeEnlargeImage(imageDiv)
{
	var enlargeDiv = document.getElementById(imageDiv)
	enlargeDiv.style.display="none"
}
function changeColor(elementId, colorCode)
{
	var elementDiv = document.getElementById(elementId)
	elementDiv.style.color = colorCode
}
function clickblePageUrl(pageUrl)
{
	location.href=pageUrl
}
function changeImage(elementId,imageName)
{
	var elementDiv = document.getElementById(elementId)
	elementDiv.src="snippet/Common_Images/"+ imageName
}

//****************** open catalog window **************
function OpenCatalogWindow(){
	var hasImg = 0
	var hasBrand = 0
	var hasMinOrder = 0
	var hasPrice = 0
	
//alert('OpenCatalogWindow ' + document.getElementById('chkBrandName').checked)
	if(document.getElementById('chkBrandName').checked) hasBrand = 1
	if(document.getElementById('chkProdImage').checked) hasImg = 1
	if(document.getElementById('chkMinOrder').checked) hasMinOrder = 1
	if(document.getElementById('chkPrice').checked) hasPrice = 1

	openNewResizeBrowser('../catalog.aspx?img=' + hasImg + '&brand=' + hasBrand + '&minOrder=' + hasMinOrder + '&price=' +  hasPrice , 835, 800)
}

//***********************************************************

/*function openNewBrowser(path,width,height )
{
	var dimension ='width=' + width + ', height='+height
	window.open( path, "_blank", dimension)
}*/