// JavaScript Document

var errorField = '';

function setErrorField (str) {
	if (!errorField) { errorField = str; }
}

function checkField(strField, strMessage) {
	if (!getObj(strField).value) {
		setErrorField(strField);
		return strMessage; 
		}
	else { return ''; }
}

function validateForm () {
	var bError = false;
	var strError = "";
	var radioObj;
	var formObj = getObj("frmSave");
	var strErrorComment = "*Required information:\n\n";

	strError += checkField("txtFirstname", "- Missing First Name\n");
	strError += checkField("txtLastname", "- Missing Last Name\n");
	strError += checkField("txtJobTitle", "- Missing Job Title\n");
	strError += checkField("txtOrganizationName", "- Missing Organization Name\n");
	strError += checkField("txtOrganizationAddress1", "- Missing Organization Address\n");
	strError += checkField("txtCity", "- Missing City\n");
	strError += checkField("txtState", "- Missing State/Province/County\n");
	strError += checkField("txtZipCode", "- Missing Zip/Postal Code\n");
	strError += checkField("txtCountry", "- Missing Country\n");
	strError += checkField("txtPhone", "- Missing Telephone\n");


	var strEmailErr = checkField("txtEmail", "- Missing Email\n");
	if (strEmailErr == "")
	{	
		if(!isValidEmail(getObj("txtEmail").value)) { strError += "- Invalid e-mail address. Please enter again.\n"; setErrorField("txtEmail"); bError = true; }
	}
	else{
		strError += strEmailErr;
	}


	strError += checkField("txtCompanyURL", "- Missing Company/Organization URL\n");
	strError += checkField("txtAnnualRevenue", "- Missing Annual Revenue\n");

	if (getObj("txtAssistantsEmail").value != "") {
		if (!isValidEmail(getObj("txtAssistantsEmail").value)) { setErrorField("txtAssistantsEmail"); strError += "- Missing Your Assistant's E-Mail\n"; bError = true; }
		}

	strError += checkField("txtAnnualBudgetIT", "- Missing Annual IT Budget\n");

	radioObj = formObj.elements['rblBudgetApplyTo'];
	if (!(radioObj[0].checked || radioObj[1].checked)) { setErrorField("rblBudgetApplyTo"); strError += "- Missing Your Responsibilities Type\n"; }

	strError += checkField("txtNumberEmployees", "- Missing Number of Employees\n");
	strError += checkField("txtYearsExperience", "- Missing Years of Experience\n");
	strError += checkField("txtToWhomReport", "- Missing To Whom Do You Report\n");

	radioObj = formObj.elements['rblHowYouHear'];
	if (!(radioObj[0].checked || radioObj[1].checked || radioObj[2].checked || radioObj[3].checked || radioObj[4].checked)) {
		strError += "- Missing How you heard about Gartner CIO Summit\n"; setErrorField("rblHowYouHear");
		}

	radioObj = formObj.elements['rblExpMember'];
	if (!(radioObj[0].checked || radioObj[1].checked)) { 
		strError += "- Missing are you a Gartner EXP Member\n"; setErrorField("rblExpMember");
		}

	if (strError) { getObj(errorField).focus(); errorField=''; alert( strErrorComment + strError); }

	return !(strError);
}

function getObj(name) {
	if (document.getElementById) { return document.getElementById(name); }
	else if (document.all) { return document.all[name]; }
	else if (document.layers) { return document.layers[name]; }
	else { return null; }
}
	
function isValidEmail(email, required) {
    if (required==undefined) {   // if not specified, assume it's required
        required=true;
    }
    if (email==null) {
        if (required) {
            return false;
        }
        return true;
    }
    if (email.length==0) {  
        if (required) {
            return false;
        }
        return true;
    }
    if (! allValidChars(email)) {  // check to make sure all characters are valid
        return false;
    }
    if (email.indexOf("@") < 1) { //  must contain @, and it must not be the first character
        return false;
    } else if (email.lastIndexOf(".") <= email.indexOf("@")) {  // last dot must be after the @
        return false;
    } else if (email.indexOf("@") == email.length) {  // @ must not be the last character
        return false;
    } else if (email.indexOf("..") >=0) { // two periods in a row is not valid
	return false;
    } else if (email.indexOf(".") == email.length) {  // . must not be the last character
	return false;
    }
    return true;
}

function allValidChars(email) {
  var parsed = true;
  var validchars = "abcdefghijklmnopqrstuvwxyz0123456789@.-_";
  for (var i=0; i < email.length; i++) {
    var letter = email.charAt(i).toLowerCase();
    if (validchars.indexOf(letter) != -1)
      continue;
    parsed = false;
    break;
  }
  return parsed;
}