function SetZipAsterisk(){
    var spanAsterisk = document.getElementById("ctl00_cph1_UCCI_snZipAsterisk");
    
    if (spanAsterisk != null) {
        var state = GetDropdownSelectedValue("ctl00_cph1_UCCI_ddState");
        if (state == 'BM'){
            spanAsterisk.style.visibility = 'hidden';
        }
        else{
            spanAsterisk.style.visibility = 'visible';
        }
    }
}

function Validation(disablelastFirst, validateZip){
	document.getElementById("ctl00_cph2_BtnSave").enabled=false;
	
	var msg='';
	
	if (!disablelastFirst) {msg = ValidateLastFirst();}
	msg += ValidateZip(validateZip) + ValidateEmailFields();
	
	if (msg!=''){
		alert('The following fields are required or need valid data entry: \n'+msg);
		document.getElementById("ctl00_cph2_BtnSave").enabled=true;
		window.scrollTo(0,0);
		return false;
	}	
	return true;	
}

function ValidateZip(validateZip){
	var msg = '';
	
	var lblAddress1 = document.getElementById("ctl00_cph1_UCCI_lblAddress1");
	setInnerText(lblAddress1, '');
	
    var spanAsterisk = document.getElementById("ctl00_cph1_UCCI_snZipAsterisk");
    if (spanAsterisk != null) {
        var lblZip = document.getElementById("ctl00_cph1_UCCI_lblZip");
        setInnerText(lblZip, '');
	    
        var state = GetDropdownSelectedValue("ctl00_cph1_UCCI_ddState");
        if (state != 'BM'){
            var zip = Trim(document.getElementById("ctl00_cph1_UCCI_txtZip").value);
    	    
            if (zip == ''){
                setInnerText(lblZip, 'Required.');
	            msg=msg+'\nZip Required.';
            }			
            else {
                if ((state != '') && (validateZip)){
	                if (!isValidZip(zip)){
	                    setInnerText(lblZip, 'Zip is in an invalid format.');
		                msg=msg+'\nZip is in an invalid format.';	
	                }
	            }
            }
        }
    }
	
	return msg;
}

function ValidateLastFirst(){
	var msg = '';
		
	document.getElementById("ctl00_cph1_UCCI_lblLastname").innerText = '';
	document.getElementById("ctl00_cph1_UCCI_lblFirstname").innerText = '';
	
	if (Trim(document.getElementById("ctl00_cph1_UCCI_txtLastname").value)=='')
	{
		document.getElementById("ctl00_cph1_UCCI_lblLastname").innerText = 'Required';
		msg=msg+'\nLast name required.';
	}
	if (Trim(document.getElementById("ctl00_cph1_UCCI_txtFirstname").value)=='')
	{
		document.getElementById("ctl00_cph1_UCCI_lblFirstname").innerText = 'Required';
		msg=msg+'\nFirst name required.';
	}	
	return msg;
}

function ValidatePhone(){
	var msg = '';
	
	var lblHomePhone = document.getElementById("ctl00_cph1_UCCI_lblHomePhone");
	setInnerText(lblHomePhone, '');
	
	if (document.getElementById("ctl00_cph1_UCCI_txtHomePhone").value=='' & document.getElementById("ctl00_cph1_UCCI_txtWorkPhone").value==''){
	    setInnerText(lblHomePhone, 'One Required.');
		msg=msg+'\nAt least one phone number required.';
	}			
	return msg;
}

function ValidateEmailFields(){
	var msg = '';
	
	var lblEmail = document.getElementById("ctl00_cph1_UCCI_lblEmail"); 
	var lblEmailRepeat = document.getElementById("ctl00_cph1_UCCI_lblEmailRepeat"); 
	var lblEmailCompare = document.getElementById("ctl00_cph1_UCCI_lblEmailCompare"); 
	var lblInvalidEmail = document.getElementById("ctl00_cph1_UCCI_lblInvalidEmail"); 
	
	setInnerText(lblEmail, '');
	setInnerText(lblEmailRepeat, '');
	setInnerText(lblEmailCompare, '');
	setInnerText(lblInvalidEmail, '');

	var txtEmail = document.getElementById("ctl00_cph1_UCCI_txtEmail");
	var email = Trim(txtEmail.value);
	
	if (email==''){
		setInnerText(lblEmail, 'Required.');
		msg=msg+'\nEmail Required.';
	} 
	else {
		if (!isValidEmail(email)){
		    setInnerText(lblInvalidEmail, 'Email is in an invalid format.');
			msg=msg+'\nEmail is in an invalid format.';	
		}
	}
	
	var txtEmailRepeat = document.getElementById("ctl00_cph1_UCCI_txtEmailRepeat");
	if (txtEmailRepeat != null)	{
	    var emailRepeat = Trim(txtEmailRepeat.value);
	    
	    if (emailRepeat==''){
		    setInnerText(lblEmailRepeat, 'Required.');
		    msg=msg+'\nEmail Repeat required.';
	    }

	    if (emailRepeat != email){			
		    setInnerText(lblEmailCompare, 'Email and Repeat email must be the same.');
		    msg=msg+'\nEmail and Repeat email must be the same.';
	    }
    }
    
	return msg;
}

// second block


function LTrim(v){
    return v.replace(/^\s+/,'');
}

function RTrim(v){        
    return v.replace(/\s+$/,'');
}

function Trim(v){			
    return v.replace(/^\s+/,'').replace(/\s+$/,'');                     
}

function GetDropdownSelectedValue(ddID){
    var dd = document.getElementById(ddID);
	var value = '';
	if (dd != null){
        value = dd.options[dd.selectedIndex].value;
    }
    return value;
}

//phone format
function formatUSPhone(input){
    var phonePattern = /^(\d\d\d)(\d\d\d)(\d\d\d\d)$/;
	var phonePatternShort = /^(\d\d\d)(\d\d\d\d)$/;
	var match;
	
	input.value = input.value.replace(/\./g, "");
	input.value = input.value.replace(/\s/g, "");
	input.value = input.value.replace(/\(/g, "");
	input.value = input.value.replace(/\)/g, "");
	input.value = input.value.replace(/\-/g, "");
	input.value = input.value.replace(/\\/g, "");
	input.value = input.value.replace(/\//g, "");
	
	if ((match = phonePattern.exec(input.value))){
		input.value = '(' + match[1] + ') ' + match[2] + '-' + match[3];
	}
	else if ((match = phonePatternShort.exec(input.value))){
		input.value = '(   ) ' + match[1] + '-' + match[2];						
	}
}	

function setInnerText(element, text){
    if (element != null) 
        element.innerText = text;
}

function isValidEmail(input) {
    var valExp = /^[\w-]+(?:\.[\w-]+)*@(?:[\w-]+\.)+[a-zA-Z]{2,7}$/;
	return valExp.test(input);
}

function isValidZip(input) {
    var valExp = /(^\d{5}$)|(^\d{5}-\d{4}$)/;
	return valExp.test(input);
}

jQuery(document).ready(function () {
    var $ = jQuery, errorDisplay = $('#ngp_form_feedback'), form = $('#ngp_form');
    if (!form) {
        return;
    }
    errorDisplay.html('');
    form.bind('submit', function () {
        var data = $(this).serialize();
        errorDisplay.hide().html('');
        $.post('signup.php', data, function (r) {
            if (r.success) {
                errorDisplay.html('<ul><li class="nobulletpoint">Thank you for signing up!</li></ul>');
                errorDisplay.show();
            }
            else {
                var msg = '';
                for (var k in r.errors) {
                    msg += '<li>' + r.errors[k] + '</li>';
                }
                errorDisplay.html('<ul>' + msg + '</ul>');
                errorDisplay.show();

            }

        }, 'json');
    });
});



