﻿
//-------------------------------------------------------------------------------------
//
// Author:	Marc Fogel
// Description:	Contained JavaScript functions specific to Subscribe page on RIMag.com
//------------------------------------------------------------------------------------- 

if (window.addEventListener) {
    //Firefox
    window.addEventListener("load", InitPage, false);
} 
else 
{
    //IE
    window.attachEvent("onload", InitPage);
}

function InitPage() {

    SetRequiredInputFlags();
//    FormType();
    FocusName();
}


//-----------------------------------------------------------------------------------------
// Inserts a red asterisk for all required fields. Note: You must add the custom attribute
// "InputReq='y'" to all label elements used as labels for input fields in the user interface.  
//-----------------------------------------------------------------------------------------
function SetRequiredInputFlags() {

	var aInputFields = document.getElementsByTagName("label");

	var sAsterisk = '<label style="color:red;font-weight:bold">*</label>'
    
	for (var i=0; i<aInputFields.length; i++) {
		var oField = aInputFields[i];
		if (oField.getAttribute("inputreq") != null) {
		var sText = oField.innerHTML;
		oField.innerHTML = "";
		oField.innerHTML = sText + " " + sAsterisk;
		}        
	}
}


//-------------------------------------------------------------------------------------
// Validate user input. Makes sure something is in all required fields.
//-------------------------------------------------------------------------------------
function ValidUserInput() {

//    alert("Reached Validation");

 //   var da = document.all;
 
 	//First Name.
    if ($("FName").value.length == 0) {
        alert("Please enter your first name.");
        $("FName").focus();
        return false;
    }	

 	//Last Name.
    if ($("LName").value.length == 0) {
        alert("Please enter your last name.");
        $("LName").focus();
        return false;
    }	

    //Email.
    if ($("Email").value.length == 0) {
        alert("Please enter your email address.");
       // $("SubscribeEmail").focus();
        return false;
    }

    var apos = $("Email").value.indexOf("@");
    var dotpos = $("Email").value.lastIndexOf(".");

	if ((apos<1) || ((dotpos-apos)<2)) {
		alert("Please enter a valid email address.");
  		//$("email").focus();
  		return false;
    }

    //Confirmation email.
    if ($("ConEmail").value.length == 0) {
        alert("Please confirm your email address.");
        $("ConEmail").focus();
        return false;
    }

    var apos = $("ConEmail").value.indexOf("@");
    var dotpos = $("ConEmail").value.lastIndexOf(".");

    if ((apos < 1) || ((dotpos - apos) < 2)) {
        alert("Please enter a valid confirmation email address.");
        //$("email").focus();
        return false;
    }

    // Make sure email and confirmation email match.
    if ($("ConEmail").value != $("Email").value) {
        alert("The two emails don't match.");
        $("ConEmail").focus();
        return false;
    }
    
    
    
 	//Company Name
    if ($("CompanyName").value.length == 0) {
        alert("Please enter your Company name.");
        $("CompanyName").focus();
        return false;
    }

    //Title.
    if ($("JobTitle").value.length == 0) {
        alert("Please enter your job title.");
        $("JobTitle").focus();
        return false;
    }
   
    //Business Type.
    if ($("BusinessType").value == "a") {
        alert("Please select your Business Type.");
        $("BusinessType").focus();
        return false;
    }

    //Business Type of Other.
    if (($("BusinessType").value == "14") & ($("BusTypeOther").value.length == 0)) {
        alert("Please tell us what type of establishment you have.");
        $("BusTypeOther").focus();
        return false;
    }
    
    //Success.
    return true;

}


//-------------------------------------------------------------------------
// Code to only allow numbers in the phone field
//-------------------------------------------------------------------------
function KeyPhoneNumberOnly(evt) {

	var charCode = (evt.which) ? evt.which : event.keyCode;
//	alert("charcode " + charCode + ", evt.which = " + evt.which);
    if ((charCode >= 48 && charCode <= 57)) {
        //Allow 0-9    
    }
    else if ((charCode == 45) || (charCode == 40) || (charCode == 41)) {
        // allow hyphen, left and right parenthesis
    }
    else {
        //Not valid.
        return false;
    }
    
    return true;    
}


//---------------------------------------------------------------------------------------
// Used to discourage SPAM by redirecting to "real" verify page.
//---------------------------------------------------------------------------------------

    var Chunk1 = "http://prefctr.na.epidm.net/Restur";
    var Chunk2 = "antsInstitutions/eNewsletter.aspx?cmd=miniformSubmit";
    var FormName = "RISubscribe";
    var Clicked = 0;
    function CL() {
        Clicked++;
        if (Clicked > 1) { return; }
        eval("document." + FormName + ".action='" + Chunk1 + Chunk2 + "'");
    }


//---------------------------------------------------------------------------------------
// Perform input validations, set the appropriate submit action, and then submit
// input data to server.
//---------------------------------------------------------------------------------------

function DoSubmit() {

	if (!ValidUserInput()) return;

	document.forms["RISubscribe"].submit(), clearForms();

}


//-----------------------------------------------------------------------------------------
// clears the form after submittal, so info is gone if user clicks on the "back button.
//-----------------------------------------------------------------------------------------
function clearForms() {
    var i;
    for (i = 0; (i < document.forms.length); i++) {
        document.forms[i].reset();
    }
}


//-----------------------------------------------------------------------------------------
// Puts focus on First Name box.
//-----------------------------------------------------------------------------------------
function FocusName() {
    $("FName").focus();
}

function $(sName) {

	return document.getElementById(sName);
}
