
function submitDoc() 
{
	if(document.beLogin.tc.checked)
	{
  		document.beLogin.refresh.value=false;
  		document.beLogin.submit();
  	}
  	else
  	{
  	    mess = "You must agree with terms and conditions!";
  	    alert(mess);
        document.beLogin.refresh.focus();
        return true;
  	}
}

function refreshDoc() 
{
  document.beLogin.refresh.value=true;
  document.beLogin.submit();
}


//Auto switch to the next field in the 3 input text boxes phone numbers forms
function phoneFieldSwitch(form, field, nextField) {
    var maxareaCodeLength=3;
    var maxexchangeCodeLength=3;
    var maxPhonefield=4;
    var fieldName="";
    var myform = "document."+form.name;
    var myfield = myform+"."+field.name;
    var myNextfield = nextField;
    var formObj = eval(myform);
    var fieldObj = eval(myfield);
    var nextFieldObj = eval(myNextfield);
    switch (field.name)  {
        case "userId1" :
            if(field.value.length != maxareaCodeLength) {
                fieldObj.focus();
            } else {
                nextFieldObj.focus();
            }
            break;
        case "userId2" :
            if (field.value.length != maxexchangeCodeLength) {
               fieldObj.focus();
            } else {
                nextFieldObj.focus();
            }
            break;
        case "userId3" :
            if (field.value.length != maxPhonefield) {
               fieldObj.focus();
            } else {
                nextFieldObj.focus();
            }
            break;
    }
}

function validatePhone(phone) {
    var p = Trim(phone.value);
    mess = "Incorrect phone number"
    if(p.length != 10){
        alert(mess);
        phone.focus();
        return false;
    }
    if((p == '0123456789') || (p == '0000000000')){
        alert(mess);
        phone.focus();
        return false;
    }
    if(! isInteger(p)){
        alert(mess);
        phone.focus();
        return false;
    }
    return true;
}

function test (){
 	alert("arg 1");
	return false;
}

function validatePhoneAndEmail5(phone1, phone2, phone3, email, phone) {
    mess = "Incorrect phone number";
    if(Trim(phone1.value).length != 3){
        alert(mess);
        phone1.focus();
        return false;
    }
    if(Trim(phone2.value).length != 3){
        alert(mess);
        phone2.focus();
        return false;
    }
    if(Trim(phone3.value).length != 4){
        alert(mess);
        phone3.focus();
        return false;
    }

    if(Trim(email.value) == ''){
        alert("Email address is empty");
        email.focus();
        return false;
    }
    if(!isEmail(email.value)){
        alert("Incorrect email address");
        email.focus();
        return false;
    }

    phone.value = "" + phone1.value + phone2.value + phone3.value;
    return true;
}


function validatePhoneAndEmail3(phone1, phone2, phone3, email, phone, tc) {
    mess = "Incorrect phone number"
    if(Trim(phone1.value).length != 3){
        alert(mess);
        phone1.focus();
        return false;
    }
    if(Trim(phone2.value).length != 3){
        alert(mess);
        phone2.focus();
        return false;
    }
    if(Trim(phone3.value).length != 4){
        alert(mess);
        phone3.focus();
        return false;
    }

    if(Trim(email.value) == ''){
        alert("Email address is empty");
        email.focus();
        return false;
    }
    if(!isEmail(email.value)){
        alert("Incorrect email address");
        email.focus();
        return false;
    }

    if(!tc.checked){
        alert("You must agree to the Terms and Condition in order to download this item.");
        tc.focus();
        return false;
    }


    deleteUnload();
    phone.value = "" + phone1.value + phone2.value + phone3.value;
    return true;
}

function validatePhoneAndEmail(phone, email) {
    if(!validatePhone(phone)) return false;

    if(Trim(email.value) == ''){
        alert("Email address is empty");
        email.focus();
        return false;
    }
    if(!isEmail(email.value)){
        alert("Incorrect email address");
        email.focus();
        return false;
    }
    deleteUnload();
    return true;
}

function validateEmail(email){
    if(Trim(email.value) == ''){
        alert("Email address is empty");
        email.focus();
        return false;
    }
    if(!isEmail(email.value)){
        alert("Incorrect email address");
        email.focus();
        return false;
    }
    return true;
}

function validatePass(pass) {
    var p = Trim(pass.value);
    mess = "Password is empty."
    if(p.length == 0){
        alert(mess);
        pass.focus();
        return false;
    }
    return true;
}

function validateLength(pass) {
    var p = Trim(pass.value);
    mess = "Password must be between 4 and 6 characters."
    if((p.length < 4) || (p.length > 6)){
        alert(mess);
        pass.focus();
        return false;
    }
    return true;
}



function validateLogin(phone, password) {
    if( ! validatePhone(phone)) return false;
    if( ! validatePass(password)) return false;
    return true;
}

function validateLogin3(phone1, phone2, phone3, password) {
    mess = "Incorrect phone number"
    if(Trim(phone1.value).length != 3){
        alert(mess);
        phone1.focus();
        return false;
    }
    if(Trim(phone2.value).length != 3){
        alert(mess);
        phone2.focus();
        return false;
    }
    if(Trim(phone3.value).length != 4){
        alert(mess);
        phone3.focus();
        return false;
    }

    if(Trim(password.value) == ''){
        alert("Password address is empty");
        password.focus();
        return false;
    }
    return true;
}

function validatePhone3(phone1, phone2, phone3) {
    mess = "Incorrect phone number"
    if(Trim(phone1.value).length != 3){
        alert(mess);
        phone1.focus();
        return false;
    }
    if(Trim(phone2.value).length != 3){
        alert(mess);
        phone2.focus();
        return false;
    }
    if(Trim(phone3.value).length != 4){
        alert(mess);
        phone3.focus();
        return false;
    }
    return true;
}




function validateChange(old, new1, new2) {
    if( ! validatePass(old)) return false;
    if( ! validateLength(new1)) return false;
    if( ! validateLength(new2)) return false;
    return true;
}

function validateCreate(new1, new2) {
    if( ! validateLength(new1)) return false;
    if( ! validateLength(new2)) return false;
    return true;
}


//test to valid email
function isEmail(mail){
   if (mail == null) return false ;
   if ( Trim(mail) == "" ) return false ;

   var supported = 0;
   if (window.RegExp){
      var tempStr = "a";
      var tempReg = new RegExp(tempStr);
      if (tempReg.test(tempStr)) supported = 1;
   }
   if (!supported) return (mail.indexOf(".") > 2) && (mail.indexOf("@") > 0);

   var r1 = new RegExp("(@.*@)|(\\.\\.)|(@\\.)|(^\\.)");
   var r2 = new RegExp("^.+\\@(\\[?)[a-zA-Z0-9\\-\\.]+\\.([a-zA-Z]{2,3}|[0-9]{1,3})(\\]?)$");
   return (!r1.test(mail) && r2.test(mail));
}


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 isChecked(el){
	var result = false;
	if (el.length) {
		for(i = 0; i < el.length; i++){
			if(el[i].checked) {
				result = true;
				break;
			}
		}
	} else {
		result = el.checked;
	}
	return result;
}


function getCheckedValue(el){
	if (el.length) {
		for(i = 0; i < el.length; i++){
			if(el[i].checked) {
				return el[i].value;
			}
		}
	}
	return "";
}


//left trim
function LTrim(String){
   var i = 0;
   var j = String.length - 1;
   if (String == null) return (false);
   for (i = 0; i < String.length; i++)
   {
      if (String.substr(i, 1) != ' ' &&
          String.substr(i, 1) != '\t')
         break;
   }
   if (i <= j)
      return (String.substr(i, (j+1)-i));
   else
      return ('');
}

//right trim
function RTrim(String){
   var i = 0
   var j = String.length - 1;
   if (String == null)
      return (false);

   for(j = String.length - 1; j >= 0; j--)
   {
      if (String.substr(j, 1) != ' ' &&
         String.substr(j, 1) != '\t')
      break;
   }
   if (i <= j)
      return (String.substr(i, (j+1)-i));
   else
      return ('');
}

//trim
function Trim(String){
   if (String == null) return (false);
   return RTrim(LTrim(String));
}

function validate(){
	var flag = true
	flag = checkPassword ();
	if (flag == true){
		flag = validatePhone1();
	}
	if (flag == true){
		flag = validatePhone2();
	}

	if (flag == true){

		flag = validatePhone3();

	}

	if (flag == true){

		flag = checkPassword();

	}

	if (flag == false){

		return false;

	}else {

		return true;

	}

}


function deleteUnload(){
    window.onunload = null;
}


function checkPassword () {



 var field = document.landingPage.password.value;

 var field2 = document.landingPage.newPassword.value;

 var field3 = document.landingPage.newPassword2.value;

 	if (field == "") {

    	alert('You didnt enter a password');

 		return false;

 	}



 	if (field2 == "") {

    	alert('You didnt enter a new password');

 		return false;

 	}



 	if (field3 == "") {

    	alert('You didnt confirm your new password');

 		return false;

 	}



    	if (field2 == field3){



	}

	else{

		alert('Please make sure that you have entered correct new password');

    		return false;



	}

    return true;

}



function validatePhone1() {



  var field = document.landingPage.mobileNumber1;



  if (field.value.length == 3){

		if (isInteger(field.value)){

				return true;

		}else{

			alert('Please enter valid number for phone');

			return false;

		}

	}else {

		alert('Please enter your area code number.');

	    return false;

	}

}

function validatePhone2() {



  var field = document.landingPage.mobileNumber2;



  if (field.value.length == 3){

		if (isInteger(field.value)){

				return true;

		}else{

			alert('Please enter valid number for phone');

			return false;

		}

	}else {

		alert('Please enter your area code number.');

	    return false;

	}

}

function validatePhone3() {



  var field = document.landingPage.mobileNumber3;



  if (field.value.length == 4){

		if (isInteger(field.value)){

				return true;

		}else{

			alert('Please enter valid number for phone');

			return false;

		}

	}else {

		alert('Please enter your area code number.');

	    return false;

	}

}
//simple browser versin testin
function whatBrowser(){
  if (document.layers) {
    browser="NN";
    return;
  }
  if (document.all){
    browser = "IE";
    return;
  }
    browser = "Unknown";
}

function validatePhoneAndEmailGawker(phone1, phone2, phone3, email, phone, tc, frm) {
    mess = "Incorrect phone number"

    if( ! isChecked(frm.service)){
            alert("Please select service.");
            frm.service[0].focus() ;
            return false;
    }

    if(Trim(phone1.value).length != 3){
        alert(mess);
        phone1.focus();
        return false;
    }
    if(Trim(phone2.value).length != 3){
        alert(mess);
        phone2.focus();
        return false;
    }
    if(Trim(phone3.value).length != 4){
        alert(mess);
        phone3.focus();
        return false;
    }

    if(Trim(email.value) == ''){
        alert("Email address is empty");
        email.focus();
        return false;
    }
    if(!isEmail(email.value)){
        alert("Incorrect email address");
        email.focus();
        return false;
    }

    if(!tc.checked){
        alert("You must agree to the Terms and Condition in order to download this item.");
        tc.focus();
        return false;
    }

    phone.value = "" + phone1.value + phone2.value + phone3.value;
    return true;
}

function validatePhoneAndEmailHeavy(phone1, phone2, phone3, email, phone, tc) {
    mess = "Incorrect phone number"

    if(Trim(phone1.value).length != 3){
        alert(mess);
        phone1.focus();
        return false;
    }
    if(Trim(phone2.value).length != 3){
        alert(mess);
        phone2.focus();
        return false;
    }
    if(Trim(phone3.value).length != 4){
        alert(mess);
        phone3.focus();
        return false;
    }

    if(Trim(email.value) == ''){
        alert("Email address is empty");
        email.focus();
        return false;
    }
    if(!isEmail(email.value)){
        alert("Incorrect email address");
        email.focus();
        return false;
    }

    if(!tc.checked){
        alert("You must agree to the Terms and Condition in order to download this item.");
        tc.focus();
        return false;
    }

    phone.value = "" + phone1.value + phone2.value + phone3.value;
    return true;
}