$(document).ready(function() {
    //jQuery("#success-section,#error-section,#pending-section").click(function(){jQuery.unblockUI();});

    $('#lead-form').ajaxForm({
        beforeSubmit:  sendRequest,
        success : sendStatus
    });
    //callTrackingFunctions("ums_form");
    $("#program-select").change(function(){
        if (validateField(this)) {
            var overwrite = "ums_program";
            callTrackingFunctions(overwrite);
        }
    });
    $("#age-fld").change(function(){
        if (validateField(this)) {
        var overwrite = "ums_age";
        callTrackingFunctions(overwrite);
        }
    });
    $("#first-name").blur(function(){
        if (validateField(this)) {
            var overwrite = "ums_first_name";
            callTrackingFunctions(overwrite);
        }
    });
    $("#last-name").blur(function(){
        if (validateField(this)) {
            var overwrite = "ums_last_name";
            callTrackingFunctions(overwrite);
        }
    });
    $("#email-fld").blur(function(){
        if (validateField(this)) {
            var overwrite = "ums_email";
            callTrackingFunctions(overwrite);
        }
    });
    $("#telephone-fld").blur(function(){
        if (validateField(this)) {
            var overwrite = "ums_telephone";
            callTrackingFunctions(overwrite);
        }
    });
    $("#address1-fld").blur(function(){
        if (validateField(this)) {
            var overwrite = "ums_telephone";
            callTrackingFunctions(overwrite);
        }
    });
    $("#city-fld").blur(function(){
        if (validateField(this)) {
            var overwrite = "ums_telephone";
            callTrackingFunctions(overwrite);
        }
    });
    $("#state-fld").change(function(){
        if (validateField(this)) {
            var overwrite = "ums_program";
            callTrackingFunctions(overwrite);
        }
    });
    $("#zip-fld").blur(function(){
        if (validateField(this)) {
            var overwrite = "ums_postal_code";
            callTrackingFunctions(overwrite);
        }
    });
    $("#country-fld").change(function(){
        if (validateField(this)) {
            var overwrite = "ums_program";
            callTrackingFunctions(overwrite);
        }
    });
});
    
function sendRequest(formData, jqForm, options) {
    var queryString = $('#lead-form').formSerialize(); 
    if (validateForm($('#lead-form')[0])) {
        blockMe();
    	$.post("/process.php", queryString, function(data, textStatus){sendStatus(data, textStatus);}); 
    }
    return false; 
}
function blockMe() {
    jQuery("#lead-form").slideUp(400);
    $("#notify-section").html('<p>Processing data, please wait...</p>').slideDown(200);
//    $.blockUI({
//        message: "<p style='font-size:14px;'>Please Wait...</p>",
//        overlayCSS: {
//                backgroundColor: '#000',
//                opacity: '0.6'
//        },
//        css: {
//                border: '4px solid #484644',
//                cursor: 'hand',
//                padding: '15px',
//                backgroundColor: '#fff',
//                '-webkit-border-radius': '8px',
//                '-moz-border-radius': '8px',
//                color: '#0c0500'
//        }
//    });

//    return true;
      
}
function sendStatus(responseText, statusText, xhr, frm) {
	var returnStatus;
	var htmlContent;
	var parameterString;
	var overwrite;
    
    overwrite = "ums_"+responseText;
	callTrackingFunctions(overwrite);
    thankMe(responseText);
            
	return false;
}
function callTrackingFunctions(ums_overwrite) {
    if (ums_overwrite.indexOf("user_errors") > -1) {
//        jQuery.blockUI({
//            message: jQuery("#error-section").html('Please fill out the form. Thank you.'),
//            overlayCSS: {
//                    backgroundColor: '#000',
//                    opacity: '0.6'
//            },
//            css: {
//                    border: '4px solid #484644',
//                    cursor: 'hand',
//                    padding: '15px',
//                    backgroundColor: '#fff',
//                    '-webkit-border-radius': '8px',
//                    '-moz-border-radius': '8px',
//                    color: '#0c0500'
//                    }
//            });
    }
    
    if (ums_overwrite.indexOf("success") > 0) {
        window.location = 'http://' + window.location.hostname + '/thanks.php';
        
    }else {
        if (typeof ums_overwrite == 'undefined')
        {
                ums_overwrite ="";
        }
        umt_acct="UA-24766196-1";
        umtLog(ums_overwrite);
        var pageTracker = _gat._getTracker(umt_acct);
        pageTracker._initData();
        pageTracker._trackPageview(ums_overwrite);
    }
}
function thankMe(message) {
    jQuery("#lead-form").slideUp(400);
    
	if(message.indexOf("success") > -1) {
        $("#notify-section").html('<h2>Thank You For Submitting Your Information.</h2><p>An Admissions representative will contact you shortly.</p>').slideDown(200);
//		jQuery.blockUI({
//            message: jQuery("#success-section"),
//            overlayCSS: {
//                    backgroundColor: '#000',
//                    opacity: '0.6'
//            },
//            css: {
//                    border: '4px solid #484644',
//                    cursor: 'hand',
//                    padding: '15px',
//                    backgroundColor: '#fff',
//                    '-webkit-border-radius': '8px',
//                    '-moz-border-radius': '8px',
//                    color: '#0c0500'
//                }
//        });
	} else if (message.indexOf("error") > -1) {
        $("#notify-section").html('<h2>Error encountered.</h2><p>We have encountered an error while saving your data. Please try again later.</p>').slideDown(200);
//		jQuery.blockUI({
//            message: jQuery("#error-section"),
//            overlayCSS: {
//                    backgroundColor: '#000',
//                    opacity: '0.6'
//            },
//            css: {
//                    border: '4px solid #484644',
//                    cursor: 'hand',
//                    padding: '15px',
//                    backgroundColor: '#fff',
//                    '-webkit-border-radius': '8px',
//                    '-moz-border-radius': '8px',
//                    color: '#0c0500'
//                    }
//            });
	} else {
        jQuery("#pending-section").slideDown(200);
//		jQuery.blockUI({
//            message: jQuery("#pending-section"),
//            overlayCSS: {
//                    backgroundColor: '#000',
//                    opacity: '0.6'
//            },
//            css: {
//                    border: '4px solid #484644',
//                    cursor: 'hand',
//                    padding: '15px',
//                    backgroundColor: '#fff',
//                    '-webkit-border-radius': '8px',
//                    '-moz-border-radius': '8px',
//                    color: '#0c0500'
//                    }
//            });
	}
}
function validateForm(f) {
    var errors = 0, match, el;
    for (var j=0; j<f.elements.length; j++) {
        el = f.elements[j];
        $("#"+el.name+'_error').html('').slideUp(300);
    }
    for (var i=0; i<f.elements.length; i++) {
        el = f.elements[i];
        switch (el.name) {
            case 'program':
                if (!el.options[el.options.selectedIndex].value) {
                    ++errors;
                    $("#program-error").html('Please select Program Type.').slideDown(300);
                }
            break;

            case 'age':
                if (!el.options[el.options.selectedIndex].value) {
                    ++errors;
                    $("#age-error").html('Please select Age.').slideDown(300);
                }
            break;

            case 'first_name':
                if (!el.value) {
                    ++errors;
                    $("#first-name-error").html('Please type in your first name.').slideDown(300);
                } else {
                    match = /[^A-Za-z .\-]/g.test(el.value);
                    if (match) {
                        ++errors;
                        $("#first-name-error").html('Invalid characters in field First Name.').slideDown(300);
                    }
                }
            break;

            case 'last_name':
                if (!el.value) {
                    ++errors;
                    $("#last-name-error").html('Please type in your last name.').slideDown(300);
                } else {
                    match = /[^A-Za-z .\-]/g.test(el.value);
                    if (match) {
                        ++errors;
                        $("#last-name-error").html('Invalid characters in field Last Name.').slideDown(300);
                    }
                }
            break;

            case 'email':
                if (!el.value) {
                    ++errors;
                    $("#email-error").html('Please type in your email.').slideDown(300);
                }
                else if (! (/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/.test(el.value))) {
                    ++errors;
                    $("#email-error").html('Invalid Email address provided.').slideDown(300);
                }
            break;

            case 'telephone':
                if (!el.value) {
                    ++errors;
                    $("#phone-error").html('Please type in your phone number.').slideDown(300);
                } else if (/[^\d\-\s()]/.test(el.value)) {
                    ++errors;
                    $("#phone-error").html('Invalid characters in field Telephone.').slideDown(300);
                } else {
                    el.value.replace(/[^\d\-]/, el.value);
                    if (!el.value) {
                        ++errors;
                        $("#phone-error").html('Please type in your phone number.').slideDown(300);
                    }
                }
            break;
            
            case 'address1':
                if (!el.value) {
                    ++errors;
                    $("#address1-error").html('Please type in your address.').slideDown(300);
                }
            break;
            
            case 'city':
                if (!el.value) {
                    ++errors;
                    $("#city-error").html('Please type in your City.').slideDown(300);
                }
            break;
            
            case 'state':
                var countrySel = el.form.elements['country'];
                var countryVal = countrySel.options[countrySel.options.selectedIndex].value;
                if (!el.options[el.options.selectedIndex].value && countryVal == 'United States') {
                    ++errors;
                    $("#state-error").html('Please select your State.').slideDown(300);
                }
            break;

            case 'postal_code':
                if (!el.value) {
                    ++errors;
                    $("#zip-error").html('Please type in your postal code.').slideDown(300);
                } else {
                    match = /^\d{5}$/.test(el.value);
                    if (!match) {
                        ++errors;
                        $("#zip-error").html('Wrong Postal Code provided.').slideDown(300);
                    }
                }
            break;
            
            case 'country':
                if (!el.options[el.options.selectedIndex].value) {
                    ++errors;
                    $("#country-error").html('Please select your Country.').slideDown(300);
                }
            break;
        }
    }
    if (errors > 0) {
        return false;
    }
    return true;
}

function validateField(fld) {
    var errors = 0;
    var match;
    switch (fld.name) {
        case 'program':
            $("#program-error").html('').slideUp(300);
            if (!fld.options[fld.options.selectedIndex].value) {
                ++errors;
                $("#program-error").html('Please select Program Type.').slideDown(300);
            }
        break;

        case 'age':
            $("#age-error").html('').slideUp(300);
            if (!fld.options[fld.options.selectedIndex].value) {
                ++errors;
                $("#age-error").html('Please select Age.').slideDown(300);
            }
        break;

        case 'first_name':
            $("#first-name-error").html('').slideUp(300);
            if (!fld.value) {
                ++errors;
                $("#first-name-error").html('Please type in your first name.').slideDown(300);
            } else {
                match = /[^A-Za-z .\-]/g.test(fld.value);
                if (match) {
                    ++errors;
                    $("#first-name-error").html('Invalid characters in field First Name.').slideDown(300);
                }
            }
        break;

        case 'last_name':
            $("#last-name-error").html('').slideUp(300);
            if (!fld.value) {
                ++errors;
                $("#last-name-error").html('Please type in your last name.').slideDown(300);
            } else {
                match = /[^A-Za-z .\-]/g.test(fld.value);
                if (match) {
                    ++errors;
                    $("#last-name-error").html('Invalid characters in field Last Name.').slideDown(300);
                }
            }
        break;

        case 'email':
            $("#email-error").html('').slideUp(300);
            if (!fld.value) {
                ++errors;
                $("#email-error").html('Please type in your email.').slideDown(300);
            }
            else if (! (/^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/.test(fld.value))) {
                ++errors;
                $("#email-error").html('Invalid Email address provided.').slideDown(300);
            }
        break;

        case 'telephone':
            $("#phone-error").html('').slideUp(300);
            if (!fld.value) {
                ++errors;
                $("#phone-error").html('Please type in your phone number.').slideDown(300);
            } else if (/[^\d\-\s()]/.test(fld.value)) {
                ++errors;
                $("#phone-error").html('Invalid characters in field Telephone.').slideDown(300);
            } else {
                fld.value.replace(/[^\d\-]/, fld.value);
                if (!fld.value) {
                    ++errors;
                    $("#phone-error").html('Please type in your phone number.').slideDown(300);
                }
            }
        break;
        
        case 'address1':
            $("#address1-error").html('').slideUp(300);
            if (!fld.value) {
                ++errors;
                $("#address1-error").html('Please type in your address.').slideDown(300);
            }
            break;
            
        case 'city':
            $("#city-error").html('').slideUp(300);
            if (!fld.value) {
                ++errors;
                $("#city-error").html('Please type in your address.').slideDown(300);
            }
            break;
            
        case 'state':
            $("#state-error").html('').slideUp(300);
            var countryLs = fld.form.elements['country'];
            if (countryLs.options[countryLs.options.selectedIndex].value == 'United States' && !fld.options[fld.options.selectedIndex].value) {
                ++errors;
                $("#state-error").html('Please select your State.').slideDown(300);
            }
            break;

        case 'postal_code':
            $("#zip-error").html('').slideUp(300);
            if (!fld.value) {
                ++errors;
                $("#zip-error").html('Please type in your postal code.').slideDown(300);
            } else {
                match = /^\d{5}$/.test(fld.value);
                if (!match) {
                    ++errors;
                    $("#zip-error").html('Wrong Postal Code provided.').slideDown(300);
                }
            }
        break;
        
        case 'country':
            $("#country-error").html('').slideUp(300);
            if (!fld.options[fld.options.selectedIndex].value) {
                ++errors;
                $("#country-error").html('Please select your Country.').slideDown(300);
            } else if (fld.options[fld.options.selectedIndex].value == 'United States') {
                $("#state-error").html('').slideUp(300);
                var stateSel = fld.form.elements['state'];
                if (!stateSel.options[stateSel.options.selectedIndex].value) {
                    ++errors;
                    $("#state-error").html('Please select your State.').slideDown(300);
                }
            }
            break;
    }
}
function getHostname(str) {
	var re = new RegExp('^(?:f|ht)tp(?:s)?\://([^/]+)', 'im');
	return str.match(re)[1].toString();
}

