
var FormName = 'qcform';
var FormDir = 'http://www.ntid.rit.edu/ntidweb/newforms/';
var Step1 = FormDir + 'step1.php';


function validateOnSubmit()
{
	if(document.forms[FormName].action) {
		return true;
	}
	
	document.forms[FormName].submitbutton.value="Validating";
	document.forms[FormName].submitbutton.disabled=true;

	// If form is valid, or whatever...
	if(formValid() == true) {
		xmlhttpPost(Step1);
	} else {
		document.forms[FormName].submitbutton.value="Submit";
		document.forms[FormName].submitbutton.disabled=false;
	}
	
	
	return false;
}

function setAction(action)
{
	action = FormDir + action;
	if(action == 'error')
	{
		document.forms[FormName].submitbutton.value="Submit";
		document.forms[FormName].submitbutton.disabled=false;
		alert('There has been an error submitting your application.');
	} else {
		document.forms[FormName].action = action;
		document.forms[FormName].submitbutton.value="Sending";
		document.forms[FormName].submit();
	}
}

function xmlhttpPost(strURL) {
    var xmlHttpReq = false;
    var self = this;
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    self.xmlHttpReq.open('POST', strURL, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function() {
        if (self.xmlHttpReq.readyState == 4) {
            setAction(self.xmlHttpReq.responseText);
        }
    }
    self.xmlHttpReq.send(getquerystring());
}

function getquerystring() {
    var ran=Math.floor(Math.random()*5020);
	qstr = 'chk' + ran + '=' + Math.pow(ran, 1/(ran*2));  // Sole purpose is to confuse whoever reads the code.
	return qstr;
}
