//---------------------------------------------------------------------
// This file contains common validation functions
//
// function validate_name (str)
// function validate_password (str)
// function validate_alpha_num (str)
// function validate_filename (str)
// function validate_subject (str)
// function validate_description (str)
// function validate_address (str)
// function validate_zip (str)
// function validate_money (str)
// function validate_number (str)
// function validate_integer (str)
// function validate_phone (str)
// function validate_ssn (str)
// function validate_acctnum (str)
// function validate_tax_id (str)
// function validate_vendor_id (str)
// function validate_date (str)
// function validate_email (str)
// function validate_duration (str)

// function LTrim(str)
// function RTrim(str)
// function Trim(str)

// function enable_wait()
// function disable_wait()
//---------------------------------------------------------------------

var typo_object;  // the most recent bad user input value
//---------------------------------------------------------------------
//---------------------------------------------------------------------
function focus_after_alert ()
{
	try{
		typo_object.focus();
		typo_object.select();
	}catch(e){ }//Don't care if I can't set focus to the item.
} // focus_after_alert ()

//---------------------------------------------------------------------
// Make sure time format for duration is MM:SS
//---------------------------------------------------------------------
function validate_duration(obj) {
	var bad_chars 	= new RegExp(/[^0-9:]/g); // bad means any bytes other than these ...
	if (bad_chars.test(obj.value) == true) {
		alert('Duration is stored in format: "MM:SS" ');
		return false;
	}
	return true;
}

//---------------------------------------------------------------------
// Make sure there are only a-z 0-9 and _ for keywords
//---------------------------------------------------------------------
function validate_keyword(obj) {
	var bad_chars 	= new RegExp(/[^A-Za-z0-9_]/g); // bad means any bytes other than these ...
	var white_space	= new RegExp(/\s/g);
	obj.value = obj.value.replace(white_space,'_');
	obj.value = obj.value.replace(bad_chars,'');
}

//---------------------------------------------------------------------
// Enforce correct input values for NAME
//---------------------------------------------------------------------
function validate_name (str,option)
{ // establish regex for valid name bytes
	var bad_chars = new RegExp(/[^A-Za-z- \']/g); // bad means any bytes other than these ...

	typo_object = str;
	if (bad_chars.test(str.value) == true) {
		if(!option) {
			setTimeout('focus_after_alert()', 5);
			alert('Enter a valid name string');
		}
		return false;
	}
	return true;
} // validate_name ()

//---------------------------------------------------------------------
// Enforce correct input values for USERNAME
//---------------------------------------------------------------------
function validate_username (str,option)
{ // establish regex for valid name bytes
	var bad_chars = new RegExp(/[^A-Za-z0-9\-]/g); // bad means any bytes other than these ...

	typo_object = str;
	if (bad_chars.test(str.value) == true) {
		if(!option){
			setTimeout('focus_after_alert()', 5);
			alert('Enter a valid name string');
		}
		return false;
	}
	return true;
} // validate_username ()

//---------------------------------------------------------------------
// Enforce correct input values for an alpha numeric including space string
//---------------------------------------------------------------------
function validate_company_name (str,option)
{ // establish regex for valid name bytes
	var bad_chars = new RegExp(/[^A-Za-z0-9&, \.\-\(\)\'\#\/\\]/g); // bad means any bytes other than these ...

	typo_object = str;
	if (bad_chars.test(str.value) == true) {
		if(!option) {
			setTimeout('focus_after_alert()', 5);
			alert('Enter a valid alpha numeric string');
		}
		return false;
	}
	return true;
} // validate_company_name()
//---------------------------------------------------------------------
// Enforce correct input values for PASSWORD
//---------------------------------------------------------------------
function validate_password (str)
{ // establish regex for valid password bytes
	var bad_chars = new RegExp(/[^A-Za-z!@#$%^&*()_-{}:;]/g); // bad means any bytes other than these ...

	typo_object = str;
	if (str.value.length < 6) {
		setTimeout('focus_after_alert()', 5);
		alert('The password must contain at least six characters.');
		return false;
	}
	if (bad_chars.test(str.value) == true) {
		setTimeout('focus_after_alert()', 5);
		alert('Invalid password character(s)');
		return false;
	}
	return true;
} // validate_password ()

//---------------------------------------------------------------------
// Enforce correct input values for an alpha numeric string
//---------------------------------------------------------------------
function validate_alpha_num (str)
{ // establish regex for valid name bytes
	var bad_chars = new RegExp(/[^A-Za-z0-9]/g); // bad means any bytes other than these ...

	typo_object = str;
	if (bad_chars.test(str.value) == true) {
		setTimeout('focus_after_alert()', 5);
		alert('Enter a valid alpha numeric string');
		return false;
	}
	return true;
} // validate_alpha_num ()

//---------------------------------------------------------------------
// Enforce correct input values for a filename string
//---------------------------------------------------------------------
function validate_filename (str)
{ // establish regex for valid name bytes
	var bad_chars = new RegExp(/[^A-Za-z0-9 _]/g); // bad means any bytes other than these ...

	typo_object = str;
	if (bad_chars.test(str.value) == true) {
		setTimeout('focus_after_alert()', 5);
		alert('Enter a valid filename');
		return false;
	}
	return true;
} // validate_filename ()

//---------------------------------------------------------------------
// Enforce correct input values for a subject string
//---------------------------------------------------------------------
function validate_subject (str)
{ // establish regex for valid name bytes
	var bad_chars = new RegExp(/[^A-Za-z0-9 _:\-;\.,]/g); // bad means any bytes other than these ...

	typo_object = str;
	if (bad_chars.test(str.value) == true) {
		setTimeout('focus_after_alert()', 5);
		alert('Enter a valid subject');
		return false;
	}
	return true;
} // validate_subject ()

//---------------------------------------------------------------------
// Enforce correct input values for a description string
//---------------------------------------------------------------------
function validate_description (str)
{ // establish regex for valid name bytes
	var bad_chars = new RegExp(/[^A-Za-z0-9 _:\-;\.,]/g); // bad means any bytes other than these ...

	typo_object = str;
	if (bad_chars.test(str.value) == true) {
		setTimeout('focus_after_alert()', 5);
		alert('Enter a valid description');
		return false;
	}
	return true;
} // validate_description ()

//---------------------------------------------------------------------
// Enforce correct input values for ADDRESS
//---------------------------------------------------------------------
function validate_address (str,option)
{ // establish regex for valid address bytes
	var bad_chars = new RegExp(/[^A-Za-z0-9, \.\-\(\)\'\#\/\\]/g); // bad means any bytes other than these ...

	typo_object = str;
	if (bad_chars.test(str.value) == true) {
		if(!option) {
			setTimeout('focus_after_alert()', 5);
			alert('Enter a valid address string');
		}
		return false;
	}
	return true;
} // validate_address ()

//---------------------------------------------------------------------
// Enforce correct input values for ADDRESS
//---------------------------------------------------------------------
function validate_zip (str,option)
{ // establish regex for valid zip bytes
	var bad_chars = new RegExp(/[^ 0-9A-Z-\']/ig); // bad means any bytes other than these ...

	typo_object = str;
	if (bad_chars.test(str.value) == true) {
		if(!option) {
			setTimeout('focus_after_alert()', 5);
			alert('Enter a valid zip code');
		}
		return false;
	}
	return true;
} // validate_zip ()

//---------------------------------------------------------------------
// Enforce correct input values for MONEY
//---------------------------------------------------------------------
function validate_number (str)
{ // change this to just regex out digits this will fix a lot
    var bad_chars = new RegExp(/[^-0-9,.]/g); // bad means any bytes other than these ...
    var dollar    = new RegExp(/^\\\$/);

    typo_object = str;
    str.value = str.value.replace(dollar,'');
    if (bad_chars.test(str.value) == true) {
        setTimeout('focus_after_alert()', 5);
        alert('Enter a valid number');
        return false;
    }
    return true;
} // validiate_number ()

//---------------------------------------------------------------------
// Enforce correct input values for MONEY
//---------------------------------------------------------------------
function validate_money (str)
{ // change this to just regex out digits this will fix a lot
	var bad_chars = new RegExp(/[^0-9.]/g); // bad means any bytes other than these ...
	var neg       = new RegExp(/^-/);
	var dollar    = new RegExp(/^\\\$/);

	typo_object = str;
	str.value = str.value.replace(dollar,'');
	if (neg.test(str.value) == true) {
		setTimeout('focus_after_alert()', 5);
		alert('Enter a positive number');
		return false;
	}
	if (bad_chars.test(str.value) == true) {
		setTimeout('focus_after_alert()', 5);
		alert('Enter a valid number');
		return false;
	}
	return true;
} // validiate_money ()

//---------------------------------------------------------------------
// Enforce correct input values for MONEY
//---------------------------------------------------------------------
function validate_percent (str)
{ // change this to just regex out digits this will fix a lot
	var bad_chars = new RegExp(/[^0-9.]/g); // bad means any bytes other than these ...
	var neg       = new RegExp(/^-/);
	var dollar    = new RegExp(/^\\\$/);

	typo_object = str;
	str.value = str.value.replace(dollar,'');
	if (neg.test(str.value) == true) {
		setTimeout('focus_after_alert()', 5);
		alert('Enter a positive number');
		return false;
	}
	if (bad_chars.test(str.value) == true) {
		setTimeout('focus_after_alert()', 5);
		alert('Enter a valid number');
		return false;
	}
	if (str.value>100){
		setTimeout('focus_after_alert()', 5);
		alert('Number cannot be more then 100% ');
		str.value='';
		return false;
	}
	return true;
} // validiate_money ()

//---------------------------------------------------------------------
// Enforce correct input values for INTEGER
//---------------------------------------------------------------------
function validate_integer (str)
{
	var bad_chars = new RegExp(/[^0-9]/g); // bad means any bytes other than these ...
	var neg       = new RegExp(/^-/);

	typo_object = str;
	if (neg.test(str.value) == true) {
		setTimeout('focus_after_alert()', 5);
		alert('Enter a positive number');
		return false;
	}
	if (bad_chars.test(str.value) == true) {
		setTimeout('focus_after_alert()', 5);
		alert('Enter a valid number');
		return false;
	}
	return true;
} // validate_integer ()

//---------------------------------------------------------------------
// Enforce correct input values for PHONE
//---------------------------------------------------------------------
function validate_phone (str,option)
{
	var bad_chars  = new RegExp(/[^()0-9 xX+-]/g); // bad means any bytes other than these ...

	typo_object = str;
	if (bad_chars.test(str.value) == true) {
		if(!option) {
			setTimeout('focus_after_alert()', 5);
			alert('\n----- Enter a phone number. -----\nNOTE: Use x for extension. \nPhone number example: (954) 555-1212 x 309\nFax number example: (954) 555-1234\nInternational numbers are also accepted.');
		}
		return false;
	}
	return true;
} // validate_phone ()

//---------------------------------------------------------------------
// Enforce correct input values for Social Security Number
//---------------------------------------------------------------------
function validate_ssn (str)
{
	var bad_chars = new RegExp(/[^0-9-]/g); // bad means any bytes other than these ...

	typo_object = str;
	if (bad_chars.test(str.value) == true) {
		setTimeout('focus_after_alert()', 5);
		alert('Enter a valid Social Security Number');
		return false;
	}
	return true;
} // validate_ssn ()

//---------------------------------------------------------------------
// Enforce correct input values for Account number
//---------------------------------------------------------------------
function validate_acctnum (str)
{
	var bad_chars = new RegExp(/[^A-Za-z0-9, \-\(\)\'\#\/\\]/g); // bad means any bytes other than these ...

	typo_object = str;
	if (bad_chars.test(str.value) == true) {
		setTimeout('focus_after_alert()', 5);
		alert('Enter a valid Account Number');
		return false;
	}
	return true;
} // validate_acctnum ()

//---------------------------------------------------------------------
// Enforce correct input values for Tax ID
//---------------------------------------------------------------------
function validate_tax_id (str)
{
	var bad_chars = new RegExp(/[^0-9-]/g); // bad means any bytes other than these ...

	typo_object = str;
	if (bad_chars.test(str.value) == true) {
		setTimeout('focus_after_alert()', 5);
		alert('Enter a valid Tax ID Number');
		return false;
	}
	return true;
} // validate_tax_id ()

//---------------------------------------------------------------------
// Enforce correct input values for vendor ID
//---------------------------------------------------------------------
function validate_vendor_id (str)
{
	var bad_chars = new RegExp(/[^0-9A-Za-z-]/g); // bad means any bytes other than these ...

	typo_object = str;
	if (bad_chars.test(str.value) == true) {
		setTimeout('focus_after_alert()', 5);
		alert('Enter a valid Vendor ID Number');
		return false;
	}
	return true;
} // validate_vendor_id ()

//---------------------------------------------------------------------
// Enforce correct input values for DATE
//---------------------------------------------------------------------
function validate_date (str)
{
//test_stuff();
	if (!str.value)	 { return true; }
	var slash  = new RegExp(/[\\\/]/g);
	var good_date = new RegExp(/[^0-9\\-]/);

	typo_object = str;
	str.value = str.value.replace(slash,'-');
	if (good_date.test(str.value) == true) {
		setTimeout('focus_after_alert()', 5);
		alert('Not a valid date. Enter date as DD/MM/YYYY.');
		return false;
	}
	else {
		var date_array = str.value.split('-');
		if (date_array.length == 3 ) {
			if (date_array[2].length > 4 || date_array[0].length > 4) {
					setTimeout('focus_after_alert()', 5);
					alert('Invalid Year');
					return false;
			}
			if (date_array[2].length == 4) {
				if (date_array[0] < 1 || date_array[0] > 12) {
					setTimeout('focus_after_alert()', 5);
					alert('Invalid Month. Enter a number between 1 and 12.');
					return false;
				}
				if (date_array[1] < 1 || date_array[1] > 31 ) {
					setTimeout('focus_after_alert()', 5);
					alert ('Invalid Day Enter. a number between 1 and 31.');
					return false;
				}
			}
			else if (date_array[0].length == 4) {
				if (date_array[1] < 1 || date_array[1] > 12) {
					setTimeout('focus_after_alert()', 5);
					alert('Invalid Month. Enter a number between 1 and 12.');
					return false;
				}
				if (date_array[2] < 1 || date_array[2] > 31 ) {
					setTimeout('focus_after_alert()', 5);
					alert ('Invalid Day. Enter a number between 1 and 31.');
					return false;
				}
			}
		}
		else {
			setTimeout('focus_after_alert()', 5);
			alert('Not a valid date. Enter date as DD/MM/YYYY.');
			return false;
		}
	}
	return true;
} // validate_date ()

//---------------------------------------------------------------------
// Enforce correct input values for an email string
//---------------------------------------------------------------------
function validate_email (str)
{
    var email     = null;
    var objFocus  = true;  
    var atfilter = /@/;
    var dotfilter = /\./;
    var goodfilter = /[&@\w.-]/;
    var badfilter = /[^&@\w.-]/;
    var splitemail;
    var domain;
        
    if( typeof(str) == "string" )
    {  
       email      = str;
       objFocus   = false;  
    }
    else
    {  
       email      = str.value; 
    }
        
    typo_object = str;
    
    if (email)
    {
    	if (atfilter.test(email))
    	{
    		splitemail = email.split("@");
    		if (splitemail.length > 2 || splitemail[1] == "" || splitemail[0] == "")
    		{
    			if(objFocus){
    			    setTimeout('focus_after_alert()', 5);
    			}
    			alert ("Invalid Email Structure");
    			return (false);
    		}
    	}
    	else
    	{
			if(objFocus){
			    setTimeout('focus_after_alert()', 5);
			}
    		alert ("Email Needs an 'At-Sign' symbol.\nExample: john@yahoo.com");
    		return (false);
    	}
    
    	domain = splitemail[1];
    	if (!dotfilter.test(domain))
    	{
			if(objFocus){
			    setTimeout('focus_after_alert()', 5);
			}
    		alert ("An email address needs a 'dot' symbol.\nExample: yahoo.com");
    		return (false);
    	}
    
    	if (!goodfilter.test(email) || badfilter.test(email))
    	{
			if(objFocus){
			    setTimeout('focus_after_alert()', 5);
			}
    		alert ("Invalid email character(s)");
    		return (false);
    	}
    }
    return (true);
} // validate_email ()

//---------------------------------------------------------------------
// Enforce correct input values for an email string
//---------------------------------------------------------------------
function validate_web_domain (str)
{
	var domain = str.value;

	var dotfilter = /\./;
	var goodfilter = /[&\w.-]/;
	var badfilter = /[^&\w.-]/;

	typo_object = str;
	if (domain)
	{
		if (!dotfilter.test(domain))
		{
			setTimeout('focus_after_alert()', 5);
			alert ("A web domain needs a 'dot' symbol.\nExample: yahoo.com");
			return (false);
		}

		if (!goodfilter.test(domain) || badfilter.test(domain))
		{
			setTimeout('focus_after_alert()', 5);
			alert ("Invalid domain character(s)");
			return (false);
		}
	}
	return (true);
} // validate_web_domain ()

//---------------------------------------------------------------------
// Left-trim white space
//---------------------------------------------------------------------
function LTrim(str)
{
   var whitespace = new String(" \t\n\r");
   var s = new String(str);
   if (whitespace.indexOf(s.charAt(0)) != -1) {
      var j=0, i = s.length;
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;
      s = s.substring(j, i);
   }
   return s;
}

//---------------------------------------------------------------------
// Right-trim white space
//---------------------------------------------------------------------
function RTrim(str)
{
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      var i = s.length - 1;
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;
      s = s.substring(0, i+1);
   }

   return s;
}

//---------------------------------------------------------------------
// Left-trim white space
//---------------------------------------------------------------------
function Trim(str)
{
   return RTrim(LTrim(str));
}


//---------------------------------------------------------------------
// ENABLE - DISABLE THE HOURGLASS CURSOR
//---------------------------------------------------------------------
function enable_wait(){
	window.document.body.style.cursor = 'wait';
}
function disable_wait(){
	window.document.body.style.cursor = 'default';
}

