function Right(str, n)
{
      if (n <= 0)
          return "";
      else if (n > String(str).length)
          return str;
      else
   {
          var iLen = String(str).length;
          return String(str).substring(iLen, iLen - n);
      }
}



var checkMail = function(m)
{

	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(m)) return true;
	else return false;
}


var UpdateForm = function()
{

	$('stateChnLoged').innerHTML = "";	
	Element.hide('stateChnLoged');	
	
}


var submitLogin = function()
{
	flagEmail = 1;
	
	var country = $('country').value.stripTags();
	
	if(country == "usa" || country == "ust") {
		var email = $('email').value.stripTags(); 
		$('email').disabled = true; 	
		
		if(email == "") {
			flagEmail = 0;
		}
	
	}
	



	var vendor_code = $('vendor_code').value.stripTags();

	//alert(vendor_code);

	if(vendor_code == "" )
	{
		vendor_code="0000";
		
	}
	


	if(vendor_code == "" || country == ""){ //|| !flagEmail ) {

		Element.show('stateChnLoged');	
		$('stateChnLoged').innerHTML = "<p>&nbsp; &nbsp;  *** All details are required </p>";
		$('email').disabled = false;
		return false;
	
	} else {
		
		if (vendor_code == "0000")
		{}
		else
		{
		if((country == "usa" || country == "ust")) {
			
				if(!checkMail($('email').value)) {
		
					Element.show('stateChnLoged');					
					$('stateChnLoged').innerHTML = " *** A valid e-mail address is required. ";
					$('email').disabled = false;	
				
					return false;
				}
				
		
		}
		}
	
	}

	

	$('country').disabled = true;
	$('vendor_code').disabled = true;
	$('btnLogin').disabled = true;
	

	var url = "login.php";
	var now = new Date();
	var nowStr=Right("20"+now.getYear(),4)+"-"+Right("0"+now.getMonth(),2)+"-"+Right("0"+now.getDate(),2);
	
	
	var pars = "vendor_code="+vendor_code+"&country="+country+"&email="+email+"&date="+nowStr;


	
	var myAjax = new Ajax.Request( url, 
		{
			method: 'post',
			parameters: pars,
			onComplete: callBack
		} 		
	);
}


var callBack = function(myObject)				
{


	//alert(myObject.responseText);
	_myObject  = eval('(' +  myObject.responseText + ')');	

	//location.href= "main.php";

	if(_myObject.login_status == "failed")
	{

		if(_myObject.country == "usa" || _myObject.country == "ust") {// || _myObject.country == "can"
			
			alert(" Your vendor number is available from previous cheque stub or remittance advice.");
			}
		

		Element.show('stateChnLoged');	
		$('stateChnLoged').innerHTML = " No payment record found  or please double check your vendor number ";
		
		
	
		$('country').disabled = false;
		$('vendor_code').disabled = false;
		$('btnLogin').disabled = false;
		$('email').disabled = false; 
		

		$('vendor_code').focus();
		
		
		
		
		
	}
	else
	{		
		//if(_myObject.email != -1)
		//{
			$('stateChn').innerHTML = "";			
			
			//if(_myObject.ref != '') {
				location.href= "main.php";
			//} else {
				//location.href= _URL + "home.php";	
			//}
		//}							
	}

}



var showEmail =function(selCountry)
{
	if(selCountry == "usa" || selCountry == "ust" ) {
		Element.show("tremail");
		//Element.show("tremail2");
		$('email').value = "";
		$('email').disabled = false; 
		
	} else {
		Element.hide("tremail")
		//Element.hide("tremail2")
	
	}

	if(selCountry == "can" ) { //selCountry == "usa" || selCountry == "ust" || 
		alert("Your vendor no is available from previous cheque stub or remittance advice");
	
	}
	
	$('vendor_code').focus();
	

}


var myGlobalHandlers = {
	onCreate: function() { 
		$('stateChn').innerHTML = " Now loading ..."; 
		Element.show('stateChn');
		Element.hide('stateChnLoged'); 
	}, 
	onComplete: function() { 
		if(Ajax.activeRequestCount == 0){  
			Element.hide('stateChn'); 				
		}
	}
};


var winLoad = function()
{
	Reminder_URL();
	
	if($('country').value == "usa" || $('country').value == "ust") {
		//Element.show("tremail");
		//$('tremail').setStyle({visibility:'hidden'});
		
		//Element.show("tremail2");		
		
	}

	//$('vendor_code').focus();		
}


Ajax.Responders.register(myGlobalHandlers);		
Event.observe (window, 'load', winLoad, false);

