// JavaScript Document
// JavaScript Document
var selectId = '';
var xmlHttp = '';

function createXMLHttpRequest() 
{
	if (window.ActiveXObject) 
	{
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	} 
	else if (window.XMLHttpRequest) {
		xmlHttp = new XMLHttpRequest();
	}
	return xmlHttp;
}
/*---------------------Add cart------------------*/

function addCart(productId) 
{   
	var url = 'ajaxFile.php';	
	xmlHttp = createXMLHttpRequest();
	queryString = url+'?task=first&pid='+productId;
	//alert(queryString);
	xmlHttp.onreadystatechange = searchResults;	
	xmlHttp.open("GET", queryString, true);
	xmlHttp.send(null);
}
function searchResults() 
{ 	
	if (xmlHttp.readyState==4)
	{	
		if(xmlHttp.responseText=='false')
		{
			alert('This Particular Item Already Available on Your Cart');
		}
		else
		{
			window.location.href='cartView.php';
		}
		//window.open ("viewCart.php","mywindow","location=1,status=1,scrollbars=yes,width=800,height=500");
		//alert(xmlHttp.responseText);
		//document.getElementById('postTags').innerHTML=xmlHttp.responseText;
		
	}
}
/*---------------------Remove cart------------------*/

function removeCart(productId) 
{   
	var url = 'ajaxFile.php';	
	xmlHttp = createXMLHttpRequest();
	queryString = url+'?task=second&pid='+productId;
	//alert(queryString);
	xmlHttp.onreadystatechange = searchResults1;	
	xmlHttp.open("GET", queryString, true);
	xmlHttp.send(null);
}
function searchResults1() 
{ 	
	if (xmlHttp.readyState==4)
	{	
		
		 	 var sPath = window.location.pathname;
			 var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
			 window.location.href=sPage;
		//window.location.href='cartView.php';
		//window.open ("viewCart.php","mywindow","location=1,status=1,scrollbars=yes,width=800,height=500");
		//alert(xmlHttp.responseText);
		//document.getElementById('postTags').innerHTML=xmlHttp.responseText;
		
	}
}

/*---------------------Remove cart------------------*/

function updateCart(productId,val,price) 
{   
	var url = 'ajaxFile.php';	
	xmlHttp = createXMLHttpRequest();
	queryString = url+'?task=third&pid='+productId+'&val='+val+'&price='+price;
	//alert(queryString);
	xmlHttp.onreadystatechange = searchResults4;	
	xmlHttp.open("GET", queryString, true);
	xmlHttp.send(null);
}
function searchResults4() 
{ 	
	if (xmlHttp.readyState==4)
	{	
		var getVal=xmlHttp.responseText.split('|');
		document.getElementById('ind'+getVal[2]).innerHTML = getVal[0];
		document.getElementById('tot').innerHTML = getVal[1];
		document.getElementById('totAmount').value = getVal[1];
		
	}
}

/*---------------------Login------------------*/

function loginChk() 
{   
	if(document.getElementById('login').value =="")
	{
		alert("Enter Your Username");
		document.getElementById('login').focus();
		return false;
	}
	var t = new RegExp("^[a-z0-9][a-z0-9_.]*[@]{1,1}[a-z0-9]+[.]{1,1}[a-z0-9]+", "i")
		v = document.getElementById('login').value;
	if(!(t.test(v)))
	{
		alert("Enter Valid E-Mail Address.");
		document.getElementById('login').select();
		return false;
	}
	if(document.getElementById('password').value =="")
	{
		alert("Enter Password");
		document.getElementById('password').select();
		return false;
	}
	var url = 'login.php';	
	xmlHttp = createXMLHttpRequest();
	var loginVal=document.getElementById('login').value;
	var pass=document.getElementById('password').value;
	queryString = url+'?task=first&user='+loginVal+'&pass='+pass;
	//alert(queryString);
	xmlHttp.onreadystatechange = searchResults2;	
	xmlHttp.open("GET", queryString, true);
	xmlHttp.send(null);
}
function searchResults2() 
{ 	
	if (xmlHttp.readyState==4)
	{	
		//alert(xmlHttp.responseText);
		if(xmlHttp.responseText==2)
		{
		document.getElementById('msg').innerHTML='Username or Password Incorrect!';
		}
		else if(xmlHttp.responseText==1)
		{
			document.getElementById('loginform').style.display='none';
			window.location.href='home.php';
		}
		else if(xmlHttp.responseText==3)
		{
			document.getElementById('loginform').style.display='none';
			 var sPath = window.location.pathname;
			 var sPage = sPath.substring(sPath.lastIndexOf('/') + 1);
			 window.location.href=sPage;
			//document.form1.action='checkOut.php';
			//document.form1.submit();
		}
	}
}


/*---------------------Login------------------*/

function loginChk1() 
{   
	if(document.getElementById('username').value =="")
	{
		alert("Enter Your Username");
		document.getElementById('username').focus();
		return false;
	}
	var t = new RegExp("^[a-z0-9][a-z0-9_.]*[@]{1,1}[a-z0-9]+[.]{1,1}[a-z0-9]+", "i")
		v = document.getElementById('username').value;
	if(!(t.test(v)))
	{
		alert("Enter Valid E-Mail Address.");
		document.getElementById('login').select();
		return false;
	}
	/*if(document.getElementById('pass').value =="")
	{
		alert("Enter Password");
		document.getElementById('pass').select();
		return false;
	}*/
	if(document.getElementById('totAmount').value<2000)
	{
		alert('The Minimum Order is Rs.2000 plus Packing and Forwarding Extra....');	
		return false;	
	}
	var url = 'login.php';	
	xmlHttp = createXMLHttpRequest();
	var loginVal=document.getElementById('username').value;
	var pass=document.getElementById('password').value;
	queryString = url+'?task=first&user='+loginVal+'&pass='+pass;
	//alert(queryString);
	xmlHttp.onreadystatechange = searchResults3;	
	xmlHttp.open("GET", queryString, true);
	xmlHttp.send(null);
}
function searchResults3() 
{ 	
	if (xmlHttp.readyState==4)
	{	
		//alert(xmlHttp.responseText);
		if(xmlHttp.responseText==2)
		{
		document.getElementById('usermsg').innerHTML='Username Incorrect!';
		}
		else if(xmlHttp.responseText==1)
		{
			document.getElementById('loginform').style.display='none';
			window.location.href='home.php';
		}
		else if(xmlHttp.responseText==3)
		{
			document.getElementById('loginform').style.display='none';
			document.form1.action='checkOut.php';
			document.form1.submit();
			//window.location.href='checkOut.php';		
		}
	}
}
