//=============================================================================
//	Function checkExisting
//
//	Function to check if the inputted data already exists in the databse
//============================== GIJS - OCCHIO ================================

function checkExisting(itemName) {

	// Asynchronously call PHP file that checks if a given field value already exists
    var txtField = document.getElementById(itemName);

		txtField.valid = false;
	    var oXmlHttp = zXmlHttp.createRequest();
	    oXmlHttp.open("get", "/lib/ajax/email-validation.php?" + itemName + "=" + encodeURIComponent(txtField.value), true);
    
		// Check load status
		oXmlHttp.onreadystatechange = function () {
	        if (oXmlHttp.readyState == 4) {
	            if (oXmlHttp.status == 200) {
					// Extract and interpret data
	                var arrInfo = oXmlHttp.responseText.split("||");
	
					// Show error if needed
					if (!eval(arrInfo[0])) {
						// Alert
						alert(arrInfo[1]);
												
						// Deny field validness if that is a word
	                    txtField.valid = false;                    
	                } 
					
					// No errors -> Acknowledge field validness if that is a word
					else {
	                    txtField.valid = true;
	                }
	                
	            } else {
	                alert("An error occurred while trying to contact the server.");
	            }
	        }
	    };
    oXmlHttp.send(null);
}



function isFormValid() {
    var frmMain = document.forms[0];
    var blnValid = true;

    for (var i=0; i < frmMain.elements.length; i++) {        
        if (typeof frmMain.elements[i].valid == "boolean") {
            blnValid = blnValid && frmMain.elements[i].valid;            
        }
    }
    
    return blnValid;
}
