var xmlHttp , xmlHttp1 , xmlHttp2 , xmlHttp3, xmlHttp4, xmlHttp5,xmlHttp6;
var xmlHttp7, xmlHttp8, xmlHttp9;
var postal_check = false;

//FIND STATE LIST
function GetXmlHttpObject(handler)
{   
	var objXMLHttp=null
	if (window.XMLHttpRequest) 
	{
		objXMLHttp=new XMLHttpRequest() 
	}   
	else if (window.ActiveXObject)  
	{
		objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")  
	}
	return objXMLHttp
}
	

var cityLoad = 0;

// Will populate data based on input
function FindStates(url, qStr,val)
	{ 
		cityLoad = val;
		if (url.length==0)
	{      
		document.getElementById("txtResult").innerHTML="";   
		return; 
	} 
  xmlHttp=GetXmlHttpObject() 
	  if (xmlHttp==null)  
	  {   
	  alert ("Browser does not support HTTP Request");   
	  return;   
	  } 
  url=url+"?"+qStr;    
   url=url+"&val="+val;
  url=url+"&sid="+Math.random(); 
  //alert(url);
  xmlHttp.onreadystatechange=stateChanged;
  xmlHttp.open("GET",url,true) ; 
  xmlHttp.send(null);
  }

function stateChanged()
{
	if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")  
	{   
		document.getElementById("txtResult").innerHTML= xmlHttp.responseText; 
	
		if((cityLoad == 2) || (cityLoad == 1) || (cityLoad == 3))
		{
			state = document.productsfrm.lstState.options[document.productsfrm.lstState.selectedIndex].value;
			FindCity(state);
		}
	} 
	else
	{
		//alert(xmlHttp.status); 
	}
}

function FindCity(stateid)
{
	countryid = document.productsfrm.lstCountry.options[document.productsfrm.lstCountry.selectedIndex].value;
	FindCityList('Include/CityList.asp', countryid, stateid);
}

 

 //FIND CITY LIST
function GetXmlHttpObject1(handler)
{   
	var objXMLHttp1=null
	if (window.XMLHttpRequest) 
	{
		objXMLHttp1=new XMLHttpRequest() 
	}   
	else if (window.ActiveXObject)  
	{
		objXMLHttp1=new ActiveXObject("Microsoft.XMLHTTP")  
	}
	return objXMLHttp1
}
	
function stateChanged1()
{
	if (xmlHttp1.readyState==4 || xmlHttp1.readyState=="complete")  
	{   
		document.getElementById("txtResult1").innerHTML= xmlHttp1.responseText; 
		validate_postalcode(document.productsfrm.txtZip.value);
	} 
	else
	{
		//alert(xmlHttp.status); 
	}
}


// Will populate data based on input
function FindCityList(url, countryid, stateid)
	{    if (url.length==0)
	{      
		document.getElementById("txtResult1").innerHTML="";   
		return; 
	} 
  xmlHttp1=GetXmlHttpObject1() 
	  if (xmlHttp1==null)  
	  {   
		  alert ("Browser does not support HTTP Request");   
		  return;   
	  } 

  url=url+"?ch="+countryid;
  url=url+"&st="+stateid;
  url=url+"&val="+cityLoad;
  url=url+"&sid="+Math.random();   
// alert(url);
  xmlHttp1.onreadystatechange=stateChanged1;
  xmlHttp1.open("GET",url,true) ; 
  xmlHttp1.send(null);
  }

// TO VALIDATE USERNAME
  function validate_username(uname)
  {
	 if (uname.length==0)
	{      
		return; 
	} 
	  xmlHttp2=GetXmlHttpObject() 
		  if (xmlHttp2==null)  
		  {   
		  alert ("Browser does not support HTTP Request");   
		  return;   
		  } 
	  url="Include/validusername.asp"
	  url=url+"?uname="+uname
	  url=url+"&sid="+Math.random();   
	  //alert(url);
	  xmlHttp2.onreadystatechange=stateChanged2;
	  xmlHttp2.open("GET",url,true) ; 
	  xmlHttp2.send(null);
  }
	
function stateChanged2()
{
	if (xmlHttp2.readyState==4 || xmlHttp2.readyState=="complete")  
	{   
		if (xmlHttp2.responseText == 1)
		{
			alert("Username is already registered. Please enter another Username !");
			document.productsfrm.txtUsername.focus();
			document.productsfrm.txtUsername.select();
			return false;
		}	
		
	} 
}

var space = /^[\ ]*$/;
var validPhone = /^[0-9]+$/;

// TO VALIDATE E-Mail Id
  function validate_email(email)
  {
	if(space.test(document.productsfrm.txtMail.value))
	{
		alert("Please enter E-Mail Id");
		document.productsfrm.txtMail.focus();
		return false;
	}
	var emailID=/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9])+$/;
	if(!emailID.test(document.productsfrm.txtMail.value))
	{
		alert("Please enter valid E-Mail Id");
		document.productsfrm.txtMail.focus();
		document.productsfrm.txtMail.select();
		return false;
	}
	else	
	  {
		if (email.length==0)
		{      
			return; 
		} 
		  xmlHttp3=GetXmlHttpObject() 
			  if (xmlHttp3==null)  
			  {   
			  alert ("Browser does not support HTTP Request");   
			  return;   
			  } 
		  url="Include/validemail.asp"
		  url=url+"?email="+email
		  url=url+"&sid="+Math.random();   
		  //alert(url);
		  xmlHttp3.onreadystatechange=stateChanged3;
		  xmlHttp3.open("GET",url,true) ; 
		  xmlHttp3.send(null);
	  }
  }
	
function stateChanged3()
{
	if (xmlHttp3.readyState==4 || xmlHttp3.readyState=="complete")  
	{   
		if (xmlHttp3.responseText == 1)
		{
			alert("Email Id is already registered. Please enter another Email Id !");
			document.productsfrm.txtMail.focus();
			document.productsfrm.txtMail.select();
			return false;
		}	
		
	} 
}

// TO VALIDATE POSTAL CODES
  function validate_postalcode(pcode)
  {  
	if(pcode != "")
	  {

		if(document.productsfrm.lstState.selectedIndex == 0)
		{
			alert("Please select the State");
			document.productsfrm.lstState.focus();
			return false;
		}
		if(document.productsfrm.lstCity.selectedIndex == 0)
		{
			alert("Please select the City");
			document.productsfrm.lstCity.focus();
			return false;
		}
		if(!space.test(document.productsfrm.txtZip.value))
		{
			if(!validPhone.test(document.productsfrm.txtZip.value))
			{
				alert("Please enter Valid Zip Code");
				document.productsfrm.txtZip.focus();
				document.productsfrm.txtZip.select();
				return false;
			} 
		}
	  }
		stateId = document.productsfrm.lstState.options[document.productsfrm.lstState.selectedIndex].value;
		stateName = document.productsfrm.lstState.options[document.productsfrm.lstState.selectedIndex].text;
		cityId = document.productsfrm.lstCity.options[document.productsfrm.lstCity.selectedIndex].value;
		cityName = document.productsfrm.lstCity.options[document.productsfrm.lstCity.selectedIndex].text;

		 if (pcode.length==0)
		{      
			return; 
		} 
		  xmlHttp4=GetXmlHttpObject() 
			  if (xmlHttp4==null)  
			  {   
			  alert ("Browser does not support HTTP Request");   
			  return;   
			  } 
		  url="Include/validpostalcode.asp"
		  url=url+"?pid="+pcode
		  url=url+"&cid=1&stateid="+stateId
		  url=url+"&sname="+stateName
		  url=url+"&cityid="+cityId
		  url=url+"&cityname="+cityName
		  url=url+"&sid="+Math.random();   
		  //alert(url);
		  xmlHttp4.onreadystatechange=stateChanged4;
		  xmlHttp4.open("GET",url,true) ; 
		  xmlHttp4.send(null);
	
  }
	
function stateChanged4()
{
	if (xmlHttp4.readyState==4 || xmlHttp4.readyState=="complete")  
	{   
		if (xmlHttp4.responseText == 0)
		{
			alert("Invalid Zip Code. Kindly enter valid Zip Code!!");
			document.productsfrm.txtZip.value = "";
			document.productsfrm.txtZip.focus();
			document.productsfrm.txtZip.select();
			return false;
		}
		else if (xmlHttp4.responseText != 0)
		{
			postal_check = true;
			return false;
		}
		
	} 
}

var ShippingValid = null;

// TO VALIDATE SHIPPING TO
  function validate_shippingto(shippingTo,val)
  {  
	  ShippingValid = val;
		if(document.productsfrm.chkbox1.checked == true)
   	    {
			countryID = document.productsfrm.lstCountry.options[document.productsfrm.lstCountry.selectedIndex].value;
			stateId = document.productsfrm.lstState.options[document.productsfrm.lstState.selectedIndex].value;
			cityId = document.productsfrm.lstCity.options[document.productsfrm.lstCity.selectedIndex].value;
			pcode = document.productsfrm.txtZip.value;
		}
		else
		{
			countryID = document.productsfrm.txtBillCountry.value;
			stateId = document.productsfrm.txtBillState.value;
			cityId = document.productsfrm.txtBillCity.value;
			pcode = document.productsfrm.txtBillpostal.value;
		}

		 if (shippingTo.length==0)
		{      
			return; 
		} 
		  xmlHttp5=GetXmlHttpObject() 
			  if (xmlHttp5==null)  
			  {   
			  alert ("Browser does not support HTTP Request");   
			  return;   
			  } 
		  url="Include/validshippingTo.asp"
		  url=url+"?sto="+shippingTo
		  url=url+"&cid="+countryID
		  url=url+"&stateid="+stateId
		  url=url+"&cityid="+cityId
   	      url=url+"&pid="+pcode
		  url=url+"&sid="+Math.random();   
		  //alert(url);
		  xmlHttp5.onreadystatechange=stateChanged5;
		  xmlHttp5.open("GET",url,true) ; 
		  xmlHttp5.send(null);
	
  }
	
function stateChanged5()
{
	if (xmlHttp5.readyState==4 || xmlHttp5.readyState=="complete")  
	{   
		if (xmlHttp5.responseText == 0)
		{
			if (ShippingValid != 1)
			{
				alert("This facility currently not available. Kindly select another!!");
				document.productsfrm.rdShipTo[0].checked = false;
				document.productsfrm.rdShipTo[1].checked = false;
				return false;
			}
			else if(ShippingValid == 1)
			{
				document.productsfrm.rdShipTo[0].checked = false;
				document.productsfrm.rdShipTo[1].checked = false;
			}

		}
	} 
}

var _CategoryId = 0;
var _SubCategoryId = 0;
var _ThirdCategoryId = 0;

// TO DISPLAY SUB CATEGORY DROPDOWN
  function ShowSubCategory(CategoryId,val,subCatId)
  {
	_CategoryId = CategoryId;
	_SubCategoryId = 0;
	_ThirdCategoryId = 0;

	 if (CategoryId.length==0)
	{      
		return; 
	} 

	  xmlHttp7=GetXmlHttpObject() 
		  if (xmlHttp7==null)  
		  {   
			  alert ("Browser does not support HTTP Request");   
			  return;   
		  } 

	  url="Include/subCategory.asp"
	  url=url+"?catid="+CategoryId
      url=url+"&val="+val
      url=url+"&subcatid="+subCatId
	  url=url+"&sid="+Math.random();   
	 // alert(url);
	  xmlHttp7.onreadystatechange=stateChanged6;
	  xmlHttp7.open("GET",url,true) ; 
	  xmlHttp7.send(null);
  }
	
function stateChanged6()
{
	if (xmlHttp7.readyState==4 || xmlHttp7.readyState=="complete")  
	{ 
		if((_CategoryId == 3) || (_CategoryId == 4))
		{
			FindProducts(_CategoryId,0,0,'notfinal');
			ShownextSubCategory(0,0,0)
			document.getElementById("ResultSubCategory").innerHTML= xmlHttp7.responseText;
		}
		else
		{
			ShownextSubCategory(0,_SubCategoryId,_ThirdCategoryId)
			document.getElementById("ResultSubCategory").innerHTML= xmlHttp7.responseText; 
			FindProducts(0,0,0,'notfinal');
		}
	} 
}

// TO DISPLAY NEXT LEVEL SUB CATEGORY DROPDOWN
  function ShownextSubCategory(val,subCatId,Thirdlevelval)
  {

	CategoryId = document.productsfrm.lstCategory.options[document.productsfrm.lstCategory.selectedIndex].value;
	_CategoryId = CategoryId;
	_SubCategoryId = subCatId;

	if (CategoryId.length==0)
	{      
		return; 
	} 

	  xmlHttp8=GetXmlHttpObject() 
		  if (xmlHttp8==null)  
		  {   
			  alert ("Browser does not support HTTP Request");   
			  return;   
		  } 

	  url="Include/nextsubCategory.asp"
	  url=url+"?catid="+CategoryId
      url=url+"&val="+val
      url=url+"&subcatid="+_SubCategoryId
	  url=url+"&tid="+Thirdlevelval
	  url=url+"&sid="+Math.random();   
	 // alert(url);
	  xmlHttp8.onreadystatechange=stateChanged7;
	  xmlHttp8.open("GET",url,true) ; 
	  xmlHttp8.send(null);
  }
	
function stateChanged7()
{
	if (xmlHttp8.readyState==4 || xmlHttp8.readyState=="complete")  
	{ 
		if(_CategoryId == 5)
		{
			document.getElementById("ResultThirdCategory").innerHTML= xmlHttp8.responseText; 
			FindProducts(_CategoryId, _SubCategoryId,0,'final');
		}
		else
		{
			document.getElementById("ResultThirdCategory").innerHTML= xmlHttp8.responseText; 
			FindProducts(_CategoryId, _SubCategoryId,0,'notfinal');
		}
	} 
}

// Will populate data based on input
function FindProducts(CatId, SubCatId, ThirdCat, resultTest)
	{ 
		//alert(CatId +"-" + SubCatId +"-" +  ThirdCat +"-" + resultTest);
		if(CatId == 0)
		{
			CategoryId = document.productsfrm.lstCategory.options[document.productsfrm.lstCategory.selectedIndex].value;
		}
		else
		{
			CategoryId = CatId;
		}

		if(SubCatId == 0)
		{
			SubCategoryId = document.productsfrm.lstSubCategory.options[document.productsfrm.lstSubCategory.selectedIndex].value;
		}else
		{
			SubCategoryId = SubCatId; 
		}

	if (ThirdCat.length==0)
	{      
		document.getElementById("ResultCategory").innerHTML="";   
		return; 
	} 

	  xmlHttp9=GetXmlHttpObject() 

	  if (xmlHttp9==null)  
	  {   
	  alert ("Browser does not support HTTP Request");   
	  return;   
	  } 
 url = "Include/productslist.asp";
  url=url+"?cid="+CategoryId;    
   url=url+"&subid="+SubCategoryId;    
   url=url+"&val=0";    
    url=url+"&tid="+ThirdCat;    
  url=url+"&rtext="+resultTest;    
  url=url+"&sid="+Math.random(); 
  //alert(url);
  xmlHttp9.onreadystatechange=stateChanged8;
  xmlHttp9.open("GET",url,true) ; 
  xmlHttp9.send(null);
  }

  function stateChanged8()
{
	if (xmlHttp9.readyState==4 || xmlHttp9.readyState=="complete")  
	{   
		document.getElementById("ResultCategory").innerHTML= xmlHttp9.responseText; 
	} 
}