var ajaxObjects = new Array();

/*****used on contact form*******/
function ajaxCheckPostcode(postcode)
{


 
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = "http://dev.branchesfurniture.co.uk/ajax/getstore.php";	
	ajaxObjects[ajaxIndex].setVar('postcode',postcode);
	ajaxObjects[ajaxIndex].onCompletion = function(){ showPostcode(ajaxIndex); };	// Specify function that will be executed after file has been found
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function	
}//end function ajaxCheckPostcode(postcode)

function showPostcode(ajaxIndex){

var storeid = ajaxObjects[ajaxIndex].response;	// Breaking response from Ajax into tokens

document.getElementById("store").style.display="block";

if(storeid>0)
{
	document.getElementById("store").disabled=true;
	for(i=0; document.getElementById("store").options[i]; i++)
	{
		if(document.getElementById("store").options[i].value==storeid)
		{
			document.getElementById("store").selectedIndex=i;		
			//set hidden field as well
			document.getElementById("store_hidden").value=document.getElementById("store").options[i].value;		
			alert('Your enquiry will be forwarded to '+document.getElementById("store").options[i].text);	
		}
	}
	
}else{
	document.getElementById("nearestlabel").style.display="block"
	document.getElementById("store").disabled=false;
	alert('Your postcode is not covered by any of stores.\r\nPlease select your nearest store from the drop down menu.');
}

}//end function showPostcode(ajaxIndex){

/******end of contact form functions*******/

/*****used on register form*******/

function ajaxCheckPostcode_register(postcode)
{
	var new_postcode = split_postcode(postcode);
	postcode_split=new_postcode.split(" ");
	var ajaxIndex = ajaxObjects.length;
	ajaxObjects[ajaxIndex] = new sack();
	ajaxObjects[ajaxIndex].requestFile = "/ajax/getstore.php";	
	ajaxObjects[ajaxIndex].setVar('postcode',postcode_split[0]);
	ajaxObjects[ajaxIndex].onCompletion = function(){ showPostcode_register(ajaxIndex); };	// Specify function that will be executed after file has been found
	ajaxObjects[ajaxIndex].runAJAX();		// Execute AJAX function	
}//end function ajaxCheckPostcode_register(postcode)

function showPostcode_register(ajaxIndex){
var storeid = ajaxObjects[ajaxIndex].response;	// Breaking response from Ajax into tokens

	if(storeid>0)
	{
		
	
	document.getElementById('selectedstoretext').style.display="block";
	document.getElementById('storeselectiondiv').style.display="block";
	document.getElementById('selectstoretext').style.display="none";
	document.getElementById("store").disabled=true;
	document.getElementById('store').style.display="block";
	document.getElementById("store_hidden").value="";
	for(i=0; document.getElementById("store").options[i]; i++)
	{	
		document.getElementById('storeselectiondiv').style.display="block";
		if(document.getElementById("store").options[i].value==storeid)
		{
			document.getElementById("store").selectedIndex=i;		
			//set hidden field as well
			document.getElementById("store_hidden").value=document.getElementById("store").options[i].value;			
		}
	}
	}else{ // if(storeid>0)

		document.getElementById('storeselectiondiv').style.display="block";
		document.getElementById('selectstoretext').style.display="block";
		document.getElementById('selectedstoretext').style.display="none";
		document.getElementById('store').style.display="block";
		document.getElementById("store_hidden").value="";
		document.getElementById("store").disabled=false;
		document.getElementById("store").selectedIndex=0;
		
	}//end else{ // if(storeid>0)

}//end function showPostcode_register(ajaxIndex)


/*******
splits postcode into two parts - adding space where needed.
based on php function written in func.php file
*******/
function split_postcode(postcode)
{
	
	//if there is a space no need to do anything
	if(postcode.indexOf(" ")=="-1")
	{
		//get length
		var length=postcode.length;	
		
		//space should be before last three characters
		//have to use -4 as count in loop is from 0
		var space = length-4;
		var new_postcode="";
		//add in space in loop
		for(i=0; i<length;i++)
		{
			//add value to new postcode
			new_postcode+=postcode[i];
			//if $i is equal to the space marker 
			//next character should be a space
			if(i==space) new_postcode+=" ";
		}
		//set to variable return
		postcode=new_postcode;
		
		/**
		 * probably a better way to do this than that silly loop
		 * but didn't know off the top of my head and didn't have the time
		 * to spend hours looking for a function for it
		 * 
		 * Dave
		 */
	}
	
	return postcode;
}//end function split_postcode(postcode)

/******end of register form functions*******/
