// misc.js 

// Miscellaneous global variables
// Regular Expressions
var reBlank = /^\s*$/; // Match for blank space, start to end 
var reEmail = /^.+@.+\..+$/; // Match for Email address format
var rePhNum = /^\d+$/; // Match for phone number
var reAlpha = /^[A-z]+$/; // Match for letters of the alphabet only
var reNumeric = /^[0-9]+$/; // Match for numbers only
var rePostCode = /^\d\d\d\d$/; // Match for a vlid postcode

// Miscellaneous functions

// Clear specified form
function clearForm ( form ){
	if (document.getElementsByTagName){
		elements = form.getElementsByTagName('input');
		var elm;
		for( i = 0, elm; elm = elements.item(i); i++ ){
			if ( elm.getAttribute('type') == "text" ){
				elm.value = '';
			}
			else if ( elm.getAttribute('type') == "checkbox" ){
				elm.checked = false;
			}
		}
		
		elements = form.getElementsByTagName('select');
		for ( i = 0, elm; elm = elements.item(i); i++ ){
			elm.selectedIndex = 0;
		}
		
		elements = form.getElementsByTagName('textarea');
		for ( i = 0, elm; elm = elements.item(i); i++ ){
			elm.value = '';
		}
	}
	else {
		elements = form.elements;
		var elm;
		for( i = 0, elm; elm = elements[i]; i++ ){
			if (elm.type == "text" || elm.type == "textarea"){
				elm.value = '';
			}
			else if ( elm.type == "select" ){
				elm.selectedIndex = 0;
			}
			else if ( elm.type == "checkbox" ){
				elm.checked = false;
			}
		}
	}
}

// Clear the default value (text) from the field
function clearDefaultText( inputField ) {
	if ( inputField.value == inputField.defaultValue ){
		inputField.value = "";
	}
}

// Reset the specified field back to its default value (text) if the current value is blank
function resetDefaultIfEmpty( inputField ) {
	if ( inputField.value == "" || inputField.value == " " ){
		resetDefaultText( inputField );
	}
}

// Reset the specified field back to its default value (text)
function resetDefaultText( inputField ) {
	inputField.value = inputField.defaultValue;
}

// Return true/false whether or not the specified value is blank.
// A blank value is null, nothing (""), 1 or more whitespaces
function isBlank ( str ){
	if ( reBlank.test(str) ){
		return true;
	}
	else {
		return false;
	}
}

// Returns true/false whether or not the specified value is in the
// format of a valid email address
function validEmail ( str ){
	if ( reEmail.test(str) ){
		return true;
	}
	else {
		return false;
	}
}

// Returns true/false depending whether or not the specified value
// is in the format of a valid phone number
function validPhNumber ( str ){
	if ( rePhNum.test(str) ){
		return true;
	}
	else {
		return false;
	}
}

function validPostCode ( pcode ){
	if ( rePostCode.test(pcode) ){
		return true;
	}
	else{
		return false;
	}
}

// Check if the two specified elements have the exact same values
function checkPass ( pass1, pass2 ){
	if ( pass1.value == pass2.value ){
		return true;
	}
	else {
		alert("The passwords you entered do not match.");
		pass1.focus();
		return false;
	}
}

// Clone the element and it's children of the specified ID and insert them
// before the other specified element
// readFromID = Id of element to clone
// writeToID =  Insert clone before the element of this Id
function addElement( readFromID, writeToID ){
	// Clone the object we want to insert	
	var newElements = document.getElementById(readFromID).cloneNode(true);
	var insertHere = document.getElementById(writeToID);
	
	newElements.id = '';
	newElements.style.display = 'block';
	
	// Insert our clone just before the element
	insertHere.parentNode.insertBefore(newElements, insertHere);
}

// Show/Hide (toggle) the element that has the specified ID
function showHide(id){
	if ( document.getElementById ){ // IE5, NS6
		// If already hidden, then show, else hide
		if ( document.getElementById(id).style.display == 'none' ){
			document.getElementById(id).style.display = 'block';
		}
		else {
			document.getElementById(id).style.display = 'none';
		}
	}
	else {
		if ( document.layers ){ // NS4
			// If already hidden, then show, else hide
			if ( document.id.display == 'none' ){
				document.id.display = 'block';
			}
			else {
				document.id.display = 'none';
			}
		}
		else { // IE4
			// If already hidden, then show, else hide
			if ( document.all.id.style.display == 'none' ){
				document.all.id.style.display = 'block';
			}
			else {
				document.all.id.style.display = 'none';
			}
		}
	}
}

// Hide the element that has the specified ID
function hide(id){
	if ( document.getElementById ){ // IE5, NS6
		document.getElementById(id).style.display = 'none'
	}
	else {
		if ( document.layers ){ // NS4
			document.id.display = 'none';
		}
		else { // IE4
			document.all.id.style.display = 'none';
		}
	}
}

// Show the element that has the specified ID
function show (id){
	if ( document.getElementById ){ // IE5, NS6
		document.getElementById(id).style.display = 'block'
	}
	else {
		if ( document.layers ){ // NS4
			document.id.display = 'block';
		}
		else { // IE4
			document.all.id.style.display = 'block';
		}
	}
}

// Accepts an array of radio buttons and checks them to make
// sure at least one of them is checked. Return true/false
function radioButtonIsChecked ( radioButton ){
	for ( var i = 0; i < radioButton.length; i++ ){
		if ( radioButton[i].checked ){
			return true;
		}
	}
	return false;
}

// Ask if user wants to delete the specified listing
function confirmDeleteListing ( listingId ){
	return confirm('Are you sure you want to DELETE listing #'+listingId+' ?');
}

// Ask if user wants to delete the specified user
function delUser ( user ){
	return confirm("Are You sure you want to DELETE the user '"+user+"'");
}

function submitForm ( varToAdd ){
	document.forms[0].action = document.forms[0].action + '&' + 
		varToAdd.name + '=' + varToAdd.value;

	document.forms[0].submit();
}

function popupAddressBook (){
	window.open( '/include/popup-addressBook.php', 'popupAddressBook', 'toolbar=0,location=0,menubar=0,directories=0,scrollbars=1,width=300,height=600' );
}

function printWindowCard(listId) {
//print_window = window.open('include/windowCardPrintable.php?listingId='+listId,'print a window card',"width=800px,scrollbars=1,menubar=1")

}

function printWindow() {

//window.print()
//window.close()
}

function gotoURL (url){
	window.location=url;
}

function getXMLHTTPObject (){
	var xmlHttp;
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	}
	catch (e){
		try {
			// Internet Explorer 6+
	    		xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
	    	}
	  	catch (e){
			try {
				// Internet Explorer 5.5+
	      			xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
	    		catch (e){
	      			xmlHttp = false;
			}
		}
	}
	return xmlHttp;
}

function clickclear(thisfield, defaulttext) {
if (thisfield.value == defaulttext) {
thisfield.value = "";
}
}
function clickrecall(thisfield, defaulttext) {
if (thisfield.value == "") {
thisfield.value = defaulttext;
}
}
