//script to auto tab to next feild for Phone enteries


<!-- This script is based on the javascript code of Roman Feldblum (web.developer@programmer.net) -->
<!-- Original script : http://javascript.internet.com/forms/format-phone-number.html -->
<!-- Original script is revised by Eralper Yilmaz (http://www.eralper.com) -->
<!-- Revised script : http://www.kodyaz.com -->

var zChar = new Array(' ', '(', ')', '-', '.');
var maxphonelength = 13;
var phonevalue1;
var phonevalue2;
var cursorposition;

function ParseForNumber1(object){
phonevalue1 = ParseChar(object.value, zChar);
}
function ParseForNumber2(object){
phonevalue2 = ParseChar(object.value, zChar);
}

function backspacerUP(object,e) {
if(e){
e = e
} else {
e = window.event
}
if(e.which){
var keycode = e.which
} else {
var keycode = e.keyCode
}

ParseForNumber1(object)

if(keycode >= 48){
ValidatePhone(object)
}
}

function backspacerDOWN(object,e) {
if(e){
e = e
} else {
e = window.event
}
if(e.which){
var keycode = e.which
} else {
var keycode = e.keyCode
}
ParseForNumber2(object)
}

function GetCursorPosition(){

var t1 = phonevalue1;
var t2 = phonevalue2;
var bool = false
for (i=0; i<t1.length; i++)
{
if (t1.substring(i,1) != t2.substring(i,1)) {
if(!bool) {
cursorposition=i
bool=true
}
}
}
}

function ValidatePhone(object){

var p = phonevalue1

p = p.replace(/[^\d]*/gi,"")

if (p.length < 3) {
object.value=p
} else if(p.length==3){
pp=p;
d4=p.indexOf('(')
d5=p.indexOf(')')
if(d4==-1){
pp="("+pp;
}
if(d5==-1){
pp=pp+")";
}
object.value = pp;
} else if(p.length>3 && p.length < 7){
p ="(" + p;
l30=p.length;
p30=p.substring(0,4);
p30=p30+")"

p31=p.substring(4,l30);
pp=p30+p31;

object.value = pp;

} else if(p.length >= 7){
p ="(" + p;
l30=p.length;
p30=p.substring(0,4);
p30=p30+")"

p31=p.substring(4,l30);
pp=p30+p31;

l40 = pp.length;
p40 = pp.substring(0,8);
p40 = p40 + "-"

p41 = pp.substring(8,l40);
ppp = p40 + p41;

object.value = ppp.substring(0, maxphonelength);
}

GetCursorPosition()

if(cursorposition >= 0){
if (cursorposition == 0) {
cursorposition = 2
} else if (cursorposition <= 2) {
cursorposition = cursorposition + 1
} else if (cursorposition <= 5) {
cursorposition = cursorposition + 2
} else if (cursorposition == 6) {
cursorposition = cursorposition + 2
} else if (cursorposition == 7) {
cursorposition = cursorposition + 4
e1=object.value.indexOf(')')
e2=object.value.indexOf('-')
if (e1>-1 && e2>-1){
if (e2-e1 == 4) {
cursorposition = cursorposition - 1
}
}
} else if (cursorposition < 11) {
cursorposition = cursorposition + 3
} else if (cursorposition == 11) {
cursorposition = cursorposition + 1
} else if (cursorposition >= 12) {
cursorposition = cursorposition
}

var txtRange = object.createTextRange();
txtRange.moveStart( "character", cursorposition);
txtRange.moveEnd( "character", cursorposition - object.value.length);
txtRange.select();
}

}

function ParseChar(sStr, sChar)
{
if (sChar.length == null)
{
zChar = new Array(sChar);
}
else zChar = sChar;

for (i=0; i<zChar.length; i++)
{
sNewStr = "";

var iStart = 0;
var iEnd = sStr.indexOf(sChar[i]);

while (iEnd != -1)
{
sNewStr += sStr.substring(iStart, iEnd);
iStart = iEnd + 1;
iEnd = sStr.indexOf(sChar[i], iStart);
}
sNewStr += sStr.substring(sStr.lastIndexOf(sChar[i]) + 1, sStr.length);

sStr = sNewStr;
}

return sNewStr;
}



// 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;
}
// 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;
}


keytotal1 = 150;
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
}
}
// **********
// List any industry or in-house recognition that the nominee has received between October 2006 and July 2007
keytotal2 = 600;
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
}
}
// **********
// List any charitable or civic-minded organizations that the nominee is involved with
keytotal3 = 150;
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
}
}
// **********
// List any charitable or civic-minded organizations that the nominee is involved with
keytotal4 = 50;
function changed4 (form, mytext4) {
var s = mytext4.name;
var index;
if (s.indexOf('[') == -1) {
index = 0;
} else {
index = s.substring( s.indexOf('[')+1,s.indexOf(']') );
}
checkit4(mytext4,index);
}

function checkit4(mytext4,zindex) {
if (!(zindex >= 0)) {
zindex = '';
}
var s = mytext4.name;
var zindex;
if (s.indexOf('[') == -1) {
zindex = 0;
}
else
{
zindex = s.substring( s.indexOf('[')+1,s.indexOf(']') );
}
if (mytext4.value.length > keytotal4) {
mytext4.value = mytext4.value.substring(0,keytotal4);
}
if (document.getElementById('charCountD'+zindex)) {
document.getElementById('charCountD'+zindex).innerHTML = mytext4.value.length
}
}
// **********



// VALIDATE FORM

function validateform()
{
	formerrors = "";
    valid = true;

	if (document.guide.companyname.value=="")
    {
        formerrors=formerrors + "- Company Name is missing \n";
        valid=false;
    }
	if (document.guide.address1.value=="")
    {
        formerrors=formerrors + "- Address missing \n";
        valid=false;
    }
	if (document.guide.city.value=="")
    {
        formerrors=formerrors + "- City is missing \n";
        valid=false;
    }
	if (document.guide.state.value=="")
    {
        formerrors=formerrors + "- State is missing \n";
        valid=false;
    }
		if (document.guide.zip.value=="")
    {
        formerrors=formerrors + "- Zip is missing \n";
        valid=false;
    }
	    if (document.guide.phone.value=="")
    {
        formerrors=formerrors + "- Phone Number is missing \n";
        valid=false;
    }
		    if (document.guide.fax.value=="")
    {
        formerrors=formerrors + "- Fax Number is missing \n";
        valid=false;
    }

	if (document.guide.website.value=="")
    {
        formerrors=formerrors + "- Website Address is missing \n";
        valid=false;
    }

	if (document.guide.keyExecutive.value=="")
    {
        formerrors=formerrors + "- Key Executive is missing \n";
        valid=false;
    }
	if (document.guide.keyExecutiveTitle.value=="")
    {
        formerrors=formerrors + "- Key Executive Title is missing \n";
        valid=false;
    }
		if (document.guide.servicesProvided.value=="")
    {
        formerrors=formerrors + "- Services Provided is missing \n";
        valid=false;
    }
		if (document.guide.description.value=="")
    {
        formerrors=formerrors + "- Company Description is missing \n";
        valid=false;
    }
		if (document.guide.cname.value=="")
    {
        formerrors=formerrors + "- Submitter Name is missing \n";
        valid=false;
    }


	if (document.guide.ccompany.value=="")
    {
        formerrors=formerrors + "- Submitter Company Name is missing \n";
        valid=false;
    }
    if (document.guide.cphone.value=="")
    {
        formerrors=formerrors + "- Submitter Phone is missing \n";
        valid=false;
    }
	    if (document.guide.cemail.value=="" || valEmail(document.guide.cemail)==null)
    {
        formerrors=formerrors + "- Submitter Email is missing or invalid \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.guide.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;
}

