// $Id: check_zipcode.js,v 1.3.2.8 2010/09/23 21:35:00 JCockayne $

// check_zip_code_rules is defined in check_zipcode_js.tpl

function check_zip_code_field(cnt,zip){
	var c_code;
	var zip_error=false;

	if (!zip||zip.value=="")
		return true;

	c_code=cnt?cnt.options[cnt.selectedIndex].value:config_default_country;

	if(window.check_zip_code_rules&&isset(check_zip_code_rules[c_code])) {
		var rules=check_zip_code_rules[c_code];

		if (isset(rules.lens)&&!isset(rules.lens[zip.value.length]))
			zip_error=true;

		if (isset(rules.re)&&zip.value.search(rules.re)!=-1)
			zip_error=true;

		if (zip_error){
			if (rules.error&&rules.error.length>0)
				alert(rules.error);
			zip.focus();
			return false;
		}
	}

	return !zip_error;
}

function check_zip_code(){
 return check_zip_code_field(document.forms["registerform"].b_country,document.forms["registerform"].b_zipcode)&&check_zip_code_field(document.forms["registerform"].s_country,document.forms["registerform"].s_zipcode); 
}

function getCSCByZIP(zip,ifr){
 window.frames[ifr].location.href="/Services.php?method=GetCSCByZIP&ZIP="+zip;
}

function handleAsync(oIFrame,idZip,idCity,idState,idCounty){
 if(!document.getElementById(idZip).value.length)return;
 var ary=oIFrame.document.body.innerHTML.split("|");
 var e;
 if(ary.length==5) {
	 if(idCity){
    e=document.getElementById(idCity)
		e.value=ary[3];
		if(e.onchange)e.onchange();
   }
	 if(idCounty){
    e=document.getElementById(idCounty)
		e.value=ary[4];
		if(e.onchange)e.onchange();
   }
   if(idState){
    e=document.getElementById(idState);
    if(e.selectedIndex){
     var i=0;
     while(i<e.length){
      if(e.options[i].value.indexOf(ary[2])>-1){ 
       e.selectedIndex=i;
       break;
      }
      i++;
     }
    }else{
     e.value=ary[2];
	  }
    if(e.onchange)e.onchange();
	 }
 }else{
  if(ary[0].length==0&&oIFrame.location.href.indexOf('.')>0){
   document.getElementById(idZip).focus();
   alert(msg_unknown_zip_code);
  }
 }
}

function initGetCSCByZIP(idCity,idState,idCounty) {
return;
	var e;
  if(idCity){
	  e=document.getElementById(idCity)
		if(e.value==''&&e.type!='hidden')e.disabled='disabled';
	}
  if(idState){
	  e=document.getElementById(idState)
		if(e.value==''&&e.type!='hidden')e.disabled='disabled';
	}
  if(idCounty){
	  e=document.getElementById(idCounty)
		if(e.value==''&&e.type!='hidden')e.disabled='disabled';
	}
}

