<!-- // JavaScript Document
var doc = document;

function serverName() { //GET THE SERVER NAME TO USE ABSOLUTE URLS WHERE NEEDED
	var server;
	if(location.href.indexOf("localhost") != -1) 		{ server = "http://localhost/paperassist.ie"; }
	else if(location.href.indexOf("dev.") != -1)		{ server = "http://www.dev.paperassist.ie"; }
	else {	if(location.href.indexOf("https") != -1)	{ server = "https://www.paperassist.ie"; }
	else												{ server = "http://www.paperassist.ie"; }}
	return server;
}


function showHideElement(divID,action) { //DISPLAY A HIDDEN ID TAG AND IF OPPOSITE IS SET, HIDE OPPOSITE THE DIVID
	var a = '#'+divID; if($(a).is(':visible')) { if(action) { $(a).hide(); } else { $(a).fadeOut(); } } 
	else { if(action) { $(a).show(); } else { $(a).show(500); } }
}

function swapDiv(show,hide) { // SWAP THE DISPLAY ATTRIBUTES OF AN ELEMENT
	$('#'+show).slideUp(); $('#'+hide).slideDown();
}

function windowSize(type) { // GET DIMS OF WINDOW FOR JQUERY FUNCTIONS
	var w = 0; var h = 0; if(!window.innerWidth){ 
	if(!(doc.documentElement.clientWidth == 0))	{ w = doc.documentElement.clientWidth; h= doc.documentElement.clientHeight; } //IE STRICT
	else 										{ w = doc.body.clientWidth; h = doc.body.clientHeight; }} //QUIRKS MODE
	else 										{ w = window.innerWidth; h = window.innerHeight; } // W3C
	if(type=="width") { return w; } else { return h; }
}

function getSelectedIndex(cellID) { //GET THE SELECTED INDEX VALUE OF A SELECT BOX
	var selectedItem = doc.getElementById(cellID).selectedIndex; var selectedvalue = doc.getElementById(cellID).options[selectedItem].value; return selectedvalue;
}

function changeThisPage(selectBox) { //CHANGE A PAGE AUTOMATICALLY FROM A SELECT BOX TO UPDATE A VIEW
	if(selectBox)	{ var url = getSelectedIndex(selectBox); } else { var url = getSelectedIndex("changePage"); } window.location = url;
}

function changeFormCountry(country,cell1,cell2) { // CHANGES COUNTRY CODE PHONE NUMBER
	$.ajax ({ url: serverName()+"/home/ajaxupdate.exe.php", type: 'GET', data: "country="+getSelectedIndex(country),
	  success: function(result) { $("#"+cell1).val(result); if($("#"+cell2)) { $("#"+cell2).val(result); } } });
}

function updateQuickSearch(type,homepageIndex,onFocus) { // CHANGES COUNTRY CODE PHONE NUMBER
	var folder; var cellId; if(homepageIndex) {
	var arrLeftPos		= Array("","95px","443px","770px");
	var cellIds			= Array("","#quickSearchPaper","#quickSearchEnvelopes","#quickSearchBrand");
	var defaultValue	= Array("","Search all Paper & Card","Search all Envelopes","Search all Brands");
	var folders			= Array("","paper","envelopes","product-library");
	$('#quickSearchPanel').css('left', arrLeftPos[homepageIndex]); cellId = cellIds[homepageIndex];
	if($(cellId).val()==defaultValue[homepageIndex]) { $(cellId).val(''); } folder = folders[homepageIndex];
	} else {
	cellId = "#quickSearch"; if($(cellId).val()=="Search all Brands") { $(cellId).val(''); }
	if(type=="envelopes") 		{ folder = "envelopes"; } else if(type == "paper") 	{ folder = "paper"; }
	else						{ folder = "product-library"; } } $('#quickSearchPanel').fadeIn(); 
	if(onFocus) { $("#quickSearchList").html('<img src="'+serverName()+'/_images/icons/load.gif" alt="Loading" style="padding:25px 0px 25px 125px;" />'); }
	$.ajax ({ url: serverName()+"/"+folder+"/ajaxupdate.exe.php", type: 'GET', data: "data="+$(cellId).val(),
	success: function(result) { $("#quickSearchList").html(result); } });
}

function doProductH3(id) {
	var a = '#productUl'+id; var b = '#productH3'+id;
	if ($(a).is(':hidden')) { $(a).slideDown(); $(b).css("background-image", "url("+serverName()+"/_images/icons/open.png)"); } 
	else					{ $(a).slideUp();  $(b).css("background-image", "url("+serverName()+"/_images/icons/closed.png)"); }
}

function logout() {  // LOGOUT OF CUSTOMER ACCOUNT
	window.location = serverName()+"/home/ajaxupdate.exe.php?logoutAccount"; 
}

function setBackground() { // SET POPUP FOR FIRST TIME IN CHECKOUT TO DISPLAY OPTIONS
	var $b = $(document).height(); $b += "px";
	scrollTo(0,0); var posLeft = (((windowSize("width"))-700)/2)+"px";
	$('#checkoutDetails').css({'left':posLeft}).fadeIn(200);
	$('#mainBackground').css({'height':$b,'opacity':0}).show().animate({'opacity':0.8},200);
}

function copyBilling() { // MONITOR AND COPIES BILLING ADDRESS TO DELIVERY ADDRESS IN THE CHECKOUT
	var pass = ""; if(doc.getElementById("copyDetails").checked == true) {
		
	doc.getElementById("delivery_address1").value = doc.getElementById("billing_address1").value;
	if(doc.getElementById("billing_address1").value == "") { pass = "first line of"; } else { doc.getElementById("delivery_address1").style.background = "#ffffff"; }
	if(doc.getElementById("delivery_company")) {
	doc.getElementById("delivery_company").value = doc.getElementById("billing_company").value;
	if(doc.getElementById("billing_company").value == "") { pass = "first line of"; } else { doc.getElementById("delivery_company").style.background = "#ffffff"; } }
	doc.getElementById("delivery_address2").value = doc.getElementById("billing_address2").value; 
	doc.getElementById("delivery_postcode").value = doc.getElementById("billing_postcode").value; 
	if(doc.getElementById("billing_postcode").value == "")	{ pass = "postcode for"; } else { doc.getElementById("delivery_postcode").style.background = "#ffffff"; }
	doc.getElementById("delivery_city").value = doc.getElementById("billing_city").value; 
	if(doc.getElementById("billing_city").value == "") { pass = "city or county in"; } else { doc.getElementById("delivery_city").style.background = "#ffffff"; }
	//this line only for customer accounts
	if(doc.getElementById("country")) { var bilValue = getSelectedIndex("billing_country_id");
	for(var i = 1; i <= 300; i++) { if(doc.getElementById("country").options[i]) {
	if(bilValue == doc.getElementById("country").options[i].value) { var newIndex = i; }}}
	if(newIndex) { doc.getElementById("country").selectedIndex = newIndex; } 
	else { alert('The billing country you have selected is not available as a country where we can deliver to.'); }}}
	else { doc.getElementById("delivery_address1").value = doc.getElementById("delivery_address2").value = doc.getElementById("delivery_postcode").value = doc.getElementById("delivery_city").value = "";  }
}

function validateAccount() { // WARNING JS POPUP THAT T&C HAS NOT BEED CHECKED WHENE CREATING AN ACCOUNT
	if(doc.getElementById("verifyAccount").checked == false){ alert("To create your account, you must verify that you accept our terms and conditions"); return false; } 
	else  													{ return true; }
}

function terms() { //POPUP TERMS AND CONDITIONS IN CHECKOUT
	window.open(serverName()+"/checkout/terms.php",'','width=800,height=500,scrollbars=1');
}

function closeAccount() {
	var answer = confirm("Are you sure that you want to delete your account with Paper Assist?"); if (answer) { window.location = serverName()+"/home/ajaxupdate.exe.php?closeAccount"; }
}

function doDocument(id,type) { // OPEN WINDOW WITH DELIVERY PRICING DETAILS
	if(type=="vat") 			{ url = serverName()+"/cart/vat.php?country="+id; } 
	else if(type=="delivery")	{ url = serverName()+"/cart/delivery.php?country="+id; }
	else						{ url = serverName()+"/checkout/terms.php"; }
	window.open(url,'','width=800,height=600,scrollbars=1');
}

function getSheet(siteArea,board_colour_id,board_weight_id,board_size_id,board_emboss_id,sales_unit_id) { //PROCESS DATA FROM getSheetInfo() FUNCTION
	if(siteArea=="labels") {
		window.location = serverName()+"/"+siteArea+"/ajaxupdate.exe.php?changeSelectedLabel="+board_colour_id+"&material="+board_weight_id+"&colour="+board_size_id+"&shape="+board_emboss_id+"&unit="+sales_unit_id;
	} else {
		window.location = serverName()+"/"+siteArea+"/ajaxupdate.exe.php?changeSelectedColour="+board_colour_id+"&weight="+board_weight_id+"&size="+board_size_id+"&emboss="+board_emboss_id+"&unit="+sales_unit_id;
	}
}

function doQuickCart() {
	if(doc.getElementById('cartQuickView')) { showHideElement('cartQuickView'); }
}

function getLabel(sheetSizeId,materialId,colourId,shapeId,stockId,labelId) {
	window.location = serverName()+"/labels/ajaxupdate.exe.php?gotolabel="+sheetSizeId+"&material="+materialId+"&colour="+colourId+"&shape="+shapeId+"&stock="+stockId+"&label="+labelId
}
