//* JQuery script for contact form*//
//* Created by:  Michael Grasso, Shock Culture, Inc. *//
//* support@shockcultureservices.com *//
//* Last updated: 7/25/2010 *//

$(document).ready(function() {

    //** functions for contact form **//
    /* validate name and email fields */
	
    $('#s-form').click(function() {
        //alert('called ');

        var fname = $("#fName").val();
        var lname = $("#lName").val();
        var email = $("#eMail").val();
        var phone = $("#pNumber").val();
        var reason = $("#cReason").val();
        var message = $('#message').val();

        var nError = " - required";
        var eError = " - required";
        var eError2 = " - format is not valid";
        var eIsAt = email.indexOf('@');
        var eIsDot = email.indexOf('\.');

        if ((fname == " " || fname.length == 0) || (lname == " " || lname.length == 0) ||
            (email == " " || email.length == 0) || (eIsAt == -1 || eIsDot == -1)) {

            if (fname == " " || fname.length == 0) {

                $("#l-fName").show();
                $("#l-fName").text(nError);
                $("#l-fName").css("color", "#f00");
                $("#fName").css("border", "1px solid #f00");
            }
            if (lname == " " || lname.length == 0) {

                $("#l-lName").show();
                $("#l-lName").text(nError);
                $("#l-lName").css("color", "#f00");
                $("#lName").css("border", "1px solid #f00");
            }

            if (email == " " || email.length == 0) {

                $("#l-eMail").show();
                $("#l-eMail").css("color", "#f00");
                $("#l-eMail").text(eError);
                $("#eMail").css("border", "1px solid #f00");

            } else if ( eIsAt == -1 || eIsDot == -1 ) {

                $("#l-eMail").show();
                $("#l-eMail").css("color", "#f00");
                $("#l-eMail").text(eError2);
                $("#eMail").css("border", "1px solid #f00");
            }

        } else {
            //alert("form is validated, send away!");
            $.ajax({
                url: base_path() + 'modules/contact-save.php',
                data: {
                    fname: fname,
                    lname: lname,
                    email: email,
                    phone: phone,
                    reason: reason,
                    message: message
                },
                type: 'post',
                success: function(output) {
                    $('#cFormReply').html(output);
                    $('#cFormReply').fadeIn("fast");
                    $("#cFormReply").fadeOut(3000);

                    $("#fName").val('');
                    $("#lName").val('');
                    $("#eMail").val('');
                    $("#pNumber").val('');
                    $('#message').val('');
                }
            });
        }

        return false;
    });

    //clear error fields on keypress into form field on appointment contact form
    $("#fName").keypress(function(event) {
        $("#l-fName").hide();
        $("#fName").css("border", "0");
    });
    $("#lName").keypress(function(event) {
        $("#l-lName").hide();
        $("#lName").css("border", "0");
    });
    $("#eMail").keypress(function(event) {
        $("#l-eMail").hide();
        $("#eMail").css("border", "0");
    });

}); //close document ready function

function base_path() {
	
	//first, place window path into variable
    var pathname = window.location.href;
    urlPath = pathname.toString();
    //alert("urlPath is: " + urlPath);
	var basePath = '';

    var marker = urlPath.indexOf('view/');
	var index = urlPath.indexOf('index.php');
	
	if (marker== -1 && index == -1) basePath = urlPath; // both missing
	
	else if (marker != -1 && index != -1) basePath = urlPath.substring(0, index); // both present
	
	else if (marker == -1 && index != -1) basePath = urlPath.substring(0, index); // view/ missing, index.php present
	
	else basePath = urlPath.substring(0, marker); //index.php missing
	
    //alert('BasePath = ' + basePath);
	
	return basePath;
}

