// JavaScript Document

//script to auto tab to next feild for Phone enteries
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;
}

// Courtesy of SimplytheBest.net - http://simplythebest.net/scripts/
// Biggest Deal in 2006/2007
keytotal1 = 375;
function changed1 (form, mytext1) {
var s = mytext1.name;
var index;
if (s.indexOf('[') == -1) {
index = 0;
} else {
index = s.substring( s.indexOf('[')+1,s.indexOf(']') );
}
checkit1(mytext1,index);
}

function checkit1(mytext1,zindex) {
if (!(zindex >= 0)) {
zindex = '';
}
var s = mytext1.name;
var zindex;
if (s.indexOf('[') == -1) {
zindex = 0;
}
else
{
zindex = s.substring( s.indexOf('[')+1,s.indexOf(']') );
}
if (mytext1.value.length > keytotal1) {
mytext1.value = mytext1.value.substring(0,keytotal1);
}
if (document.getElementById('charCountA'+zindex)) {
document.getElementById('charCountA'+zindex).innerHTML = mytext1.value.length
}
}
// **********
// Statement
keytotal2 = 500;
function changed2 (form, mytext2) {
var s = mytext2.name;
var index;
if (s.indexOf('[') == -1) {
index = 0;
} else {
index = s.substring( s.indexOf('[')+1,s.indexOf(']') );
}
checkit2(mytext2,index);
}

function checkit2(mytext2,zindex) {
if (!(zindex >= 0)) {
zindex = '';
}
var s = mytext2.name;
var zindex;
if (s.indexOf('[') == -1) {
zindex = 0;
}
else
{
zindex = s.substring( s.indexOf('[')+1,s.indexOf(']') );
}
if (mytext2.value.length > keytotal2) {
mytext2.value = mytext2.value.substring(0,keytotal2);
}
if (document.getElementById('charCountB'+zindex)) {
document.getElementById('charCountB'+zindex).innerHTML = mytext2.value.length
}
}
// **********
// Why do you qualify...?
keytotal3 = 500;
function changed3 (form, mytext3) {
var s = mytext3.name;
var index;
if (s.indexOf('[') == -1) {
index = 0;
} else {
index = s.substring( s.indexOf('[')+1,s.indexOf(']') );
}
checkit3(mytext3,index);
}

function checkit3(mytext3,zindex) {
if (!(zindex >= 0)) {
zindex = '';
}
var s = mytext3.name;
var zindex;
if (s.indexOf('[') == -1) {
zindex = 0;
}
else
{
zindex = s.substring( s.indexOf('[')+1,s.indexOf(']') );
}
if (mytext3.value.length > keytotal3) {
mytext3.value = mytext3.value.substring(0,keytotal3);
}
if (document.getElementById('charCountC'+zindex)) {
document.getElementById('charCountC'+zindex).innerHTML = mytext3.value.length
}
}
// **********

// VALIDATE FORM

function validateform()
{
	formerrors = "";
    valid = true;

	if (document.allstars.firstname.value=="")
    {
        formerrors=formerrors + "- Nominee First Name is missing \n";
        valid=false;
    }
	if (document.allstars.lastname.value=="")
    {
        formerrors=formerrors + "- Nominee Last Name is missing \n";
        valid=false;
    }
	if (document.allstars.title.value=="")
    {
        formerrors=formerrors + "- Title is missing \n";
        valid=false;
    }
	if (document.allstars.company.value=="")
    {
        formerrors=formerrors + "- Company is missing \n";
        valid=false;
    }
	if (document.allstars.biggestdeal.value=="")
    {
        formerrors=formerrors + "- Biggest Deal in 2008/2009 is missing \n";
        valid=false;
    }
	if (document.allstars.primarycategory.value=="")
    {
        formerrors=formerrors + "- Primary Category is missing \n";
        valid=false;
    }
	if (document.allstars.statement.value=="")
    {
        formerrors=formerrors + "- Statement is missing \n";
        valid=false;
    }
	if (document.allstars.cname.value=="")
    {
        formerrors=formerrors + "- Submitter Contact Name is missing \n";
        valid=false;
    }
	if (document.allstars.ccompany.value=="")
    {
        formerrors=formerrors + "- Submitter Company is missing \n";
        valid=false;
    }
    if (document.allstars.cphone1.value=="" || document.allstars.cphone2.value=="" || document.allstars.cphone3.value=="" )
    {
        formerrors=formerrors + "- Submitter Phone is missing \n";
        valid=false;
    }
    if (document.allstars.cemail.value=="" || valEmail(document.allstars.cemail)==null)
    {
        formerrors=formerrors + "- Submitter Email is missing or invalid \n";
        valid=false;
    }
	if (document.allstars.whydoyouqualify.value=="")
    {
        formerrors=formerrors + "- Why do you or your nominee qualify as a Commercial Brokerage All-Star? is missing \n";
        valid=false;
    }

	if (formerrors!="")
	{
		alert("Please correct the following errors: \n" + formerrors);
	}
    return valid;
}
function valEmail(eml) {
	if (eml.value.indexOf("@")<1) return null;
	else return 1;
}
function valForm()
{
	formerrors2 = "";
    valid = true;
		if (document.allstars.initials.value=="")
    {
        formerrors2=formerrors2 + "- Please put in your initals before submitting \n";
        valid=false;
    }

	if (formerrors2!="")
	{
		alert("Please correct the following errors: \n" + formerrors2);
	}

    return valid;
}
