// JavaScript Document
var xmlHttp=null;	
function Mid(str, start, len)
{
// Make sure start and len are within proper bounds
    if (start < 0 || len < 0) return "";
    var iEnd, iLen = String(str).length;
    if (start + len > iLen)
          iEnd = iLen;
    else
          iEnd = start + len;
    return String(str).substring(start,iEnd);
}

function InStr(strSearch, charSearchFor)
{
            for (i=0; i < strSearch.length; i++)
            {
                  if (charSearchFor == Mid(strSearch, i, 1))
                  {
                        return i;
                  }
            }
            return -1;
}

function echeck(str) 
{
		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
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false
		 }

 		 return true					
}

function Trim(str)
{
	//trim leding spaces
	while(true)
	{
		if(str.charAt(0) == ' ')
			str = str.substr(1);
		else
			break;
	}
	
	//trim trailing spaces
	while(true)
	{
		if(str.charAt(str.length-1) == ' ')
			str = str.substr(0,str.length-1);
		else
			break;
	}
	return(str);	
}

function roundNumber(rnum, rlength) { // Arguments: number to round, number of decimal places
  var newnumber = Math.round(rnum*Math.pow(10,rlength))/Math.pow(10,rlength);
  return newnumber; // Output the result to the form field (change for your purposes)
}
/*clear quantity*/


function fn_custregister(pagename,customerid)
{
	if(pagename=="Register") 
	{
		if (Trim(document.getElementById("username").value)=="")
		{
			alert("Enter User Name");
			document.getElementById("username").focus();
			return false;
		}
		if (Trim(document.getElementById("pwd").value)=="")
		{
			alert("Enter Password");
			document.getElementById("pwd").focus();
			return false;
		}
		if (Trim(document.getElementById("confirmpwd").value)=="")
		{
			alert("Enter Confirmation Password");
			document.getElementById("confirmpwd").focus();
			return false;
		}	
		
		if(document.getElementById("confirmpwd").value!=document.getElementById("pwd").value)
		{
			alert("Password does not match");
			document.getElementById("pwd").value="";
			document.getElementById("confirmpwd").value="";
			document.getElementById("pwd").focus();
			return false;			
		}

		if (Trim(document.getElementById("email").value)=="")
		{
			alert("Enter Email");	
			document.getElementById("email").focus();
			return false;
		}	
		if (echeck(document.getElementById("email").value)==false)
		{
			alert("Invalid Email Id");
			document.getElementById("email").focus();
			return false;
		}		

		if (Trim(document.getElementById("billtitle").value)=="")	
		{
			alert("Select Title");
			document.getElementById("billtitle").focus();
			return false;
		}

		if (Trim(document.getElementById("firstname").value)=="")	
		{
			alert("Enter First Name");	
			document.getElementById("firstname").focus();
			return false;
		}
		if (Trim(document.getElementById("lastname").value)=="")	
		{
			alert("Enter Last Name");	
			document.getElementById("lastname").focus();
			return false;
		}	
		if (Trim(document.getElementById("address").value)=="")
		{
			alert("Enter Address");	
			document.getElementById("address").focus();
			return false;
		}
	
		if (Trim(document.getElementById("city").value)=="")
		{
			alert("Enter City");	
			document.getElementById("city").focus();
			return false;
		}

		if (Trim(document.getElementById("zipcode").value)=="")
		{
			alert("Enter Zip Code");	
			document.getElementById("zipcode").focus();
			return false;
		}	

		if (Trim(document.getElementById("BillCountry").value)=="")
		{
			alert("Select Country");	
			document.getElementById("BillCountry").focus();
			return false;
		}
		if (Trim(document.getElementById("contactnumber").value)=="")
		{
			alert("Enter Contact Number");	
			document.getElementById("contactnumber").focus();
			return false;
		}
		if (Trim(document.getElementById("howdoyouknow").value)=="")
		{
			alert("Select How do you know about GSM");	
			document.getElementById("howdoyouknow").focus();
			return false;
		}	
		document.frm_payment.method="post";
		document.frm_payment.action="custregister_action.asp";
		document.frm_payment.submit();	
	}
	else if (pagename=="payment") 
	{
		
		if (Trim(document.getElementById("billtitle").value)=="")	
		{
			alert("Select Title");
			document.getElementById("billtitle").focus();
			return;
		}
		if (Trim(document.getElementById("firstname").value)=="")	
		{
			alert("Enter First Name");	
			document.getElementById("firstname").focus();
			return;
		}
		if (Trim(document.getElementById("lastname").value)=="")	
		{
			alert("Enter Last Name");	
			document.getElementById("lastname").focus();
			return;
		}	
		if (Trim(document.getElementById("address").value)=="")
		{
			alert("Enter Address");	
			document.getElementById("address").focus();
			return;
		}
	
		if (Trim(document.getElementById("city").value)=="")
		{
			alert("Enter City");	
			document.getElementById("city").focus();
			return;
		}

		if (Trim(document.getElementById("zipcode").value)=="")
		{
			alert("Enter Zip Code");	
			document.getElementById("zipcode").focus();
			return;
		}	
		
		if (Trim(document.getElementById("BillCountry").value)=="")
		{
			alert("Select Country");	
			document.getElementById("BillCountry").focus();
			return;
		}
		
		if (Trim(document.getElementById("contactnumber").value)=="")
		{
			alert("Enter Contact Number");	
			document.getElementById("contactnumber").focus();
			return;
		}
		
		if (Trim(document.getElementById("howdoyouknow").value)=="")
		{
			alert("Select How do you know about GSM");	
			document.getElementById("howdoyouknow").focus();
			return;
		}	
		
			
			
		if(document.getElementById("cpybillship").checked==true)
		{
			if (Trim(document.getElementById("shiptitle").value)=="")	
			{
				alert("Select Title");	
				document.getElementById("shiptitle").focus();
				return;
			}
			if (Trim(document.getElementById("shipfirstname").value)=="")	
			{
				alert("Enter First Name");
				document.getElementById("shipfirstname").focus();
				return;
			}
			if (Trim(document.getElementById("shiplastname").value)=="")	
			{
				alert("Enter Last Name");
				document.getElementById("shiplastname").focus();
				return;
			}
			if (Trim(document.getElementById("shipaddress").value)=="")	
			{
				alert("Enter Address");
				document.getElementById("shipaddress").focus();
				return;
			}			
			if (Trim(document.getElementById("shipcity").value)=="")	
			{
				alert("Enter City");
				document.getElementById("shipcity").focus();
				return;
			}
			if (Trim(document.getElementById("shipzipcode").value)=="")	
			{
				alert("Enter Zip Code");
				document.getElementById("shipzipcode").focus();
				return;
			}
			if (Trim(document.getElementById("shipcontactnumber").value)=="")	
			{
				alert("Enter Contact Number");
				document.getElementById("shipcontactnumber").focus();
				return;
			}
			if (Trim(document.getElementById("ShipCountry").value)=="")	
			{
				alert("Select Country");
				document.getElementById("ShipCountry").focus();
				return;
			}			
		}	


		if (customerid=="")
		{
			if (Trim(document.getElementById("username").value)=="")
			{
				alert("Enter User Name");
				document.getElementById("username").focus();
				return;
			}
	
			if (Trim(document.getElementById("pwd").value)=="")
			{
				alert("Enter Password");
				document.getElementById("pwd").focus();
				return;
			}
	
			if (Trim(document.getElementById("confirmpwd").value)=="")
			{
				alert("Enter Confirmation Password");
				document.getElementById("confirmpwd").focus();
				return;
			}	
			
			if(document.getElementById("confirmpwd").value!=document.getElementById("pwd").value)
			{
				alert("Password does not match");
				document.getElementById("pwd").value="";
				document.getElementById("confirmpwd").value="";
				document.getElementById("pwd").focus();
				return;			
			}			
	
			if (Trim(document.getElementById("email").value)=="")
			{
				alert("Enter Email");	
				document.getElementById("email").focus();
				return;
			}	
	
			if (echeck(document.getElementById("email").value)==false)
			{
				alert("Invalid Email Id");
				document.getElementById("email").focus();
				return;
			}			
		}
		document.frm_payment.method="post";
		document.frm_payment.action="payment_action.asp";
		document.frm_payment.submit();				
	}

}

function fn_copyBilltoShip()
{
	if(document.getElementById("cpybillship").checked==true)
	{		
		document.getElementById("shiptitle").value=document.getElementById("billtitle").value;
		document.getElementById("shipfirstname").value=document.getElementById("firstname").value;
		document.getElementById("shiplastname").value=document.getElementById("lastname").value;
		document.getElementById("shipaddress").value=document.getElementById("address").value;
		document.getElementById("shipcity").value=document.getElementById("city").value;
		document.getElementById("shipzipcode").value=document.getElementById("zipcode").value;
		document.getElementById("shipcontactnumber").value=document.getElementById("contactnumber").value;
//		document.getElementById("ShipCountry").value=document.getElementById("BillCountry").value;	
	}
	else
	{
		document.getElementById("shiptitle").value="";
		document.getElementById("shipfirstname").value="";
		document.getElementById("shiplastname").value="";
		document.getElementById("shipaddress").value="";
		document.getElementById("shipcity").value="";
		document.getElementById("shipzipcode").value="";
		document.getElementById("shipcontactnumber").value="";
//		document.getElementById("ShipCountry").value="";
	}
	
}


function GetXmlHttpObject()
{
var xmlHttp;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}

function deletecart(id)
{
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
	  alert ("Your browser does not support AJAX!");
	  return;
	} 
	url="AddProduct.asp";
	url=url+"?id="+id;
	url=url+"&action=delete";
	url=url+"&displaytype=fulltext";	
	xmlHttp.onreadystatechange=stateChangedDelUpdate;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);	
}

function stateChangedDelUpdate() 
{ 

	if (xmlHttp.readyState==4)
	{
		var result;
		result=Trim(xmlHttp.responseText);
		document.getElementById("txtHint").innerHTML=result;		
	}
}

function updatecart(id,val)
{
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
	  alert ("Your browser does not support AJAX!");
	  return;
	} 
	url="AddProduct.asp";
	url=url+"?id="+id;
	url=url+"&qty="+val;	
	url=url+"&action=update";
	url=url+"&displaytype=fulltext";	
	xmlHttp.onreadystatechange=stateChangedDelUpdate;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);	
}

function fn_AddtoCart(id,qty)
{
	var url;
	url=window.location.href;
	if (url.indexOf("online-shop.html")>0)
	{
		producttype="N";
	}
	else if (url.indexOf("complimentary-products.html")>0)
	{
		producttype="C";
	}

	var prodid,selquantity;
	prodid=id;
	selquantity=document.getElementById(qty).value;
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
	  alert ("Your browser does not support AJAX!");
	  return;
	} 
	url="AddProduct.asp";
	url=url+"?id="+prodid;
	url=url+"&qty="+selquantity;
	url=url+"&producttype="+producttype;
	url=url+"&action=insert";	
	url=url+"&displaytype=insert";
	xmlHttp.onreadystatechange=stateChangedAddProduct;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);	
}	

function stateChangedAddProduct() 
{ 
	if (xmlHttp.readyState==4)
	{
		var divres,functionname,nProductID;
		nProductID=Trim(xmlHttp.responseText);
//		alert(nProductID);
		if (nProductID>0)
		{
			divres="divresult"+	nProductID;
			alert("Product Added");
		}
		else if (nProductID==0)
		{
			alert("Complimentary Product Added Already");
		}
		else if (nProductID<0)
		{
			alert("Please add normal products to the cart and proceed adding complimentary products");		
		}
		else
		{
			alert("Complimentary Product Added Already");
		}
		//setTimeout("dispdivtag("+nProductID+")",1000);
	}
}

function dispdivtag(val)
{
	var	div_dispmsg;
	div_dispmsg="divresult"+val;
	document.getElementById(div_dispmsg).style.display="block";
	setTimeout("hidedivtag("+val+")",1000);	
}

function hidedivtag(val)
{
	var	div_dispmsg;
	div_dispmsg="divresult"+val;
	document.getElementById(div_dispmsg).style.display="none";
}


function fn_countrychange(val)
{
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
	  alert ("Your browser does not support AJAX!");
	  return;
	} 
	url="AddProduct.asp";
//	url=url+"?id="+prodid;
	url=url+"?action=ship";
	url=url+"&countrycode="+val;
	url=url+"&displaytype=fulltext";	
	xmlHttp.onreadystatechange=stateChangedDelUpdate;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);	
}

function fn_shopcart_continue(shipcountry,customerid)
{
	if (shipcountry=="")
	{
		alert("Select Shipping Country");
		document.getElementById("shipcountry").focus();
		return;
	}
	if (customerid=="")
	{
		showForm();
		return;
	}
	else
	{
		document.frm_shopcart.method="post";
		document.frm_shopcart.action="payment.asp";
		document.frm_shopcart.submit();
	}
}

function showForm ()
{
	Effect.Grow ($('login_div'));
}
	
function fn_payment_continue()
{
	document.frm_payment.method="post";
	document.frm_payment.action="payment_action.asp";
	document.frm_payment.submit();	
}

function changecurrency(currency)
{
	urll=window.location.href;
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
	  alert ("Your browser does not support AJAX!");
	  return;
	} 
	url="changecurrency.asp";
	url=url+"?curr="+currency;	
	url=url+"&url="+urll;
	xmlHttp.onreadystatechange=stateChangeCurrency;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);		
}

function stateChangeCurrency() 
{ 

	if (xmlHttp.readyState==4)
	{
		var result,param;
		result=Trim(xmlHttp.responseText);
//		param=result.split("<-->");		
		document.getElementById("txtHint").innerHTML=result;		
	}
}

function fn_loginaction(username,password)
{
	var uname,pword;
	uname=document.getElementById(username).value;
	pword=document.getElementById(password).value;
	
	if(Trim(uname)=="")
	{
		alert("Enter Username");
		document.getElementById(username).focus();
		document.getElementById(username).value="";
		return;
	}
	if(Trim(pword)=="")
	{
		alert("Enter Password");
		document.getElementById(password).focus();
		document.getElementById(password).value="";		
		return;
	}	
	
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
	  alert ("Your browser does not support AJAX!");
	  return;
	} 
	url="CheckMailPwd.asp";
	url=url+"?uname="+uname;	
	url=url+"&pword="+pword;		
	xmlHttp.onreadystatechange=stateChangeLogin;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);	
	
}

function stateChangeLogin()
{
	if (xmlHttp.readyState==4)
	{
		var result,param;
		result=Trim(xmlHttp.responseText);
		if (result==1)
		{
			location.href="shopcart.asp";
		}
		else
		{
			document.getElementById("div_loginerr").innerHTML="Invalid Username/Password";
		}
	}	
}


function logincheck()
{
	if (xmlHttp.readyState==4)
	{
		var result,param;
		result=Trim(xmlHttp.responseText);
		if (result=="Y")
		{
			document.getElementById("logindiv").style.display="block";
		}
		else
		{
			document.getElementById("logoutdiv").style.display="block";			
		}
	}
}

function fn_productload_changecurrency(val)
{
	var url;
	url=window.location.href;
	if (url.indexOf("online-shop.html")>0)
	{
		producttype="N";
	}
	else if (url.indexOf("complimentary-products.html")>0)
	{
		producttype="C";
	}
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
	  alert ("Your browser does not support AJAX!");
	  return;
	} 
	url="GetAllProductCurrency.asp";
	url=url+"?curr="+val;
	url=url+"&producttype="+producttype;
	xmlHttp.onreadystatechange=stateChangedAllProducts;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);	
}

function fn_productload(producttype)
{
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null)
	{
	  alert ("Your browser does not support AJAX!");
	  return;
	} 
	url="GetAllProduct.asp";
	url=url+"?producttype="+producttype;
	xmlHttp.onreadystatechange=stateChangedAllProducts;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);	
}

function stateChangedAllProducts() 
{ 
	if (xmlHttp.readyState==4)
	{
		var xmlDoc=xmlHttp.responseXML.documentElement;
		var totalids,param,currencydiv;
		totalids=xmlDoc.getElementsByTagName("totalids")[0].childNodes[0].nodeValue;
		currencydiv=xmlDoc.getElementsByTagName("currencydiv")[0].childNodes[0].nodeValue;
		param=totalids.split(",");
		for(i=0;i<param.length;i++)
		{
			var a;
			tname="priceid"+param[i];
			qtyid="xmlqtyid"+param[i];
			proddet=xmlDoc.getElementsByTagName(tname)[0].childNodes[0].nodeValue;	
			prodqty=xmlDoc.getElementsByTagName(qtyid)[0].childNodes[0].nodeValue;		
			if (proddet!="")
			{				
//				document.getElementById(tname).innerHTML=proddet;			
//				document.getElementById(tname).style.background="url('http://store.himalayahealthcare.com/images/spacer.gif')";
				document.getElementById(tname).innerHTML=proddet;
				document.getElementById(qtyid).innerHTML=prodqty;				
				//document.getElementById(tname).style.backgroundColor="#EDF6F5";

			}
		}
		document.getElementById("currencydiv").innerHTML=currencydiv;
	}
}

function login_load()
{
	var url;
	url=window.location.href;
	if (url.indexOf("online-shop.html")>0)
	{
		fn_productload('N');
	}
	else if (url.indexOf("complimentary-products.html")>0)
	{
		fn_productload('C');
	}	
	else
	{
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null)
		{
		  alert ("Your browser does not support AJAX!");
		  return;
		} 
		url="logincheck.asp";
		xmlHttp.onreadystatechange=logincheck;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);			
	}
}

function fn_forgotuname()
{
	if(Trim(document.getElementById("txt_emailid").value)=="")
	{
		alert("Enter Email Id");
		document.getElementById("txt_emailid").focus();
		return false;
	}
	else if (echeck(document.getElementById("txt_emailid").value)==false)
	{
		alert("Invalid Email Id");
		document.getElementById("txt_emailid").focus();
		return false;
	}
	else if (Trim(document.getElementById("txt_fname").value)==false)
	{
		alert("Enter First Name");
		document.getElementById("txt_fname").focus();
		return false;
	}
	else if (Trim(document.getElementById("txt_lname").value)==false)
	{
		alert("Enter Last Name");
		document.getElementById("txt_lname").focus();
		return false;
	}
	else
	{
		return true;
	}
}

function fn_forgotpwd()
{
	if (Trim(document.getElementById("txt_uname").value)==false)
	{
		alert("Enter User Name");
		document.getElementById("txt_uname").focus();
		return false;
	}
	else if(Trim(document.getElementById("txt_emailid").value)=="")
	{
		alert("Enter Email Id");
		document.getElementById("txt_emailid").focus();
		return false;
	}
	else if (echeck(document.getElementById("txt_emailid").value)==false)
	{
		alert("Invalid Email Id");
		document.getElementById("txt_emailid").focus();
		return false;
	}
	else
	{
		return true;
	}
}

function fn_addcompliment(cnt)
{
	var ctrl,flag,compprodids;
	ctrl="Comp_Prod";
	flag=false;
	selectedcnt=0;
	compprodids="";
	for (i=1;i<=cnt;i++)
	{
		ctrl="Comp_Prod"+i;
		if(document.getElementById(ctrl).checked==true)
		{
			flag=true;
			selectedcnt=i;
			prodid="Comp_Prodid"+selectedcnt;
			prodid=document.getElementById(prodid).value;

			if (compprodids!="")
			{
				compprodids=compprodids+","+prodid;
			}
			else
			{
				compprodids=prodid;
			}
			//break;
		}		
	}
	if(compprodids!="")
	{
		prodid="Comp_Prodid"+selectedcnt;
		prodid=document.getElementById(prodid).value;
		xmlHttp=GetXmlHttpObject();
		if (xmlHttp==null)
		{
		  alert ("Your browser does not support AJAX!");
		  return;
		} 
		url="AddComplimentaryProd.asp";
		url=url+"?id="+compprodids;
		url=url+"&qty=1";
		url=url+"&action=insert";
		url=url+"&ProductType=C";
		url=url+"&displaytype=fulltext";
//		alert(url);
//		return;
		xmlHttp.onreadystatechange=stateChangedDelUpdate;
		xmlHttp.open("GET",url,true);
		xmlHttp.send(null);	
	}
	else
	{
		alert("Select any of the Complimentary Product");
		return;
	}
}