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

document.getElementsByClassName = function(cl) {
	var retnode = [];
	var myclass = new RegExp('\\b'+cl+'\\b');
	var elem = this.getElementsByTagName('*');
	for (var i = 0; i < elem.length; i++) {
		var classes = elem[i].className;
		if (myclass.test(classes)) retnode.push(elem[i]);
	}
	return retnode;
}

function hide_menu() {
	children = document.getElementsByClassName('child');
	for(i=0; i<children.length; i++) {
		children[i].style.backgroundImage = "";
		var child_display = children[i].className.indexOf('opened') > -1 ? 'block' : 'none';
		children[i].style.display = child_display;
	}
}

function toogle_menu(elem) {
	/*
	var nav = navigator.userAgent.toLowerCase();
	if(nav.indexOf("msie") != -1) {
		elem.parentNode.nextSibling.style.display = elem.parentNode.nextSibling.style.display == 'block' ? 'none' : 'block' ;
	} else {
		elem.parentNode.nextSibling.style.display = elem.parentNode.nextSibling.style.display == 'block' ? 'none' : 'block' ;
	}
	*/
	//var site_url = 'http://192.168.2.97/~vlad/hozparts/';
	var site_url = 'http://spares.hozelock.com/';
	
	var sibling = elem.parentNode.nextSibling;
	sibling.style.display = sibling.style.display == 'block' ? 'none' : 'block' ;
	
	var elem_class = elem.parentNode.className;
	var arrow = elem_class.indexOf('blue') > -1 ? 'blue' : 'green';
	if(sibling.style.display == 'block') {
		elem.parentNode.style.backgroundImage = "url(" + site_url + "wp-content/themes/hozservices/img/sidemenu-arrow-" + arrow + "-open.png)";
	} else {
		elem.parentNode.style.backgroundImage = "url(" + site_url + "wp-content/themes/hozservices/img/sidemenu-arrow-" + arrow + "-close.png)";
	}
}

/*** POSTCODE LOOKUP FUNCTIONS ***/

var account_code = 'HOZEL11111';
var license_code = 'TD53-DJ54-GC76-RY42';
var machine_id   = '';

// function needed to hide the pca account information
function getAddress() {	
	pcaByPostcodeBegin();
}

function setAddress() {
	document.getElementById('PossibleAddress').style.display = 'none';
	if(document.getElementById('PossibleAddress').value) {
		pcaFetchBegin();
	} else {
		showAddressFields();
	}
}

function showAddressFields() {
	clearPcaFields();
	document.getElementById('Uneditable').value = 0;
	document.getElementById('AddressFieldsContainer').style.display = 'block';
}

function clearPcaFields() {
	document.getElementById("wp_sdpcat_shop_users_address1").value='';
	document.getElementById("wp_sdpcat_shop_users_address2").value='';
	document.getElementById("wp_sdpcat_shop_users_address3").value='';
	document.getElementById("wp_sdpcat_shop_users_address4").value='';
	document.getElementById("wp_sdpcat_shop_users_address5").value='';
	document.getElementById("wp_sdpcat_shop_users_POSTCODE").value='';
	document.getElementById("wp_sdpcat_shop_users_COUNTRY").value='';	
}

function pcaByPostcodeBegin() {
	var postcode = document.getElementById('wp_sdpcat_shop_users_POSTCODE').value;
	var scriptTag = document.getElementById("pcaScript");
	var headTag = document.getElementsByTagName("head").item(0);
	var strUrl = "";

	//Build the url
	strUrl = "http://services.postcodeanywhere.co.uk/inline.aspx?";
	strUrl += "&action=lookup";
	strUrl += "&type=by_postcode";
	strUrl += "&postcode=" + escape(postcode);
	strUrl += "&account_code=" + escape(account_code);
	strUrl += "&license_code=" + escape(license_code);
	strUrl += "&machine_id=" + escape(machine_id);
	strUrl += "&callback=pcaByPostcodeEnd";

	//Make the request
	if (scriptTag) {
		try {
			headTag.removeChild(scriptTag);
		} catch (e) {
			//Ignore
		}
	}
	scriptTag = document.createElement("script");
	scriptTag.src = strUrl
	scriptTag.type = "text/javascript";
	scriptTag.id = "pcaScript";
	headTag.appendChild(scriptTag);
}

function pcaByPostcodeEnd() {
	if (pcaIsError) {
		//Show the error message
		alert(pcaErrorMessage);
	} else {
		//Check if there were any items found
		if (pcaRecordCount==0) {
			alert("Sorry, no matching items found");
		} else {
			var possible_address = document.getElementById('PossibleAddress');
			possible_address.options.length = 0;
			possible_address.options[0] = new Option("My address isn't listed", "", false, false);

			var possible_address_inner = '<option value="">My address isn\'t listed.</option>';
			for(i=0; i<pcaRecordCount; i++) {
				possible_address.options[i+1] = new Option(pca_description[i], pca_id[i], false, false);
			}
			possible_address.style.display = 'block';
			document.getElementById('UneditableAddressDiv').style.display = 'none';
			document.getElementById('AddressFieldsContainer').style.display = 'none';
		}
	}
}

function pcaFetchBegin()  {
	var address_id = document.getElementById("PossibleAddress").value;
	var scriptTag = document.getElementById("pcaScriptTag");
	var headTag = document.getElementsByTagName("head").item(0);
	var strUrl = "";

	//Build the url
	strUrl = "http://services.postcodeanywhere.co.uk/inline.aspx?";
	strUrl += "&action=fetch";
	strUrl += "&id=" + escape(address_id);
	strUrl += "&account_code=" + escape(account_code);
	strUrl += "&license_code=" + escape(license_code);
	strUrl += "&machine_id=" + escape(machine_id);
	strUrl += "&callback=pcaFetchEnd";

	//Make the request
	if (scriptTag) {
		//The following 2 lines perform the same function and should be interchangeable
		headTag.removeChild(scriptTag);
		//scriptTag.parentNode.removeChild(scriptTag);
	}
	scriptTag = document.createElement("script");
	scriptTag.src = strUrl
	scriptTag.type = "text/javascript";
	scriptTag.id = "pcaScriptTag";
	headTag.appendChild(scriptTag);
}

function pcaFetchEnd() {
	//Test for an error
	if (pcaIsError) {
		//Show the error message
		alert(pcaErrorMessage);
	} else  {
		//Check if there were any items found
		if (pcaRecordCount==0) {
			alert("Sorry, no matching items found");
		} else {
			
			document.getElementById("wp_sdpcat_shop_users_address1").value=pca_line1[0];
			document.getElementById("wp_sdpcat_shop_users_address2").value=pca_line2[0];
			document.getElementById("wp_sdpcat_shop_users_address3").value=pca_line3[0] + ' ' + pca_line4[0] + ' ' + pca_line5[0];
			document.getElementById("wp_sdpcat_shop_users_address4").value=pca_post_town[0];
			document.getElementById("wp_sdpcat_shop_users_address5").value=pca_county[0];
			document.getElementById("wp_sdpcat_shop_users_POSTCODE").value=pca_postcode[0];
			document.getElementById("wp_sdpcat_shop_users_COUNTRY").value='UK';
			
			document.getElementById("UneditableAddressDiv").style.display = 'block';
			document.getElementById('Uneditable').value = 1;
			var uneditable_address = '';
			
			uneditable_address += pca_organisation_name[0] + '<br />';
			uneditable_address += pca_line1[0] + '<br />';
			uneditable_address += pca_line2[0] + '<br />';
			uneditable_address += pca_line3[0] + ' ';
			uneditable_address += pca_line4[0] + ' ';
			uneditable_address += pca_line5[0] + '<br />';
			uneditable_address += pca_post_town[0] + '<br />';
			uneditable_address += pca_county[0] + '<br />';
			uneditable_address += pca_postcode[0] + '<br />';
			uneditable_address += 'United Kingdom';
			document.getElementById('UneditableAddress').innerHTML = uneditable_address;
		}
	}
}
