// JavaScript Document

// DISABLES BUTTON AND SUBMITS FORM -- NO ERROR CHECKING

function submitFormDisableButton (formID,buttonID)
{
    document.getElementById(buttonID).className='submit_disabled';
    document.getElementById(formID).submit();
    document.getElementById(buttonID).disabled = true; 
}

// DYNAMIC LP: STEP 1 WITH ERROR CHECKING

function submitStep1 (formID,buttonID)
{
    if (step1IsComplete(formID))
    {
        document.getElementById(buttonID).className='submit_disabled';
		document.getElementById(formID).submit();
		return false;
    }
}

function step1IsComplete(form)
{
    var return_value = true;

    if (evalCell(document.getElementById(form)["model.areacode"].value + document.getElementById(form)["model.prefix"].value + document.getElementById(form)["model.suffix"].value) == false) {
        document.getElementById("phone_entry_error").style.display = "block";
        return_value = false;
	}else{
        document.getElementById("phone_entry_error").style.display = "none";
    }

    if (evalEmailAddress(document.getElementById(form)["model.email"],form) == false) {
        document.getElementById("email_entry_error").style.display = "block";
        return_value = false;
    }else{
        if (document.getElementById(form)["model.email"]){
            document.getElementById("email_entry_error").style.display = "none";
        }  
    }

    if (evalTnC(document.getElementById(form)["model.tnc"],form) == false) {
        document.getElementById("tnc_entry_error").style.display = "block";
        return_value = false;
	}else{
        if (document.getElementById(form)["model.tnc"]){
            document.getElementById("tnc_entry_error").style.display = "none";
        }
    }
    
    return return_value;
}

// DYNAMIC LP: STEP 2 WITH ERROR CHECKING

function submitStep2 (formID,buttonID)
{
    if (step2IsComplete(formID))
    {
        document.getElementById(buttonID).className='submit_disabled';
		document.getElementById(formID).submit();
		return false;
    }
}

function step2IsComplete(form)
{
    var return_value = true;

    if ((document.getElementById(form)["model.password"].value.length == 0) || (document.getElementById(form)["model.password"].value == null)) {
        document.getElementById("pin_entry_empty").style.display = "block";
        return_value = false;
	}else{
        document.getElementById("pin_entry_empty").style.display = "none";
    }

    if (evalTnC(document.getElementById(form)["model.tnc"],form) == false) {
        document.getElementById("tnc_entry_error").style.display = "block";
        return_value = false;
	}else{
        if (document.getElementById(form)["model.tnc"]){
            document.getElementById("tnc_entry_error").style.display = "none";
        }
    }

    if (evalTracfone(document.getElementById(form)["tracfone"],form) == false) {
        document.getElementById("tracfone_entry_error").style.display = "block";
        return_value = false;
	}else{
        if (document.getElementById(form)["tracfone"]){
            document.getElementById("tracfone_entry_error").style.display = "none";
        }
    }

    if (evalPhoneList(document.getElementById(form)["model.deviceId"],form) == false) {
        document.getElementById("phone_select_error").style.display = "block";
        return_value = false;
	}else{
        if (document.getElementById(form)["model.deviceId"]){
            document.getElementById("phone_select_error").style.display = "none";
        }
    }

    return return_value;
}

// DYNAMIC LP ERROR CHECKING FUNCTIONS

function evalCell(phone)
{
    var regex = /^\d{10}$/;
    return regex.test(phone);
}

function evalEmailAddress(email,form)
{
    //check to see if model.email exists on the page -- if it exists test for correct syntax
    if (document.getElementById(form)["model.email"]){
        var regex = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        return regex.test(email.value);
    }
    return true;
}

function evalTnC(checkbox,form)
{
    //check to see if model.tnc exists on the page -- if it exists test to see if checkbox is checked
    if (document.getElementById(form)["model.tnc"]){
        return checkbox.checked;
    }
    return true;
}

function evalTracfone(tracfone,form)
{
    //check to see if tracfone_select exists on the page -- if it exists test to see if any of the radio buttons are selected
    if (document.getElementById(form)["tracfone"]){
         if (document.getElementById(form)["password_tracfone1"].checked || document.getElementById(form)["password_tracfone2"].checked || document.getElementById(form)["password_tracfone3"].checked){
            return true;
         }else{
             return false;
         }
    }
    return true;
}

function evalPhoneList(phoneList,form)
{
    //check to see if model.deviceId exists on the page -- if it exists test to see if the default header value is still selected
    if (document.getElementById(form)["model.deviceId"]){
         if (phoneList.options[phoneList.selectedIndex].value == -2){
             return false;
         }
    }
    return true;
}



// FOR STEP 1 IN PURCHASE FLOW

function submitFormLoggedIn(buttonID)
{
    if (onlyCheckbox(document.getElementById(buttonID)))
    {
  		document.getElementById(buttonID).href='#';
		document.getElementById(buttonID).id='submit_lg';
		document.getElementById('submit_lg').className='submit_disabled';
		document.getElementById('submit_lg').innerHTML='Please Wait...';
		document.getElementById('step_a').submit();
		return false;
	 }
}

function submitForm(buttonID)
{
    if (isCompleteBuy(document.getElementById(buttonID)))
    {
  		document.getElementById(buttonID).href='#';
		document.getElementById(buttonID).id='submit_lg';
		document.getElementById('submit_lg').className='submit_disabled';
		document.getElementById('submit_lg').innerHTML='Please Wait...';
		document.getElementById('step_a').submit();
		return false;
  	}
}

function isCompleteBuy(form)
{
    if (evalPhoneBuy(document.getElementById('step_a').areacode.value + document.getElementById('step_a').prefix.value + document.getElementById('step_a').suffix.value) == false) {
      alert("Phone number must be 10 numeric digits.");
      document.getElementById('step_a').areacode.focus();
      return false;
    }
    if (evalEmail(document.getElementById('step_a').email.value) == false) {
      alert("Please provide a valid email address.");
      document.getElementById('step_a').email.focus();
      return false;

    }
    if (evalCheckboxBuy(document.getElementById('step_a').tnc) == false) {
      alert("You must agree with the Terms of Service.");
      document.getElementById('step_a').tnc.focus();
      return false;
    }

    return true;
}

function onlyCheckbox(form)
{
    if (evalCheckboxBuy(document.getElementById('step_a').tnc) == false) {
      alert("You must agree with the Terms of Service.");
      document.getElementById('step_a').tnc.focus();
      return false;
    }

    return true;
}

function evalPhoneBuy(phone)
{
    var regex = /^\d{10}$/;

    return regex.test(phone);
}

function evalEmail(email)
{
    var regex = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;

    return regex.test(email);
}


function evalCheckboxBuy(checkbox)
{
	if (document.getElementById('step_a').tnc){
    return checkbox.checked;
	 }
	 return true;
}

// END FOR PURCHASE FLOW STEP 1 ----


// EVALUATE PHONE NUMBER
function phoneIsComplete(){
	if (evalPhone(document.getElementById('phone').areacode.value + document.getElementById('phone').prefix.value + document.getElementById('phone').suffix.value) == false) {
	alert("Phone number must be 10 numeric digits.");
	document.getElementById('phone').areacode.focus();
	return false;
	}
	if (evalCheckbox(document.getElementById('phone').tnc) == false) {
	  alert("You must agree with the Terms & Conditions.");
	  document.getElementById('phone').tnc.focus();
	  return false;
	}
	return true;
}

function forgotPhoneIsComplete(){
	if (evalPhone(document.getElementById('forgot').areacode.value + document.getElementById('forgot').prefix.value + document.getElementById('forgot').suffix.value) == false) {
	alert("Phone number must be 10 numeric digits.");
	document.getElementById('forgot').areacode.focus();
	return false;
	}
	document.getElementById('forgot').submit();
	return true;
}

function loginPhoneIsComplete(){
	if (evalPhone(document.getElementById('login').areacode.value + document.getElementById('login').prefix.value + document.getElementById('login').suffix.value) == false) {
	alert("Phone number must be 10 numeric digits.");
	document.getElementById('login').areacode.focus();
	return false;
	}
	if ( (document.getElementById('login').password.value.length == 0) || (document.getElementById('login').password.value == null) ){
	document.getElementById('enter_your_password').style.color = "red";
	document.getElementById('loginKeyEnter').style.borderColor = "red";
	document.getElementById('login').password.focus();
	return false;
	}	
	document.getElementById('login').submit();
	return true;
}

function evalPhone(phone)
{
    var regex = /^\d{10}$/;
    return regex.test(phone);
}

function evalCheckbox(checkbox)
{
	if (document.getElementById('phone').tnc){
    return checkbox.checked;
	 }
	 return true;
}

// EVALUATE PASSWORD

function pwdIsComplete(event){
		if ( (document.getElementById('password').pin.value.length == 0) || (document.getElementById('password').pin.value == null) )
			{
			alert("Please enter your password.");
			// event.keyCode = null;
			document.getElementById('password').pin.focus();
			return false;			
			}
			return true;
}


// AUTO TAB

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 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;
	}

