var xmlHttp
var choice
var gParentId
//var baseurl = "http://"
//****************************
// Function to create XmlHttpObject starts
function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari, NetScape
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 //Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}
//****************************
//Function to create XmlHttpObject ends

//****************************
// Function to Get all categories

function getAllCategories()
{ 
choice = 0
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 }
var url="home.php"

xmlHttp.onreadystatechange=Getdata
xmlHttp.open("POST",url,true)
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
//xmlHttp.send("param1="+newusr+"&param2="+bid+"&param3="+rid+"&param4="+namef+"&param5="+namem+"&param6="+namel+"&param7="+eml)
xmlHttp.send(null)
}

//****************************
//Function to get all categories ends

//****************************
// Function to Get category

function getCategory(Catid)
{ 
choice = 0
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 }
var url="categorydetail.php"

xmlHttp.onreadystatechange=Getdata
xmlHttp.open("POST",url,true)
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
xmlHttp.send("Code="+Catid)
//xmlHttp.send(null)
}

//****************************
//Function to get category detail ends

//****************************
// Function to Get Product

function getProduct(Prodid)
{ 
choice = 0
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 }
var url="productdetail.php"

xmlHttp.onreadystatechange=Getdata
xmlHttp.open("POST",url,true)
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
xmlHttp.send("Code="+Prodid)
//xmlHttp.send(null)
}



//****************************
//Function to get product detail ends

// Function to View Cart

function viewCart(Sessid)
{ 
choice = 0
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 }
var url="viewcart.php"

xmlHttp.onreadystatechange=Getdata
xmlHttp.open("POST",url,true)
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
xmlHttp.send("Code="+Sessid)
}

// Function to Get Contact

function getContact()
{ 
choice = 0
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 }
var url="contactus.php"

xmlHttp.onreadystatechange=Getdata
xmlHttp.open("POST",url,true)
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
xmlHttp.send(null)
}


// Function to Get About Us

function getAbout()
{ 
choice = 0
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 }
var url="aboutus.php"

xmlHttp.onreadystatechange=Getdata
xmlHttp.open("POST",url,true)
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
xmlHttp.send(null)
}

//Function to get special product

function getSpecials()
{ 
choice = 0
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 }
var url="speciallist.php"

xmlHttp.onreadystatechange=Getdata
xmlHttp.open("POST",url,true)
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
xmlHttp.send(null)
}
function getSearch()
{ 
choice = 0
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 }
 
 var scat = document.searchvresult.sltCategory.value;
  var svalue = document.searchvresult.skeyword.value;
  
var url="search_result.php";

url = url+"?scat="+scat+"&svalue="+svalue;

xmlHttp.onreadystatechange=Getdata
xmlHttp.open("POST",url,true)
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
xmlHttp.send(null)
}


//Function to add product
function addProduct(ParentId,ProdId,ModeId,Qty,Price)
{
	//alert("Code=" + ProdId + "&Mode=" + ModeId + "&Quantity=" + Qty + "&UnitPrice=" + Price);
gParentId = ParentId

if (ModeId == 1){
choice = 1
}
else {
	choice = 0
}

xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 }
var url="addproduct.php"

xmlHttp.onreadystatechange=Getdata
xmlHttp.open("POST",url,true)
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
xmlHttp.send("Code=" + ProdId + "&Mode=" + ModeId + "&Quantity=" + Qty + "&UnitPrice=" + Price)
//xmlHttp.send(null)*/

}

//Add Special product 
function addSpeProduct(ParentId,ProdId,ModeId,Qty,Price)
{
	alert("Code=" + ProdId + "&Mode=" + ModeId + "&Quantity=" + Qty + "&UnitPrice=" + Price);
gParentId = ParentId

if (ModeId == 1){
choice = 1
}
else {
	choice = 0
}

xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 }
var url="add_spe_product.php";
alert(url);

xmlHttp.onreadystatechange=Getdata
xmlHttp.open("POST",url,true)
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
xmlHttp.send("Code=" + ProdId + "&Mode=" + ModeId + "&Quantity=" + Qty + "&UnitPrice=" + Price)
//xmlHttp.send(null)*/

}

//Add Special Product end

function cartData(SessId)
{
choice = 2
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 }
var url="cartdata.php"

xmlHttp.onreadystatechange=Getdata
xmlHttp.open("POST",url,true)
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
xmlHttp.send("Code=" + SessId)
//xmlHttp.send(null)*/
}

//****************************
//Function to get product detail ends

function checkall(myform)
{
	var iCnt= myform.elements.length;
	
	for (var i=0;i<iCnt;i++)
	{
		myform.elements[i].checked=myform.chkselectall.checked;
	}
}

function IsChecked(myform){
	iCnt= myform.elements.length;
	var cnt;
	cnt=0;
	for (var i=0;i<iCnt;i++){
		if (myform.elements[i].checked){
			cnt++;
			if (cnt>=2)
			  break;
		}
	}
	if (i<iCnt)
		return cnt;
	else{
		if (cnt>0)
			return cnt;
		else
			return -1;
	}
}

function doDelete(myform,SessId,chkarr,allData){
	if(allData){
		if(confirm("Do you want to remove all products from your cart"))
		cartDelete(SessId,chkarr,allData)
	}
	else {	
	if (IsChecked(myform)==-1){
		alert("Please select the record to be deleted");
	}
	else
	var blnConfirm = confirm("Do you want to delete?");
	if (blnConfirm){
		cartDelete(SessId,chkarr,allData)
	}
	}
}

function cartDelete(SessId,chkarr,allData)
{

choice = 0
var tmparr= "";
var arrlength = chkarr.length;

if (!arrlength){
	tmparr =  "'" + chkarr.value + "'"
}
else{
	for (ctr = 0; ctr < arrlength; ctr = ctr + 1){
		if(chkarr[ctr].checked){
			if (tmparr!="") tmparr += ",";
 			tmparr +=  "'" + chkarr[ctr].value + "'"
		}
	}
}

xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 }
var url="deletecart.php"

xmlHttp.onreadystatechange=Getdata
xmlHttp.open("POST",url,true)
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
xmlHttp.send("Sess=" + SessId + "&SArr=" + tmparr + "&Cart=" + allData)
//xmlHttp.send(null)*/
}

function calShipCost(SessID)
{
	var bselIdx = frmcart.sltship.selectedIndex;
   	var bSel = frmcart.sltship.options[bselIdx].value;
	
	choice = 3
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null) {
 		alert ("Browser does not support HTTP Request")
 	return
 	}
	
	var url="shipcost.php?Code=" + SessID + "&Type=" + bSel;

	xmlHttp.onreadystatechange=Getdata
	xmlHttp.open("POST",url,true)
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
	xmlHttp.send(null)	

	return
	
}

function chkshipping()
{
	if (frmcart.sltship.options[frmcart.sltship.selectedIndex].value == 0)
	{
		alert("Please Select Shipping Option");
		frmcart.sltship.focus;
	}
	else
	{
		ship(frmcart.shipcost.value,frmcart.carttotal.value,frmcart.grandtotal.value);
		return
	}	
}

function ship(shiptotal, carttotal, grandtotal)
{ 
choice = 0
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 }
var url="shippingdetail.php"

xmlHttp.onreadystatechange=Getdata
xmlHttp.open("POST",url,true)
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
xmlHttp.send("ShipCost=" + shiptotal + "&CartCost=" + carttotal + "&GrandTotal=" + grandtotal)
//xmlHttp.send(null)
}

function shipConfirm(tosend)
{ 
choice = 0
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
 {
 alert ("Browser does not support HTTP Request")
 return
 }
var url="shipconfirm.php"

xmlHttp.onreadystatechange=Getdata
xmlHttp.open("POST",url,true)
xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
xmlHttp.send("Data="+tosend)
//xmlHttp.send(null)
}

//----------------------------
function Getdata() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{ 
 if (choice==1)
 {
  getCategory(gParentId)
 }
 else if (choice==2)
  document.getElementById("cData").innerHTML=xmlHttp.responseText 
 else if (choice==3)
  document.getElementById("sData").innerHTML=xmlHttp.responseText 
 else
  document.getElementById("divdata").innerHTML=xmlHttp.responseText 
} 
}
//****************************
//Function to get data ends

//////////////////////////////////////////////////////////////////
//VALIDATION FUNCTIONS
//////////////////////////////////////////////////////////////////
function ShowError(element, message) {
	alert(message);
	element.focus();
}

function isInteger(element){  
	var reInteger = /^\d+$/;
    if(element.value.match(reInteger))
		return true;
	else
		return false;
}

function chkData(form) {
	
	var passed = false;
	var mysend1;
	var mysend2;
	var mysend3;
	var mysend4;
	var mysend5;
	var mysend6;
	var mysend7;
	var mysend8;
	var tosend;
			
	if (form.txtbfname.value == "") {
	ShowError(form.txtbfname, "Please enter first name in shipping details.");
	}
	else if (form.txtbsname.value == "") {
	ShowError(form.txtbsname, "Please enter last name in shipping details.");
	}
	else if (form.txtbadd1.value == "") {
	ShowError(form.txtbadd1, "Please enter address1 in shipping details.");
	}
	else if (form.txtbcity.value == "") {
	ShowError(form.txtbcity, "Please enter city in shipping details.");
	}
	else if (form.txtbstate.selectedIndex == 0) {
	ShowError(form.txtbstate, "Please enter state in shipping details.");
	}
	else if (form.txtbzip.value == "") {
	ShowError(form.txtbzip, "Please enter zip in shipping details.");
	}
	else if (form.txtbphone.value == "") {
	ShowError(form.txtbphone, "Please enter phone number in shipping details.");
	}
	else if (form.txtbemail.value =="" || 
			 form.txtbemail.value.indexOf("@") == -1 ||
			 form.txtbemail.value.indexOf(".") == -1) {
		ShowError(form.txtbemail, "Please enter proper email address.");
	}
	else if (form.txtbcnfemail.value =="" || 
			 form.txtbcnfemail.value.indexOf("@") == -1 ||
			 form.txtbcnfemail.value.indexOf(".") == -1) {
		ShowError(form.txtbcnfemail, "Please enter proper confirm email address.");
	}
	else if (form.txtbcnfemail.value != form.txtbemail.value) {
		ShowError(form.txtbcnfemail, "Confirm Email does not match.");
	}
/*	
	else if (form.txtpfname.value == "") {
	ShowError(form.txtpfname, "Please enter first name in payment details.");
	}
	else if (form.txtpmname.value == "") {
	ShowError(form.txtpmname, "Please enter middle name in payment details.");
	}
	else if (form.txtpsname.value == "") {
	ShowError(form.txtpsname, "Please enter last name in payment details.");
	}
	else if (form.txtpadd1.value == "") {
	ShowError(form.txtpadd1, "Please enter address1 in payment details.");
	}
	else if (form.txtpcity.value == "") {
	ShowError(form.txtpcity, "Please enter city in payment details.");
	}
	else if (form.txtpstate.value == "") {
	ShowError(form.txtpstate, "Please enter state in payment details.");
	}
	else if (form.txtpzip.value == "") {
	ShowError(form.txtpzip, "Please enter zip in payment details.");
	}
	else if (form.txtpphone.value == "") {
	ShowError(form.txtpphone, "Please enter phone number in payment details.");
	}
	else if (form.txtpemail.value =="" || 
			 form.txtpemail.value.indexOf("@") == -1 ||
			 form.txtpemail.value.indexOf(".") == -1) {
		ShowError(form.txtpemail, "Please enter proper email address.");
	}
	else if (form.txtpcnfemail.value =="" || 
			 form.txtpcnfemail.value.indexOf("@") == -1 ||
			 form.txtpcnfemail.value.indexOf(".") == -1) {
		ShowError(form.txtpcnfemail, "Please enter proper confirm email address.");
	}
	else if (form.txtpcnfemail.value != form.txtpemail.value) {
		ShowError(form.txtpcnfemail, "Confirm Email does not match.");
	}
*/
	else {
			passed = true;
			
			var bselIdx = form.sltbcountry.selectedIndex;
   			var bSel = form.sltbcountry.options[bselIdx].text;
			var pselIdx = 0;//form.sltpcountry.selectedIndex;
   			var pSel = 0; //form.sltpcountry.options[pselIdx].text;
			bsadd1 = form.txtbadd1.value
			bsadd1 = bsadd1.replace(/,/g, "*cm*")
			//bsadd2 = form.txtbadd2.value
			//bsadd2 = bsadd2.replace(/,/g, "*cm*")
			//psadd1 = form.txtpadd1.value
			//psadd1 = psadd1.replace(/,/g, "*cm*")
			//psadd2 = form.txtpadd2.value
			//psadd2 = psadd2.replace(/,/g, "*cm*")
			
			mysend1 = form.txtbfname.value + "," + form.txtbmname.value + "," + form.txtbsname.value
			mysend2 = bsadd1 + "," + form.txtbcity.value
			mysend3 = form.txtbstate.value + ","  + form.txtbzip.value + "," + bSel + "," + form.txtshipcost.value
			mysend4 = form.txtbphone.value + "," + form.txtbemail.value
			mysend5 = '';//form.txtpfname.value + "," + form.txtpmname.value + "," + form.txtpsname.value
			mysend6 = '';//psadd1 + "," + form.txtpcity.value
			mysend7 = '';//form.txtpstate.value + "," + form.txtpzip.value + "," + pSel
			mysend8 = '';//form.txtpphone.value
			
			tosend = mysend1 + "," + mysend2 + "," + mysend3  + "," + mysend4 + "," + mysend5 + "," + mysend6 + "," + mysend7 + "," + mysend8

			var st = document.getElementById("txtbstate").options[document.getElementById("txtbstate").selectedIndex].value;

			if(st.toUpperCase()=="AK" || st.toUpperCase()=="A.K." || st.toUpperCase()=="AK" || st.toUpperCase()=="ALASKA" || st.toUpperCase()=="HI" || st.toUpperCase()=="H.I." || st.toUpperCase()=="HAWAII" || st.toUpperCase()=="PR" || st.toUpperCase()=="P.R." || st.toUpperCase()=="PUERTO RICO")
			{
				
			}
			else
			{
				document.getElementById("txttaxcost").value=parseFloat(document.getElementById("txttaxcost").value)*parseFloat(document.getElementById("txtcartcost").value);
			}
			var val1 = parseFloat(document.getElementById("txttaxcost").value);
			var val2 = parseFloat(document.getElementById("txtsurcharge").value);
			var val3 = parseFloat(document.getElementById("txtgrandtotal").value);

			document.getElementById("txtgrandtotal").value=val1 + val2 + val3;
			form.submit()
			return true;
			//shipConfirm(tosend)
		 }
return passed;
}


function fillData(form,chk) {
	var passed = false;
	
	if (chk.checked) {
	form.txtpfname.value = 	form.txtbfname.value;
	form.txtpmname.value =  form.txtbmname.value;
	form.txtpsname.value = 	form.txtbsname.value
	form.txtpadd1.value = 	form.txtbadd1.value
	form.txtpadd2.value = 	form.txtbadd2.value
	//form.txtpadd2.disabled=true;
	form.txtpcity.value = 	form.txtbcity.value
	form.txtpstate.selectedIndex = 	form.txtbstate.selectedIndex
	form.txtpzip.value = 	form.txtbzip.value
	form.txtpphone.value = 	form.txtbphone.value
	form.txtpemail.value = 	form.txtbemail.value
	form.txtpcnfemail.value = form.txtbcnfemail.value
	}
	else{
	form.txtpfname.value = 	"";
	form.txtpmname.value =  "";
	form.txtpsname.value = "";
	form.txtpadd1.value = "";
	form.txtpadd2.value = "";
	//form.txtpadd2.disabled=false;
	form.txtpcity.value = "";
	form.txtpstate.selectedIndex = 0;
	form.txtpzip.value = "";
	form.txtpphone.value = "";
	form.txtpcnfemail.value = "";
	}
	passed = true;
return passed;
}

function salestax(val)
{	
	if(val.toUpperCase()=="CA" || val.toUpperCase()=="C.A." || val.toUpperCase()=="CALIFORNIA")
	{
		document.forms['frmship'].elements['txttaxcost'].value=0.0925;
		document.forms['frmship'].elements['txtsurcharge'].value=0.00;
	}
	else if(val.toUpperCase()=="AK" || val.toUpperCase()=="A.K." || val.toUpperCase()=="ALASKA")
	{
		getsurcharge('Alaska_surcharge');
		document.forms['frmship'].elements['txttaxcost'].value=0.00;
	}
	else if(val.toUpperCase()=="HI" || val.toUpperCase()=="H.I." || val.toUpperCase()=="HAWAII")
	{
		getsurcharge('Hawaii_surcharge');
		document.forms['frmship'].elements['txttaxcost'].value=0.00;
	}
	else if(val.toUpperCase()=="PR" || val.toUpperCase()=="P.R." || val.toUpperCase()=="PUERTO RICO")
	{
		getsurcharge('PuertoRico_surcharge');
		document.forms['frmship'].elements['txttaxcost'].value=0.00;
	}
	else
	{
		document.forms['frmship'].elements['txttaxcost'].value=0.00;
		document.forms['frmship'].elements['txtsurcharge'].value=0.00;
	}
}

function getsurcharge(state)
{ 
	choice = 0
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
	{
	 alert ("Browser does not support HTTP Request")
	 return
	}
	var url="surcharge.php?state="+state
	
	xmlHttp.onreadystatechange=Getcharge
	xmlHttp.open("POST",url,true)
	xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
	
	xmlHttp.send(null)
}

function Getcharge() 
{ 
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
	{		
		document.forms['frmship'].elements['txtsurcharge'].value=xmlHttp.responseText 
	} 
}