// JScript File
// Everything in this file pertains to the "Communicating with You" section

function addLoadListener(fn)
{
	if (typeof window.addEventListener != 'undefined')
	{
			window.addEventListener('load', fn, false);
	}
	else if (typeof document.addEventListener != 'undefined')
	{
			document.addEventListener('load', fn, false);
	}
	else if (typeof window.attachEvent != 'undefined')
	{
			window.attachEvent('onload', fn);
	}
	else
	{
		var oldfn = window.onload;
		if (typeof window.onload != 'function')
		{
			window.onload = fn;
		}
		else
		{
			window.onload = function()
			{
				oldfn();
				fn();
			};
		}
	}
}	

addLoadListener(PageInitialize);

//function open_url(url){
//	if (window.opener==null) {
//		window.location = url;
//	} else {
//		window.opener.location = url;
//		window.opener.focus();
//	}
//}

function checkIt()
{
	var TB = document.getElementById("txtOtherSocialNetwork")
	if (document.getElementById("ddlHowHear").value == "Other Social Network")
	{
		TB.style.visibility = "visible";
		TB.focus();
	}
	else
	{
		TB.style.visibility = "hidden";
		TB.value="";
	}
	
}

function PageInitialize()
{
	var mCountry = document.getElementById("ddlCountries").value;
	var txtCellPhone = document.getElementById("txtCellPhone");
    var sEmail = document.getElementById("txtEmail").value;
    var txtEmail = document.getElementById("txtEmail");
    var chkViaTextMessaging = document.getElementById("chkViaTextMessaging");
    var chkViaEmail = document.getElementById("chkViaEmail");
    var chkReminderNo =  document.getElementById("chkReminderNo");
   	var chkReminderNo =  document.getElementById("chkReminderNo");
   	var ddlHowHear = document.getElementById("ddlHowHear");
    var NoEmailLink = document.getElementById("lnkNoEmail");
    var ddlCountry = document.getElementById("ddlCountries");
    var txtCommunicationCellPhone = document.getElementById("txtCommunicationCellPhone");
    var txtCommunicationEmail = document.getElementById("txtCommunicationEmail");
	
	if(txtCellPhone.value.length > 0)
	{
	    if(mCountry === "United States" || mCountry == "Canada")
	    {
	        if(ValidUSPhoneNumber(txtCellPhone.value))
	        {
	            txtCommunicationCellPhone.value = txtCellPhone.value;
	        }
	        else
	        {
	            txtCommunicationCellPhone.value = "";
	            chkViaTextMessaging.checked = false;
	        }
	    }
	    else
	    {
	        if(txtCellPhone.value.length >= 10)
	        {
	             txtCommunicationCellPhone.value = txtCellPhone.value;
	        }
	        else
	        {
	             txtCommunicationCellPhone.value = "";
	             chkViaTextMessaging.checked = false;
	        }
	    }
	}
	else
	{
        txtCommunicationCellPhone.value = "";
        chkViaTextMessaging.checked = false;
	}
   
	if(sEmail.length > 0 && ValidEmail(sEmail))
	{
		txtCommunicationEmail.value = txtEmail.value;
	}

		
	txtCellPhone.onblur = function()
	{
	   txtCommunicationCellPhone.value = this.value;
	   verifyTextMessaging(mCountry);  
	}
	    
	txtEmail.onblur = function()
	{
	    if(ValidEmail(this.value))
	    {
	        txtCommunicationEmail.value = this.value;
	    }
	    else
	    {
	        txtCommunicationEmail.value = "";
	        chkViaEmail.checked = false;
	    }
	}
	
	
	chkViaTextMessaging.onclick = function()
	{
		verifyTextMessaging(mCountry);
	}
	
	
	chkViaEmail.onclick = function()
	{
		var sEmail2 = document.getElementById("txtEmail").value;
		if(sEmail2.length == 0 || !ValidEmail(sEmail2))
		{
		    chkViaEmail.checked = false;
		    alert("Please enter a valid Email address.")
		    txtEmail.focus();
		    return;
		}
	}
	
	
	chkReminderNo.onclick = function()
	{
		chkReminderNoChecked();
	}
	
    
    if(chkReminderNo.checked)
    {
        chkReminderNoChecked();
    }


    ddlHowHear.onchange = function()
	{
		checkIt();
	}
  	
    ddlCountry.onchange = function()
    {
	    mCountry = ddlCountry.value;
	    verifyTextMessaging(mCountry);
    }
}

function ValidEmail(strEmailIn)
{
	// Use regular expressions to validate if proper email address
	var EmailRE = /^([\w-]+\.?)*\w+@([\da-zA-z-]+\.)+[a-zA-z]{2,6}$/;
	return EmailRE.test(strEmailIn);
}

function ValidUSPhoneNumber(phoneNumber)
{
    var phoneNumRE = /^[01]?[- .]?(\([2-9]\d{2}\)|[2-9]\d{2})[- .]?\d{3}[- .]?\d{4}$/;
    return phoneNumRE.test(phoneNumber);
}

function verifyTextMessaging(sCountry)
{
	var txtCellPhone = document.getElementById("txtCellPhone");
    	var sCellPhone = txtCellPhone.value;
	var chkViaTextMsg = document.getElementById("chkViaTextMessaging");
	if(chkViaTextMsg.checked == true)
	{
	    if(sCountry == "United States" || sCountry == "Canada")
	    {
		    if(sCellPhone.length == 0 || !ValidUSPhoneNumber(sCellPhone))
		    {
		        chkViaTextMsg.checked = false;
		        alert("Please enter a valid cell phone number.")
		        txtCellPhone.focus();
		        return;
		    }
	    }
	    else
	    {
		    if(sCellPhone.length < 10)
		    {
		        chkViaTextMsg.checked = false;
		        alert("Please enter a valid cell phone number.")
		        txtCellPhone.focus();
		        return;
		    }
	    }
	}
}

function chkReminderNoChecked()
{
	if(document.getElementById("chkReminderNo").checked == true)
	{
		document.getElementById("chkReminderEmail").disabled = true;
		document.getElementById("chkReminderText").disabled = true;
		document.getElementById("chkReminderPostcard").disabled = true;
	    document.getElementById("txtReminderEmail").disabled = true;
		document.getElementById("txtReminderText").disabled = true;
		
	    document.getElementById("chkReminderEmail").checked = false;
		document.getElementById("chkReminderText").checked = false;
		document.getElementById("chkReminderPostcard").checked = false;
	    document.getElementById("txtReminderEmail").value = "";
		document.getElementById("txtReminderText").value = "";
	}
	else
	{
		document.getElementById("chkReminderEmail").disabled = false;
		document.getElementById("chkReminderText").disabled = false;
		document.getElementById("chkReminderPostcard").disabled = false;
		document.getElementById("txtReminderEmail").disabled = false;
		document.getElementById("txtReminderText").disabled = false;
	}
}
