// JavaScript Document

window.onload = function(){


setinput('quick');
setinput('company_name');
setinput('function');
setinput('city');
//setinput('category');
setinput('product');
//setinput('region');
setinput('brand');
//setinput('certification');
createLinks();
del();
//alert(document.getElementById('rightmenu').style.height);


init_regform();
}
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };

String.prototype.get_num = function() { return this.replace(/[^0-9]+/g, ''); };

function init_regform(){
if($('regform')) {

function mylive(zip,regionz,city){
  if ($(zip)){
    $(zip).setAttribute('title','');
    $(zip).onkeyup = function (){
      var searchval = String($(zip).value);
      searchval = searchval.trim();
      if  ((searchval.length==4) && ($(zip).getAttribute('title')!=searchval) ){
        var pars='zip=' +escape($(zip).value);
        var myAjax = new Ajax.Request( '/live_place.php',
         { method: 'post', parameters: pars, onComplete: function(originalRequest){
              resdoc = originalRequest.responseText;
               var loc_array = resdoc.split('|');
               $(city).value=loc_array[1];
               var selIndex = $(regionz);
               for (var i=0; i<=selIndex.options.length; i++){
                 if (selIndex.options[i].value==loc_array[0]){
                  
                    $(regionz).selectedIndex=i;
                 }
               }
         } 
         });
       $(zip).setAttribute('title',$(zip).value);
      }
  } 
}
}

mylive('zipcode','region1','city1');
mylive('invoice_zip_code','invoice_region','invoice_place');
mylive('post_zip_code','post_region','post_place');
mylive('off_zip_code1','off_region1','off_place1');
mylive('off_zip_code2','off_region2','off_place2');

  if ($('cp_szc')){
    $('cp_szc').onclick=function(){  
          $('invoice_zip_code').value=$F('zip_code');
          $('invoice_place').value=$F('city1');
          $('invoice_name').value=$F('name');
          $('invoice_street').value=$F('street');
          $('invoice_region').selectedIndex=$('region1').selectedIndex;
        }
        }
  if ($('cp_pc')){
    $('cp_pc').onclick=function(){  
          $('post_zip_code').value=$F('zip_code');
          $('post_place').value=$F('city1');
          $('post_street').value=$F('street');
          $('post_region').selectedIndex=$('region1').selectedIndex;
        }
  
  }







    var valid = new Validation('regform', {useTitles:true, immediate:true});
    /*
		$('regform').onsubmit = function() {
			if(valid.validate()) {
         $('regform').submit();
				return false;
			}
			return false;
		}
		*/
		
	}
}












function del(){

  if ($('delete')){
    $('delete').onclick=function(){


    $('company_name').value = $('company_name').getAttribute('title');
    $('function').value = $('function').getAttribute('title');
    $('city').value = $('city').getAttribute('title');
    $('product').value = $('product').getAttribute('title');
    $('brand').value = $('brand').getAttribute('title');
    $('category').selectedIndex = 0;
    $('region').selectedIndex = 0;
    $('certification').selectedIndex = 0;

      //Field.clear('company_name');
/*
      $('function');
      $('city');
      //setinput('category');
      $('product');
      //setinput('region');
      $('brand');
      //setinput('certification');
      */  
      
      return false;
  }
  }
  
    if ($('qd')){
      $('qd').onclick=function(){
        $('quick').value = $('quick').getAttribute('title');
        return false;
      }
    }
  
  
}



function on_off(id){
  Effect.toggle(id, 'appear',{duration: 0.45})
}


function createLinks()
    {
  	
    linksArray = document.getElementsByTagName('a');
    for(var i = 0; i < linksArray.length; i++)
        {
        if(linksArray[i].getAttribute('rel') == 'external')
             {
             linksArray[i].onclick = new Function("window.open('" + linksArray[i].href + "'); return false;");
             linksArray[i].id = "autoId" + i;
             }
        }
    }



function setinput(id){
      if ($(id)){
      
      	if(($F(id) == '') || ($F(id) == $(id).getAttribute('title')) ) {
		    	$(id).value = $(id).getAttribute('title');
		    } else {
          $(id).style.background='#faba00';
          $(id).style.border='2px solid #faba00';
        }
        $(id).onclick = function(e){
          var title = this.getAttribute('title');
          if ($F(this)==title){
            Field.clear(this);
          }
        }
        $(id).onblur = function(e){
          var title = this.getAttribute('title');
          var val = $F(this);
          if (val.trim()==''){
            this.value=title;
          }
        }                    
      }
}



function cpop(tourl,xsize,ysize)
{
		if ((typeof popup=='undefined') || popup.closed)
		{
		popup=window.open(tourl, 'popup', "toolbar=no,location=no,resizable=no,status=no,scrollbars=yes,menubar=no,width=" + xsize + ",height=" + ysize + ",left=" + ((screen.width/2)- xsize/2) + ",top=" + ((screen.height/2)- ysize/2));
		} else {
		popup.location.href=tourl;
		popup.focus();
		}
    return false;
}

function zpop(tourl)
{
		return !window.open(tourl, null, "toolbar=yes,location=yes,resizable=yes,status=yes,scrollbars=yes,menubar=yes,width=" + screen.width + ",height=" + screen.height + ",left=0,top=0");
}


function mappop(tourl,xsize,ysize)
{
		if ((typeof popup=='undefined') || popup.closed)
		{
		popup=window.open(tourl, 'map', "toolbar=no,location=no,resizable=no,status=no,scrollbars=no,menubar=no,width=" + xsize + ",height=" + ysize + ",left=" + ((screen.width/2)- xsize/2) + ",top=" + ((screen.height/2)- ysize/2));
		} else {
		popup.location.href=tourl;
		popup.focus();
		}
    return false;
}

