//-----------------------------------------------------------------------------
// Extracts a param from a URL
//-----------------------------------------------------------------------------
function getHTTPObject(){
   if (window.ActiveXObject) 
   	return new ActiveXObject("Microsoft.XMLHTTP");
   else if (window.XMLHttpRequest) 
   	return new XMLHttpRequest();
   else {
      alert("Your browser does not support the functionality to make this request, please upgrade to a later version.");
      return null;
   }
}   

//-----------------------------------------------------------------------------
// Extracts a param from a URL
//-----------------------------------------------------------------------------
function getCaptcha() {
    var date = new Date(); 
    document.getElementById('CaptchaImage').src = "/wp-content/custom/captcha.php?t=" + date.getTime();
}

//-----------------------------------------------------------------------------
// Extracts a param from a URL
//-----------------------------------------------------------------------------
function getURLParam(name) {
    name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
    var regexS = "[\\?&]"+name+"=([^&#]*)";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if(results == null)
        return "";
    else
        return results[1];
}

//-----------------------------------------------------------------------------
// Disable input on the form
//-----------------------------------------------------------------------------
function disableInput() {
    var buttonSubmit = document.getElementById("ButtonSubmit");
    buttonSubmit.disabled = "disabled";
}

//-----------------------------------------------------------------------------
// Enable input on the form
//-----------------------------------------------------------------------------
function enableInput() {
    var buttonSubmit = document.getElementById("ButtonSubmit");
    buttonSubmit.disabled = "";
}

//-----------------------------------------------------------------------------
// Validates the form, using regular expressions to match possible user input.
// The validated values get stored in an array which is returned.  If any
// validation fails, null is returned (and should therefore be checked for 
// in any caller).
//-----------------------------------------------------------------------------
function isFormValidate() {
    var valid = true;
    
    var name = document.getElementById("TextName");
    var emailAddress = document.getElementById("TextEmailAddress");
    var captchaText = document.getElementById("TextCaptcha");
    var statusLabel = document.getElementById("LabelStatus");
    
    statusLabel.style.color = "";
    statusLabel.innerHTML = "Checking...";
    
    if (name.value.length == 0) {
            statusLabel.style.color = "Red";
            statusLabel.innerHTML = "* Enter your name";
            return false;
        } else {
            var regexS = "\\w";
            var regex = new RegExp(regexS);
            var results = regex.exec(name.value);
            if (results == null) {
                statusLabel.style.color = "Red";
                statusLabel.innerHTML = "* Enter your name";  
                return false;
            }
    }
    
    if (emailAddress.value.length == 0) {
        statusLabel.style.color = "Red";
        statusLabel.innerHTML = "* Enter your email address";
        return false;
    } else {
        var regexS = "^([a-zA-Z0-9_\\-\\.]+)@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.)|(([a-zA-Z0-9\\-]+\\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\\]?)$";
        var regex = new RegExp(regexS);
        var results = regex.exec(emailAddress.value);
        if (results == null) {
            statusLabel.style.color = "Red";
            statusLabel.innerHTML = "* Enter a valid email address";  
            return false;    
        }
    }
    
    if (captchaText.value.length == 0) {
        statusLabel.style.color = "Red";
	statusLabel.innerHTML = "* Enter the verification text";
        return false;
    } else {
        var regexS = "^[a-zA-Z0-9]{6}$";
	var regex = new RegExp(regexS);
        var results = regex.exec(captchaText.value);
        if (results == null) {
            statusLabel.style.color = "Red";
            statusLabel.innerHTML = "* Enter a valid verification string";  
            return false;   
        }
    }    
    
    return true;
}


//-----------------------------------------------------------------------------
// The main function to start the pay process
//-----------------------------------------------------------------------------
function startDownload(fileName, listName) {
    if (isFormValidate()) {
    	var invoiceEmail = document.getElementById("TextEmailAddress");
    	var captchaText = document.getElementById("TextCaptcha");
    	var nameText = document.getElementById("TextName");
    	var queryString = "?email=" + invoiceEmail.value + "&captcha=" + captchaText.value + "&name=" + nameText.value + "&fileName=" + fileName + "&list=" + listName;
    	disableInput();
    	sendRequest(queryString);
    }
}

//-----------------------------------------------------------------------------
// Send the email
//-----------------------------------------------------------------------------
function sendRequest(queryString){  
    httpObject = getHTTPObject();
    if (httpObject != null) {
        httpObject.open("GET", "/wp-content/custom/serve.php" + queryString, true);
        httpObject.send(null);
        httpObject.onreadystatechange = setReturn;
        return true;
    } else {
    	return false;
    }
}

//-----------------------------------------------------------------------------
// The return value from the email process
//-----------------------------------------------------------------------------
function setReturn(){
    if(httpObject.readyState == 4) {
    	var statusLabel = document.getElementById("LabelStatus");	
        if (httpObject.responseText.length == 0 || httpObject.responseText == "ERROR") {
            statusLabel.style.color = "Red";
	    statusLabel.innerHTML = "* Unable to start download.";
	    enableInput();
        } else  if (httpObject.responseText == "CAPTCHA") {
            statusLabel.style.color = "Red";
	    statusLabel.innerHTML = "* Incorrect verification text.";
	    enableInput();	    
        } else  if (httpObject.responseText == "FILE") {
            statusLabel.style.color = "Red";
	    statusLabel.innerHTML = "* File not found.";
	    enableInput();	    
        } else  if (httpObject.responseText == "SUCCESS") {
            statusLabel.style.color = "";
	    statusLabel.innerHTML = "Details processed, thank you. Your download should now start.";
	    enableInput();	    
        }else {
	    statusLabel.style.color = "";
	    statusLabel.innerHTML = "Details processed, thank you. Your download should now start.";
	    downloadFrame = document.getElementById('downloadIFrame');
	    downloadFrame.src = "/wp-content/custom/download.php?uid=" + httpObject.responseText;
        }
    }
 
}