var qdbPrime = "https://www.quickbase.com/db/bc5ycaqrf";
var button;
var doenter;
var dospin;
/////////////Start the page/////////////
	jQuery(document).ready(function(){
		// Your code here
		dospin = "images/prci_gui_images/wait16trans.gif";
		dospin2 = "images/prci_gui_images/wait28trans.gif";
		doenter = dospin;
	});
/////////////////////////////////////////////////////////////////////////////////////
	/*
	 * Uses the proxy to submit an idea. 
	 * 
	 */
	function qdbidea(e, errn) {
		return qdbexecute(e, errn, "idea", qdbideagood, qdbbad);
	}
	function qdbideagood(data, status) {
	if (button) {
		button.src = doenter;
	}
	e = document.getElementById('ideaerror');
	if ((idx = data.indexOf("Success")) != -1) {
		//Successful.
//		e.innerHTML = "****Idea Submission Successful****<br />" 
//			+ data.substr(idx) + "<br />"
//			+ data.substr(0,idx-1);
		//Reset top to donepage
		sURL = document.getElementById("donepage").value;
		window.location.replace( sURL );
	} else {
		nHTML = "****Error(s) encountered****\n" + data;
		e.innerHTML = nHTML;
	}
	return false;
}
	/*
	 * Uses the proxy to register a user. 
	 * It will check email addresses and return error if either exists.
	 * If not, adds this as a new user.
	 */
	function qdbregister(e, errn) {
		return qdbexecute(e, errn, "register", qdbregistergood, qdbbad);
	}
	function qdbgood(data, status) {
	if (button) {
		button.src = doenter;
	}
	if (data.indexOf("<ticket>") == -1) {
		qdbbad("", "", "Login Problem");
//		alert(data);
	} else {
		document.login.rdr.value = qdbPrime;
		document.login.username.value = document.auth.qdbusername.value;
		document.login.password.value = document.auth.qdbpassword.value;
		document.login.act.value = document.auth.qdbact.value;
		document.login.submit();
	}
	return false;
}
	function qdbregistergood(data, status) {
	if (button) {
		button.src = doenter;
	}
	e = document.getElementById('registererror');
	if ((idx = data.indexOf("Success")) != -1) {
		//Successful.
//		e.innerHTML = "****Registration Request Successful****<br />" 
//			+ data.substr(idx) + "<br />"
//			+ data.substr(0,idx-1);
		//Reset top to donepage
		sURL = document.getElementById("donepage").value;
		window.location.replace( sURL );
	} else {
		idx = data.indexOf("already")
		nHTML = "****Error(s) encountered****\n" + data;
		if (data.indexOf("already") != -1) {
			nHTML += '<p><a href="https://www.quickbase.com/db/main?a=ForgotPassword&what=" target="_blank">Forgot your password?</a></p>';
		}
		e.innerHTML = nHTML;
	}
	return false;
}
	function qdbusergood(data, status) {
	if (button) {
		button.src = doenter;
	}
	if (data.indexOf("<ticket>") == -1) {
		qdbbad("", "", "Login Problem");
		alert(data);
	} else if (data.indexOf("<record>") != -1) {
		alert("****Cannot Enter Request, Already Registered****\n" + data);
	} else {
//		document.auth.rdr.value = qdbPrime;
//		document.auth.submit();
		alert("****Can Enter Request, No Record Found****\n" + data);
	}
	return false;
}
function qdbclear() {
	if (button) {
		button.src = doenter;
		button = null;
	}
	e = document.getElementById("qdberror");
	e.innerHTML = "";
	return false;
}
function qdbbad(request, status, error) {
	if (button) {
		button.src = doenter;
	}
	e = document.getElementById("qdberror");
	e.innerHTML = error;
	return false;
}
/*
 * Uses the proxy to validate the username/password supplied
 * and, if successful, redirects to the quickbase PRIME application
 * with the same credentials.
 */
function qdblogin(e) {
	frm = e.parentNode;
	button = e;
	doenter = e.src;
	e.src = dospin;
	gopath = "/scripts/proxyqdb.php";
	qdburl = "https://www.quickbase.com";
	qdbact = "API_Authenticate";
	var thePost = new Object;
	thePost['qdbusername'] = htmlspecialchars(frm.qdbusername.value);
	thePost['qdbpassword'] = htmlspecialchars(frm.qdbpassword.value);
	thePost['qdbdbid'] = "main";
	thePost['qdbact'] = frm.qdbact.value
	jQuery.ajax({
		url: gopath,
		data: thePost,
		dataType: "html",
		type: "POST",
		async: true,
		success: qdbgood,
		error: qdbbad
	});//jQuery.ajax()

	return false;
}
/*
 * Uses the proxy to check if the email address exists
 * and, if not, adds this as a new user.
 */
function qdbuser(e) {
	frm = e.parentNode;
	button = e;
	doenter = e.src;
	e.src = dospin;
	gopath = "/scripts/proxyqdb.php";
	qdburl = "https://www.quickbase.com";
	var thePost = new Object;
	thePost['doFunction'] = "checkemail";
	thePost['qdbemail'] = frm.qdbemail.value;
	jQuery.ajax({
		url: gopath,
		data: thePost,
		dataType: "html",
		type: "POST",
		async: true,
		success: qdbusergood,
		error: qdbbad
	});//jQuery.ajax()

	return false;
}
/*
 * Uses the proxy to execute the request. 
 */
function qdbexecute(e, errn, fName, fGood, fBad) {
	frm = errn.parentNode;
	button = e;
	doenter = e.src;
	e.src = dospin;
	gopath = "/scripts/proxyqdb.php";
	var thePost = new Object;
	thePost['doFunction'] = fName;
	for (i = 0; i < frm.elements.length; i++) {
		ce = frm.elements[i];
		if (ce.type == "text") {
			thePost[ce.name] = ce.value.toString();
		} else if (ce.type == "checkbox") {
			thePost[ce.name] = ce.checked?"1":"0";
		} else if (ce.type == "file") {
			thePost[ce.name] = ce.value.toString();
		} else if (ce.tagName.toLowerCase() == "select") {
			thePost[ce.name] = ce.value.toString();
		} else if (ce.tagName.toLowerCase() == "textarea") {
			thePost[ce.name] = ce.value.toString();
		} else if (ce.type == "hidden") {
			thePost[ce.name] = ce.value.toString();
		}
	}

	jQuery.ajax({
		url: gopath,
		data: thePost,
		dataType: "html",
		type: "POST",
		async: true,
		success: fGood,
		error: fBad
	});//jQuery.ajax()

	return false;
}
function htmlspecialchars(str) {
	if (typeof(str) == "string") {
		str = str.replace(/&/g, "&amp;"); /* must do &amp; first */
		str = str.replace(/"/g, "&quot;");
//		str = str.replace(/'/g, "&apos;");
		str = str.replace(/'/g, "&#039;");
		str = str.replace(/</g, "&lt;");
		str = str.replace(/>/g, "&gt;");
	}
	return str;
}

