// JavaScript Document

// js file for validation and form submission.

function validateName(strName){
    return (/^[a-z]+?[a-z]+$/i).test(strName);
}

function validateSchool(strName){
    return (/^[a-z]|[- ]+?[a-z]+$/i).test(strName);
}

function ValidateEmail(strEmailId){
    if ((/^\w+[\+\.\w-]*@([\w-]+\.)*\w+[\w-]*\.([a-z]{2,4}|\d+)$/i).test(strEmailId)) {
        return '';
    }
    else {
        return "Please enter a valid email address";
    }
}

function ValidateNewsLetter(){
    strEmailId = $("#newsemailid").val();
    var error = '';
    error = ValidateEmail(strEmailId);
    if (error == '') {
        if (!$('input[name=signmecheck]').is(':checked')) {
            $("#errors").html("Please check the check box.");
            $("#errors").show();
            return false;
        }
        else {
            $("#errors").hide();
            document.newslettersub.submit();
        }
    }
    else {
        $("#errors").html(error);
        $("#errors").show();
        return false;
    }
}

function ValidateContactUs(){

    strSchool = $('#school').val();
    strMailAdd1 = $('#MailAdd1').val();
    strMailAdd2 = $('#MailAdd2').val();
    strCity = $('#city').val();
    strState = $('#state').val();
    strZipCode = $('#zipcode').val();
    strEmailId = $('#emailid').val();
    strPhone = $('#phone').val();
    strMethodPref = $('#methodpre').val();
    
    
    // full name validation. *Required
    var arrUserName = $.trim($('#fullname').val()).split(" ");
    var i = 0;
    var bUserValue = true;
    if (arrUserName != '') {
        for (i = 0; i < arrUserName.length; i++) {
            if ($.trim(arrUserName[i]) != '') {
                if (!(validateName(arrUserName[i]))) {
                    bUserValue = false;
                }
            }
        }
    }
    else {
        bUserValue = false;
    }
    
    
    if (bUserValue) {   
        // school validation. Optional
        if (strSchool != '') {
            if (!(validateSchool(strSchool))) {
                bUserValue = false;
            }
            else {
                bUserValue = true;
            }
        }
        else {
            bUserValue = true
        }
        
        if (bUserValue) {
            // mailing address *Required.
            if (strMailAdd1 == '') {
                $("#errors").html('Please enter mailing address');
        		$("#errors").show();
				$('#MailAdd1').focus();
        		return false;
            }
            else {
                // city validation *Required.
                if (strCity != '') {					
					var arrCity = $.trim($('#city').val()).split(" ");
					var i = 0;
					var bUserValue = true;
					if (arrCity != '') {
						for (i = 0; i < arrCity.length; i++) {
							if ($.trim(arrCity[i]) != '') {
								if (!(validateName(arrCity[i]))) {
									bUserValue = false;
								}
							}
						}
					}
					else {
						bUserValue = false;
					}
					
                    if ((!bUserValue)) {
					   $("#errors").html('Please enter a valid city name');
						$("#errors").show();
						$('#city').focus();
						return false;
                    }
                    else {
                    
                        // state validation. *Required.
                        if (strState == 'Select...') {
                            $("#errors").html('Please select state');
							$("#errors").show();
							$('#state').focus();
							return false;
                        }
                        else {
                        
                            // Zipcode. *Required.
							if( !checkZip(strZipCode ) ){
                            //if ( ( strZipCode == '' ) || (strZipCode.length < 5) || ( isNaN( strZipCode )) ) {
                               $("#errors").html('Please enter a valid zip code');
								$("#errors").show();
								$('#zipcode').val('');
								$('#zipcode').focus();
								return false;
                            }
                            else {
                            
                                // email *Required.
                                error = ValidateEmail(strEmailId);
                                if (error == '') {                                
                                    
									// phone validation. *Required.
                                    if (!validatePhone( $('#phone').val(), $('#ext').val() )) {
										$("#errors").html('Please enter a valid Phone number (format 123-123-1234) ');
										$("#errors").show();
										$('#phone').focus();
										return false;
                                    }
                                    else {
                                        document.contactus.submit();
                                    }
                                }
								else {
									$("#errors").html('Please enter a valid email address');
									$("#errors").show();
									$('#emailid').focus();
									return false;
								}
                            }
                        }
                    }
                }
                else {
                    $("#errors").html('Please enter city name');
					$("#errors").show();
					$('#city').focus();
					return false;
                }
            }
        }
		else {
			$("#errors").html('Please enter a valid school/District name');
			$("#errors").show();
			$('#school').focus();
			return false;
		}
    }
    else {        
        $("#errors").html('Please enter a valid name');
        $("#errors").show();
		$('#fullname').focus();
        return false;
    }
}

function formatPhone(e){	
	if( e.which != 8 ){
		var iPhn = $('#phone').val();
		if ((iPhn.length == 3) || (iPhn.length == 7)) {
			$('#phone').val(iPhn + '-');
		}
	}
}

function formatZip(e)
{	
	if( e.which != 8 ){		
		var iZip = $('#zipcode').val();
		if (iZip.length == 5) {
			$('#zipcode').val(iZip + '-');
		}
	}
}

function setZip()
{
	var iZip = $('#zipcode').val();
	arrZip = iZip.split("-");
	alert( typeof(arrZip));
	//if( arrZip[1].length == 0){
		//$('#zipcode').val( arrZip[0] );
	//}
}

function checkZip(iZip)
{
	strReturn = false;
	if( iZip.length >= 5 ){
		arrZip = iZip.split("-");		
		for( i=0; i<arrZip.length; i++ )
		{
			if( isNaN(arrZip[i]) )
			{
				strReturn = false;
			}
			else {
				strReturn = true;
			}
		}
	}
	else {
		if( isNaN(iZip) || iZip.length < 5 )
		{
			strReturn = false;
		}
		else {
				strReturn = true;
		}
	}
	return strReturn;
}

function validatePhone( iPhone, iExt ){
	
    var bReturn = true;
    if (iPhone.length == 0) {
        bReturn = false;
    }
    else {
        arrPhone = iPhone.split("-");
        if (arrPhone.length < 3) {
            return false;
        }
        else {
            iPhone = arrPhone[0] + arrPhone[1] + arrPhone[2];
        }
        if ((iPhone.length < 10) || (iPhone.length > 10)) {
            bReturn = false;
        }
        else {
            if (isNaN(iPhone)) {
                bReturn = false;
            }
            else {
                bReturn = true;
            }
        }
    }
	
	
	if( iExt != '' ){
		if( isNaN( iExt ) )
		{
			bReturn = false;
		}
		else {
			bReturn = true;
		}
	}
	
    return bReturn;
}

function closeErrorMsg()
{
	$("#errors").html('');
	$("#errors").hide();
}
