var isNN = (navigator.appName.indexOf("Netscape")!=-1);

function autoTab(input,len, e) {
var keyCode = (isNN) ? e.which : e.keyCode; 
var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
if(input.value.length >= len && !containsElement(filter,keyCode)) {
input.value = input.value.slice(0, len);
input.form[(getIndex(input)+1) % input.form.length].focus();
}

function containsElement(arr, ele) {
var found = false, index = 0;
while(!found && index < arr.length)
if(arr[index] == ele)
found = true;
else
index++;
return found;
}

function checkEnter(e){ //e is event object passed from function invocation  
   var keyCode = (isNN) ? e.which : e.keyCode; 
   if(keyCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
    document.forms[0].submit() //submit the form
    return false 
   } else {
    return true 
   }
}

function getIndex(input) {
var index = -1, i = 0, found = false;
while (i < input.form.length && index == -1)
if (input.form[i] == input)index = i;
else i++;
return index;
}
return true;
}
function echeck(str) {
  var at="@"
  var dot="."
  var lat=str.indexOf(at)
  var lstr=str.length
  var ldot=str.indexOf(dot)
  
		if (str.indexOf(at)==-1){
		   return false
     	}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
           return false
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		   return false
		}

		 if (str.indexOf(at,(lat+1))!=-1){
 	       return false
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    return false
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    return false
		 }
		
		 if (str.indexOf(" ")!=-1){
		    return false
		 }

 		 return true					
}

function isNum(str) {
   var allValid = true;	   
    var checkOK = "0123456789";
	
   for (i = 0;  i < str.length;  i++) {
     ch = str.charAt(i);  
     for (j = 0;  j < checkOK.length;  j++)
       if (ch == checkOK.charAt(j))
         break;
       if (j == checkOK.length) {
		    allValid = false;
             break;
       } // end if
  } // for
  
  return allValid;

} // isNum
function Trim(TRIM_VALUE){
if(TRIM_VALUE.length < 1){
return"";
}
TRIM_VALUE = RTrim(TRIM_VALUE);
TRIM_VALUE = LTrim(TRIM_VALUE);
if(TRIM_VALUE==""){
return "";
}
else{
return TRIM_VALUE;
}
} //End Function

function RTrim(VALUE){
var w_space = String.fromCharCode(32);
var v_length = VALUE.length;
var strTemp = "";
if(v_length < 0){
return"";
}
var iTemp = v_length -1;

while(iTemp > -1){
if(VALUE.charAt(iTemp) == w_space){
}
else{
strTemp = VALUE.substring(0,iTemp +1);
break;
}
iTemp = iTemp-1;

} //End While
return strTemp;

} //End Function

function LTrim(VALUE){
var w_space = String.fromCharCode(32);
if(v_length < 1){
return"";
}
var v_length = VALUE.length;
var strTemp = "";

var iTemp = 0;

while(iTemp < v_length){
if(VALUE.charAt(iTemp) == w_space){
}
else{
strTemp = VALUE.substring(iTemp,v_length);
break;
}
iTemp = iTemp + 1;
} //End While
return strTemp;
} //End Function


function forceNum() {
 var code = (isNN) ? event.which : event.keyCode; 
  
 if (isNum(String.fromCharCode(code))){
  return true;	
 }	else {
  return false;
 } 
} // forceNum

function putSlash(obj) {
  if ((obj.value.length == 2) || (obj.value.length == 5))   
    obj.value=obj.value+"/";      	
}



function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    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++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}


function checkForm(frmRegister, isNewsletter) {
      var errStr = "";
      if (Trim(frmRegister.email.value) == "")
      {
        errStr+= "Please enter your email address\n";
      } else {
        if (!isNewsletter) {
        if (Trim(frmRegister.email2.value) == "") {
        errStr+= "Please confirm your email address\n";
        }
        else 
        {
          if (Trim(frmRegister.email2.value.toLowerCase()) != Trim(frmRegister.email.value.toLowerCase())) {          
           errStr+= "The email addresses you have entered do not match\n";
          } else {
            if (echeck(frmRegister.email.value) == false) {
              errStr += "The email address you have entered is invalid\n"
            }
          }           
        }      
        } else {
         if (echeck(frmRegister.email.value) == false) {
              errStr += "The email address you have entered is invalid\n"
           }
        
        }
      }
      if (Trim(frmRegister.firstname.value) == "") {
        errStr += "Please enter your first name\n";
      }
      if (Trim(frmRegister.lastname.value) == "") {
        errStr += "Please enter your last name\n";
      }
      
      if (Trim(frmRegister.age.value) == "") {
        errStr += "Please enter your age\n";     
       } else { 
         if (!isNum(Trim(frmRegister.age.value))) {
          errStr += "Please enter a numeric value for your age\n";
         } else {     
         if (frmRegister.age.value < 18) {
           errStr += "Registration is valid for only persons above the age of 18\n";
         }
         }
       }
      
      if (!isNewsletter) 
      {
          if (Trim(frmRegister.friend1name.value) == "") {
            errStr+= "Please enter the name of your first referral\n";
          } 
          if (Trim(frmRegister.friend2name.value) == "") {
            errStr += "Please enter the name of your second referral\n";
          }
          
          if (Trim(frmRegister.friend1email.value) == "") {
            errStr += "Please enter the email address of your first referral\n";
          } else {
            if (echeck(frmRegister.friend1email.value) == false) {
              errStr += "The email address you have entered for your first referral, is invalid\n";
            }      
          }
          
          if (Trim(frmRegister.friend2email.value) == "") {
            errStr += "Please enter the email address of your second referral\n";
          } else {
            if (echeck(frmRegister.friend2email.value) == false) {
              errStr += "The email address you have entered for your second referral, is invalid\n";
            } else {
            
              if (Trim(frmRegister.friend1email.value.toLowerCase()) == Trim(frmRegister.friend2email.value.toLowerCase())) {
                 errStr +="Please enter two different email addresses for your referrals\n";
              }
            }      
          }
      }
      
       if (errStr == "") {
         return true;
       } else {
         alert(errStr);
         return false;
       }
    }

function chkChanged(obj) {
    frmRegister.btnSubmit.disabled = !obj.checked;
  }

