var qdbPrime = "https://www.quickbase.com/db/bc5ycaqrf";
var button;
var dospin = "images/prci_gui_images/wait16trans.gif";
var dospin2 = "images/prci_gui_images/wait28trans.gif";
var doenter = dospin;
var inited=false;
/////////////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 simpleGetXmlElem(elem, xml) {
	//finds the first occurence of the elem and returns the text
	var txt = "";
	var ts = xml.indexOf("<"+elem+">");
	if (ts != -1) {
		ts += elem.length+2;
		var te = xml.indexOf("</"+elem+">");
		txt = xml.substr(ts,te-ts);
	}
	return txt;
}
function qdbticket(data, status) {
	if (button) {
		button.src = doenter;
	}
	var ret = new Object();
	ret.ticket = simpleGetXmlElem("ticket", data);
	ret.userid = simpleGetXmlElem("userid", data);
	ret.errcode = simpleGetXmlElem("errcode", data);
	ret.errtext = simpleGetXmlElem("errtext", data);
//	if (ret.errcode != 0) {
//		e = document.getElementById("qdberror");
//		e.innerHTML = ret.errtext;
//	} else {
//		e = document.getElementById("qdberror");
//		e.innerHTML = "<p>ticket:"+ret.ticket+":</p>" + "<p>userid:"+ret.userid+":</p>";
//		e.innerHTML += "<pre>"+data+"</pre>";
//	}
	return ret;
}
function qdbecho(data, status) {
	//returns the data
	if (button) {
		button.src = doenter;
	}
	e = document.getElementById("qdberror");
	e.innerHTML = "<pre>"+data+"</pre>";
	return false;
}
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);
		doNotBlock = true;
	} else if (data.indexOf("<record>") != -1) {
		alert("****Cannot Enter Request, Already Registered****\n" + data);
		doNotBlock = true;
	} else {
//		document.auth.rdr.value = qdbPrime;
//		document.auth.submit();
		alert("****Can Enter Request, No Record Found****\n" + data);
		doNotBlock = true;
	}
	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 = "<pre>"+error+"</pre>";
	return false;
}
/*
 * Uses the proxy to validate the username/password supplied
 * and, if successful, redirects to the quickbase PRIME application
 * with the same credentials.
 * 
 * gf is an optional parameter specifying the "good" return function
 * bf is an optional parameter specifying the "bad" return function
 */
function qdblogin(e, gf, bf, fm) {
	if (gf) {
		doGood = gf;
	} else {
		doGood = qdbgood;
	}
	if (bf) {
		doBad = bf;
	} else {
		doBad = qdbbad;
	}
	if (fm) {
		frm = document.getElementById(fm);
	} else {
		frm = e.parentNode;
	}
	button = e;
	doenter = e.src;
	e.src = dospin;
	var path = window.location.pathname;
	path = path.substr(0,path.lastIndexOf("/"));
	gopath = path + "/scripts/proxyqdb.php";
	qdburl = "https://www.quickbase.com";
	qdbact = "API_Authenticate";
	var thePost = new Object;
	thePost['doFunction'] = "login";
	thePost['qdbusername'] = htmlspecialchars(frm.qdbusername.value);
	thePost['qdbpassword'] = htmlspecialchars(frm.qdbpassword.value);
//	thePost['qdbusername'] = frm.qdbusername.value;
//	thePost['qdbpassword'] = frm.qdbpassword.value;

	thePost['qdbdbid'] = "main";
	thePost['qdbact'] = frm.qdbact.value
	jQuery.ajax({
		url: gopath,
		data: thePost,
		dataType: "html",
		type: "POST",
		async: true,
		success: doGood,
		error: doBad
	});//jQuery.ajax()

	return false;
}
/*
 * Uses the proxy to validate the username/password supplied
 * and, if successful, retrieves the related query data.
 * The query are specified through the form.
 * 
 * gf is an optional parameter specifying the "good" return function
 * bf is an optional parameter specifying the "bad" return function
 * fm is the form id.
 */
function qdbquery(e, gf, bf, fm) {
	if (gf) {
		doGood = gf;
	} else {
		doGood = qdbgood;
	}
	if (bf) {
		doBad = bf;
	} else {
		doBad = qdbbad;
	}
	if (fm) {
		frm = document.getElementById(fm);
	} else {
		frm = e.parentNode;
	}
	button = e;
	doenter = e.src;
	e.src = dospin;
	var path = window.location.pathname;
	path = path.substr(0,path.lastIndexOf("/"));
	gopath = path + "/scripts/proxyqdb.php";
	qdburl = "https://www.quickbase.com";
//	qdbact = "API_DoQuery";
	var thePost = new Object;
	thePost['doFunction'] = "query";
	thePost['qdbusername'] = frm.username.value;
	thePost['qdbpassword'] = frm.password.value;
	thePost['qdbdbid'] = frm.qdbdbid.value;
	thePost['qdbqid'] = frm.qdbqid.value;
	thePost['qdbact'] = frm.qdbact.value
	jQuery.ajax({
		url: gopath,
		data: thePost,
		dataType: "html",
		type: "POST",
		async: true,
		success: doGood,
		error: doBad
	});//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;
}
