﻿//Sets the current tab being used.
var currentTab = 1; 

/*$('#gameChoices').tabs({
	onShow: function()
	{
		NextTab(2);
	}
});
$('#contactYou').tabs({
	onShow: function()
	{
		NextTab(3);
	}
});
$('#submit').tabs({
	onShow: function()
	{
		NextTab(4);
	}
});*/


function canSubmit(bSuppress)
{
	if(bSuppress == true)
	{
		if(!validatePageOne(true) && !validatePageTwo(true))
			return false; 
	}else{
		if(!validatePageOne(false))
			return false; 
		if(!validatePageTwo(false))
			return false;
	}
	return true; 
}

function isEditMode()
{
    var passContainer = document.getElementById("ctl00_MainContentPlaceHolder_EditRegistration1_ChangePasswordContainer"); 
    return(passContainer != null); 
}
 
function NextTab(nextTab)
{ 
	if(isEditMode() && currentTab != nextTab)
    {
		switch(currentTab)
		{
			case 1: 
				if(validatePageOne(false))
				{
					$('#formContainer > ul').tabs('select', nextTab - 1);
					hideError();
					currentTab = nextTab; 
				}else{
					$('#formContainer > ul').tabs('select', currentTab - 1);
				}
			break;
			case 2: 
				if(validatePageTwo(false))
				{
					$('#formContainer > ul').tabs('select', nextTab - 1);
					hideError();
					currentTab = nextTab; 
				}else{
					$('#formContainer > ul').tabs('select', currentTab - 1);
				}
			break;
		}
	}
}

function validatePageOne(bSuppress, button) {
	var reason = "";
	var fName = document.getElementById("ctl00_MainContentPlaceHolder_EditRegistration1_FirstName"); 
	var lName = document.getElementById("ctl00_MainContentPlaceHolder_EditRegistration1_LastName"); 
	var monthValue = document.getElementById("ctl00_MainContentPlaceHolder_EditRegistration1_Month");
	var dayValue = document.getElementById("ctl00_MainContentPlaceHolder_EditRegistration1_DayOfMonth");
	var yearValue = document.getElementById("ctl00_MainContentPlaceHolder_EditRegistration1_YearOfBirth");
	var emailValue = document.getElementById("ctl00_MainContentPlaceHolder_EditRegistration1_Email");
	var confirmEmailValue = document.getElementById("ctl00_MainContentPlaceHolder_EditRegistration1_ConfirmEmail");
	var zipValue = document.getElementById("ctl00_MainContentPlaceHolder_EditRegistration1_Zip");
	var listValue = document.getElementById("ctl00_MainContentPlaceHolder_EditRegistration1_RegionDropDownList");
	var passValue = document.getElementById("ctl00_MainContentPlaceHolder_EditRegistration1_Password");
	var pass2Value = document.getElementById("ctl00_MainContentPlaceHolder_EditRegistration1_ConfirmPassword");
	
	if(!isEditMode())
	{
	    if((monthValue == null || dayValue == null || yearValue == null) ||(validatePassword(passValue) != "" || validateConfirm(passValue, pass2Value, "error") != ""))
	    {
		    if(!bSuppress)
		    {
			    setError("Please enter a valid password and confirm. See the About You tab.");
			    $('#formContainer > ul').tabs('select', 0);
		    }
		    return false; 
	    }
	}else{
	    var passText = $("#ctl00_MainContentPlaceHolder_EditRegistration1_Password").val(); 
	    if(passText != "")
	    {
	        var retVal = validatePassword(passValue);
	        if(retVal == "")
	        {
	            if(validateConfirm(passValue, pass2Value, "error") != "")
	            {
	                setError("Please enter a valid password and confirm. See the About You tab.");
			        $('#formContainer > ul').tabs('select', 0);
    	            return false;
	            }
	        }else{
	            setError(retVal);
			    $('#formContainer > ul').tabs('select', 0);
			    return false;
	        }
	    }
	}
	
	reason += validateEmpty(fName);
	reason += validateEmpty(lName);
	if(monthValue && dayValue && yearValue){
		reason += validateEmpty(monthValue);
		reason += validateEmpty(dayValue);
		reason += validateEmpty(yearValue);
	}
	reason += validateEmail(emailValue);
	reason += validateEmail(confirmEmailValue);
	reason += validateEmpty(zipValue);
	reason += validateEmptyList(listValue);
	
  if (reason != "") {
	if(!bSuppress){
		setError("Please enter a value for all required fields. See the About You tab.");
		$('#formContainer > ul').tabs('select', 0);
	}
    return false;
  }
  
  if(validateConfirm(emailValue, confirmEmailValue) != "")
	{
		if(!bSuppress){
			setError("Please confirm your email address. See the About You tab.");
			$('#formContainer > ul').tabs('select', 0);
		}
		return false; 
	}
  

  return true;
}

function setError(errMessage)
{
	var eRegion = document.getElementById("errorRegion"); 
	eRegion.innerHTML=errMessage; 
	eRegion.style.background = 'yellow';
	eRegion.style.border = '1px dashed';
	eRegion.style.color = 'red';
	eRegion.style.margin = '5 5 5 5';
}

function hideError()
{
    var eRegion = document.getElementById("errorRegion"); 
	eRegion.innerHTML=''; 
	eRegion.style.background = '';
	eRegion.style.border = '';
	eRegion.style.color = '';
	eRegion.style.margin = '';
}

function validatePageTwo(bSuppress){
	var radioGetEmails = document.getElementById("ctl00_MainContentPlaceHolder_EditRegistration1_OptInEmail"); 
	var getViaEmails = document.getElementById("ctl00_MainContentPlaceHolder_EditRegistration1_EmailUpdate"); 
	var getCellEmails = document.getElementById("ctl00_MainContentPlaceHolder_EditRegistration1_CellCheckBox"); 
	var cellEmail = document.getElementById("ctl00_MainContentPlaceHolder_EditRegistration1_MobileEmail"); 

	if(radioGetEmails.checked == true && (!getViaEmails.checked && !getCellEmails.checked))
	{
		if(!bSuppress)
		{
			setError("Please confirm if you would like alerts to your cell phone or email address. See the Get Results tab.");
			$('#formContainer > ul').tabs('select', 1);
		}
		return false;
	}
	
	if(radioGetEmails.checked == true && !isGameCheckboxCheck())
	{
		if(!bSuppress)
		{
			setError("Please indicate what games you would like to recieve. See the Get Results tab.");
			$('#formContainer > ul').tabs('select', 1);
		}
		return false;
	}
	
	if(radioGetEmails.checked == true && getCellEmails.checked && cellEmail.value.length < 1)
	{
		if(!bSuppress)
		{
			setError("Please enter your cell phone email address. See the Get Results tab.");
			$('#formContainer > ul').tabs('select', 1);
		}
		return false;
	}
	return true; 
}

function isGameCheckboxCheck()
{
	var stringVal = "ctl00_MainContentPlaceHolder_EditRegistration1_GameResultsCheckBoxList"; 
	var tempCheckVal = ""; 
	var checkVal; 
	
	for(var i = 0; i < 8; i++)
	{
		tempCheckVal = stringVal + "_" +i;
		if(document.getElementById(tempCheckVal)){
			if(document.getElementById(tempCheckVal).checked == true)
				return true; 
		}else{
			return false; 
		}
	}
	return false; 
}

function validateEmptyList(fld) {
    var error = "";
    if(fld.selectedIndex < 1)
    {
		fld.style.background = 'Yellow'; 
        error = "The required field has not been filled in.\n"
    }
	else {
        fld.style.background = 'White';
    }
    return error;   
}

function validateEmpty(fld) {
    var error = "";
  
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "The required field has not been filled in.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;   
}

function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
} 

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                       
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
    
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              
        fld.style.background = 'Yellow';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "You didn't enter a correct email address.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');     

   if (fld.value == "") {
        error = "You didn't enter a phone number.\n";
        fld.style.background = 'Yellow';
    } else if (isNaN(parseInt(stripped))) {
        error = "The phone number contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else if (!(stripped.length == 10)) {
        error = "The phone number is the wrong length. Make sure you included an area code.\n";
        fld.style.background = 'Yellow';
    } 
    return error;
}

function validatePassword(fld) 
{
    var error = "";
    var illegalChars = /[\W_]/; 
 
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter a password.\n";
    } else if ((fld.value.length < 6) || (fld.value.length > 15)) {
        error = "The password is the wrong length. \n";
        fld.style.background = 'Yellow';
    } else if (illegalChars.test(fld.value)) {
        error = "The password contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else if (!((fld.value.search(/(a-z)+/)) && (fld.value.search(/(0-9)+/)))) {
        error = "The password must contain at least one numeral.\n";
        fld.style.background = 'Yellow';
    } else {
        fld.style.background = 'White';
    }
   return error;
} 

function validateConfirm(fld, fld2, errorStatement)
{
	 var error = "";
	 if(fld.value != fld2.value)
	 {
		fld.style.background = 'Yellow';
		fld2.style.background = 'Yellow';
        error = errorStatement;
	 }
	 return error; 
}