//<!--
/******************************/
/*Function to tri  left spaces*/
/******************************/
/*
      Name        date          Bug#        description
------------------------------------------------------------------------------------------------
     Sunil       05-11-06	   57           leading + should be permissible in the phone number


*/
function lTrim(str)
{
    var whitespace = new String(" \t\n\r");

    var s = new String(str);

    if (whitespace.indexOf(s.charAt(0)) != -1) {
        // We have a string with leading blank(s)...

        var j=0, i = s.length;

        // Iterate from the far left of string until we
        // don't have any more whitespace...
        while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
            j++;


        // Get the substring from the first non-whitespace
        // character to the end of the string...
        s = s.substring(j, i);
    }

    return s;
}


/******************************/
/*Function to trim  right spaces*/
/******************************/
function rTrim(str)
{
    var whitespace = new String(" \t\n\r");

    var s = new String(str);

    if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
        // We have a string with trailing blank(s)...

        var i = s.length - 1;       // Get length of string

        // Iterate from the far right of string until we
        // don't have any more whitespace...
        while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
            i--;


        // Get the substring from the front of the string to
        // where the last non-whitespace character is...
        s = s.substring(0, i+1);
    }

    return s;
}

/******************************/
/*Function to trim  spaces	  */
/******************************/
function trim(str)
{
	return rTrim(lTrim(str));
}

/*
function validateEmail(fld) {
    var error="";
    var theStr=trim(fld.value);
	var atIndex = theStr.indexOf('@'); 
	var dotIndex = theStr.indexOf('.', atIndex); 
	theSub = theStr.substring(0, dotIndex+1) 
	
	if ((atIndex < 1)||(atIndex != theStr.lastIndexOf('@'))||(dotIndex < atIndex + 2)||(theStr.length <= theSub.length)) 
	{ 
		error = "Please enter a valid email address.\n";
		fld.style.background = 'Yellow';
	} 
	return error;
	 
}*/

function validateEmail(fld) 
{
	//Reverting back the changes done for ACTRSVP-543.
	var error = validateBlank(fld, "Please input the email address.\n");
	if (error!="") {
		return error;
	}
	
	//var email = fld.replace(/\s/g,'');
	var email = trim(fld.value);
	var spacesFound  = hasInterveningSpaces(/\s/g,email);
	var validEmailFormat = (/^[a-zA-Z0-9!#$%*\-\?\/{}\^'`~|&+=_\.]+@(?:[a-zA-Z0-9-]+\.)+[a-zA-Z0-9]{2,6}$/.test(email));
	if(!isBlank(fld) && (spacesFound || !(validEmailFormat)))
	{
		error = "The email address must have the form \"Name@Domain\", where Domain has at least two words separated by only a dot (.) \n";
		fld.style.background = 'Yellow';		
	}
	return error;
}

function hasInterveningSpaces(re, str)
{
   if (re.test(str))
   {
      return true;
   }
   else
   {
     return false;
   }
}

function validatePhone(fld,phoneNumberMandatory)
{
	var error="";
	/*var str=trim(fld.value);
	var openbraceCount = 0;
	var closedbraceCOunt = 0;
	for(i=0;i<str.length;i++)
	{
		if(str.charAt(i)<'0'||str.charAt(i)>'9')
		{	
			if((str.charAt(i))=="-" )
			{
				i=i+1;
				continue;
			}
			if((str.charAt(i))==" " )
			{
				i=i+1;
				continue;
			}
			if ((str.charAt(i))=="(")
			{
			  openbraceCount = openbraceCount + 1;
			  continue;
			}
			if  ((str.charAt(i))==")")
			{
			  closedbraceCOunt = closedbraceCOunt + 1;
			  continue;
			}
			if (i==0 && (str.charAt(i))=="+")
			{
				continue;
			}
			error = "The phone number contains illegal characters.\n";
			fld.style.background = 'Yellow';
			break;
		}
		
		if (!(openbraceCount == closedbraceCOunt))
    	{
      		error = "The phone number is not correctly formatted.\n";
      		fld.style.background = 'Yellow';
        }
		return error;*/
		var str=trim(fld.value);
		if(phoneNumberMandatory && str.length == 0)
		{
			error = "Please input the phone number.\n";
			fld.style.background = 'Yellow';
			return error;
		}
		if (str.length > 0) {
			if(str.length > 20){
				error = "Length of the phone field cannot be more than 20.\n";
				fld.style.background = 'Yellow';
			}
			if(!isValidPhone(str)) {
				error = "Invalid Phone number.\n";
				fld.style.background = 'Yellow';
			}	
		}
		return error;
} 

function isValidPhone(val)
{
	//Allows only alphanumeric and ()- 
	//Allows a-zA-Z0-9 and ()-
	//(123) 456-7890
//	if (val.match(/^[a-zA-Z0-9()-]+$/))
//	if (val.match(/^[(][a-zA-Z0-9]+[)][ ]?[a-zA-Z0-9]+[-]?[a-zA-Z0-9]+$/) || val.match(/^[a-zA-Z0-9]+[ ]?[a-zA-Z0-9]+[-]?[a-zA-Z0-9]+$/))
//	if (val.match(/^[(][a-zA-Z0-9-]+[)][ ]?[a-zA-Z0-9-]+$/) || val.match(/^[a-zA-Z0-9-]+[ ]?[a-zA-Z0-9-]+$/))
//	{
//		return true;
//	}
//	else
//	{
//		return false;
	//} 
	
	if (val.match(/^[ ( ][a-zA-Z0-9-+. ]+[ ) ][ ]?[a-zA-Z0-9-+. ]+$/) || val.match(/^[+a-zA-Z0-9-. ]+[ ]?[a-zA-Z0-9-. ]+$/))
	{
		return true;
	}
	else
	{
		return false;
	} 
	
}
/*Commneting this for ACTRSVP-626 as this was already not there in place.
function validatePostalCode(fld)
{
	var error="";
	//var str=trim(fld.value);
	
	var str=trim(fld.value);
	if(str.length==0)
	{
		return "";
	}
	
	if(!validZip(trim(fld.value))) {
		error = "The Postal Code is Invalid.\n";
		fld.style.background = 'Yellow';
	}
	else
		error = validateInvalidTags(fld.value,"Please remove invalid tags from Postal Code.\n");
	return error;
}
*/
function validZip(zip)
{
	if (zip.match(/^[0-9]{5}$/)) {
			return true;
	}
	zip=zip.toUpperCase();
	if (zip.match(/^[A-Z][0-9][A-Z][0-9][A-Z][0-9]$/)) {
		return true;
	}
	if (zip.match(/^[A-Z][0-9][A-Z].[0-9][A-Z][0-9]$/)) {
		return true;
	}
	return false;
}




function validateState(fld)
{
	var error="";
	if(fld.value == ' '  || fld.value  =='  ')
	{
		error = "Please enter a state or province abbreviation.\n";
		return error;
	}
	var str=trim(fld.value);
	if(str.length!=0)
	{
		if(str.length > 10 || str.length < 2)
		{
			error = "The State is of wrong length.\n";
			fld.style.background = 'Yellow';
		}
		else
			error = validateInvalidTags(fld.value,"Please remove invalid tags from State.\n");
			if(error != "")
			{
				fld.style.background = 'Yellow';
			}
	}
	return error;
}

function isBlank(fld)
{
	var str=trim(fld.value);
	if(str.length==0)
	{
		return true;
	}
	return false;
}

function validateBlank(fld, errorMsg)
{
	var error="";
	var str=trim(fld.value);
	if(str.length==0)
	{
		error=errorMsg;
		fld.style.background = 'Yellow';
	}
	return error;
}


function validateCompany(fld,organizationMandatory)
{
	if(organizationMandatory)
	{
		var error = validateBlank(fld, "Please input the company name.\n");
		if(error != "")
			return error;
		else
			return validateInvalidField(fld,"Please remove invalid tags from company name.\n");
	}
	else return "";
}

function validateFirstName(fld) {
	var error = validateBlank(fld, "Please input first name.\n");
	if(error != "")
		return error;
	else
		return validateInvalidField(fld,"Please remove invalid tags from first name.\n");
}

function validateLastName(fld) {
	var error = validateBlank(fld, "Please input last name.\n");
	if(error != "")
		return error;
	else
		return validateInvalidField(fld,"Please remove invalid tags from last name.\n");
}


function validateText(fld, status)
{
	if(status==true)
	{
		return true;
	}
	var str=trim(fld.value);
	if(str.length==0)
	{
		return true;
	}
}

function validateCombo(fld, status)
{
	if(status==true)
	{
		return true;
	}
	if(fld.options[fld.selectedIndex].value=="")
	{
		return true;
	}
}

function validateInvalidField(fld,errorMsg)
{
	var error = "";
	error = validateInvalidTags(fld.value,errorMsg);
	if(error != "")
	{
		fld.style.background = 'Yellow';
	}
	return error;
}

function validateInvalidTags(fldValue,errorMsg)
{
	var error = "";
	var startIndex;
	var endIndex;
	var value = trim(fldValue);
	var indexOfLessThenTag = value.indexOf("<");
	var indexOfEncodedLessThenTag = value.toLowerCase().indexOf("&lt;");
	
	var indexOfGreaterThenTag = value.lastIndexOf(">");
	var indexOfEncodedGreaterThenTag = value.toLowerCase().lastIndexOf("&gt;");
	if(indexOfLessThenTag != indexOfEncodedLessThenTag)
	{
		if(indexOfLessThenTag == -1)
		{
			startIndex = indexOfEncodedLessThenTag;
		}
		else if(indexOfEncodedLessThenTag == -1)
		{
			startIndex = indexOfLessThenTag;
		}
		else					
			startIndex = indexOfLessThenTag < indexOfEncodedLessThenTag ? 
					indexOfLessThenTag : indexOfEncodedLessThenTag;
	}	
	if(indexOfGreaterThenTag != indexOfEncodedGreaterThenTag)
	{
		if(indexOfGreaterThenTag == -1)
		{
			endIndex = indexOfEncodedGreaterThenTag+3;
		}
		else if(indexOfEncodedGreaterThenTag == -1)
		{
			endIndex = indexOfGreaterThenTag;
		}
		else					
			endIndex = indexOfGreaterThenTag > indexOfEncodedGreaterThenTag ? 
					indexOfGreaterThenTag : indexOfEncodedGreaterThenTag+3;
	}
	if((startIndex != endIndex) && (endIndex == (value.length-1)) && (startIndex == 0))
	{
		error = errorMsg;
	}
	return error;
}