var whitespace = " \t\n\r";

var digitsInZIPCode1 = 5
var digitsInZIPCode2 = 9
var defaultEmptyOK = false

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

function stripWhitespace (s)

{   return stripCharsInBag (s, whitespace)
}

function isWhitespace (s)

{   var i;

    if (isEmpty(s)) return true;

    for (i = 0; i < s.length; i++)
    {   
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    return true;
}

function stripCharsInBag (s, bag)

{   var i;
    var returnString = "";

    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }

    return returnString;
}

function Trim(string)
{
 var newStr = "";
 newStr = RTrim(LTrim(string));
 return (newStr);	
}

function LTrim(string)
{
 var oldStr = string;
 var newStr = "";
 var notDone = true;
 for (i = 0;  i < oldStr.length;  i++)
 {
   ch = oldStr.charAt(i);
   if (ch==" " && notDone)
   {
    // remove spaces 
   }
   else
   {
	newStr = newStr + ch;
	notDone = false;
   }
 }
 return (newStr);	
}
function RTrim(string)
{
 var oldStr = string;
 var newStr = "";
 var notDone = true;
 for (i = oldStr.length-1;  i >= 0;  i--)
 {
   ch = oldStr.charAt(i);
   if (ch==" " && notDone)
   {
    // remove spaces 
   }
   else
   {
	newStr = ch + newStr;
	notDone = false;
   }
 }
 return (newStr);	
}

strNameInvalid ="!@#$%^&*()_+|\={}][:;<>?/`~1234567890"
strJobtitleInvalid = "!@#$%^&*()_+|\={}][:;<>?`~"
strCompanyInvalid = "!@#$%^*()_+|\={}][:;<>?/`~"

strNamevalid ="' , . -"
strJobtitlevalid ="/ ' , . -1234567890"
strComapnyvalid ="& ' , . -1234567890"

function isValidName (s,strType)
{   var i;
	var p;
    var strInvalid;
    var strValid;
    var intValidcount;
    var intAlphaNumeric;
    var validChar;
    
    if (strType == 'firstname' || strType == 'lastname'){strInvalid = strNameInvalid; strValid = strNamevalid;}
    if (strType == 'jobtitle'){strInvalid = strJobtitleInvalid; strValid = strJobtitlevalid;}
    if (strType == 'company'){strInvalid = strCompanyInvalid; strValid = strComapnyvalid;}
        
    if (isWhitespace(s)) {
		return false;
	}
	
	 intValidcount =0;
	 intAlphaNumeric = 0;
	 
    for (i = 0; i < s.length; i++)
    {   
        for (p = 0; p < strInvalid.length; p++)
        {
			if (s.charAt(i) == strInvalid.charAt(p)||s.charAt(i) == '"'||s.charAt(i) == '\\') {
				return false;
			}
        }
        validChar = false;
        
        for (p = 0; p < strValid.length; p++)
        {
			if (s.charAt(i) == strValid.charAt(p)) {
			    intValidcount = intValidcount + 1
			    validChar = true;
			}
		}
        if (!validChar || isInteger(s.charAt(i))){intAlphaNumeric = intAlphaNumeric + 1;}
        
    }
    
    
   if (intValidcount == s.length){return false;}
      
   for (p = 0; p < strValid.length; p++){
	 if (strType == 'firstname' && s.length == 1){
		if (s.charAt(0) == strValid.charAt(p)){return false;}
	 }
	 
	 if (strType == 'lastname' && s.length == 2){
		if (s.charAt(0) == strValid.charAt(p) || s.charAt(1) == strValid.charAt(p)){return false;}
	 }
	 
  }
		
	if (strType == 'firstname' && s.length > 1 && intAlphaNumeric < 1){return false;}
	if (strType == 'lastname' && s.length > 2 && intAlphaNumeric < 2){return false;}
	if (strType == 'jobtitle' && s.length >= 2 && intAlphaNumeric < 2){return false;}
  	if (strType == 'company' && s.length >= 2 && intAlphaNumeric < 2){return false;}
  	
    return true;
}

function isZIPCode (s)
{  if (isEmpty(s)) 
       if (isZIPCode.arguments.length == 1) return defaultEmptyOK;
       else return (isZIPCode.arguments[1] == true);
   return (isInteger(s) && 
            ((s.length == digitsInZIPCode1) ||
             (s.length == digitsInZIPCode2)))
}


function isEmail (s)
{   if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return defaultEmptyOK;
       else return (isEmail.arguments[1] == true);
   
    if (isWhitespace(s)) return false;
    
    var i = 1;
    var sLength = s.length;
	
	while ((i < sLength) && (s.charAt(i) != " ") && (s.charAt(i) != ","))
    	{ i++
    	}
	if ((s.charAt(i) == " ") || (s.charAt(i) == ",")) {
		return false;
	}

	i = 1
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    while ((i < sLength) && (s.charAt(i) != ".") && (s.charAt(i) != "@"))
    { i++
    }

	if (s.charAt(i) == "@") {
		return false;
	}

    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}
function IsMyAcctConfirmPhoneValid(phone){

	if (phone.length!=12 || !isDigit(phone.charAt(0))|| !isDigit(phone.charAt(1)) || !isDigit(phone.charAt(2)) || !isDigit(phone.charAt(4)) || !isDigit(phone.charAt(5)) || !isDigit(phone.charAt(6)) || !isDigit(phone.charAt(8)) || !isDigit(phone.charAt(9)) || !isDigit(phone.charAt(10)) || !isDigit(phone.charAt(11)) || phone.charAt(3)!="-"||phone.charAt(7)!="-")
	{
		return false;
	}
	return true;
}
function isDigit (c)
{   return ((c >= "0") && (c <= "9"))
}
function isInteger (s)

{   var i;

    if (isEmpty(s)) 
       if (isInteger.arguments.length == 1) return defaultEmptyOK;
       else return (isInteger.arguments[1] == true);
  
   for (i = 0; i < s.length; i++)
    {   
         var c = s.charAt(i);

        if (!isDigit(c)) return false;
    }

    return true;
}

function isURL(strURL) 
{	
	var i = 1;
    var sLength = strURL.length;
    var strLastThree
	
	if (isEmpty(strURL)) {
		return false;
	}
       
	if (isWhitespace(strURL)) {
		return false;
	}
    
    while ((i < sLength) && (strURL.charAt(i) != ","))
    { i++
    }
    
    if (strURL.charAt(i) == ",") {
		return false;
	}
    
    i = 1;
    while ((i < sLength) && (strURL.charAt(i) != "."))
    { i++
    }

    if ((i >= sLength) || (strURL.charAt(i) != ".")) {
		return false;
	}
    else {
		i += 2;
	}

    while ((i < sLength) && (strURL.charAt(i) != "."))
    { i++
    }
    if ((i >= sLength - 1) || (strURL.charAt(i) != ".")) {
		return false;
	}
	i++
	strLastThree = strURL.substring (i);
	strLastThree = strLastThree.toUpperCase();

	return true;
}

function isDate(DateToCheck){
if(DateToCheck==""){return true;}
var m_strDate = FormatDate(DateToCheck);
if(m_strDate==""){
return false;
}
var m_arrDate = m_strDate.split("/");
var m_DAY = m_arrDate[0];
var m_MONTH = m_arrDate[1];
var m_YEAR = m_arrDate[2];
if(m_YEAR.length > 4){return false;}
m_strDate = m_MONTH + "/" + m_DAY + "/" + m_YEAR;
var testDate=new Date(m_strDate);
if(testDate.getMonth()+1==m_MONTH){
return true;
} 
else{
return false;
}
}//end isDate function

function FormatDate(DateToFormat,FormatAs){
if(DateToFormat==""){return"";}
if(!FormatAs){FormatAs="dd/mm/yyyy";}

var strReturnDate;
FormatAs = FormatAs.toLowerCase();
DateToFormat = DateToFormat.toLowerCase();
var arrDate
var arrMonths = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
var strMONTH;
var Separator;

while(DateToFormat.indexOf("st")>-1){
DateToFormat = DateToFormat.replace("st","");
}

while(DateToFormat.indexOf("nd")>-1){
DateToFormat = DateToFormat.replace("nd","");
}

while(DateToFormat.indexOf("rd")>-1){
DateToFormat = DateToFormat.replace("rd","");
}

while(DateToFormat.indexOf("th")>-1){
DateToFormat = DateToFormat.replace("th","");
}

if(DateToFormat.indexOf(".")>-1){
Separator = ".";
}

if(DateToFormat.indexOf("-")>-1){
Separator = "-";
}

if(DateToFormat.indexOf("/")>-1){
Separator = "/";
}

if(DateToFormat.indexOf(" ")>-1){
Separator = " ";
}

arrDate = DateToFormat.split(Separator);
DateToFormat = "";
	for(var iSD = 0;iSD < arrDate.length;iSD++){
		if(arrDate[iSD]!=""){
		DateToFormat += arrDate[iSD] + Separator;
		}
	}
DateToFormat = DateToFormat.substring(0,DateToFormat.length-1);
arrDate = DateToFormat.split(Separator);

if(arrDate.length < 3){
return "";
}

var DAY = arrDate[0];
var MONTH = arrDate[1];
var YEAR = arrDate[2];

if(parseFloat(arrDate[1]) > 12){
DAY = arrDate[1];
MONTH = arrDate[0];
}

if(parseFloat(DAY) && DAY.toString().length==4){
YEAR = arrDate[0];
DAY = arrDate[2];
MONTH = arrDate[1];
}

for(var iSD = 0;iSD < arrMonths.length;iSD++){
var ShortMonth = arrMonths[iSD].substring(0,3).toLowerCase();
var MonthPosition = DateToFormat.indexOf(ShortMonth);
	if(MonthPosition > -1){
	MONTH = iSD + 1;
		if(MonthPosition == 0){
		DAY = arrDate[1];
		YEAR = arrDate[2];
		}
	break;
	}
}

var strTemp = YEAR.toString();
if(strTemp.length==2){

	if(parseFloat(YEAR)>40){
	YEAR = "19" + YEAR;
	}
	else{
	YEAR = "20" + YEAR;
	}

}

	if(parseInt(MONTH)< 10 && MONTH.toString().length < 2){
	MONTH = "0" + MONTH;
	}
	if(parseInt(DAY)< 10 && DAY.toString().length < 2){
	DAY = "0" + DAY;
	}
	switch (FormatAs){
	case "dd/mm/yyyy":
	return DAY + "/" + MONTH + "/" + YEAR;
	case "mm/dd/yyyy":
	return MONTH + "/" + DAY + "/" + YEAR;
	case "dd/mmm/yyyy":
	return DAY + " " + arrMonths[MONTH -1].substring(0,3) + " " + YEAR;
	case "mmm/dd/yyyy":
	return arrMonths[MONTH -1].substring(0,3) + " " + DAY + " " + YEAR;
	case "dd/mmmm/yyyy":
	return DAY + " " + arrMonths[MONTH -1] + " " + YEAR;	
	case "mmmm/dd/yyyy":
	return arrMonths[MONTH -1] + " " + DAY + " " + YEAR;
	}

return DAY + "/" + strMONTH + "/" + YEAR;;

} //End FormatDate Function


function getEncodeURLParams(){
  var is_input = location.search.indexOf('?');
  var url_params = "";

  if (is_input != -1)
    url_params = location.search.substring(is_input+1, location.search.length);
 
  document.form1.elements["partnerparam"].value = url_params;
}

function getUniqueId()
{
     var dateObject = new Date();
     var uniqueId =
          dateObject.getFullYear() + '' +
          dateObject.getMonth() + '' +
          dateObject.getDate() + '' +
          dateObject.getTime();

     return uniqueId;
}

