
nameRe  = /^[^0-9\`\~\!\@\#\$\%\^\&\*\(\)_\+\=\{\[\}\]\|\;\:\<,>?\\/"]+$/;
phoneRe = /\([0-9]{3}\) [0-9]{3}-[0-9]{4}/;
emailRe = /^([0-9a-zA-Z]+([_.-]?[0-9a-zA-Z]+)*@[0-9a-zA-Z]+[0-9,a-z,A-Z,.,-]*(.){1}[a-zA-Z]{2,4})+$/;
zipRe   = /^[0-9]{5}(-[0-9]{4})?$/;

pleaseProvide = "Please provide your {0}.";
pleaseEnterOnly   = "Please enter only letters, spaces, dashes(-) and apostrophes(\') for your {0}.";
pleaseEnter = "Please enter {0}";
pleaseProvideMsg = "Please provide {0}";
lineBreak   = "<br/><br/>";
phoneNumber = "({0}) {1}-{2}";

document.onkeypress = checkKey;

function checkKey(keyStroke)
{
    if(window.event) {
        myKey = event.keyCode; 
    }
    else if(keyStroke.which) {
        myKey = keyStroke.which; 
    }
    if (isSubmitKey(myKey))  
    {                        
	submitPage(); 
        return false;
    }
    return true;  
}    

    
    // Returns true if the specified key code should cause
//	a submit operation
function isSubmitKey(key) {
	// 13 is 'return'/'enter'; 3 is numeric
	//	keypad Enter key on Mac/Safari
	if (13 == key || 3 == key) {
		return true;
	}	
	return false;
}


function submitPage(comp, message_id) 
{   
    document[comp].message_id.value = message_id;
    document[comp].submit(); 
}


function checkFields(comp)
{
    if (document[comp].procedure_location.length == 1)
    {
        document[comp].procedure_location.disabled = true;
    }
    
}

function setLabel(labelStr)
{
    var labelObj = document.getElementById("label");
    if (labelObj != null)
    {    
        labelObj.innerHTML = labelStr;
    }
}

function checkContactInfo(){
    var warningMessages = "";
    
    var firstName = document.forms.GetEmmi.first_name.value;
    if (firstName.length == 0){
	warningMessages = warningMessages + pleaseProvide.format("first name")  + lineBreak;
    }else if (!firstName.match( nameRe )){
	warningMessages = warningMessages + pleaseEnterOnly.format("first name") + lineBreak;
    }
    
    var lastName = document.forms.GetEmmi.last_name.value;
    if (lastName.length == 0){
	warningMessages = warningMessages + pleaseProvide.format("last name")  + lineBreak;
    }else if (!lastName.match( nameRe )){
	warningMessages = warningMessages + pleaseEnterOnly.format("last name") + lineBreak;
    }

    var title = document.forms.GetEmmi.title.value; 
    if (title.length == 0 ){
	warningMessages = warningMessages + pleaseProvide.format("title") + lineBreak;
    }
    
    var phone1 = document.forms.GetEmmi.primaryPhoneAC.value;
    var phone2 = document.forms.GetEmmi.primaryPhonePrefix.value;
    var phone3 = document.forms.GetEmmi.primaryPhoneNumber.value;
    var ext    = document.forms.GetEmmi.primaryPhoneExt.value;
    var validExt = true;
    
    if( ext.length > 0 ){
	if( !ext.match(/[0-9]+/) ){
	    validExt = false;
	}
    }

    if (phone1.length == 0 || phone2.length == 0 || phone3.length == 0){
	warningMessages = warningMessages + pleaseProvide.format("phone") + lineBreak;
    }
    else{
	var phone = phoneNumber.format(phone1, phone2, phone3);

	if(!phone.match( phoneRe )){
	    warningMessages = warningMessages +  pleaseProvideMsg.format("a valid phone number.") + lineBreak;
	}else if(validExt){
	    if (ext.length > 0){
		document.forms.GetEmmi.phone.value = phone + " X" + ext;
	    }else{
		document.forms.GetEmmi.phone.value = phone;
	    }
	}
	
    }

    if (!validExt){
	warningMessages = warningMessages + "The phone number extension should contain only numbers." + lineBreak;
    } 
    
    var email = document.forms.GetEmmi.email.value;
    if ( email.length == 0 ){
	warningMessages = warningMessages + pleaseProvide.format("email") + lineBreak;
    }else if (!email.match( emailRe )){
	warningMessages = warningMessages + pleaseProvideMsg.format("a valid email address.") + lineBreak; 
    }

    return warningMessages;   
}

function checkOrgInfo(){
    var warningMessages = "";
    
    var company = document.forms.GetEmmi.company.value;
    if (company.length == 0){
	warningMessages = warningMessages + pleaseProvideMsg.format("the name of the Organization.") + lineBreak;
    }
    
    var address1 = document.forms.GetEmmi.addr1.value;
    var address2 = document.forms.GetEmmi.addr2.value;
    var city     = document.forms.GetEmmi.city.value;
    var state    = document.forms.GetEmmi.state.value;
    var zip      = document.forms.GetEmmi.zip.value;
    
    if (address1.length > 0 || address2.length > 0 || city.length > 0 || state.length > 0 || zip.length > 0){
	if (address1.length == 0){
	    warningMessages = warningMessages + pleaseProvideMsg.format("the address of the Organization.") + lineBreak;
	}else{
	    document.forms.GetEmmi.street.value = address1 + "\n" + address2;
	}

	if(city.length == 0){
	    warningMessages = warningMessages + pleaseProvideMsg.format("the city of the Organization.") + lineBreak; 
	}

	if(state.length == 0){
	    warningMessages = warningMessages + "Please select the Organization\'s state." + lineBreak;
	}
	
	if(zip.length == 0){
	    warningMessages = warningMessages + pleaseProvideMsg.format("the Organization\'s zip code.") + lineBreak;
	}else if (!zip.match( zipRe )){
	    warningMessages = warningMessages + "Please enter a valid zip code." + lineBreak;
	}
    }

    return warningMessages;
 
}

function submitPage(){
    var contactInfoWarnings = checkContactInfo();
    var orgInfoWarnings     = checkOrgInfo();
    
    var ok = true;
    if(contactInfoWarnings != ""){
	ok = false;
	document.getElementById("contactInfoBracket").innerHTML = "<img src=\"img/bracket150.gif\" height=\"150\" width=\"20\" />";
	document.getElementById("contactInfoMsg").innerHTML     = "<span class=\"copyRed\"><i>" + contactInfoWarnings + "</i></span>";
    }else{
	document.getElementById("contactInfoBracket").innerHTML = "<img src=\"img/spacer.gif\" height=\"150\" width=\"20\" />";
	document.getElementById("contactInfoMsg").innerHTML     = "<div align=\"right\"><p align=\"left\"><img src=\"img/silo1.gif\" width=\"78\" height=\"130\"><br></p></div>";
    }

    if(orgInfoWarnings != ""){
	ok = false;
	document.getElementById("orgInfoBracket").innerHTML = "<img src=\"img/bracket180.gif\" height=\"180\" width=\"20\" />";
	document.getElementById("orgInfoMsg").innerHTML     = "<span class=\"copyRed\"><i>" + orgInfoWarnings + "</i></span>";
    }else{
	document.getElementById("orgInfoBracket").innerHTML = "<img src=\"img/spacer.gif\" height=\"180\" width=\"20\" />";
	document.getElementById("orgInfoMsg").innerHTML     = "<div align=\"right\"><img src=\"img/silo2.gif\" width=\"86\" height=\"140\"></div>";

    }
    
    if (ok){
	document.forms.GetEmmi.submit();
    }
}
