// JavaScript Document



//Function to create an XMLHttp Object.
function getxmlhttp (){
	
	//Create a boolean variable to check for valid IE instance.
	var xmlhttp = false;

	//Check if we are using IE.
	try{
		//If the Javascript version is greater than 5.
		xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
	} catch (e) {
		//If not, then use the older active x object.
		try {
			//If we are using Internet Explorer.
			xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
		} catch (E) {
			//Else we must be using a non-IE browser.
			xmlhttp = false;
		}
	}

	//If we are using a non-IE browser, create a javascript instance of the object.
	if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
		xmlhttp = new XMLHttpRequest();
	}
	
	return xmlhttp;
}
// END Function to create an XMLHttp Object.


// Function to process an XMLHttpRequest.
function processajax (serverPage, obj, getOrPost, str){
	//Get an XMLHttpRequest object for use.
	xmlhttp = getxmlhttp ();
	if (getOrPost == "get"){
		xmlhttp.open("GET", serverPage);
		xmlhttp.onreadystatechange = function () {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				obj.innerHTML = xmlhttp.responseText;
			}
		}
		xmlhttp.send(null);
	} else {
		xmlhttp.open("POST", serverPage, true);
		xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		xmlhttp.onreadystatechange = function() {
			if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
				obj.innerHTML = xmlhttp.responseText;
			}
		}
		xmlhttp.send(str);
	}
}
// END Function to process an XMLHttpRequest.

// Require all checkboxes to be checked
function checkCheckBoxes(theForm) {
	var formok = true;
	for (var i = 0; i < theForm.elements.length; i++){
		if (theForm.elements[i].type == "checkbox") {
			
			if (theForm.elements[i].checked == false ){
				formok = false;
			}
		} 
	}
	
	return formok;
}
// END checkform function

// Require 1 checkbox to be checked
function need1CheckOrRadio(theForm,minCheck,maxCheck) {
	
	var isChecked = false;
	var checkCounter = 0;
	var formType = null;
	for (var i = 0; i < theForm.elements.length; i++){
		if (theForm.elements[i].type == "checkbox") {
			var formType = "checkbox";
			if (theForm.elements[i].checked == true ){
				
				checkCounter = checkCounter + 1;
				
			}
		} else {
			if (theForm.elements[i].type == "radio") {
				
				if (theForm.elements[i].checked == true ){
					isChecked = true;
				}
			}
		}
	}
	if (formType == "checkbox") {
		if (checkCounter >= minCheck && checkCounter <= maxCheck) {
				isChecked = true;
			} else {
				if (minCheck == maxCheck) {
					alert("Please select " + minCheck + " items.");
				} else {
					alert("Please select " + minCheck + " to " + maxCheck + " items.");
				}
			}
		
	}
	return isChecked;
}
// END Require 1 checkbox to be checked



//Function to validate the addtask form.
function validatetask (checked, thevalue, thename) {
	
	var nowcount = true;
	
	if (thename == "delete") {
		if (checked == false) {
			nowcount = false;
		}
	}	
	
	if (thename == "disable") {
		if (checked == false) {
			nowcount = false;
		}
	}
	
	if (thename == "approve") {
		if (checked == false) {
			nowcount = false;
		}
	}
	
	if (thename == "yarp") {
		if (checked == false) {
			nowcount = false;
		}
	}
	

	
	if (thename == "thisorthat") {
		if (thevalue == "") {
			nowcount = false;
		}
	}
	
	return nowcount;
}
//End validate function.


//Function to prevent enter from submitting ajax form
function noenter() {
  return !(window.event && window.event.keyCode == 13); }

//Functions to submit a form.

// getformvalues
var aok;
function getformvalues (fobj, valfunc){
	
	var str = "";
	aok = true;
	var val;
	
	//Run through a list of all the objects contained within the form.
	for (var i = 0; i < fobj.elements.length; i++){
		if(valfunc) {
			if(aok == true){
				val = valfunc (fobj.elements[i].checked,fobj.elements[i].value,fobj.elements[i].name);
				if (val == false){
					aok= false;
				}
			}
		}
		
		if (fobj.elements[i].type == "checkbox" && fobj.elements[i].checked == true) {
			str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&";
		} else {
			if (fobj.elements[i].type == "radio" && fobj.elements[i].checked == true) {
				str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value) + "&";
			} else {
				if (fobj.elements[i].type != "checkbox" && fobj.elements[i].type != "radio") {
					str += fobj.elements[i].name + "=" + escape(fobj.elements[i].value).replace( /\+/g, "%2B" ) + "&";
				}
			}
		}
	}
	//.replace( /\+/g, "%2B" )
	//Then return the string values.
	return str;
}
// END getformvalues



// submitform function
function submitform (theform, serverPage, objID, valfunc) {
	var file = serverPage;
	var str = getformvalues(theform, valfunc);
	//If the validation is ok.
	if (aok == true) {
		
		obj = document.getElementById(objID);
		obj.innerHTML = "<div align='center'><img src='ajax-loader.gif' /> Loading...</div>";
		processajax (serverPage, obj, "post", str);
	}
}
// END submitform function


//Relocate to top of page after adding item to art.  Not currently used.
function relocate(hash) {
    if (location.hash != hash) {
        if (document.images)
            location.replace('index.php#' + hash);
        else
            location.href = 'index.php#' + hash;
    }
}
// END Relocate to top of page after adding item to art.  Not currently used.



function ismaxlength(obj){
var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
if (obj.getAttribute && obj.value.length>mlength)
obj.value=obj.value.substring(0,mlength)
}

function replaceChars(entry) {
		var arrReplace = new Array();
		arrReplace[0] = [/\+/g, "***"];
		//arrReplace[1] = [/&/g, "@@@"];
		//arrReplace[2] = [/c/g, "x"];
		
		for(var i = 0; i < arrReplace.length; i++){
			entry = entry.replace(arrReplace[i][0], arrReplace[i][1]);
		}
		//document.formgametitle.GameTitle.value = entry;
		return entry;
	}
