// JavaScript Document

function validatezip(zipfld){
	var testzip = zipfld.value.replace( /\s/g, "" ); 
	if(testzip==""){	
		zipfld.value=zipfld.value.replace( /\s/g, "" ); 
		return false;
	}
	return true;
}

function hopewiserlookup(numfld,zipfld,cntfld,thisform,url){//number,zip,country - field
	if(validatezip(zipfld)) {
		var d = new Date();
		var randomnumber = d.getTime();//stops cache
		var countrycode = cntfld.value;
		var xurl=url+"/book/ajax/hopewiser.cfm?action=partiallookup&nosession=true&zip="+zipfld.value+"&number="+numfld.value+"&countrycode="+countrycode+"&rts="+randomnumber;
		hopewiserajax(xurl,thisform);
	} else {
		alert('You have not entered your post code');
		hidepleasewait();
	}
}

function hopewiserajax(xurl,thisform){
	var xmlhttp = getxmlhttp();
	xmlhttp.open("GET", xurl,true);
	xmlhttp.onreadystatechange=function() {
		if (xmlhttp.readyState==4) {
			rtxt=xmlhttp.responseText;
			if (rtxt.search("nomatch") >= 0){
				hidepleasewait();
				pleasewait('notfoundaddress');
				showsinglelayer('manualaddress');
				hidesinglelayer('autoaddress');
			}
			else if (rtxt.search("offline") >= 0) {
				hidepleasewait();
				pleasewait('erroraddress');
				showsinglelayer('manualaddress');
				hidesinglelayer('autoaddress');
			}
			else{
				hidepleasewait();
				populateAddresses(rtxt,thisform);
			}
		} //end ready state
	}//end function
	xmlhttp.send(null);	
}

function addOption(theSel, theText, theValue){
	theText = theText.replace(/^\s+|\s+$/g,"");
  var newOpt = new Option(theText, theValue);
  var selLength = theSel.length;
  theSel.options[selLength] = newOpt;
}

function populateAddresses(theList,thisform){	
	thisform.hwaddress.length=0;//empty dropdown addresses
	var arAddress = theList.split("|");//split list into address group array 
	if(thisform.hwaddress.disabled==true){
		thisform.hwaddress.disabled=false;
	}
	for (var i=0; i < arAddress.length; i++){
		elem = arAddress[i];
		arElem = elem.split("$$");
		txtOpt = arElem[0];//address as one string
		valOpt = arElem[1]+"|"+arElem[2]+"|"+arElem[3]+"|"+arElem[4]+"|"+arElem[5]+"|"+arElem[6]+"|"+arElem[7]+"|"+arElem[8]+"|"+arElem[9]+"|"+arElem[10];		
		addOption(thisform.hwaddress,txtOpt,valOpt);	
	}
	changeAddress(thisform);
}

function changeAddress(thisform){
	var addressSel = document.getElementById('hwaddress');
	var unsplitaddress =addressSel.options[addressSel.selectedIndex].value;
	var arSelAddress = unsplitaddress.split("|");
	//assign hopewiser keys to fields
	thisform.hwmanualkey.value=arSelAddress[0];
	thisform.hwdedupkey.value=arSelAddress[1];
	thisform.hwextdedupkey.value=arSelAddress[2];	
	thisform.hwpostcodestatus.value=arSelAddress[3];
	//address fields	
	thisform.supplement.value=arSelAddress[4];
	//(dutch only) try to apply the auto search parameter of supplement
	if(thisform.supplement.value==''){
		try{
			thisform.supplement.value=thisform.hwsupplement.value;
		}
		catch(err){
		}
	}
	thisform.number.value=arSelAddress[5];
	thisform.street.value=arSelAddress[6];
	thisform.city.value=arSelAddress[7];
	thisform.state.value=arSelAddress[8];
	thisform.zip.value=arSelAddress[9];	
}


