/*-------------------------------------------------------------------------
*	validation_general.js
*   
*	javascript generic data validation function library.
*
*	Copyright 2000 bluefusion, inc. 312-377-2777 
------------------------------------------------------------------------*/
function Validate_First_Name(first_name)
{
  if (isBlank(first_name)) {
    alert("Please provide First Name.");
    first_name.focus();
    return (false);
  }
  return (true);
}

function Validate_Last_Name(last_name)
{
  if (isBlank(last_name)) {
    alert("Please provide Last Name.");
    last_name.focus();
    return (false);
  }
  return (true);
}

function Validate_Job_Title(job_title)
{
   if (isBlank(job_title)) {
    alert("Please provide Job Title.");
    job_title.focus();
    return (false);
  }
  return (true);
}

function Validate_Co_Name(company_name)
{
   if (isBlank(company_name)) {
    alert("Please provide Company Name.");
    company_name.focus();
    return (false);
  }
  return (true);
}

function Validate_Address(co_address)
{
   if (isBlank(co_address)) {
   		alert("Please provide Address.");
    	co_address.focus();
    	return (false);
  }
  return (true);
}

function Validate_City(co_city)
{
   if (isBlank(co_city))  {
    alert("Please provide City.");
    co_city.focus();
    return (false);
  }
  return (true);
}

function Validate_State(state_id)
{
  if (state_id.selectedIndex == 0)  {
     alert("Please select a State from the list shown.");
     state_id.focus();
     return (false);
	    }  
   return (true);
}

function isUSZip (InString)  {
	if (InString.length==0) 
		return (false);
	if ((InString.length!=5) && (InString.length!=10))
		return (false);
	RefString="1234567890-";
	for (Count=0; Count < InString.length; Count++)  {
		TempChar= InString.substring (Count, Count+1);
		if (RefString.indexOf (TempChar, 0)==-1) 
			return (false);
	}
	return (true);
}

function Validate_Number(area_code, prefix, suffix)
{
	if ((testForLength(area_code.value,3,3,3)   == false)   ||  (isNumberString (area_code.value)   == false)){
       	alert("Please provide a valid Area Code.")
    	area_code.focus();
		return false; } 
	if ((testForLength(prefix.value,3,3,3)   == false)   ||  (isNumberString (prefix.value)   == false)){
        alert("Please provide a valid Prefix.")
    	prefix.focus();
		return false; } 
  	if ((testForLength(suffix.value,4,4,4)   == false)   ||  (isNumberString (suffix.value)   == false)){
        alert("Please provide the last four digits of this Number.")
    	suffix.focus();   
		return false; } 
  	return (true); 
}

function Validate_Email_Address(email_address)
{
	Ctrl = email_address;
	var DomPos=Ctrl.value.lastIndexOf(".");
	var AddrLen=Ctrl.value.length - 1;
	var DomLen=AddrLen-DomPos;
    if (Ctrl.value == "" || (Ctrl.value.indexOf ('@', 0) == -1) || (Ctrl.value.length < 5)){
		return (false); }
    if (DomLen <2 || DomLen >3){
        return (false);
        }
	else
		return (true);
}
function Valid_Other (Ctrl,Ctrl_Other) {
	if (Ctrl.value == "Other" && Ctrl_Other.value ==""){
	alert("If you selected 'Other' you must specify");
	Ctrl_Other.focus();
	return false;
	}
}

function isBlank (Ctrl) {
	var InString = " ";
	if (Ctrl.value == null) return (!false);
	InString = Ctrl.value;
	InString = spaceTrim(InString);
		if (InString.length!=0) {
			return (!true);     }
		else {
			return (!false);
			 }
}
//Returns true if value is an integer defined as
//   having an optional leading + or -.
//   otherwise containing only the characters 0-9.
function _CF_checkinteger(object_value)
    {
    if (object_value.length == 0)
        return false;
 	var decimal_format = ".";
	var check_char;
	check_char = object_value.indexOf(decimal_format)
    if (check_char < 1)
		return _CF_checknumber(object_value);
    else
		return false;
}
	
//Returns true if value is a number defined as
//   having an optional leading + or -.
//   having at most 1 decimal point.
//   otherwise containing only the characters 0-9.
function _CF_checknumber(object_value)
    {
    if (object_value.length == 0)
        return true;
	var start_format = " .+-0123456789";
	var number_format = " .0123456789";
	var check_char;
	var decimal = false;
	var trailing_blank = false;
	var digits = false;
	check_char = start_format.indexOf(object_value.charAt(0))
	if (check_char == 1)
	    decimal = true;
		else if (check_char < 1)
			return false;  
	//Remaining characters can be only . or a digit, but only one decimal.
	for (var i = 1; i < object_value.length; i++) 	{
		check_char = number_format.indexOf(object_value.charAt(i))
		if (check_char < 0)
			return false;
		else if (check_char == 1) {
			if (decimal)		// Second decimal.
				return false;
			else
				decimal = true; }
		else if (check_char == 0) {
			if (decimal || digits)	
				trailing_blank = true; }
	        else if (trailing_blank)
			return false;
		else
			digits = true; }	
    return true
}

function ifExists (CheckVar) {
	CheckVar = "" + CheckVar; 
	if (CheckVar == "<undefined>")
		return (false);
	else
		return (true);
}

function isNumberString (InString)  {
	if(InString.length==0) 
		return (false);
	RefString="1234567890";
	for (Count=0; Count < InString.length; Count++)  {
		TempChar= InString.substring (Count, Count+1);
		if (RefString.indexOf (TempChar, 0)==-1)  
			return (false);
	}
	return (true);
}

function spaceTrim(InString) {
	var LoopCtrl=true;
	while (LoopCtrl) {
		if (InString.indexOf("  ") != -1) {
			Temp = InString.substring(0, InString.indexOf("  "))
			InString = Temp + InString.substring(InString.indexOf("  ")+1, 
				InString.length)
		} else
			LoopCtrl = false;
	}
	if (InString.substring(0, 1) == " ")
		InString = InString.substring(1, InString.length)
	if (InString.substring (InString.length-1) == " ")
		InString = InString.substring(0, InString.length-1)
	return (InString)
}

function testForLength (InString, Abs, LTE, GTE) {
	if (Abs != -1) {if(InString.length==Abs) return(true);} else
	if (LTE != -1) {if(InString.length<=LTE) return(true);} else
	if (GTE != -1) {if(InString.length>=GTE) return(true);} 
		return (false);
}

function Validate_Email(existingForm) {
    if (Validate_Email_Address(existingForm.Existing_Email_Addr) == false) {
        alert("Please supply a valid Email Address");
		existingForm.Existing_Email_Addr.focus();
		return false;
    }else{
		return true;
    }
}

function Submit_Form(){
if (Validate_Form(document.theForm)) {
   		document.theForm.submit();  }
        return;
}

/*  Validate the form. */
function Validate_Form(theForm) {

    if (isBlank(theForm.First_Name)) {
		alert("Please enter your first name");
		theForm.First_Name.focus();
		return false;
		}
	if (isBlank(theForm.Last_Name)) {
		alert("Please enter your last name");
		theForm.Last_Name.focus()
		return false;
		}
	if (isBlank(theForm.Title)) {
		alert("Please enter a Title");
		theForm.Title.focus();
		return false;
		}
    if (Validate_Email_Address(theForm.Email_Addr) == false) {
        alert("Please supply a valid Email Address");
		theForm.Email_Addr.focus();
		return false;
		} 
    if (isBlank(theForm.Work_Organization)) {
		alert("Please enter a Work Organization");
		theForm.Work_Organization.focus();
		return false;
		}
	if (isUSZip(theForm.Zip_Code.value) == false){
		alert("Postal code must be 5 digits, OR 5 digits, a hyphen, and 4 more digits.");
    	theForm.Zip_Code.focus();
		return false;
		}
	if (Valid_Other(theForm.Occupation,theForm.Occupation_Other) == false) {
		return false;
		}
	if (Valid_Other(theForm.Specialty,theForm.Specialty_Other) == false) {
		return false;
		}
	if (Valid_Other(theForm.How_Hear,theForm.How_Hear_Other) == false) {
		return false;
		}
	else {
	return true;}
}