	/**
	@author zhangliang
	For ajax : when user input suburb name, this function can get suburb array 
	from server by dwr.
	*/ 
	
	var divCount = 0;
	var keycode = -1 ;
	var inputWidth =150; 
	var inputHeight =16;


	if(document.addEventListener){ //code for Moz 

		document.addEventListener("keyup",getSuburbByStateIdx,false);
	}else{ 
		document.attachEvent("onkeyup",getSuburbByStateIdx);
	} 

	
	function getSuburbByStateIdx(e){
		var inputId = "" ;
		if (!e) var e = window.event;
			e.cancelBubble = true;
		if (e.stopPropagation)
		{
		 e.stopPropagation();
		}
		
		if(!document.all){
			keycode = e.keyCode;
			inputId = e.target.id;
		}else{
		 	keycode =event.keyCode;
		 	inputId =event.srcElement.id;
		}
		
		if(inputId=="insuburb" && e.type=="keyup"){
			var  subsurStr = DWRUtil.getValue("insuburb");
			var div= document.getElementById("inSuburbDiv");
			clearStreet_name();
			locationDiv(div, "insuburb");
			if(subsurStr ==""){
				div.style.display = "none";
				locationDiv(div, "insuburb");
				divCount= 0;
			}else if(keycode == 13 &&div.style.display == "block"){
				setValueByCount(divCount,"inSuburbDiv", "insuburb" );
			}else  if(keycode == 40 && div.style.display == "block"){
				divCount++;
				focusToDivByCount("inSuburbDiv", divCount);
			}else  if(keycode == 38 && div.style.display == "block"){
				divCount--;
				focusToDivByCount("inSuburbDiv", divCount);
			}else if(keycode == 9){
			div.style.display = "none";
			}
			else if(keycode != 13 ){
				var stateId = DWRUtil.getValue("stateId");
				search.getSuburbByStateId(stateId, subsurStr, getSubsurList);
				divCount= 0;
			}else {
				div.style.diplay="none" ;
			}
		}
		else if(inputId=="street_name" && e.type=="keyup") {
			var  streetStr = Trim(DWRUtil.getValue("street_name"));
			var subsurStr = Trim(DWRUtil.getValue("insuburb"));
			var div= document.getElementById("inStreetDiv");
			locationDiv(div, "street_name");
			if(streetStr =="" ){
				div.style.display = "none";
			}else if(keycode == 13&& div.style.display == "block"){
				setValueByCount(divCount,"inStreetDiv", "street_name" );
			}else  if(keycode == 40&& div.style.display == "block"){
				divCount++;
				focusToDivByCount("inStreetDiv", divCount);
			}else  if(keycode == 38&& div.style.display == "block"){
				divCount--;
				focusToDivByCount("inStreetDiv", divCount);
			}else if(keycode != 13 ){
				var stateId = DWRUtil.getValue("stateId");
				
				search.getStreetByStateAndSuburb(stateId, subsurStr,streetStr, getStreetList);
			}else {
				div.style.diplay="none" ;
			}
		}else {
			return ; 
		}
	}
	function getSubsurList(suburList){
	 //alert("suburList");
		var inSuburbDiv= document.getElementById("inSuburbDiv");
		var inStreetDiv= document.getElementById("inStreetDiv");
		
		if(inStreetDiv.style.display !="none")
		{
			inStreetDiv.style.display = "none";
			locationDiv(inStreetDiv, "street_name");
		}
		inSuburbDiv.innerHTML ="";
		if(suburList !=null && suburList.length >0){
			divCount =0;
	   		 createInnerDiv( suburList,inSuburbDiv);
			  inSuburbDiv.style.display = "block";
			}
			else{
				inSuburbDiv.style.display = "none";
			}
			
	}	
	
	
	function getStreetList(streetList){
	
		var inStreetDiv= document.getElementById("inStreetDiv");
		
		//var inSuburbDiv= document.getElementById("inSuburbDiv");
		//if(inSuburbDiv.style.display !="none")
		//inSuburbDiv.style.display = "none";
		
		inStreetDiv.innerHTML ="";
	if(streetList !=null && streetList.length>0){
	
	divCount =0;
    for (var i in streetList)
	    {
	    var div = document.createElement("div");
	    var valueStr = streetList[i]; 
	    var count = 0;
	    //alert(valueStr);
	    div.className="innerDiv";
	   
	   	var   sarray=new   Array();   
  		sarray=valueStr.split("-*");  
	   
	   
	   if(sarray.length ==3){
	   		div.id=count+"inneriv-*"+sarray[1]+"-*"+sarray[2] ;
	   		div.innerHTML= formatStr(sarray[0])+" - "+formatStr(sarray[1]);
	    }else {
	    	div.id=count+"inneriv" ;
	   		div.innerHTML = formatStr(sarray[0]); 
	    }
		count = parseInt(i)+1;
		div.name = parseInt(count);
		div.onmouseover=function view(){
			divCount = parseInt(this.name);
		    focusToDivByCount("inStreetDiv", divCount);
		 }
	    
	    div.onclick= function (){
	  
	    document.getElementById("inStreetDiv").style.display = "none";
	    document.searchForm.street_name.focus();
	    };
	    inStreetDiv.appendChild(div);
	    }
	    var inStreetDiv =  document.getElementById("inStreetDiv"); 
	    
		inStreetDiv.style.display = "block";
		}
		else{
		inStreetDiv.style.display = "none";
		}
	  
	    
	}


/**mock getBoundingClientRect(IE) function in firefox. */
var getLocation=function(p)
{
  var x = p.offsetLeft; 
  var y = p.offsetTop; 
  while( p = p.offsetParent ) 
  { 
    if ( p.style.position == 'absolute' || p.style.position == 'relative' || ( p.style.overflow != 'visible' && p.style.overflow != '' ) ) break; 
    x += p.offsetLeft; 
    y += p.offsetTop; 
  } 
  return {x:x, y:y};
}


function submitSearchForm(){
	
	var inSuburbDiv= document.getElementById("inSuburbDiv");
	var inStreetDiv= document.getElementById("inStreetDiv");
	var insuburb =  Trim(DWRUtil.getValue("insuburb"));
	var stateId = DWRUtil.getValue("stateId"); 
	var streetNo = DWRUtil.getValue("street_num");
	var unitNo = DWRUtil.getValue("unit_num");
	
	DWRUtil.setValue("street_num",Trim(DWRUtil.getValue("street_num")));
	DWRUtil.setValue("unit_num",Trim(DWRUtil.getValue("unit_num")));
	
	if(inSuburbDiv.style.display == "block"||inStreetDiv.style.display == "block"){
		return false;
	}else if(stateId=="-1"){
		alert("Please select a State!");
		document.searchForm.stateId.focus();
		return false;
		
	}
	else if(insuburb == null ||insuburb == ""){
		alert("Please enter a Suburb!");
		document.searchForm.insuburb.focus();
		return false;
	}
	//else if(streetNo!="" && isNaN(streetNo)){
	//  alert("Street No must be a number!");
	// document.searchForm.street_num.focus();
	//	return false;
	//}
	//else if(unitNo!="" && isNaN(unitNo)){
	//  alert("Unit No must be a number!");
	// document.searchForm.unit_num.focus();
	//	return false;
	//}
	else {
		return true;
		//document.searchForm.submit();
	}
}

function submitSearchFormWidget(){
	var inSuburbDiv= document.getElementById("inSuburbDiv");
	var inStreetDiv= document.getElementById("inStreetDiv");
	
	var insuburb =  Trim(DWRUtil.getValue("insuburb"));
	var stateId = DWRUtil.getValue("stateId"); 
	var streetName = DWRUtil.getValue("street_name");
	var streetNo = DWRUtil.getValue("street_num");
	var unitNo = DWRUtil.getValue("unit_num");
		
	DWRUtil.setValue("street_num",Trim(DWRUtil.getValue("street_num")));
	DWRUtil.setValue("unit_num",Trim(DWRUtil.getValue("unit_num")));

	if(inSuburbDiv.style.display == "block"||inStreetDiv.style.display == "block"){
		return false;
	} else if(stateId=="-1"){
		alert("Please select a State!");
		document.searchForm.stateId.focus();
		return false;
	} else if(insuburb == null ||insuburb == "" || insuburb == 'Suburb'){
		alert("Please enter a Suburb!");
		document.searchForm.insuburb.focus();
		return false;
	} else if (streetName == null || streetName == '' || streetName == 'Street') {
		alert("Please select a Street!");
		document.searchForm.street_name.focus();
		return false;
	} else if (streetNo == null || streetNo == '' || streetNo == 'Street No') {
		alert("Please select a Street No!");
		document.searchForm.street_num.focus();
		return false;
	} else if (unitNo == 'Unit No') {
		DWRUtil.setValue("unit_num", "");
	} else {
		return true;
	}
}
	
function focusToDivByCount(parentDiv, count){

	var parentDiv= document.getElementById(parentDiv);
	var divArray = parentDiv.childNodes;
	if(divArray.length >= count &&count >=1)
	{
	
		for (i =0 ; i< divArray.length; i++){
		var div = divArray[i]; 
			
			if(i==(count-1)){
				div.className ="innerDivOver";
				
				/*** auto set filed value
				var cDivArray = div.childNodes;
				if(cDivArray.length==1){
					DWRUtil.setValue("street_name",(document.all)?div.innerText:div.textContent);
				}else if(cDivArray.length==3){
					DWRUtil.setValue("insuburb",(document.all)?cDivArray[0].innerText:cDivArray[0].textContent);
				}
				*/
			}else{
				div.className="innerDiv";
			}
		}//end for
	}//end if
	else if(count >divArray.length ){
		divCount = divArray.length;
	}else if(count <=0){
		divCount =0; 
		var div = divArray[0]; 
		div.className="innerDiv";
	}
}
// set filed value by current div innerText.
function setValueByCount(count, divName, setFiledName){
	var divP= document.getElementById(divName);
	var divArray = divP.childNodes;
	var selectState = document.searchForm.stateId;
	
	if(divArray.length >= count && count>=1){
	  var div = divArray[count-1]; 
  	if(setFiledName =="insuburb"){
			var spanArray = div.getElementsByTagName("div");
			var insuburbValue = spanArray[0].innerHTML;
			document.searchForm.insuburb.value = insuburbValue;
			//DWRUtil.setValue("insuburb",Trim(insuburbValue));
			
			var optionArray =  selectState.getElementsByTagName("option");
			// set state value
			for(i=0; i<optionArray.length; i++){
			 	var  oValue = parseInt(optionArray[i].value);
			 	var  sValue = parseInt(spanArray[1].name);
			 	if(oValue==sValue){
			 		optionArray[i].selected="selected"; 
			 		break;
			 	}
			}
			DWRUtil.setValue("postCode",spanArray[2].innerHTML);
			document.searchForm.insuburb.focus();
	  }
		else	{
			var strValue = div.id;
			var saValue = div.innerHTML;
			var sa=new Array();   
	  	sa = saValue.split(" - "); 
	  	var streetnameValue = sa[0];
	  	if (streetnameValue.indexOf("&")>0){
	  	  streetnameValue = streetnameValue.replace("&amp;","&");
	  	}
	  	document.searchForm.street_name.value = streetnameValue;
			//DWRUtil.setValue(setFiledName,Trim(streetnameValue));
				
			var sName = Trim(DWRUtil.getValue("insuburb"));
			if (sName == ""){
				var ss=new Array();   
		 		ss=strValue.split("-*"); 
		  			
		  	if(ss.length==3){
					DWRUtil.setValue("insuburb",formatStr(ss[1]));
					var optionArray =  selectState.getElementsByTagName("option");
						// set state value
					 	for(i=0; i<optionArray.length; i++){
					 		var  oValue = parseInt(optionArray[i].value);
					 		var  sValue = parseInt(ss[2]);
					 		if(oValue==sValue){
					 			optionArray[i].selected="selected"; 
					 			break;
					 		}
					 	}//end for
					}//end ss.length==3
				}//end sName == ""
			document.searchForm.street_name.focus();
		}//end else
	  divP.style.display="none";
    divCount=0; 
  }	//end length 
}




//close div
function closeDiv(divName, setFiledName ){
	var div= document.getElementById(divName);
	setValueByCount(divCount,divName, setFiledName  );
	div.style.display="none";
}

function clearAllField(){
	DWRUtil.setValue("insuburb","");
	DWRUtil.setValue("street_name","");
	DWRUtil.setValue("unit_num","");
	DWRUtil.setValue("street_num","");
}

function clearAllFieldWidget(){
//	DWRUtil.setValue("insuburb","");
//	DWRUtil.setValue("street_name","");
//	DWRUtil.setValue("unit_num","");
//	DWRUtil.setValue("street_num","");
}

function clearField(fieldId, defaultValue){
	var fieldValue = DWRUtil.getValue(fieldId);
	if (fieldValue == defaultValue) {
		DWRUtil.setValue(fieldId,"");
	}
}

function clearStreet_name(){
	DWRUtil.setValue("street_name","");
}


function createInnerDiv(suburList , inSuburbDiv){

	    	for (var i in suburList)
		    {
			    var div = document.createElement("div");
			    var valueStr = suburList[i] ;
			    //alert(valueStr);
			    div.className="innerDiv";
			    var count = 0;
			    count = parseInt(i)+1;
			    div.name = parseInt(count);
			    
			    var lSpan = document.createElement("div");
			    var mSpan = document.createElement("div");
			    var rSpan = document.createElement("div");
			    lSpan.className="lSpan";
			    mSpan.className="mSpan";
			    rSpan.className="rSpan";
			    
			     var   sarray=new   Array();   
  				sarray=valueStr.split("-*");  
  				//alert(sarray); 

			    
			    lSpan.innerHTML=formatStr(sarray[0]);
			    mSpan.innerHTML=sarray[1].toUpperCase();
			    rSpan.innerHTML=sarray[2] ;
			    
			    
			    //set middle span name by stateId
			    mSpan.name=sarray[3];
			    
			    div.appendChild(lSpan);
			    div.appendChild(mSpan);
			    div.appendChild(rSpan);
			    
			    div.onmouseover=function view(){
			   		divCount = parseInt(this.name);
			    	focusToDivByCount("inSuburbDiv", divCount);
			    };
				
			    div.onclick= function (){
			    	var spanArray = this.getElementsByTagName("div");
			    	//DWRUtil.setValue("insuburb",(document.all)?spanArray[0].innerText:spanArray[0].textContent);
			    	document.getElementById("inSuburbDiv").style.display = "none";
			    	document.searchForm.insuburb.focus();
			    };
			    inSuburbDiv.appendChild(div);
		   	}//end for 


}

function locationDiv( div ,  fieldId){
  var   location   = getLocation(document.getElementById(fieldId))  ; 
  div.style.left = location.x+'px';
	div.style.top = (location.y +inputHeight)+'px';
}

/*************************************************************************************
 * @ Leoneil Batulan
 * @since 01-09-2009
 * @classDescription Dynamically populating the list box with suburbs per state search * 						
 *************************************************************************************/

var sfsIndex       = new Object;
sfsIndex.populated = false;

/* Typing a name into the suburb entry field will scroll the suburb select
	field to a matching suburb name (if any).
*/
function sfsComplete () {
	if (!document.getElementById) return;
	var text   = document.getElementById('insuburb');
	var select = document.getElementById('u');
	if (!sfsIndex.populated) sfsBuildIndex();
	var suburb = text.value.match(/,*([^,]+)$/);
	if (suburb) {
		var name = suburb[1].toUpperCase().replace(/^\s*/, '').replace(/\s*$/, '');
		for (var i = sfsIndex[name.charAt(0)]; i < select.options.length; i++) {
			if (select.options[i].text.toUpperCase().indexOf(name) == 0) {
				select.selectedIndex = i;
				break;
			}
			else { select.selectedIndex = -1; }
		}
	}
}

/* On click event for the suburb select box. When clicked it populates
	the suburb entry field then deselects the clicked suburb. Focus is
	returned to the suburb entry field.
*/
function sfsInsert () {
	if (!document.getElementById) return false;
	var text    = document.getElementById('insuburb');
	var select  = document.getElementById('u');
	// An IE bug means we need another event before select.selectedIndex
	// will update from -1. Since we need to swap the focus anyway we'll
	// do it before we look at what was clicked in the select box.	
	text.focus();
	text.value = "";
	var suburb = select.options[select.selectedIndex];
	
	// short circuit on 'show all suburbs'
	/*if (select.selectedIndex == 0) {
		text.value = '';
		return true;
	}*/
 
	// see if the selected suburb is in the list already
	var textSuburbs = text.value.split(/\s*,\s*/);
	if (!textSuburbs) return false;
	var pattern = new RegExp('\s*' + suburb.text + '\s*$');
	for (var i = 0; i < textSuburbs.length; i++) {
		// if it's in the list don't do anything
		if (pattern.exec(textSuburbs[i])) return false;
	}
	// its not in the list so lets add it replacing any incomplete words
	// var newvalue = text.value.replace(/(?:(?:^|\n)\s+|\s+(?:$|\n))/g,"" + suburb.text + ', ');
	var newvalue = text.value.replace(/(^|,)([^,]*)$/,"$1 " + suburb.text );
	text.value = newvalue;
	return false;
}

/* Builds an index based on the first occurrence of suburb starting with a
	letter. This speeds up the 'for' search of the select because we can
	then skip straight to suburbs starting with that letter.
*/
function sfsBuildIndex () {
	if (!document.getElementById) return;
	var select = document.getElementById('u');
	for (var i = select.options.length; i--;) {
		sfsIndex[select.options[i].text.toUpperCase().charAt(0)] = i;
	}
	sfsIndex.populated = true;
}

