function submitForm() {
	document.write("<form name='submitForm' id='submitForm' action='view-cart.php' method='post'>");
	document.write("<input type='text' name='test'>");
	document.write("</form>");
	document.submitForm.submit();
}
function position(o) {
	var pos = Position.get(o);
	Position.set('addtocartPopUp',pos);
}
function closeme() {
	var LayerDiv = document.getElementById('addtocartPopUp');
	LayerDiv.style.display = 'none';
}

//==================== Ajax function For populating catetories combobox 
function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}
function ltrim(stringToTrim) {
	return stringToTrim.replace(/^\s+/,"");
}
function rtrim(stringToTrim) {
	return stringToTrim.replace(/\s+$/,"");
}

// example of using trim, ltrim, and rtrim
function TrimAll(myString)
{
	myString = trim(myString);
	myString = ltrim(myString);
	myString = rtrim(myString);
	return myString;
}
	function ajaxCategoryAction(productId,myitemname,myimg,myitemprice) {
			var ajaxRequest;  // The variable that makes Ajax possible!
			var queryString ="";
			var optionValues = "";
			try{
				// Opera 8.0+, Firefox, Safari
				ajaxRequest = new XMLHttpRequest();
			   } catch (e){
				// Internet Explorer Browsers
							try{
								ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
							   } 		
								catch (e) {
											try{
												ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
												} 
												catch (e){
														// Something went wrong
															alert("Your browser does not support Ajax. You are using old browser!");
														  }	return false;
											}
							}
			
		// Create a function that will receive data sent from the server
			ajaxRequest.onreadystatechange = function()
				{
					if(ajaxRequest.readyState == 4)
						{
							
							optionValues = TrimAll(ajaxRequest.responseText);
							//alert(optionValues);
							optionValues = optionValues.split(":");
							if(optionValues[0] == "true")
							{	
								var Quantity = parseFloat(optionValues[1]);
								document.getElementById("viewQuantity").innerHTML = '('+Quantity+' items)';
								showPopup(myitemname,myimg,myitemprice);
							}
							else if(optionValues[0] == "false")
							{
								alert('You can not add more, this product is out of stock now.');
							}
														
						}
				}
		
			queryString = "?pId=" + productId;
			ajaxRequest.open("GET", "ajaxaddtocart-action.php" + queryString,true);
			ajaxRequest.send(null);		
		
	}

function showPopup(myitemname,myimg,myitemprice)
{
		//alert('i m in popup');
		//call here Ajax for Add to Cart
		// if return true please open popup
var popupHTML = "<table width='350' border='1' bordercolor='white' cellpadding='2' cellspacing='0' bgcolor='#000000'>"+
				"<tr>"+
					"<td valign='top'>"+
						"<table width='100%' border='0' cellpadding='0' cellspacing='0'>"+
						"<tr>"+
							"<td class='tblink'><div align='left'>&nbsp;&nbsp;Your item has been added!</div></td>"+
						"</tr>"+
						"<tr>"+
							"<td>&nbsp;</td>"+
						"</tr>"+
						"<tr>"+
							"<td>"+
								"<table width='100%' border='0' cellpadding='0' cellspacing='2'>"+
								"<tr>"+
									"<td width='100' valign='top'>"+
										"<span style='padding-left:4px;'>"+
											"<img src='administrator/productImages/"+myimg+"' width=75px class='borderpic1' />"+
										"</span>"+
									"</td>"+
									"<td valign='top'>"+
										"<table width='100%' border='0' cellspacing='0' cellpadding='0'>"+
										"<tr>"+
											"<td align='left' class='protitle'>"+myitemname+"</td>"+
										"</tr>"+
										"<tr>"+
											"<td height='14'></td>"+
										"</tr>"+
										"<tr>"+
											"<td align='left' class='pricetitle'>&pound; "+myitemprice+"</td>"+
										"</tr>"+
										"<tr>"+
											"<td height='14'></td>"+
										"</tr>"+
										"<tr>"+
											"<td align='left' >"+
												"<input name='Submit3' type='button' onClick='closeme();return false;' class='btngreys1' value='Continue Shopping' />"+
												"<input name='Submit2' onClick='submitForm();' type='button' class='btnreds' value='Checkout' />"+
											"</td>"+
										"</tr>"+
										"</table>"+
									"</td>"+
								"</tr>"+
								"</table>"+
							"</td>"+
						"</tr>"+
						"</table>"+
					"</td>"+
				 "</tr>"+
				"</table>";
		
		//var popupHTML = "<table width='400' border='0' cellpadding='3' cellspacing='3' bgcolor='#000000'><tr><td valign='top'><table width='100%' border='0' cellpadding='0' cellspacing='0'><tr><td class='tblink'>Your item has been added!</td></tr><tr><td>&nbsp;</td></tr><tr><td><table width='100%' border='0' cellpadding='0' cellspacing='0'><tr><td width='100' valign='top'><span style='padding-left:4px;'><img src='administrator/productImages/"+myimg+"' class='borderpic' width='79' height='78'/></span></td><td valign='top'><table width='100%' border='0' cellspacing='0' cellpadding='0'><tr><td class='protitle'>"+myitemname+"</td></tr><tr><td height='14'></td></tr><tr><td class='pricetitle'>&pound; "+myitemprice+"</td></tr><tr><td height='14'></td></tr><tr><td ><a href='#' onClick='closeme();return false;'><img src='images/btn_continueshop.jpg' width='122' height='26' border='0'/></a>&nbsp;<a href='viewcart.php?ca=1' title='click here to checkout'><img src='images/checkout.jpg' width='90' height='26' border='0'/></a></td></tr></table></td></tr></table></td></tr></table></td></tr></table>";
		
		var LayerDiv = document.getElementById('addtocartPopUp');
		LayerDiv.style.display = 'block';						
		LayerDiv.innerHTML=popupHTML;
		 
	}
	
	
	function ajaxActionNewsletterSub(email,contextPath)
	{
		//alert('hi');
		if(email != "" && isValidEmail (email) )
		{
			var ajaxRequest;  // The variable that makes Ajax possible!
			var queryString ="";
			var Values = "";
			try{
				// Opera 8.0+, Firefox, Safari
				ajaxRequest = new XMLHttpRequest();
			   } catch (e){
				// Internet Explorer Browsers
							try{
								ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
							   } 		
								catch (e) {
											try{
												ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
												} 
												catch (e){
														// Something went wrong
															alert("Your browser does not support Ajax. You are using old browser!");
														  }	return false;
											}
							}
			
			// Create a function that will receive data sent from the server
			ajaxRequest.onreadystatechange = function()
				{
					if(ajaxRequest.readyState == 4)
						{
								Values = ajaxRequest.responseText;
								if(Values != "")
								{		
									var MessagePasted = "";
									if (Values == 2)
									{
										MessagePasted = "<br />Your Email address has been added to the newsletter subscription list.";	
									}
									else if (Values == 1)
									{
										MessagePasted = "<br />Your Email address is already present in the newsletter subscription list.";	
									}
									else if (Values == 3)
									{
										MessagePasted = "<br />There was a problem while processing your request. Please try again.";	
									}
									else if (Values == 0)
									{
										MessagePasted = "<br />There was a problem while processing your request. Please try again.";	
									}
									document.getElementById("loadingImage").innerHTML = MessagePasted; 
								}
								else
								{
									MessagePasted = "<br />There was a problem while processing your request. Please try again.";	
									document.getElementById("loadingImage").innerHTML = MessagePasted; 
								}
						}
				}
		
			
			queryString = "?txtnewsletter_email=" + email;
			ajaxRequest.open("GET", "/"+contextPath+"/newsletterajaxaction.php" + queryString, true);
			ajaxRequest.send(null);
			window.scrollTo(0,600);
		}
		
		else 
		{
			alert('Please enter a valid Email Address.');
			document.getElementById('txtnewsletter_email').focus();
		}	
	}


String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}





function isValidEmail(str) {   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);}	


function Validate(strToValidate,RegPattern)
{
	var expr = new RegExp(RegPattern);
	var result = expr.test(strToValidate);
	if(result==true){
		return true;
	}else{
		return false;
	}
}

function frmvalidation()
{

	if(document.getElementById("txtfullname").value == "") {
		alert("Please enter your full name.");
		document.getElementById("txtfullname").focus();
		return false;
	}
	
	if(Validate(document.getElementById("txtfullname").value,"[^A-Za-z\\ ]") == true) {
		alert("Please enter valid name only characters are allowed.");
		document.getElementById("txtfullname").focus();
		return false;
	}
	
	if(document.getElementById("txtemail").value == "") {
		alert("Please enter your email address.");	
		document.getElementById("txtemail").focus();
		return false;
	}	
	
	if(Validate(document.getElementById("txtemail").value,"[A-Za-z0-9_\\.][A-Za-z]*@[A-Za-z]*\\.[A-Za-z0-9]") == false)	{
		alert("Please enter valid email address.");
		document.getElementById("txtemail").focus();
		return false;
	}
	
	
	
	if ((document.getElementById("txtphone").value==null)||(document.getElementById("txtphone").value=="")){
		alert("Please enter your phone number.");
		document.getElementById("txtphone").focus();
		return false;
	}
	if (checkInternationalPhone(document.getElementById("txtphone").value)==false){
		alert("Please enter 12 digits Phone Number. Only digits and +,- and ( ) are allowed.");
			document.getElementById("txtphone").focus();
		return false
	}
	
	
	
	if (document.getElementById("txtmob").value !='' && checkInternationalPhone(document.getElementById("txtmob").value)==false){
		alert("Please enter 12 digits Mobile Number. Only digits and +,- and ( ) are allowed.");
			document.getElementById("txtmob").focus();
		return false
	}
	

	if(Validate(document.getElementById("txtpostcode").value,"[^0-9A-Za-z0-9\\ ]") == true) {
		alert("Please enter valid postcode");
		document.getElementById("txtpostcode").focus();
		return false;
	}
	
	if(document.getElementById("comment").value == "" ){
		alert("Please enter your comments.");	
		document.getElementById("comment").focus();
		return false;
	}
	return true;
}
function FrmPasswordValidation() {
	if(document.getElementById("currentPassword").value.trim() == "") {
		alert("Please enter a Old Password.");
		document.getElementById("currentPassword").focus();
		return false;
	}		
	if(document.getElementById("currentPassword").value != document.getElementById("oldPassword").value) {
		alert("The Old Password is wrong.");
		document.getElementById("currentPassword").focus();
		return false;
	}		
	
	if(document.getElementById("newPassword").value.trim() == "") {
		alert("Please enter a New Password.");
		document.getElementById("newPassword").focus();
		return false;
	}		
	if(document.getElementById("confirmPassword").value.trim() == "") {
		alert("Please enter a value in the Confirm New Password field.");
		document.getElementById("confirmPassword").focus();
		return false;
	}	
	if(document.getElementById("newPassword").value.indexOf(" ") >0) {
		alert("Please enter a valid New Password. Spaces are not allowed.");
		document.getElementById("newPassword").focus();
		return false;
	}
	if(document.getElementById("confirmPassword").value.indexOf(" ") >0) {
		alert("Please enter a valid value in the Confirm New Password field. Spaces are not allowed.");
		document.getElementById("confirmPassword").focus();
		return false;
	}		
	if(document.getElementById("newPassword").value != document.getElementById("confirmPassword").value) {
		alert("The values in New Password and Confirm New Password field must match.");
		document.getElementById("confirmPassword").focus();
		return false;
	}		
	return true;
}
function ForgotPassValidation() {
	if(document.getElementById("email").value == "") {
		alert("Please enter your Email address.");	
		document.getElementById("email").focus();
		return false;
	}
	if(isValidEmail(document.getElementById("email").value) == false) {
		alert("Please enter valid email address.");
		document.getElementById("email").focus();
		return false;
	}	
	
	
	return true;
}

function submitPaging(frm,page)
{
	
	document.frmPaging.pageNo.value = page;
	document.frmPaging.submit();
}

function CheckLogin() {
	if(document.getElementById('txtUserName').value == '') {
		alert('Please enter email.');
		document.getElementById('txtUserName').focus();
		return false;
	}
	
	if(document.getElementById('txtPassword').value == '') {
		alert('Please enter password.');
		document.getElementById('txtPassword').focus();
		return false;
	}
}

function ValidationOnForm123() { 

	if(document.getElementById("firstName").value == "") {
			alert("Please enter your first name.");	
			document.getElementById("firstName").focus();
			return false;
	}	
	if(document.getElementById("lastName").value == "") {
		alert("Please enter your last name.");	
		document.getElementById("lastName").focus();
		return false;
	}	
	
	if(document.getElementById("emailAddress").value == "") {
		alert("Please enter your Email address.");	
		document.getElementById("emailAddress").focus();
		return false;
	}
	if(document.getElementById("password").value == "") {
		alert("Please enter your password.");	
		document.getElementById("password").focus();
		return false;
	}
	if(document.getElementById("password").value != document.getElementById("confirmPassword").value) {
		alert("Password and Confirm password should match.");	
		document.getElementById("confirmPassword").focus();
		return false;
	}
	
	
	if(isValidEmail(document.getElementById("emailAddress").value) == false) {
		alert("Please enter valid email address.");
		document.getElementById("emailAddress").focus();
		return false;
	}
	
	
	
	if ((document.getElementById("mobilePhone").value==null)||(document.getElementById("mobilePhone").value=="")){
		alert("Please enter your phone number.");
		document.getElementById("mobilePhone").focus();
		return false;
	}
	if (checkInternationalPhone(document.getElementById("mobilePhone").value)==false){
		alert("Please enter 12 digits Phone Number. Only digits and +,- and ( ) are allowed.");
			document.getElementById("mobilePhone").focus();
		return false
	}
	
		
	if(document.getElementById("baddress1").value == "") {
		alert("Please enter your billing address.");	
		document.getElementById("baddress1").focus();
		return false;
	}
	if(document.getElementById("bcity").value == "") {
		alert("Please enter your billing city.");	
		document.getElementById("bcity").focus();
		return false;
	}
	
	if(document.getElementById("bPostcode").value == "") {
		alert("Please enter your billing post code.");	
		document.getElementById("bPostcode").focus();
		return false;
	}	

	if(document.getElementById("bPostcode").value !="") {
		if(Validate(document.getElementById("bPostcode").value,"[^0-9A-Za-z0-9\\ ]") == true) {
			alert("Please enter valid billing post code");
			document.getElementById("bPostcode").focus();
			return false;
		}
	}
		
	if(document.getElementById("address1").value == "") {
		alert("Please enter your shipping address.");	
		document.getElementById("address1").focus();
		return false;
	}

	if(document.getElementById("city").value == "") {
		alert("Please enter your city.");	
		document.getElementById("city").focus();
		return false;
	}
	
	if(document.getElementById("postCode").value == "") {
		alert("Please enter your post code.");	
		document.getElementById("postCode").focus();
		return false;
	}	

	if(document.getElementById("postCode").value !="") {
		if(Validate(document.getElementById("postCode").value,"[^0-9A-Za-z0-9\\ ]") == true) {
			alert("Please enter valid post code");
			document.getElementById("postCode").focus();
			return false;
		}
	}
	return true;
}
function ValidationOnForm() { 

	if(document.getElementById("firstName").value == "") {
			alert("Please enter your first name.");	
			document.getElementById("firstName").focus();
			return false;
	}	
	if(document.getElementById("lastName").value == "") {
		alert("Please enter your last name.");	
		document.getElementById("lastName").focus();
		return false;
	}	
	
	if(document.getElementById("emailAddress").value == "") {
		alert("Please enter your Email address.");	
		document.getElementById("emailAddress").focus();
		return false;
	}
		
	if(isValidEmail(document.getElementById("emailAddress").value) == false) {
		alert("Please enter valid email address.");
		document.getElementById("emailAddress").focus();
		return false;
	}
	
	if(document.getElementById("mobilePhone").value !="") {
		if(Validate(document.getElementById("mobilePhone").value,"[^0-9+\\-\\+\\(\\)\\ ]") == true) {
			alert("Please enter 12 digits Phone Number. Only digits and +,- and ( ) are allowed.");
			document.getElementById("mobilePhone").focus();
			return false;
		}
	}
		
	if(document.getElementById("baddress1").value == "") {
		alert("Please enter your billing address.");	
		document.getElementById("baddress1").focus();
		return false;
	}
	if(document.getElementById("bcity").value == "") {
		alert("Please enter your billing city.");	
		document.getElementById("bcity").focus();
		return false;
	}
	
	if(document.getElementById("bPostcode").value == "") {
		alert("Please enter your billing post code.");	
		document.getElementById("bPostcode").focus();
		return false;
	}	

	if(document.getElementById("bPostcode").value !="") {
		if(Validate(document.getElementById("bPostcode").value,"[^0-9A-Za-z0-9\\ ]") == true) {
			alert("Please enter valid billing post code");
			document.getElementById("bPostcode").focus();
			return false;
		}
	}
	
	if(document.getElementById("address1").value == "") {
		alert("Please enter your shipping address.");	
		document.getElementById("address1").focus();
		return false;
	}

	if(document.getElementById("city").value == "") {
		alert("Please enter your city.");	
		document.getElementById("city").focus();
		return false;
	}
	
	if(document.getElementById("postCode").value == "") {
		alert("Please enter your post code.");	
		document.getElementById("postCode").focus();
		return false;
	}	

	if(document.getElementById("postCode").value !="") {
		if(Validate(document.getElementById("postCode").value,"[^0-9A-Za-z0-9\\ ]") == true) {
			alert("Please enter valid post code");
			document.getElementById("postCode").focus();
			return false;
		}
	}
	return true;
}

function openPopUp(url) {
	w = 700;
	h = 550;
	var left = (screen.width/2)-(w/2);
	var top = (screen.height/2)-(h/2);
	newwindow=window.open(url,'name','width=700,height=550, toolbar=no, top='+top+', left='+left+', status=no, scrollbars=yes, resize=yes, menubar=no');
	if (window.focus) {newwindow.focus()}
	return false;
}

function chanageAddress() {
	if(document.getElementById("same").checked){
		document.getElementById("address1").value = document.getElementById("baddress1").value;
		document.getElementById("address2").value = document.getElementById("baddress2").value;
		document.getElementById("city").value = document.getElementById("bcity").value;
		document.getElementById("county").value = document.getElementById("bcounty").value;
		document.getElementById("postCode").value = document.getElementById("bPostcode").value;		
	}
}


/**
 * DHTML phone number validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */

// Declaring required variables
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

function isInteger(s)
{   var i;
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}
function trim(s)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not a whitespace, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (c != " ") returnString += c;
    }
    return returnString;
}
function stripCharsInBag(s, bag)
{   var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checkInternationalPhone(strPhone){
var bracket=3
strPhone=trim(strPhone)
if(strPhone.indexOf("+")>1) return false
if(strPhone.indexOf("-")!=-1)bracket=bracket+1
if(strPhone.indexOf("(")!=-1 && strPhone.indexOf("(")>bracket)return false
var brchr=strPhone.indexOf("(")
if(strPhone.indexOf("(")!=-1 && strPhone.charAt(brchr+2)!=")")return false
if(strPhone.indexOf("(")==-1 && strPhone.indexOf(")")!=-1)return false
s=stripCharsInBag(strPhone,validWorldPhoneChars);
return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}
