/*window.onload=function() {
	if (document.getElementById("focus")) {
		document.forms[0][0].focus();
		document.getElementById("focus").focus();
	}
	//initForms();
}
*/
function ajaxFunction(div, string) {
	var xmlHttp;
	cursor_wait();
	
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e) {
		// Internet Explorer
	
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
	
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
			  alert("Your browser does not support AJAX!");
			  return false;
			}
		}
	
	}
	xmlHttp.onreadystatechange=function() {
		if (xmlHttp.readyState==4) {
			cursor_clear();
			document.getElementById(div).innerHTML = xmlHttp.responseText;
		}
	}
	xmlHttp.open("GET", string, true);
	xmlHttp.send(null);

}

function addToCart(productId, quantity, prefix, opt1, opt2, opt3) {

	var optionString = "";
	var quantity = parseFloat(quantity);
	
	// add options to string
	if (opt1 == 'x') {
		opt1 = '';
		opt2 = '';
		opt3 = '';
	} else {
		opt1 = '';
		opt2 = '';
		opt3 = '';
		if (document.getElementById('opt')) {
			opt1 = '&opt1='+document.getElementById('opt').value;
		}
		if (document.getElementById('opt2')) {
			opt2 = '&opt2='+document.getElementById('opt2').value;
		}
		if (document.getElementById('opt3')) {
			opt3 = '&opt3='+document.getElementById('opt3').value;
		}
	}
	/*
	if (document.getElementById('totalOptions')) {
		var i=0;
		var total = document.getElementById('totalOptions').value;
		var divID;
		
		while (i<=total) {
			divID = 'option_'+i;
			if (document.getElementById(divID)) {
				optionString+='&option_'+i+'='+document.getElementById(divID).value;
			}
			i=i+1;
		}
	}
	*/
	if (!isNaN(quantity) && quantity != '0') {
		
		ajaxFunction('cart',prefix+'functions/addToCart.php?id='+productId+'&quantity='+quantity+opt1+opt2+opt3);
		//window.scrollTo(0,0);
		document.getElementById('screen_overlay').style.display = 'block';
		
		// position the popup	
		divWidth = document.getElementById('basket_confirm').offsetWidth;
		divHeight = document.getElementById('basket_confirm').offsetHeight;
		vpWidthVal = vpWidth();
		vpHeightVal = vpHeight();

		//var scrollingLeft = Math.round((divWidth/2)-(document.getElementById('basket_confirm').style.width/2)) - (divWidth / 2) - 490;
		//var scrollingTop = Math.round((divHeight/2)-(document.getElementById('basket_confirm').style.height/2)+document.documentElement.scrollTop) - (divHeight / 2);
		var scrollingLeft = Math.round((vpWidthVal/2)-(divWidth/2));
		var scrollingTop = Math.round((vpHeightVal/2)-(divHeight/2));
	
		document.getElementById('basket_confirm').style.top = scrollingTop+'px';
		document.getElementById('basket_confirm').style.left = scrollingLeft+'px';
// end positioniing		
		
//		document.getElementById('basket_confirm').style.display = 'block';
		document.getElementById('cartWrap').style.display = 'block';
		//document.getElementById('basketError').innerHTML = '';
	} else {
		document.getElementById('basketError').innerHTML = '<p style="color: #cc0000;">Please enter a quantity.</p>';
		return false;
	}
}
function vpWidth() {
	return window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth;
}

function vpHeight() {
	return window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight;
}
/*
function addToCart(productId, quantity, prefix, opt1, opt2, opt3) {

	var optionString = "";
	var quantity = parseFloat(quantity);
	// add options to string

	var opt1 = '';
	var opt2 = '';
	var opt3 = '';
	
	if (document.getElementById('opt')) {
		opt1 = '&opt1='+document.getElementById('opt').value;
	}
	if (document.getElementById('opt2')) {
		opt2 = '&opt2='+document.getElementById('opt2').value;
	}
	if (document.getElementById('opt3')) {
		opt3 = '&opt3='+document.getElementById('opt3').value;
	}
	/*
	if (document.getElementById('totalOptions')) {
		var i=0;
		var total = document.getElementById('totalOptions').value;
		var divID;
		
		while (i<=total) {
			divID = 'option_'+i;
			if (document.getElementById(divID)) {
				optionString+='&option_'+i+'='+document.getElementById(divID).value;
			}
			i=i+1;
		}
	}
	*//*
	if (!isNaN(quantity) && quantity != '0') {
		//ajaxFunction('cart',prefix+'functions/addToCart.php?id='+productId+'&quantity='+quantity+optionString);

		ajaxFunction('cart',prefix+'functions/addToCart.php?id='+productId+'&quantity='+quantity+opt1+opt2+opt3);
		window.scrollTo(0,0);
		document.getElementById('basket_confirm').style.display = 'block';
		document.getElementById('cartWrap').style.display = 'block';
		//document.getElementById('basketError').innerHTML = '';
	} else {
		document.getElementById('basketError').innerHTML = '<p style="color: #cc0000;">Please enter a quantity.</p>';
		return false;
	}
}
*/
function buttonFiddle() {
	document.getElementById('cartBtn').style.display = 'none';
	document.getElementById('updtAdd').style.display = 'block';
}

function basket_confirm_close() {
	document.getElementById('basket_confirm').style.left = '-9999px';
//	document.getElementById('basket_confirm').style.display = 'none';
	document.getElementById('screen_overlay').style.display = 'none';
}

//function updtAddress(clientId, Email, DName, DTitle, DAddress1, DAddress2, DAddress3, DAddress4, DTown, DCounty, DPostcode, DPhone, prefix) {

function updtAddress(clientId, Email, DName, DTitle, DAddress1, DAddress2, DTown, DCounty, DPostcode, DPhone, comments, prefix, sessID) {
// Payment Sense option
	if (DName != '' && DAddress1 != '' && DTown != '' && DCounty != '' && DPostcode != '' && DPhone != '' && Email != '') {
		//alert(clientId+' - '+Email+' - '+DName+' - '+DTitle+' - '+DAddress1+' - '+DAddress2+' - '+DTown+' - '+DCounty+' - '+DPostcode+' - '+DPhone+' - '+prefix);
//ajaxFunction('',prefix+'functions/updtClient.php?id='+clientId+'&Email='+Email+'&DName='+DName+'&DTitle='+DTitle+'&DAddress1='+DAddress1+'&DAddress2='+DAddress2+'&DAddress3='+DAddress3+'&DAddress4='+DAddress4+'&DTown='+DTown+'&DCounty='+DCounty+'&DPostcode='+DPostcode+'&DPhone='+DPhone);
		ajaxFunction('addressError','/functions/updtClient.php?id='+clientId+'&Email='+Email+'&DName='+DName+'&DTitle='+DTitle+'&DAddress1='+DAddress1+'&DAddress2='+DAddress2+'&DTown='+DTown+'&DCounty='+DCounty+'&DPostcode='+DPostcode+'&DPhone='+DPhone+'&comments='+comments);
		//window.location = 'http://localhost/fyh/secure/'
		//window.location = prefix
		
		//window.location = 'https://secure.furnishyourhome.co.uk/secure/index.php?sessID='+sessID;
		//window.location = 'http://fyh/secure/index.php?sessID='+sessID;
		
		window.location = prefix+'index.php?sessID='+sessID;
	} else {
		document.getElementById('addressError').innerHTML = '<p style="color: #cc0000;">Please complete the address.</p>';
		return false;
	}
}

//function updtAddress2(clientId, Email, DName, DTitle, DAddress1, DAddress2, DAddress3, DAddress4, DTown, DCounty, DPostcode, DPhone, prefix) {
function updtAddress2(clientId, Email, DName, DTitle, DAddress1, DAddress2, DTown, DCounty, DPostcode, DPhone, comments, prefix, sessID) {
// PayPal option
	if (DName != '' && DAddress1 != '' && DTown != '' && DCounty != '' && DPostcode != '' && DPhone != '' && Email != '') {
		ajaxFunction('','/functions/updtClient.php?id='+clientId+'&Email='+Email+'&DName='+DName+'&DTitle='+DTitle+'&DAddress1='+DAddress1+'&DAddress2='+DAddress2+'&DTown='+DTown+'&DCounty='+DCounty+'&DPostcode='+DPostcode+'&DPhone='+DPhone+'&comments='+comments);
		//window.location = 'http://localhost/fyh/secure/'
		window.location = prefix+'index2.php?sessID='+sessID;
	} else {
		document.getElementById('addressError').innerHTML = '<p style="color: #cc0000;">Please complete the address.</p>';
		return false;
	}
}

function updtAddress3(clientId, first_name, last_name, salutation, address1, address2, city, state, zip, tel, prefix) {
// PayPal option
	var full_name = first_name + ' ' + last_name;

	if (full_name != '' && address1 != '' && city != '' && state != '' && zip != '' && tel != '') {
		ajaxFunction('addressError','/functions/updtClient.php?id='+clientId+'&full_name='+full_name+'&salutation='+salutation+'&address1='+address1+'&address2='+address2+'&city='+city+'&state='+state+'&zip='+zip+'&tel='+tel);
		document.Form.submit();
	} else {
		document.getElementById('addressError').innerHTML = '<p style="color: #cc0000;">Please complete the address.</p>';
		return false;
	}
}

function copyAddress() {
	//document.getElementById('Name').value = document.getElementById('DName').value;
	//document.getElementById('Title').value = document.getElementById('DTitle').value;
	if (document.getElementById("Dsalutation") != null) {
		document.getElementById('SalutationHidden').value = document.getElementById('Dsalutation').innerHTML;
	}
	if (document.getElementById("Dfname") != null) {
		document.getElementById('NameHidden').value = document.getElementById('Dfname').innerHTML;
	}
	if (document.getElementById("Dadd1") != null) {
		document.getElementById('Address1').value = document.getElementById('Dadd1').innerHTML;
	}
	if (document.getElementById("Dadd2") != null) {
		document.getElementById('Address2').value = document.getElementById('Dadd2').innerHTML;
	}
/*	if (document.getElementById("Dadd3") != null) {
		document.getElementById('Address3').value = document.getElementById('Dadd3').innerHTML;
	}
	if (document.getElementById("Dadd4") != null) {
		document.getElementById('Address4').value = document.getElementById('Dadd4').innerHTML;
	}
*/	if (document.getElementById("Dtown") != null) {
		document.getElementById('City').value = document.getElementById('Dtown').innerHTML;
	}
	if (document.getElementById("Dcounty") != null) {
		document.getElementById('State').value = document.getElementById('Dcounty').innerHTML;
	}
	if (document.getElementById("Dpostcode") != null) {
		document.getElementById('Postcode').value = document.getElementById('Dpostcode').innerHTML;
	}
	if (document.getElementById("Dphone") != null && document.getElementById("Dphone").innerHTML != '') {
		document.getElementById('PhoneHidden').value = document.getElementById('Dphone').innerHTML;
	}
	return false;
}

function copyAddress2() {
	//document.getElementById('Name').value = document.getElementById('DName').value;
	//document.getElementById('Title').value = document.getElementById('DTitle').value;
	if (document.getElementById("Dsalutation") != null) {
		document.getElementById('SalutationHidden').value = document.getElementById('Dsalutation').innerHTML;
		document.getElementById('salutation').value = document.getElementById('Dsalutation').innerHTML;
	}
	if (document.getElementById("Dfname") != null) {
		document.getElementById('NameHidden').value = document.getElementById('Dfname').innerHTML;
	}
	if (document.getElementById("Dadd1") != null) {
		document.getElementById('address1').value = document.getElementById('Dadd1').innerHTML;
	}
	if (document.getElementById("Dadd2") != null) {
		document.getElementById('address2').value = document.getElementById('Dadd2').innerHTML;
	}
/*	if (document.getElementById("Dadd3") != null) {
		document.getElementById('Address3').value = document.getElementById('Dadd3').innerHTML;
	}
	if (document.getElementById("Dadd4") != null) {
		document.getElementById('Address4').value = document.getElementById('Dadd4').innerHTML;
	}
*/	if (document.getElementById("Dtown") != null) {
		document.getElementById('city').value = document.getElementById('Dtown').innerHTML;
	}
	if (document.getElementById("Dcounty") != null) {
		document.getElementById('state').value = document.getElementById('Dcounty').innerHTML;
	}
	if (document.getElementById("Dpostcode") != null) {
		document.getElementById('zip').value = document.getElementById('Dpostcode').innerHTML;
	}
	if (document.getElementById("Dphone") != null && document.getElementById("Dphone").innerHTML != '') {
		document.getElementById('PhoneHidden').value = document.getElementById('Dphone').innerHTML;
		document.getElementById('tel').value = document.getElementById('Dphone').innerHTML;
	}
	return false;
}

// Changes the cursor to an hourglass
function cursor_wait() {
	document.body.style.cursor = 'wait';
}

// Returns the cursor to the default pointer
function cursor_clear() {
	document.body.style.cursor = 'default';
}

function confirmDelete(pageID, prefixURL, type){
	var divWidth = '235';
	var divHeight = '200';
	var content = '<div class="shadow" style="padding-top: 10px; padding-left: 10px; padding-bottom: 10px; background-color: #ffffff; border: solid 1px #666666; border-top:solid 30px #661C3F; height: 85px; width: 300px; "><h2>Delete item from basket</h2><p>Are you sure you want to delete this item?</p><div class="buttonWrap"><a onclick="return deleteConfirmed('+pageID+',\''+prefixURL+'\',\''+type+'\');" class="button">OK</a> <a onclick="return displayNone(\'popupDiv\');" class="button">Cancel</a></div></div>';
	
	popupDiv(content, divWidth, divHeight);
}

function deleteConfirmed(pageID, prefixURL, type){
//	window.location.href=prefixURL+'functions/cartDel.php?recordID='+pageID;
	var optionString = "";
	var quantity = "";
	var typeURL = "?type="+type;
	
	//ajaxFunction('',prefixURL+'functions/cartDel.php?recordID='+pageID+'&quantity='+quantity+optionString);
	//window.location.href=prefixURL+'Basket.htm'+typeURL;
	
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e) {
		// Internet Explorer
	
		try {
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
	
			try {
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
			  alert("Your browser does not support AJAX!");
			  return false;
			}
		}
	
	}
	xmlHttp.onreadystatechange=function() {
		if (xmlHttp.readyState==4) {
			cursor_clear();
			window.location.href=prefixURL+'Basket.htm'+typeURL;
		}
	}
	xmlHttp.open("GET", prefixURL+'functions/cartDel.php?recordID='+pageID+'&quantity='+quantity+optionString, true);
	xmlHttp.send(null);
}

function displayNone(div){
document.getElementById(div).style.display = 'none';
}

function displayBlock(div){
document.getElementById(div).style.display = 'block';
}

function popupDiv(content, divWidth, divHeight){
	var divID = 'popupDiv';
	var box;
	
	if(document.getElementById(divID)){
		box = document.getElementById(divID);
	} else {
		box = document.createElement("div");
		box.setAttribute("id",divID);
		document.body.appendChild(box);
	}

	var scrollingLeft = Math.round((document.documentElement.clientWidth/2)-(box.style.width/2)) - (divWidth / 2);

	var scrollingTop = Math.round((document.documentElement.clientHeight/2)-(box.style.height/2)+document.documentElement.scrollTop) - (divHeight / 2);

	box.style.width = divWidth+'px';
	box.innerHTML = content;
	box.style.display = 'block';
	box.style.position = 'absolute';
	box.style.top = scrollingTop+'px';

	box.style.left = scrollingLeft+'px';
	box.style.zIndex = '1';

	//return hideShowSelects('hidden');
	return
}

function hideShowSelects(toggle){

	if (navigator.appName == 'Microsoft Internet Explorer') {
	
		var e=document.getElementsByTagName("select");
		
			for(var i=0;i<e.length;i++) {
				e[i].style.visibility = toggle;
			}
		
		return false;
	
	} else {
		return false;
	}

}

function onlyNumbers(e) {

	var iKeyCode;

	if (!e) {
		var e = window.event;
	}

	if (e.keyCode) {
		iKeyCode = e.keyCode;
	} else {
		if (e.which) {
			iKeyCode = e.which;
		}
	}

	switch(iKeyCode) {

		case 8:
		case 9:
		case 35:
			break;
		case 36:
			break;
		case 37:
		case 38:
		case 39:
		case 40:
		case 46:
			break;
		case 48:
		case 49:
		case 50:
		case 51:
		case 52:
		case 53:
		case 54:
		case 55:
		case 56:
		case 57:
			if (e.shiftKey || e.altKey){
				return false;
			}
			break;
		case 96:
		case 97:
		case 98:
		case 99:
		case 100:
		case 101:
		case 102:
		case 103:
		case 104:
		case 105:
			//return correct numeric from keypad
			return iKeyCode - 48; break;
		case 110:
		//case 190:
			//if you are supporting decimal points
		//	return '.';	break;
		default: return false;
	}
}

function initForms() {
	for (var i=0; i< document.forms.length; i++) {
		document.forms[i].onsubmit = function() {return validForm();}
	}
}

function validForm() {
	var allGood = true;
	var allTags = document.getElementsByTagName("*");

	for (var i=0; i<allTags.length; i++) {
		if (!validTag(allTags[i])) {
			allGood = false;
		}
	}
	return allGood;

	function validTag(thisTag) {
		var outClass = "";
		var allClasses = thisTag.className.split(" ");
	
		for (var j=0; j<allClasses.length; j++) {
			outClass += validBasedOnClass(allClasses[j]) + " ";
		}
	
		thisTag.className = outClass;
	
		if (outClass.indexOf("invalid") > -1) {
			thisTag.focus();
			if (thisTag.nodeName == "INPUT") {
				thisTag.select();
			}
			return false;
		}
		return true;
		
		function validBasedOnClass(thisClass) {
			var classBack = "";
		
			switch(thisClass) {
				case "":
				case "invalid":
					break;
				case "reqd":
					if (allGood && thisTag.value == "") classBack = "invalid ";
					classBack += thisClass;
					document.getElementById("reqhead").className = "textRedBold";
					break;
				default:
					classBack += thisClass;
			}
			return classBack;
		}
	}
}

function goThere(){
	//var prefixURL = '/fyh/';
	var prefixURL = 'http://www.furnishyourhome.co.uk/';
	var x=document.getElementById("delivery");
	var qry = x.options[x.selectedIndex].value;
	window.location.href=prefixURL+'Basket.htm?type='+qry;
}

function getPromo(){
	//var prefixURL = '/fyh/';
	var prefixURL = 'http://www.furnishyourhome.co.uk/';
	var x=document.getElementById("promo_code").value;
	window.location.href=prefixURL+'Basket.htm?promo_code='+x;
}

function validField(thisTag,toDo) {
//	var bg_image = thisTag.currentStyle.backgroundImage;
	var bg_image = 'url(../images/'+thisTag.name+'BG.gif)';
	if (thisTag.value == "" && toDo == "") {
		thisTag.style.backgroundImage=bg_image;
	} else {
		thisTag.style.backgroundImage='none';
	}
}

jQuery(function() {
	jQuery('.fadein div:gt(0)').hide();
	setInterval (
		function() {
			jQuery('.fadein div:first-child').fadeOut(1000)
			.next().fadeIn(1000)
			.end().appendTo('.fadein') ;}, 8000);
});



