﻿// JScript File

function hidediv(q) {
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(q).style.display = 'none';
	}
	else {
		if (document.layers) { // Netscape 4
			eval("document." + q + ".display = 'none'");
		}
		else { // IE 4
			eval("document.all." + q + ".style.display = 'none'");
		}
	}
}

function showdiv(q) {
	if (document.getElementById) { // DOM3 = IE5, NS6
		document.getElementById(q).style.display = 'inline';
	}
	else {
		if (document.layers) { // Netscape 4
			eval("document." + q + ".display = 'inline'");
		}
		else { // IE 4
			eval("document.all." + q + ".style.display = 'inline'");
		}
	}
}

function changeBorderColor(q, color)
{
    document.getElementById(q).style.border = color;
}

function isZip(s) 
{
     // Check for correct zip code
     reZip = new RegExp(/(^\d{5}$)|(^\d{5}-\d{4}$)/);

     if (!reZip.test(s)) {          
          return false;
     }

     return true;
}

function isEmail(s)
{
    reEmail = new RegExp(/^([A-Za-z0-9!#-\'\*\+\-\/=\?\^_`\{-~]+(\.[A-Za-z0-9!#-\'\*\+\-\/=\?\^_`\{-~]+)*@[A-Za-z0-9!#-\'\*\+\-\/=\?\^_`\{-~]+(\.[A-Za-z0-9!#-\'\*\+\-\/=\?\^_`\{-~]+)*)$|^$/);

    if (!reEmail.test(s)) {          
          return false;
     }

     return true;
}

function isPhone(acID, preID, numID)
{
    var validPhone = true;
    var ac = document.getElementById(acID);
    var pre = document.getElementById(preID);
    var num = document.getElementById(numID);
    
    if(ac.value.length != 3 || isNaN(ac.value)) {
        validPhone = false;
    }
    else if(pre.value.length != 3 || isNaN(pre.value)){
        validPhone = false;
    }
    else if(num.value.length != 4  || isNaN(num.value)){
        validPhone = false;
    }

    return  validPhone;
}

function formValidation()
{
    var isValid = true;

    if(document.getElementById('txtAddress').value == "" || document.getElementById('txtAddress').value == "Example: 123 Main Street")
    {
        changeBorderColor('txtAddress', '1px solid #C62626');
        showdiv('errAddress');
        isValid = false;
    }
    else
    {
        changeBorderColor('txtAddress', '1px solid #CCCCCC');
        hidediv('errAddress');
    }
    
    if(document.getElementById('txtZip').value == "" || document.getElementById('txtZip').value == "ZIP Code" || !isZip(document.getElementById('txtZip').value))
    {
        changeBorderColor('txtZip', '1px solid #C62626');
        showdiv('errZip');
        isValid = false;
    }
    else
    {
        changeBorderColor('txtZip', '1px solid #CCCCCC');
        hidediv('errZip');
    }
    
    if(document.getElementById('txtEmail').value == "" || document.getElementById('txtEmail').value == "Email" || !isEmail(document.getElementById('txtEmail').value))
    {
        changeBorderColor('txtEmail', '1px solid #C62626');
        showdiv('errEmail');
        isValid = false;
    }
    else
    {
        changeBorderColor('txtEmail', '1px solid #CCCCCC');
        hidediv('errEmail');
    }   
    
    if(isPhone('txtPhoneAc', 'txtPhonePre', 'txtPhoneNum'))
    {
        changeBorderColor('txtPhoneAc', '1px solid #CCCCCC');
        changeBorderColor('txtPhonePre', '1px solid #CCCCCC');
        changeBorderColor('txtPhoneNum', '1px solid #CCCCCC');
        hidediv('errPhone');
    }
    else    
    {
        changeBorderColor('txtPhoneAc', '1px solid #C62626');
        changeBorderColor('txtPhonePre', '1px solid #C62626');
        changeBorderColor('txtPhoneNum', '1px solid #C62626');
        showdiv('errPhone');
        isValid = false;
    }
    
        
    return isValid;
}



