// function lib used by townsearch client

function OpenWindow ( url, width, height, options, name )
{      
    if (!width) width = 800;
    if (!height) height = 536;
    if (!options) options = "scrollbars=no,menubar=no,toolbar=no,location=no,status=no,resizable=yes";   
    // identifier; not title!
    if(!name) name = "preview";
    // alert( url + "-"+ width + "-" + height + "-" +options+ "-"+ name);
    var win = window.open( url, name, "width="+width+",height="+height+","+options );
    win.focus();
}


// formname             - name of form that information is read from
// cityFieldName        - name of input field that contains city 
// postCode1FieldName   - name of input field that contains postCode1
// postCode1FieldName   - name of input field that contains postCode2
// countryCodeFieldName - name of input field that contains country
// fixedCountry         - signal that country will not be editable
// doValidate           - signal to do a validate on the initial inpput
// templateSet          - template postfix eg '', 'standalone'
//
// when using townsearch in standone mode, the only fill in templateSet (standalone)
//
function townSearchUrl(formName, cityFieldName, postCode1FieldName, postCode2FieldName, countryCodeFieldName, fixedCountry, doValidate,templateSet){    
    var url = "/pl/townsearch?";
    if(formName){
       var form = eval("document." + formName);
      if(!form){
         alert("form "+ formName +  "not found!");
      }
      var cityField        = locateField(form, cityFieldName);
      var postCode1Field   = locateField(form, postCode1FieldName);
      var postCode2Field   = locateField(form, postCode2FieldName);
      var postCodeValue = postCode1Field.value;
      var countryCodeField = locateField(form, countryCodeFieldName);
      if(postCode2Field){
        postCodeValue = postCodeValue + postCode2Field.value;
      }
      url +="postCode=" + postCodeValue +
	    "&countryCode="+countryCodeField.value +
            "&city="+ cityField.value+
            "&callerFormName="+ form.name +
	    "&callerFormFieldCity=" + cityFieldName +
	    "&callerFormFieldPostCode1="+ postCode1FieldName+
	    "&callerFormFieldPostCode2="+ postCode2FieldName+
	    "&callerFormFieldCountryCode="+ countryCodeFieldName+
	    "&fixedCountry="+fixedCountry;
    
     if(doValidate && (doValidate == 'yes')){
       url += "&action.check.x=1";
     }
   }
   if(templateSet){
       url += "&templateSet="+ templateSet;
   }
    return(url);
   
}

// can be done w eval?
function locateField(form, name){
   for (var i = 0; i< form.elements.length; i++) {
	if(form.elements[i].name == name){
	  return(form.elements[i]);
        }
   }
}

