// checkOut stage 1
var alert_fName="Please Enter First Name";
var alert_lName="Please Enter Last Name";
var alert_email="Please Enter Email";
var alert_emailError="Email not Valid";
var alert_address="Please Enter Address";
var alert_city="Please Enter City";
var alert_state="Please Choose Province/State";
var alert_zip="Please Enter zip/postal code";
var alert_phone="Please Enter Phone Number";

// checkOut stage 2
var alert_creditNum="Please Enter Card Number";
var alert_expMonth="Please Enter Card Expire Month";
var alert_expYear="Please Enter Card Expire Year";
var alert_nameOnCard="Please Enter Name On Card";
 
var confirm_order="Are You Sure You Want To Submit The Order?";

//********* menu ****************************************************************
function enable_menues_for_ie()
{
	if (document.all)
	{
        uls = document.getElementsByTagName('UL');

        for(i = 0; i < uls.length; i++)
        {
            if (uls[i].className == 'dropdown')
            {
                var lis = uls[i].getElementsByTagName('li');

                for (j = 0; j < lis.length; j++)
                {
                    if(lis[j].lastChild.tagName == 'UL')
                    {
                        lis[j].onmouseover = function() { this.lastChild.style.display = 'block'; }
                        lis[j].onmouseout = function() { this.lastChild.style.display = 'none'; }
                    }
                }
            }
        }
    }
}


window.onload = enable_menues_for_ie;
//***********************************************************************************

function getHTTPObject()
{
	try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch (e) {}
	try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch (e) {}
	try { return new XMLHttpRequest(); } catch(e) {}
	alert("XMLHttpRequest not supported");
	return null;
 }

function LoadHTML(url)
{

	var xmlHttp = getHTTPObject();
	xmlHttp.open("GET",url, false);
	xmlHttp.onreadystatechange = function()
	{
		   if (xmlHttp.readyState != 4)  { return; }
		   var serverResponse = xmlHttp.responseText;

	}
	xmlHttp.send(null);
	return xmlHttp.responseText;
}

function getShippingPrice(state, weight)
{
	url="ajaxGetShippingPrice.php?state="+state+"&weight="+weight;
	ret=LoadHTML(url);
	arr=ret.split(";");
	shipping=parseFloat(arr[0]);
	tax=parseFloat(arr[1]);
	totalPrice=document.getElementById("productsPrice").value;
	document.getElementById("shippingCost").innerHTML="$"+shipping;
	priceIncludeShipping=parseFloat(totalPrice)+shipping;
	taxSum=priceIncludeShipping*tax;
	taxSum=Math.round(taxSum*100)/100; 
	finalPrice=priceIncludeShipping+taxSum;
	
	finalPrice=Math.round(finalPrice*100)/100;
	document.getElementById("taxSpan").innerHTML="$"+taxSum;
	document.getElementById("totalPriceSpan").innerHTML="$"+finalPrice;
	document.getElementById("orderTotal").innerHTML="$"+finalPrice;
	document.getElementById("totalPrice").value=finalPrice;
	

 	
}


function checkOutStage1()
{
	curForm=document.order;
	fieldArray=new Array("fName","lName","email","address","city","state","zip","phone");
	
	for(i=0;i<fieldArray.length;i++)
	{
		curField=fieldArray[i];
		if(curForm[curField].value=="")
		{
			cMessage=eval("alert_"+curField);
			alert(cMessage);
			curForm[curField].focus();
			return false;			
		} 
	}

	 if (!checkEmail(curForm["email"].value))
		{
			alert(alert_emailError);
			curForm["email"].focus();
			return false;
		 }

	if(curForm["country"][0].checked)
		selectID="provinceSelect";
	else
		selectID="stateSelect";
		
		if(document.getElementById(selectID).selectedIndex==0)
		{
			alert(alert_state);
			return false
		 }
	return true;
		
}

function checkOutStage2()
{
	curForm=document.order;
	fieldArray=new Array("creditNum","expMonth","expYear","nameOnCard");
	for(i=0;i<fieldArray.length;i++)
	{
		curField=fieldArray[i];
		if(curForm[curField].value=="")
		{
			cMessage=eval("alert_"+curField);
			alert(cMessage); 
			curForm[curField].focus();
			return false;			
		} 
	}
	if(confirm(confirm_order))
		return true;
	else
		return true;
}

function updateShopQty(shopItem,qty)
{
	
	location.replace("shopCart.php?changeQty=true&shopItem="+shopItem+"&qty="+qty);	
}


function changeCurrency(currency)
{
	loc=location.href;
	array=loc.split('?');
	if(array[1]=="")
	{
		getStr="currency="+currency;
		window.location.replace(location.href+"?"+getStr);
	}
	else
	{
		getStr=array[1]+"&currency="+currency;
		window.location.replace(array[0]+"?"+getStr);
	}
		
	
//
//	if(loc.indexOf('?'))
//		window.location.replace(window.location+"&currency="+currency);
//	else
//		window.location.replace(window.location+"?currency="+currency);
} 


function showBigColor(type, color)
{
//	alert(type);
	colorDiv=document.getElementById("showColor");
	if(type=="image")
		colorDiv.innerHTML="<img src='"+color+"' width='70' height='70'>";
	else
		colorDiv.innerHTML="<div style='width:60px; height:60px; background-color:"+color+"'>&nbsp;</div>";
	
	
}

function changeBg(element,bg)
{
	element.bgColor=bg;
	
	
}

function popupWindow(popUrl, width, height)
{
	if (!navigator.appName.indexOf("Microsoft")) width+=20;
	height+=5;
	topVar=((screen.height / 2)-(height/2));
	leftVar=((screen.width / 2)-(width/2));
	window.open(popUrl, "PopUp", "height="+height+", width="+width+", top="+topVar+", left="+leftVar+", scrollbars=yes, status=no, location=no, resize=yes, menubar=no, titlebar=no, toolbar=no");
}

function addColors()
{
	check=document.colorList.colorCheck;
	colors=opener.addPrd.colors;
	colors.value="";
	for(i=0;i<check.length;i++)
		if(check[i].checked)
			colors.value+=check[i].value+";";
	
	window.close();
	
	
}

function checkEmail(str) {
///// function for validating email address
		var at="@"
		var dot="."
		var lat=str.indexOf(at)
		var lstr=str.length
		var ldot=str.indexOf(dot)

		if (str.indexOf(at)==-1){
		    return false
		} else if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		    return false
		} else 	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		} else  if (str.indexOf(at,(lat+1))!=-1){
		    return false
		} else 	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		   return false
		} else  if (str.indexOf(dot,(lat+2))==-1){
		    return false
		} else if (str.indexOf(" ")!=-1){
		     return false
		} else {
 		 	return true
 		}
}

function colorSelected(colorArray, value)
{
	for(j=0;j<colorArray.length-1;j++)
	if(colorArray[j]==value)
		return true; 
		
	return false;
}

function checkSelected()
{

	colors=opener.addPrd.colors;
	colorArray=colors.value.split(";");
	check=document.colorList.colorCheck;
	for(i=0;i<check.length;i++)
	{
		if(colorSelected(colorArray,check[i].value))
			check[i].checked=true;

	}
	
	
}

function selectColor(index)
{
	slctColor=document.forms["addToCart"].slctColor.options;
	slctColor[index].selected=true;
	
	
}


function colapseExpand(divID)
{
	inline_display = (navigator.appName == "Microsoft Internet Explorer") ? "inline":"table-cell";
	if (document.getElementById(divID).style.display==inline_display)
		document.getElementById(divID).style.display="none";
	else
		document.getElementById(divID).style.display= inline_display;
}

function colapseExpand2(divID)
{
	inline_display = (navigator.appName == "Microsoft Internet Explorer") ? "inline":"inline";
	if (document.getElementById(divID).style.display=="inline")
		document.getElementById(divID).style.display="none";
	else
		document.getElementById(divID).style.display= "inline";
}


function showCheckOutStage(stage)
{
	colapseExpand2("checkOut1");
	colapseExpand2("checkOut2");
	
}

function changeSelect(val)
{

	if(val=="canada")
	{
		div1="provinceList";
		div2="stateList";
		slct1="provinceSelect";
		slct2="stateSelect";
		
	}
	else
	{
		div2="provinceList";
		div1="stateList";
		slct2="provinceSelect";
		slct1="stateSelect";
	}
	 
	document.getElementById(div1).style.display="inline"
	document.getElementById(div2).style.display="none"
	document.getElementById(slct1).disabled=false;
	document.getElementById(slct2).disabled=true;
	
}

function changeRowColor(checkbox)
{
	
	if(checkbox.checked)
		rowBgColor="#EEBBBB";
	else
		rowBgColor="#FFFFFF";
	row=checkbox.parentNode.parentNode;
	row.bgColor=rowBgColor;
	
}