var ns4 = document.layers;
var ie = document.all;
var ns6 = document.getElementById && !document.all;
var alertboxmessage = "";
var frm;


function Question(type,name,divid,pattern,message)
{
        this.type=type;
        this.name=name;
        this.divid=divid;
        this.pattern=pattern;
        this.message=message;
}

function getElement(id)
{
        if (ie)
        {
                obj = document.all[id];
        }
                else if (ns6)
        {
                obj = document.getElementById(id);
        }

        if (!obj) 
        {
                return null;
        }
        else
        {
                return obj;
        }
}

function getValue(q)
{
        //check if form element is valid

        if (q.type=="text")
        {
                return frm.elements[q.name].value;
        }

        if (q.type=="select")
        {
                var pickedindex=frm.elements[q.name].selectedIndex;
                return frm.elements[q.name].options[pickedindex].value;
        }
}


function processForm()
{
	//add other_application
	if (frm.elements['other_application'].value!=""){

		var otherapp = "Other: " + frm.elements['other_application'].value;
		// append to existing checked element
		var found=false;

		for (var i=0;i<frm.elements['00N30000001Goqi'].length;++i)
		{
			if (frm.elements['00N30000001Goqi'][i].checked)
			{
				frm.elements['00N30000001Goqi'][i].value=frm.elements['00N30000001Goqi'][i].value+"; " + otherapp;
				found=true;
			}
		}
		// if nothing selected
		if (found==false)
		{
			frm.elements['00N30000001Goqi'][0].value = otherapp;
			frm.elements['00N30000001Goqi'][0].checked = true;
		}
	}

	// Get selected country
	var countryField=document.forms['contact'].elements["00N30000001UlfP"];
	var selectedCountry=countryField.options[countryField.selectedIndex].value;

	//Set region field
	var region=getRegion(selectedCountry);
	document.forms['contact'].elements['00N30000001V1vY'].value=region;
	
	//populate OURL
	frm.elements['00N30000001Yh27'].value=document.referrer;
	
}

function validateForm(FormName)
{
var validform=true;
frm=document.forms[FormName];

//define question parameters
//question type, field name, field error div id, regular expression, alertbox message

var questions = new Array();
questions[0]=new Question('text','first_name','first_name_error','[\\S]+','Please enter your first name.');
questions[1]=new Question('text','last_name','last_name_error','[\\S]+','Please enter your last name.');
questions[2]=new Question('text','company','company_error','[\\S]+','Please enter your company.');
questions[3]=new Question('text','email','email_error','^\\w+([\\.-]?\\w+)*@\\w+([\\.-]?\\w+)*\\.(\\w{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$','Please enter a valid e-mail address.');
questions[4]=new Question('select','00N30000001UlfP','country_error','[\\S]+','Please select your country.');
questions[5]=new Question('text','phone','phone_error','\\d.*\\d.*\\d.*\\d.*\\d.*\\d.*\\d.*\\d.*\\d','Please enter your phone number.');
questions[6]=new Question('text','description','description_error','[\\S]+','Please enter your question or need.');
questions[7]=new Question('select','00N30000001Goqh','subject_error','[\\S]+','Please select the subject of your question.');


//hide previous warnings
alertboxmessage="";
for (var x=0;x<questions.length;++x)
{
        hideWarning(questions[x].divid);
}

//loop through question array
for (var x=0;x<questions.length;++x)
{
        if (checkValue(questions[x])==false)
        {
                validform=false;
        }
}


if (validform==true)
{
	processForm();
	return validform;
}
else if (ns4)
{
        alert(alertboxmessage);
        return validform;
}
else
{
        showError("submit_error","");
        self.scroll(0,200);
        return validform;
}

}

function checkValue(qst)
{
        var RegEx = new RegExp(qst.pattern);

        if (RegEx.test(getValue(qst)))
        {
                return true;
        }
        else
        {
                showError(qst.divid,qst.message);
                return false;
        }
        return true;
}


function showError(id,message)
{
        alertboxmessage=alertboxmessage +"\n" + message;
        obj=getElement(id);
        if (!obj) 
        {
                return false;
        }
        else
        {
                obj.style.display = "inline";
                return true;
        }
}


function hideWarning(id)
{
        obj=getElement(id);
        if (!obj)
        {
                return false;
        }
        else
        {
                obj.style.display = "none";
                return true;
        }
}

