// JavaScript Document

// Prints the current window
function printWindow(){
	if (window.print){
		window.print();
	}
}

// The function stripBlanks removes leading and trailing blanks from the given string
function stripBlanks(fld) {
	var result = "";
	var c = 0;
	for (i=0; i<fld.length; i++) {
		if (fld.charAt(i) != " " || c > 0) {
			result += fld.charAt(i);
			if (fld.charAt(i) != " ") c = result.length;
		}
	}
	return result.substr(0,c);
}

// Determine if the given field is valid as a required field.
function validRequiredField(fld) {
	fld = stripBlanks(fld);
	if (fld == '') return false;
	// other validations for this field, if any, to be added here
	return true;
}

function validateRequiredField(fieldObj) {
	if (!validRquiredField(fieldObj.value)) {
		alert("No go");
	}
}

// Boolean function to determine if all the input fields of the comment form
// have been entered correctly
function validateCommentForm(formObj) {
	valid = true;
	
	field = document.getElementById("first");
	if (!validRequiredField(field.value)) {alert("First name required"); valid = false; }
	
	field = document.getElementById("last");
	if (!validRequiredField(field.value)) {alert("Last name required"); valid = false; }
	
	field = document.getElementById("location");
	if (!validRequiredField(field.value)) {alert("Location required"); valid = false; }
	
	field = document.getElementById("subject");
	if (!validRequiredField(field.value)) {alert("Subject required"); valid = false; }
	
	field = document.getElementById("comments");
	if (!validRequiredField(field.value)) {alert("Comments required"); valid = false; }
	
	return valid;
	//return false;
}

//alert("JS file loaded");

// Support for limiting comment size 
function limiter() {
	var retval = true;
	var tex = document.comment_form.comments.value;
	var len = tex.length;
	if (len > commentsLimit) {
		tex = tex.substring(0,commentsLimit);
		document.comment_form.comments.value = tex;
		len = commentsLimit;
		retval = false;
	}
	document.comment_form.limit.value = commentsLimit-len;
	return retval;
}

// Shopping pages help

function cancelEdit() {
	window.location.href = "index.php";
	return false;
}

function commitEdit(formObj) {
	formObj.action = "commit.php";
	formObj.submit();
	return 0;
}

function resetEdit(formObj) {
	formObj.reset();
	return 0;
}

function newXmlHttpObj(readyFunction) // url not used anymore
//function newXmlHttpObj()
{
	xmlHttp = null;
	try
	{
		// Firefox, Opera 8.0+, Safari  
		xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer  
		try
		{    
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e)
		{
			try
			{
				xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e)
			{
				return null;      
			}    
		}  
	}
	
	// object creation successful
	xmlHttp.onreadystatechange = 
		function()
		{
			if (xmlHttp.readyState==4) readyFunction(xmlHttp.responseText);
		};
//	xmlHttp.open("POST", url, true);

	return xmlHttp;
}