var siteURL = "http://www.northern-golf.com/"

// This is the array for error handling
var vError =  [];

$(document).ready(function() {
	
    $("#validateForm :text").blur(function() { validateIt(this); });
    $("#validateForm :password").blur(function() { validateIt(this); });

    $("#validateForm input[class*=required]").each(function() {
      vError.push(this.id);
	  
    });    
	$('#submitButton').removeAttr("disabled");
});

// Set this to your validation PHP script, default is "validate.php?value="
var vUrl = siteURL + "application/view/includes/ajaxValidate.php?value=";

// Refers to the function
http = getHTTPObject();

function getHTTPObject() {

  var xmlhttp;

  if(!xmlhttp && typeof XMLHttpRequest != 'undefined'){
    try {
      xmlhttp = new XMLHttpRequest();
    }catch(e){
      xmlhttp = false;
    }
  }
 
  return xmlhttp;

}

// The main validation-function
function validateIt(vInput) {

    // Each input field's id
    vId = vInput.id;
    vValue = vInput.value;

    // Separate the class attr of each input field
    getValue = vInput.className;
    if(getValue.indexOf(",") == -1 ) {
            vType = getValue;
            vRequired = "";
    } else {
            vRules = vInput.className.split(",");
            vRequired = vRules[0];
            vType = vRules[1];
    }

    // The whole URL
    var url = vUrl + (vValue) + "&required=" + (vRequired) + "&type=" + (vType);

    // And finally we send it to the url we specified
    http.open("GET", url, true);

    // The handleHttpResponse is the function we call, when we have an
    // answer back from the PHP script
    var handler = function() {
      handleHttpResponse(vId);
    };
    http.onreadystatechange = handler;

    http.send(null);
}

// A function to handle the response from the PHP script
function handleHttpResponse(vId) {
        
    if(http.readyState == 4) {
        $('#submitButton').removeAttr("disabled");
        if(http.responseText == "none") {
            document[vId].src = siteURL + "images/blank.gif";
        }

        if(http.responseText == "false") {
            document[vId].src = siteURL + "images/error_missing.png";
            if (vError.indexOf(vId) == -1) {
                vError.push(vId);
            }
        }

        if(http.responseText == "invalid") {
            document[vId].src = siteURL + "images/error_invalid.png";
            if (vError.indexOf(vId) == -1) {
                vError.push(vId);
            }
        }
        
        if(http.responseText == "emailexists") {
            document[vId].src = siteURL + "images/error_email.png";
            if (vError.indexOf(vId) == -1) {
                vError.push(vId);
            } 
        }
        
        if(http.responseText == "true") {

            document[vId].src = siteURL + "images/error_tick.png";

            // We do a check if our element is in the error array, and if
            // so, we can delete it from the array
            if(vError.indexOf(vId) != -1) {

                var aId = vError.indexOf(vId);
                vError.splice(aId, 1);
            }
        }

        if(vError.length < 1) {
            $('#submitButton').removeAttr("disabled");
            $('#submitButton').css("cursor", "pointer");
            $('#submitButton').attr("src", siteURL + "images/join-now-button.png");
        }
        else {
            //$('#submitButton').attr("disabled", "disabled");
            $('#submitButton').css("cursor", "default");
            $('#submitButton').attr("src", siteURL + "images/join-now-button-disabled.png");
        }
    }
}

