var xmlHttp;
function createXMLHttpRequest() 
{

	if (window.ActiveXObject) 
	{
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	} 
	else if (window.XMLHttpRequest) 
	{
		xmlHttp = new XMLHttpRequest();
	}

}
function MakeRequestForSizes(productId)
{
  var styleId = document.getElementById('selectStyle').value;
  createXMLHttpRequest();
  xmlHttp.onreadystatechange = handleSizeChange;
  xmlHttp.open("GET", "ajax.php?id="+styleId+"&productId="+productId+"&msg=forStyle", true);
  xmlHttp.send(null);
}
function fbs_click() 
{
	var u='http://www.facebook.com/dishonestapparel';
	var t=document.title; 
	window.open('http://www.facebook.com/sharer.php?u=' +encodeURIComponent(u)+'&t='+ encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436'); 
	return false; 
}


function twitter_click() 
{
	var u='http://www.twitter.com/dishonestees';
	var t=document.title; 
	window.open('http://twitter.com/home?status=' + encodeURIComponent(u));              
	return false; 
}  
function handleSizeChange() 
{
	if(xmlHttp.readyState == 4) 
	{

		if(xmlHttp.status == 200) 
		{
			 var vals = xmlHttp.responseText.split("~");
			 //alert(vals);
			 document.getElementById('selectSize').length=0;
			 var opselect = new Option('Select', '', false, false);
	
			 var sel = document.getElementById('selectSize');
			 sel.options[0] = opselect;
			 
			 for (i=0; i<vals.length-1; i++)
			 {    
				if (vals[i]!='')
				{     
					var sizes = vals[i].split("|");
					var op = new Option(sizes[1], sizes[0], false, false);
					sel.options[sel.length] = op;
				}     
			 }
		}
	}
}

function MakeRequestForColors(productId)
{
  var styleId = document.getElementById('selectStyle').value;
  var sizeId = document.getElementById('selectSize').value;
  
  createXMLHttpRequest();
  xmlHttp.onreadystatechange = handleColorChange;
  xmlHttp.open("GET", "ajax.php?id="+styleId+"&sizeId="+sizeId+"&productId="+productId+"&msg=forSize", true);
  xmlHttp.send(null);
}

function addToWishList()
{
   if(isEmpty(document.getElementById('selectStyle').value))
   {
	  alert('Please choose a style.');
	  document.frmAddCart.selectStyle.focus();
	  return false ; 
   }
   if(isEmpty(document.getElementById('selectSize').value))
   {
	  alert('Please choose a size.');
	  document.frmAddCart.selectSize.focus();
	  return false ; 
   }
   if(isEmpty(document.getElementById('selectColor').value))
   {
	  alert('Please choose a color.');
	  document.frmAddCart.selectColor.focus();
	  return false ; 
   }
   document.getElementById('frmAddCart').action = "orderHandler.php?act=wl";
   document.getElementById('frmAddCart').submit();

}

function addToCart()
{
   if(isEmpty(document.getElementById('selectStyle').value))
   {
	  alert('Please choose a style.');
	  document.frmAddCart.selectStyle.focus();
	  return false ; 
   }
   if(isEmpty(document.getElementById('selectSize').value))
   {
	  alert('Please choose a size.');
	  document.frmAddCart.selectSize.focus();
	  return false ; 
   }
   if(isEmpty(document.getElementById('selectColor').value))
   {
	  alert('Please choose a color.');
	  document.frmAddCart.selectColor.focus();
	  return false ; 
   }
   document.getElementById('frmAddCart').action = "orderHandler.php?act=ct";
   document.getElementById('frmAddCart').submit();
}

function handleColorChange() 
{
	if(xmlHttp.readyState == 4) 
	{

		if(xmlHttp.status == 200) 
		{
			 var vals = xmlHttp.responseText.split("~");
			 document.getElementById('selectColor').length=0;
			 var opselect = new Option('Select', '', false, false);
	
			 var sel = document.getElementById('selectColor');
			 sel.options[0] = opselect;
			 
			 
	
			 for (i=0; i<vals.length-1; i++)
			 {    
				if (vals[i]!='')
				{     
					var colors = vals[i].split("|");
					var op = new Option(colors[1], colors[0], false, false);
					sel.options[sel.length] = op;
				}     
			 }
		}
	}
}
function copyShippingInfo()
{
	if(document.getElementById('check').checked == true)
	{
		document.getElementById('billingFirstName').value = document.getElementById('shippingFirstName').value;
		document.getElementById('billingLastName').value = document.getElementById('shippingLastName').value;
		document.getElementById('billingAddress').value = document.getElementById('shippingAddress').value;
		document.getElementById('billingAddress2').value = document.getElementById('shippingAddress2').value;
		document.getElementById('billingPostCode').value = document.getElementById('shippingPostCode').value;
		document.getElementById('billingCity').value = document.getElementById('shippingCity').value;
		document.getElementById('billingState').value = document.getElementById('shippingState').value;
		document.getElementById('billingCountry').value = document.getElementById('shippingCountry').value;
		document.getElementById('billingContactNumber').value = document.getElementById('shippingContactNumber').value;
	}
	else
	{
		document.getElementById('billingFirstName').value = "";
		document.getElementById('billingLastName').value = "";
		document.getElementById('billingAddress').value = "";
		document.getElementById('billingAddress2').value = "";
		document.getElementById('billingPostCode').value = "";
		document.getElementById('billingCity').value = "";
		document.getElementById('billingState').value = "";
		document.getElementById('billingCountry').value = "";
		document.getElementById('billingContactNumber').value = "";
	}
}
function validateShillingInformation()
{
	if(document.getElementById('shipiingMethod1').checked==true)
	{
		var i=1;
		while(true)
		{
			var chk = document.getElementById('a'+i);
			if(chk == null || chk == "undefined")
			{
				alert('Please choose a shipping method.');
				return false;
			}
			else
			{
				if(chk.checked)
				{
					break;
				}
			}
			i++;
		}
	}
	else
	{
		var i=1;
		while(true)
		{
			var chk = document.getElementById('b'+i);
			if(chk == null || chk == "undefined")
			{
				alert('Please choose a shipping method.');
				return false;
			}
			else
			{
				if(chk.checked)
				{
					break;
				}
			}
			i++;
		}
	}
	if(isEmpty(document.getElementById('shippingFirstName').value))
	{
		alert('Please enter shipping first name.');
		document.frmCheckout2.shippingFirstName.focus();
		return false ;
	}
	if(isEmpty(document.getElementById('shippingLastName').value))
	{
		alert('Please enter shipping last name.');
		document.frmCheckout2.shippingLastName.focus();
		return false ;
	}
	if(isEmpty(document.getElementById('shippingAddress').value))
	{
		alert('Please enter shipping address.');
		document.frmCheckout2.shippingAddress.focus();
		return false ;
	}
	/*if(isEmpty(document.getElementById('shippingAddress2').value))
	{
		alert('Please enter shipping Address Line 2/Apartment ');
		document.frmCheckout2.shippingAddress2.focus();
		return false ;
	}*/
	if(isEmpty(document.getElementById('shippingPostCode').value))
	{
		alert('Please enter shipping post code.');
		document.frmCheckout2.shippingPostCode.focus();
		return false ;
	}
	if(isEmpty(document.getElementById('shippingCity').value))
	{
		alert('Please enter shipping city.');
		document.frmCheckout2.shippingCity.focus();
		return false ;
	}
	if(isEmpty(document.getElementById('shippingState').value))
	{
		alert('Please enter shipping state.');
		document.frmCheckout2.shippingState.focus();
		return false ;
	}
	if(isEmpty(document.getElementById('shippingCountry').value))
	{
		alert('Please enter shipping country.');
		document.frmCheckout2.shippingCountry.focus();
		return false ;
	}
	if(isEmpty(document.getElementById('shippingContactNumber').value))
	{
		alert('Please enter shipping contact number.');
		document.frmCheckout2.shippingContactNumber.focus();
		return false ;
	}
	if(isEmpty(document.getElementById('billingFirstName').value))
	{
		alert('Please enter billing first number.');
		document.frmCheckout2.billingFirstName.focus();
		return false ;
	}
	if(isEmpty(document.getElementById('billingLastName').value))
	{
		alert('Please enter billing last name.');
		document.frmCheckout2.billingLastName.focus();
		return false ;
	}
	if(isEmpty(document.getElementById('billingEmailAddress').value))
	{
		alert('Please enter billing email address.');
		document.frmCheckout2.billingEmailAddress.focus();
		return false ;
	}
	if(!IsValidEMail(document.getElementById('billingEmailAddress').value))
	 {
		alert('Billing email address not valid.');
		document.frmCheckout2.billingEmailAddress.focus();
		return false ;
	 }
	if(isEmpty(document.getElementById('billingAddress').value))
	{
		alert('Please enter billing address.');
		document.frmCheckout2.billingAddress.focus();
		return false ;
	}
	if(isEmpty(document.getElementById('billingPostCode').value))
	{
		alert('Please enter billing post code.');
		document.frmCheckout2.billingPostCode.focus();
		return false ;
	}
	if(isEmpty(document.getElementById('billingCity').value))
	{
		alert('Please enter billing city.');
		document.frmCheckout2.billingCity.focus();
		return false ;
	}
	if(isEmpty(document.getElementById('billingState').value))
	{
		alert('Please enter billing state.');
		document.frmCheckout2.billingState.focus();
		return false ;
	}
	if(isEmpty(document.getElementById('billingCountry').value))
	{
		alert('Please enter billing country.');
		document.frmCheckout2.billingCountry.focus();
		return false ;
	}
	if(isEmpty(document.getElementById('billingContactNumber').value))
	{
		alert('Please enter billing contact number.');
		document.frmCheckout2.billingContactNumber.focus();
		return false ;
	}
		document.getElementById('frmCheckout2').action = "checkOutStep3.php";
	    document.getElementById('frmCheckout2').submit();
}

function validateCreditCardForOrder()
{
	var cvv = document.getElementById('cvvNumber');
	var creditNumber = document.getElementById('creditCardNum').value;
	if(isEmpty(document.getElementById('creditCardType').value))
	{
		alert('Please select a credit card type.');
		document.frmPaymentInfo.creditCardType.focus();
		return false ;
	}
	if(isEmpty(document.getElementById('creditCardNum').value))
	{
		alert('Please enter a credit card number.');
		document.frmPaymentInfo.creditCardNum.focus();
		return false ;
	}
	else if (!luhnCheck(creditNumber))
	{
		alert('Please enter a valid credit card number.');
		document.frmPaymentInfo.creditCardNum.focus();
		return false ;	
	}
	if(isEmpty(document.getElementById('creditCardHolder').value))
	{
		alert('Please enter card holder name.');
		document.frmPaymentInfo.creditCardHolder.focus();
		return false ;
	}
	if(!isEmpty(document.getElementById('cvvNumber').value))
	{
	 if(!isNumeric(cvv))
	 {
		alert('Please enter a valid cvv number.');
		document.frmPaymentInfo.cvvNumber.focus();
		return false ;
	 }
	}
	document.getElementById('frmPaymentInfo').action = "checkOutStep4.php";
	document.getElementById('frmPaymentInfo').submit();
	
}
function isNumeric(elem)
{
  var numericExpression = /^[0-9]+$/;
  if(elem.value.match(/^[0-9]+$/))
  {
     return true ;
  }
  else
  {
   return false;
   }
}

function validateUserLogin()
{
	if(isEmpty(document.getElementById('username').value))
	{
		alert('Please enter your email.');
		document.frmLogin.username.focus();
		return false ;
	}
	if(!IsValidEMail(document.getElementById('username').value))
	 {
		alert('Email address is not valid.');
		document.frmLogin.username.focus();
		return false ;
	 }
	if(isEmpty(document.getElementById('password').value))
	{
		alert('Please enter your password.');
		document.frmLogin.password.focus();
		return false ;
	}
	document.frmLogin.submit();
}
function swapImageProducts(id)
{
   var filename = id.src.substr(id.src.indexOf('filename='));
   var filename1 = document.getElementById('primaryImage').src.substr(document.getElementById('primaryImage').src.indexOf('filename='));
   id.src = id.src.replace(filename,filename1);
   var anchorId = document.getElementById('aid');
   document.getElementById('primaryImage').src = document.getElementById('primaryImage').src.replace(filename1,filename);
   var file = filename.substr(filename.toLowerCase().indexOf('filename=')+9);
   if(file.indexOf("&") != -1)
   {
	   file = file.substr(0,file.indexOf("&"));
   }
   anchorId.href = 'Admin/' + file;
}
function validateUserRegistration()
 {
	 if(isEmpty(document.getElementById('emailId').value))
	 {
		alert('Please enter your email.');
		document.frmRegister.emailId.focus();
		return false ;
	 }
	 if(!IsValidEMail(document.getElementById('emailId').value))
	 {
		alert('Email address not valid.');
		document.frmRegister.emailId.focus();
		return false ;
	 }
	 if(isEmpty(document.getElementById('passwrd').value))
	 {
		alert('Please enter your password.');
		document.frmRegister.passwrd.focus();
		return false ;
	 }
	 if(document.getElementById('passwrd').value!=document.getElementById('passwrdConfirm').value)
	 {
		alert("Please verify your password.");
		document.frmRegister.passwrdConfirm.focus();
		return false;
	 }
	 if(isEmpty(document.getElementById('firstName').value))
	 {
		alert('Please enter your first name.');
		document.frmRegister.firstName.focus();
		return false ;
	 }
	 if(isEmpty(document.getElementById('city').value))
	 {
		alert('Please enter your city.');
		document.frmRegister.city.focus();
		return false ;
	 }
	 if(isEmpty(document.getElementById('dateOfBirth').value))
	 {
		alert('Please enter your birth date.');
		document.frmRegister.dateOfBirth.focus();
		return false ;
	 }
	 if(isEmpty(document.getElementById('code').value))
	 {
		alert('Please enter security Code as shown the image above.');
		document.frmRegister.code.focus();
		return false ;
	 }
	 document.frmRegister.submit();
 }
	
	/*
 +-------------------------------------------------------------------+
 |	PURPOSE : 	Checks if the provided field value is empty
 |	ARGUMENTS : value of the field
 |	RETURN VALUE : true or false
 +-------------------------------------------------------------------+
*/

function isEmpty(elementValue)
{
	if( (elementValue=="") || (elementValue==null) )
	{
		return true;	//if empty
	}
	else
	{
		return false;	//if not empty
	}
}
function checkFunction(alerts)
{
	var r=confirm(alerts);
	if (r==true)
	{
	 return true;
	}
    else
    {
	  return false;
    }

}
function IsValidEMail(strValue)
{
	nNoOfArguments = IsValidEMail.arguments.length;
	//if no parameter is supplied
	if(nNoOfArguments < 1)
	{
		return false;
	}	
	
	var strVal	= new String(strValue);
	var bReturn1 = false;
	var bReturn2 = false;
	var bReturn3 = false;
	var bReturn4 = false;
	var bReturn5 = false;
	var bReturn5 = false;
	
	//if '@' comes after first character then true
	bReturn1	=	(strValue.indexOf("@") > 0);							
	
	//if '.' comes after first character then true
	bReturn2	=	(strValue.indexOf(".") > 0);							
	
	//if single '@' exist in string then true
	bReturn3	=	(strValue.indexOf("@") == strValue.lastIndexOf("@"));	
	
	//if '.' exist after @ in string then true
	bReturn4	=	(strValue.indexOf(".",strValue.indexOf("@")) > (strValue.indexOf("@")));		
	
	//if '.' does not comes immediatly after @ then true
	bReturn5	=	(strValue.indexOf(".",strValue.indexOf("@")) != (strValue.indexOf("@")+1));		
	
	//if '.' does not comes immediatly before @ then true
	bReturn6	=	(strValue.lastIndexOf(".",strValue.indexOf("@")) != (strValue.indexOf("@")-1)); 
	
	//if all values are true return true else false.
	return(bReturn1 && bReturn2 && bReturn3 && bReturn4 && bReturn5 && bReturn6);
}




	
	
  function validateURL(argvalue) 
	{
	 //var argvalue=document.getElementById('txt').value;
	 
	  if (argvalue.indexOf(" ") != -1)
		{
		 alert("Enter the valid url.");
		 return false;
		} 
	  else if (argvalue.indexOf("http://") == -1)
	   {
		 alert("Enter the valid url.");
		 return false;
		} 
	  else if (argvalue == "http://")
		{
		 alert("Enter the valid url.");
		 return false;
		} 
	  else if (argvalue.indexOf("http://") > 0)
	   {
		 alert("Enter the valid url.");
		 return false;
		} 
	
	  argvalue = argvalue.substring(7, argvalue.length);
	  if (argvalue.indexOf(".") == -1)
		{
		 alert("Enter the valid url.");
		 return false;
		} 
	  else if (argvalue.indexOf(".") == 0)
		{
		 alert("Enter the valid url.");
		 return false;
		} 
	  else if (argvalue.charAt(argvalue.length - 1) == ".")
		{
		 alert("Enter the valid url.");
		 return false;
		} 
	
	  if (argvalue.indexOf("/") != -1) {
		argvalue = argvalue.substring(0, argvalue.indexOf("/"));
		if (argvalue.charAt(argvalue.length - 1) == ".")
		  {
		 alert("Enter the valid url.");
		 return false;
		} 
	  }
	
	  if (argvalue.indexOf(":") != -1) 
	  {
		if (argvalue.indexOf(":") == (argvalue.length - 1))
		  {
		 alert("Enter the valid url.");
		 return false;
		} 
		else if (argvalue.charAt(argvalue.indexOf(":") + 1) == ".")
		  {
		 alert("Enter the valid url.");
		 return false;
		} 
		argvalue = argvalue.substring(0, argvalue.indexOf(":"));
		if (argvalue.charAt(argvalue.length - 1) == ".")
		  {
		 alert("Enter the valid url.");
		 return false;
		} 
	  }
	
	  return true;
	
	}


/*============================================================================*/


/*************************************************************************\
boolean luhnCheck([String CardNumber])
return true if CardNumber pass the luhn check else return false.
Reference: http://www.ling.nwu.edu/~sburke/pub/luhn_lib.pl
\*************************************************************************/
function luhnCheck(card_number) 
{
    var CardNumber = card_number;
	var no_digit = CardNumber.length;
	var oddoeven = no_digit & 1;
	var sum = 0;
    

	for (var count = 0; count < no_digit; count++) 
	{
		var digit = parseInt(CardNumber.charAt(count));
		if (!((count & 1) ^ oddoeven)) 
		{
			digit *= 2;
			if (digit > 9)
				digit -= 9;
		}
			sum += digit;
	}
	if (sum % 10 == 0)
		return true;
	else
		return false;
}
 
function validateChangePassword()
 {
	  if(isEmpty(document.getElementById('currentPasswrd').value))
	  {
		alert('Please enter your current password.');
		document.frmEditProfile.currentPasswrd.focus();
		return false ;
	  }
	  if(isEmpty(document.getElementById('newPasswrd').value))
	  {
		alert('Please enter your new password.');
		document.frmEditProfile.newPasswrd.focus();
		return false ;
	  }
	  if(document.getElementById('newPasswrd').value!=document.getElementById('confirmPaaswrd').value)
	  {
		alert("Please verify your password.");
		document.frmEditProfile.confirmPaaswrd.focus();
		return false;
	  }
	  document.getElementById('frmEditProfile').submit();
 }
 
 function validateManageUsersDetails()
	 {
	    
		 if(isEmpty(document.getElementById('userEmailId').value))
		 {
			alert('Please enter your email.');
			document.frmEditProfile.userEmailId.focus();
			return false;
		 }
		 if(!IsValidEMail(document.getElementById('userEmailId').value))
		 {
			alert('Email address not valid.');
			document.frmEditProfile.userEmailId.focus();
			return false;
		 }
		 if(isEmpty(document.getElementById('firstName').value))
		 {
			alert('Please enter your first name.');
			document.frmEditProfile.firstName.focus();
			return false ;
		 }
		 if(isEmpty(document.getElementById('userCity').value))
		 {
			alert('Please enter your city.');
			document.frmEditProfile.userCity.focus();
			return false ;
		 }
		 if(isEmpty(document.getElementById('dateOfBirth').value))
		 {
			alert('Please enter your birth date.');
			document.frmEditProfile.dateOfBirth.focus();
			return false ;
		 }
		 if(!isEmpty(document.getElementById('billingEmailAddress').value))
		 {
			 if(!IsValidEMail(document.getElementById('billingEmailAddress').value))
			 {
				alert('Billing email address not valid.');
				document.frmEditProfile.billingEmailAddress.focus();
				return false ;
			 }
		 }
		 document.getElementById('frmEditProfile').submit();
	 }


///////// Text removing from search box	////////////////////////
function removingText()
{
	if(document.getElementById('search1').value == "Begin Typing...")
	{
		document.getElementById('search1').value ="";
		document.frmHeaderSearch.search1.focus();
		return false ;
	}
}