/*
	NAME			: functions.js
	DESCRIPTION		: general purpose javascript functions library
	OSDATE-VERSION	: osDate 2.1.x (LGPL)
	MODIFICATIONS	: source code formatting
					  some minor modifications of the function internals, but no change in the functionality
	LICENSE			: GPL
	AUTHOR			: Darren Gates, Vijay Nair for original script
	AUTHOR			: Ralf Strehle for this version (ralf dot strehle at yahoo dot de) (osDate forum user name: exotic)
	COPYRIGHT		: 2009, by author
	SUPPORT			: by author
	
	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
	GNU General Public License for more details.
	
	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/

if (use_popups == undefined)
{
	var use_popups = true;
}

var lf = "\n";

var alphabeticChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
var numericChars = "0123456789";

//var popUpWin = null;	// not needed now. was used to close open popup windows when a new window was opened.

/* Create the htprequest object and process the readystate contents */

function createRequestObject()
{
	if (window.ActiveXObject) {
		try { 
			return new ActiveXObject('MSXML2.XMLHTTP');
		} catch (e) {
			try {
				return new ActiveXObject('Microsoft.XMLHTTP');
			} catch (e) {
				alert('Error creating XMLHttpRequest with ActiveXObject()');
				return false;
			}
		}
	}
	
	if (window.XMLHttpRequest) {
		try {
			return new XMLHttpRequest();
		} catch (e) {
			alert('Error creating XMLHttpRequest with XMLHttpRequest()');
			return false;
		}
	}
	
	alert('Your browser does not support AJAX.');
	return false;
}

//--------------------
// var http should be used only for user invoked http requests like cascading country selection
//--------------------
var http = createRequestObject();

function handleResponse()
{
	if (http.readyState == 4) {
		if (http.status == 200) {
			var response = http.responseText;
			if (response != 'undefined' && response != '') {
				if (response.indexOf('|||') != -1) {
					var update = response.split('|||');
					for (var i = 1; i<update.length; i++) {
						var up2 = update[i].split("|:|");
						if (up2[0] != 'undefined' && up2[0] != '' && document.getElementById(up2[0])) {
							document.getElementById(up2[0]).innerHTML = up2[1];
						}
					}
				}
			}
		}
	}
}

function updateRenderedPage(url)
{
	http.open('get', url, true);
	http.onreadystatechange = handleResponseRenderedPage;
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.send(null);
}

function updateRenderedPagePost(url, parameters)
{
	http.open('post', url, true);
	http.onreadystatechange = handleResponseRenderedPage;
	http.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
	http.send(parameters);
}

function handleResponseRenderedPage()
{
	if (http.readyState == 4) {
		if (http.status == 200) {
			if (http.responseText != 'undefined' && http.responseText != '') {
				document.getElementById('rendered_page').innerHTML = http.responseText;
			}
		}
	}
}

//--------------------
// update online time on page reload
//--------------------
//
// THIS IS DONE IN INIT.PHP
// THERE WAS A TIMEOUT IN ORIGINAL SCRIPT THAT CAUSES THE SESSION NEVER TO TIME OUT
/*
var http_online_time = createRequestObject();

function updateOnlineTime()
{
	http_online_time.open('get', doc_root + 'updateonlinetime.php', true);
	http_online_time.send(null);
}
*/

//--------------------
// update online users and count every 15 seconds
//--------------------

var http_online_users = createRequestObject();

function get_online_users()
{
	if (http_online_users) {
		http_online_users.open('get', doc_root+'onlineusers.ajax.php', true);
		http_online_users.onreadystatechange = handleResponse_online_users;
		http_online_users.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		http_online_users.send(null);
		setTimeout('get_online_users()', 15 * 1000);
	}
}

function handleResponse_online_users()
{
	if (http_online_users.readyState == 4) {
		if (http_online_users.status == 200) {
			var response = http_online_users.responseText;
			// alert(response);
			if (response != 'undefined' && response != '') {
				if (response.indexOf('|||') != -1) {
					var update = response.split('|||');
					for (var i = 1; i<update.length; i++) {
						var up2 = update[i].split("|:|");
						if (up2[0] != 'undefined' && up2[0] != '' && document.getElementById(up2[0])) {
							document.getElementById(up2[0]).innerHTML = up2[1];
						}
					}
				}
			}
		}
	}
}


function confirm_one(form, cmd, id, msg_one)
{
	if (msg_one == null || msg_one.length == 0) {
		form.cmd.value = cmd;
		form.id.value = id;
		form.submit();
	} else if (confirm(msg_one)) {
		form.cmd.value = cmd;
		form.id.value = id;
		form.submit();
	} else {
		return false;
	}
}

function confirm_multi(form, cmd, msg_nocheck, msg_one, msg_multi)
{
	var checked = 0;
	
	for (i = 0; i < form.length; i++) {
		if (form.elements[i].name == 'check[]' && form.elements[i].type == 'checkbox' && form.elements[i].checked == true) {
			checked++;
		}
	}
	
	if (checked == 0) {
		alert(msg_nocheck);
		return false;
	} else if (checked == 1) {
		if (msg_one == null || msg_one.length == 0) {
			form.cmd.value = cmd;
			form.submit();
		} else if (confirm(msg_one)) {
			form.cmd.value = cmd;
			form.submit();
		} else {
			return false;
		}
	} else {
		if (msg_multi == null || msg_multi.length == 0) {
			form.cmd.value = cmd;
			form.submit();
		} else if (confirm(msg_multi)) {
			form.cmd.value = cmd;
			form.submit();
		} else {
			return false;
		}
	}
}

function isValidEmail(fieldValue)
{
	if ( /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,7})+$/.test(fieldValue)) {
		return true;
	}
	
	return false;
}


function isValidURL(url)
{
	if (url == null) {
		return false;
	}
	
	/* space extr */
	var reg='^ *';
	/* protocol */
	reg = reg+'(?:([Hh][Tt][Tt][Pp](?:[Ss]?))(?:\:\\/\\/))?';
	/* usrpwd */
	reg = reg+'(?:(\\w+\\:\\w+)(?:\\@))?';
	/* domain */
	reg = reg+'([0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}|localhost|([Ww][Ww][Ww].|[a-zA-Z0-9].)[a-zA-Z0-9\\-\\.]+\\.[a-zA-Z]{2,6})';
	/* port */
	reg = reg+'(\\:\\d+)?';
	/* path */
	reg = reg+'((?:\\/.*)*\\/?)?';
	/* filename */
	reg = reg+'(.*?\\.(\\w{2,4}))?';
	/* qrystr */
	reg = reg+'(\\?(?:[^\\#\\?]+)*)?';
	/* bkmrk */
	reg = reg+'(\\#.*)?';
	/* space extr */
	reg = reg+' *$';
	
	return url.match(reg);
}

// returns true if checkStr contains only characters specified in checkOK
//probably can be replaced with a more efficient regular expression 

function isValidString(checkStr, checkOK)
{
	if (!checkOK) {
		var checkOK = '';
	}
	
	var allValid = true;
	
	for (i = 0; i < checkStr.length; i++) {
		ch = checkStr.charAt(i);
		
		for (j = 0; j < checkOK.length; j++) {
			if (ch == checkOK.charAt(j)) {
				break;
			}
		}
			
		if (j == checkOK.length) {
			allValid = false;
			break;
		}
	}
	
	return allValid;
}

function isNumeric(fieldValue)
{
	if (/[0-9]/.test(fieldValue)) {
		return true;
	}
	
	return false;
}

function isNumeric(val, addChars)
{
	return isValidString(val, numericChars + addChars);
}

function isAlphabetic(val)
{
	if (/[A-Za-z]/.test(val)) {
		return true;
	}
	
	return false;
}

function isAlphabetic(val, addChars)
{
	return isValidString(val, alphabeticChars + addChars);
}

function isAlphaNumeric(val)
{
	if (/\w/.test(val)) {
		return true;
	}
	
	return false;
}

function isAlphaNumeric(val, addChars)
{
	return isValidString(val, alphabeticChars + numericChars + addChars);
}

function DispDispHide ( disp1, disp2, hide )
{
	if (hide) hide.style.display = 'none';
	if (disp1) disp1.style.display = 'inline';
	if (disp2) disp2.style.display = 'inline';
}

function DispHideHide ( disp, hide1, hide2 )
{
	if (hide1) hide1.style.display = 'none';
	if (hide2) hide2.style.display = 'none';
	if (disp) disp.style.display = 'inline';
}

function showHide( paramA, paramB)
{
	if (paramA.value == 'US') {
		paramB.rows['row_usstates'].style.display ='inline';
	} else {
		paramB.rows['row_usstates'].style.display = 'none';
	}
	
	if (paramA.value == 'CA') {
		paramB.rows['row_castates'].style.display ='inline';
	} else {
		paramB.rows['row_castates'].style.display = 'none';
	}
	
	if (paramA.value == 'AU') {
		paramB.rows['row_austates'].style.display ='inline';
	} else {
		paramB.rows['row_austates'].style.display = 'none';
	}
	
	if (paramA.value == 'GB') {
		paramB.rows['row_gbstates'].style.display ='inline';
	} else {
		paramB.rows['row_gbstates'].style.display = 'none';
	}
}

function showHide( paramA)
{
	if (paramA == 'US') {
		document.getElementById('row_usstates').style.display ='inline';
	} else {
		document.getElementById('row_usstates').style.display = 'none';
	}
	
	if (paramA == 'CA') {
		document.getElementById('row_castates').style.display ='inline';
	} else {
		document.getElementById('row_castates').style.display = 'none';
	}
	
	if (paramA == 'AU') {
		document.getElementById('row_austates').style.display ='inline';
	} else {
		document.getElementById('row_austates').style.display = 'none';
	}
	
	if (paramA == 'GB') {
		document.getElementById('row_gbstates').style.display ='inline';
	} else {
		document.getElementById('row_gbstates').style.display = 'none';
	}
}

function showHidePref(paramA, paramB)
{
	if (paramA.value == 'US') {
		paramB.rows['row_lookusstates'].style.display ='inline';
	} else {
		paramB.rows['row_lookusstates'].style.display = 'none';
	}
	
	if (paramA.value == 'CA') {
		paramB.rows['row_lookcastates'].style.display ='inline';
	} else {
		paramB.rows['row_lookcastates'].style.display = 'none';
	}
	
	if (paramA.value == 'AU') {
		paramB.rows['row_lookaustates'].style.display ='inline';
	} else {
		paramB.rows['row_lookaustates'].style.display = 'none';
	}
	
	if (paramA.value == 'GB') {
		paramB.rows['row_lookgbstates'].style.display ='inline';
	} else {
		paramB.rows['row_lookgbstates'].style.display = 'none';
	}
}

function showHidePref(paramA)
{
	if (paramA == 'US') {
		document.getElementById('row_lookusstates').style.display = 'inline';
	} else {
		document.getElementById('row_lookusstates').style.display = 'none';
	}
	
	if (paramA == 'CA') {
		document.getElementById('row_lookcastates').style.display = 'inline';
	} else {
		document.getElementById('row_lookcastates').style.display = 'none';
	}
	
	if (paramA == 'AU') {
		document.getElementById('row_lookaustates').style.display = 'inline';
	} else {
		document.getElementById('row_lookaustates').style.display = 'none';
	}
	
	if (paramA == 'GB') {
		document.getElementById('row_lookgbstates').style.display = 'inline';
	} else {
		document.getElementById('row_lookgbstates').style.display = 'none';
	}
}


function launchTellFriend()
{
	if (use_popups == false) {
		window.location.href = doc_root+'tellafriend.php';
		return;
	}
	
	var left = (screen.width/2) - 400/2;
	var top = (screen.height/2) - 400/2;
	var win = "width=300,height=250,left=" + left + ",top=" + top + ",copyhistory=no,directories=no,menubar=no,location=no,resizable=yes,scrollbars=no";
	
	window.open(dov_root+'tellafriend.php', 'tellfriend', win);
}


function launchTellFriendProfile(sID)
{
	if (use_popups == false) {
		window.location.href = doc_root+'tellafriend.php?ID=' + sID;
		return;
	}
	
	var left = (screen.width/2) - 280/2;
	var top = (screen.height/2) - 280/2;
	var win = "width=280,height=300,left=" + left + ",top=" + top + ",copyhistory=no,directories=no,menubar=no,location=no,resizable=yes,scrollbars=yes";
	
	window.open(doc_root+'tellfriend.php?ID=' + sID, 'tellfriendprofile', win);
}


function popUpWindowMessage(URLStr, align, width, height, msgid)
{
	width = 450;
	height = 450;
	
	if (use_popups == false) {
		window.location.href = URLStr;
		return;
	}
	
	//	if(popUpWin)
	//	{
	//		if(!popUpWin.closed) popUpWin.close();
	//	}
	
	if (align == 'center') {
		var left = (screen.width/2) - width/2;
		var top = (screen.height/2) - height/2;
	} else {
		var left = 0;
		var top = 0;
	}
	
	popUpWin = open(URLStr, 'popUpWin'+msgid, 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
}


function showIM(msgid)
{
	popUpWindow(doc_root+'showinstantmsg.php?id=' + msgid, 'center', 320, 260, msgid);
}


function popUpWindow(URLStr, align, width, height, msgid)
{
	if (use_popups == false) {
		window.location.href = URLStr;
		return;
	}
	
	if (align == 'center') {
		var left = (screen.width - width) / 2;
		var top = (screen.height - height) / 2;
	} else {
		var left = 0;
		var top = 0;
	}
	
	// debug:
	// popUpWin = open(URLStr, 'popUpWin'+msgid, 'toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes,scrollbar=no,resizable=yes,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
	popUpWin = open(URLStr, 'popUpWin'+msgid, 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbar=no,resizable=yes,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
	popUpWin.opener.name = 'abc1';
}


function popUpScrollWindow(URLStr, align, width, height, token)
{
	if (use_popups == false) {
		window.location.href = URLStr;
		return;
	}
	
	width = Math.min(width, screen.availWidth - 50);
	height = Math.min(height, screen.height - 50);
	
	if (align == 'center') {
		var left = (screen.availWidth - width) / 2;
		var top = (screen.height - height) / 2;
	} else if (align == 'top') {
		var left = (screen.availWidth - width) / 2;
		var top = 0;
	} else {
		var left = 0;
		var top = 0;
	}
	
	popUpWin = open(URLStr, 'popup_'+token, 'toolbar=no,location=no,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,copyhistory=yes,width='+width+',height='+height+',left='+left+',top='+top+',screenX='+left+',screenY='+top+'');
	popUpWin.focus();
	
	return false;
}

function popUpScrollWindow2(url, align, width, height, token)
{
	if (use_profilepopups == false) {
		window.location.href = url;
		return;
	}
	
	width = Math.min(width, screen.availWidth);
	height = Math.min(height, screen.height - 275);
	
	if (align == 'center') {
		var left = (screen.width - width) / 2;
		var top = (screen.height - height) / 2;
	} else if (align == 'top') {
		var top = 0;
		var left = (screen.width - width) / 2;
	} else {
		var top = 0;
		var left = 0;
	}
	
	popUpWin = open(url, 'popup_'+token, 'toolbar=no,location=yes,directories=no,status=yes,menubar=no,scrollbars=yes,resizable=yes,copyhistory=yes,width='+width+',height='+height+',left='+left+',top='+top+',screenX='+left+',screenY='+top+'');
	popUpWin.focus();
	
	return false;
}

var prevRow = null;

function toggleRow(rwId, num)
{
	if (prevRow != null) {
		prevRow.style.display ='none';
	}
	
	prevRow = obj = document.getElementById(rwId);
	obj.style.display ='inline';
	
	for (i=0; i<document.getElementById('tblSelect').length; i++) {
		if (i == num) {
			document.getElementById('tblSelect')[i].className = "s_table_blue";
		} else {
			document.getElementById('tblSelect')[i].className = "s_table_white";
		}
	}
}

/*************
 SITE POLL
**************/

function pollvote(id)
{
	var f = document.getElelmentById('frmpoll');
	var num_options = f.sel.length;
	var selection = 0;
	
	for (var i=0; i < num_options; i++) {
		if (f.sel[i].checked) {
			selection = f.sel[i].value;
		}
	}
	
	if (selection == 0) {
		alert('Please select one of the options.');
		return;
	}
	
	if (use_popups == false) {
		window.location.href = doc_root+'pollvote.php?pollid='+id+'&sel='+selection;
		return;
	}
	
	var width = 600;
	var height = 378;
	var left = (screen.width - width) / 2;
	var top = (screen.height - height) / 2;
	
	openpopup = window.open(doc_root+'pollvote.php?pollid='+id+'&sel='+selection,'poll','width='+width+',height='+height+',left='+left+',top='+top+',resizable=yes,scrollbars=yes,status=no');
	openpopup.opener.name = 'abc';
}

function pollresult(id)
{
	if (use_popups == false) {
		window.location.href = doc_root+'pollresult.php?pollid='+id;
		return;
	}
	
	var width = 700;
	var height = 400;
	var left = (screen.width - width) / 2;
	var top = (screen.height - height) / 2;
	
	openpopup = window.open(doc_root+'pollresult.php?pollid='+id,'poll','width='+width+',height='+height+',left='+left+',top='+top+',resizable=yes,scrollbars=yes,status=no');
	openpopup.opener.name = 'abc';
}

function userpollresult(id)
{
	if (use_popups == false) {
		window.location.href = doc_root+'userpollresult.php?pollid='+id;
		return;
	}
	
	var width = 700;
	var height = 400;
	var left = (screen.width - width) / 2;
	var top = (screen.height - height) / 2;
	
	openpopup = window.open(doc_root+'userpollresult.php?pollid='+id,'poll','width='+width+',height='+height+',left='+left+',top='+top+',resizable=yes,scrollbars=yes,status=no');
	openpopup.opener.name = 'abc';
}

function pollprevious()
{
	if (use_popups == false) {
		window.location.href = doc_root+'pollprevious.php';
		return;
	}
	
	var width = 700;
	var height = 400;
	var left = (screen.width - width) / 2;
	var top = (screen.height - height) / 2;
	
	openpopup = window.open(doc_root+'pollprevious.php','poll','width='+width+',height='+height+',left='+left+',top='+top+',resizable=yes,scrollbars=yes,status=no');
	openpopup.opener.name = "abc";
}

/************************
	GENERAL PURPOSE
*************************/

function selectRdo(form,rdo)
{
	for (i = 0; i < form.length; i++) {
		if (form.elements[i].type=='radio' && form.elements[i].name=='searchby' && form.elements[i].value == rdo) {
			form.elements[i].checked = true;
		}
	}
}

function checkAll(form, name, val)
{
	for (i = 0; i < form.length; i++) {
		if (form.elements[i].type == 'checkbox' && form.elements[i].name == name) {
			form.elements[i].checked = val;
		}
	}
}

function datefromtovalid(sy,sm,sd,ey,em,ed,msg)
{
	month = new Array("JAN", "FEB", "MAR", "APR", "MAY", "JUN", "JUL", "AUG", "SEP", "OCT", "NOV", "DEC");
	
	var syear=sy[sy.selectedIndex].value;
	var smonth=sm[sm.selectedIndex].value;
	var sdays=sd[sd.selectedIndex].value;
	var eyear=ey[ey.selectedIndex].value;
	var emonth=em[em.selectedIndex].value;
	var edays=ed[ed.selectedIndex].value;
	
	for (var count=0;count<12;count++) {
		if ((smonth== month[count])) {
			smonth=count;
		}
		if ((emonth== month[count])) {
			emonth=count;
		}
	}
	
	from_date = new Date(syear,smonth,sdays);
	to_date = new Date(eyear,emonth,edays);
	
	if (from_date > to_date) {
		alert(msg);
		return false;
	}
	
	return true;
}


function DateCheck(syr, smt, sdt, msg)
{
	hdt=sdt[sdt.selectedIndex].value;
	hmt=smt[smt.selectedIndex].value;
	hyr=syr[syr.selectedIndex].value;
	
	hms_maxval=31;
	
	if ((hmt=="APR") || (hmt=="JUN") || (hmt=="SEP") || (hmt=="NOV")) {
		hms_maxval=30;
	}
	
	if ((hmt=="FEB") && (hyr%4)==0) {
		hms_maxval=29;
	}
	
	if ((hmt=="FEB") && (hyr%4)!=0) {
		hms_maxval=28;
	}
	
	if (parseInt(hdt) > hms_maxval) {
		alert(msg);
		return false;
	}
	
	return true;
}


// After clicking a link to delete something, pops up a window asking the user to confirm
// If the user clicks ok, it tacks delete=Y to the url. If the user clicks cancel, 
// it returns false which leaves the user on the page. 
// 
// Ex. <a href="bloglist.php?id=2&cmd=delete" onclick="return confirmLink(this, 'Blog Entry')">Delete</a>
// 
function confirmLink(theLink, theMessage)
{
	var is_confirmed = confirm(theMessage);
	
	if (is_confirmed) {
		theLink.href += '&delete=Y';
	}
	
	return is_confirmed;
}


// After clicking a submit button, pops up a window asking the user to confirm 
// If the user clicks ok, it proceeds. If the user clicks cancel, it cancels submitting
// the form
// 
// ex. <input type="submit" class="formbutton" value="Delete" name="delete" onclick="return confirmButton('Blog Entries')" />
// 
function confirmButton(theMessage)
{
	return confirm(theMessage)
}

// The next two functions comprise the text counting for a text box.
// 
function countCheck(countLimit)
{
	if (document.getElementById('comment').value.length > countLimit) {
		alert('Too many characters in the comment box!');
		document.getElementById('comment').comment.focus();
		return false;
	}
	
	return true;
}


function countText(countLimit)
{
	var v = document.getElementById('comment').value;
	
	if (v.length > countLimit) {
		alert('Too many characters in the comment box!');
		document.getElementById('comment').value = v.substring(0, countLimit);
		v = document.getElementById('im_msg').value;
	}
	
	document.getElementById('counter').value = v.length;
}

function openInParentWindow(url)
{
	window.opener.document.location.href=url;
	window.opener.focus();
}

/*********************************
DATA VALIDATION

has been converted to regular expressions
tests have been reorganized
not in use:
checkOK = "0123456789.,[]{}=+-_#,/ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ()_:;'\\*^%$@<>?'\"\'";
this one is only used in admin for things like profile question, profile answers, rating names, poll questions, poll options etc
we may assume admin knows what he is doing, and all chars are entity encoded anyway
we keep this test anyway, but always return true
**********************************/

function CheckFieldString(type, formField, strMsg)
{
	var checkStr = formField.value;
	// var checkOK;
	
	if (type == 'noblank')
	{
		if (checkStr == '') {
			ErrorCount++;
		 	ErrorMsg[ErrorCount] = strMsg;
			if (ErrorCount == 1) formField.focus();
			return false;
		}
		return true;
	}
	
	if (type == 'whitespace')
	{
		var regexp = /^\S+$/;
	}
	else if (type == 'charset_username')
	{
		var regexp = regexp_username;
	}
	else if (type == 'charset_password')
	{
		var regexp = regexp_password;
	}
	else if (type == 'charset_name')
	{
		var regexp = regexp_name;
	}
	else if (type == 'charset_location')
	{
		var regexp = regexp_location;
	}
	else if (type == 'charset_full')
	{
		// checkOK = "0123456789.,[]{}=+-_#,/ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz ()_:;'\\*^%$@<>?'\"\'";
		return true;
	}
	else if (type == 'email')
	{
		// modified to allow gmail accounts with a + in the username
		var regexp = /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,7})+$/;
		// old additional check for valid characters. regexp should be sufficient
		// checkOK = "0123456789_-@.ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
	}
	else if (type == 'integer')
	{
		// must not start with 0
		var regexp = /^[1-9]+\d*$/;
	}
	else if (type == 'decimal')
	{
		// must have some digits before and after the period. integers are entered like 3.0
		var regexp = /^[1-9]+\d*\.\d+$/;
	}
	else if (type == 'phone')
	{
		// only check for valid characters and do not allow blanks. improve later
		var regexp = /^[\d\-\+\(\)]+$/;
	}
	else if (type == 'URL')
	{
		// only check for valid characters. improve later
		var regexp = /^[a-zA-Z0-9\.\:\/\\]+$/;
	}
	else if (type == 'path')
	{
		// only check for valid characters. improve later
		var regexp = /^[a-zA-Z_0-9 #,\.\+\-\(\)\/\\]+$/;
	}
	else
	{
		ErrorCount++;
		ErrorMsg[ErrorCount] = "Unknown data validation '" + type + "' for field '" + formField.name + "'";
		return false;
	}
	
	if (checkStr.length == 0) {
		return true;
	}
	
	if (regexp.test(checkStr)) {
		return true;
	}
	
	ErrorCount++;
	ErrorMsg[ErrorCount] = strMsg ;
	if (ErrorCount == 1) formField.focus();
	return false;
}


function validateLogin(form)
{
	ErrorMsg = new Array();
	ErrorMsg[0] = "------------------------- The Following Errors Occured -------------------------" + String.fromCharCode(13);
	
	CheckFieldString("noblank", form.txtusername, "{$lang.signup_js_errors.username_noblank}");
	CheckFieldString("noblank", form.txtpassword, "{$lang.signup_js_errors.password_noblank}");
	
	CheckFieldString("charset_username", form.txtusername, "{$lang.signup_js_errors.username_charset}");
	CheckFieldString("charset_password", form.txtpassword, "{$lang.signup_js_errors.password_charset}");
	
	// concat all error messages into one string
	result = '';
	
	if (ErrorCount > 0) {
		alert(ErrorMsg[1]);
		return false;
	}
	
	return true;
}

/***********************
	CAPTCHA
************************/

function reloadCaptcha()
{
	var now = new Date();
	var capObj = document.getElementById('spam_code_img');
	
	if (capObj) {
		capObj.src = capObj.src + (capObj.src.indexOf('?') > -1 ? '&' : '?') + Math.ceil(Math.random()*(now.getTime()));
	}
}

/* UTILITIES */

function checkImageSizes()
{
    // Find images which have width or height different than their natural
    // width or height, and give them a stark and ugly marker, as well
    // as a useful title.
    var imgs = document.getElementsByTagName("img");
    for (i = 0; i < imgs.length; i++) {
        var img = imgs[i];
        if (img.naturalWidth) {
            if ((img.naturalWidth != 1) && (img.naturalHeight != 1)) {
                // For each image with a natural width which isn't
                // a 1x1 image, check its size.
                var wrongWidth = (img.width != img.naturalWidth);
                var wrongHeight = (img.height != img.naturalHeight);
                if (wrongWidth || wrongHeight) {
                    img.style.border = "3px red dotted";
                    img.style.margin = "-3px";
                    img.style.background = "yellow";
                    img.title = "Forced to wrong size: " +
                        img.width + "x" + img.height + ", natural is " +
                        img.naturalWidth + "x" + img.naturalHeight + "!";
                }
            }
        }
    }
}

function checkBrowserName(name)
{  
	var agent = navigator.userAgent.toLowerCase();  
	if (agent.indexOf(name.toLowerCase())>-1) {  
		return true;  
	}  
	return false;  
}  