/*

	Version control
	0.1		Development version			Mike Gray		December 2003

	Library Functions:
*/

/////////////////////////// General ////////////////////////////
function IsControlOnForm(sCtlName,sForm) {
// return true if sCtlName is a control on the form
	var bFound=false;

	with (top.document) {
		for (i = 0; i < forms[sForm].elements.length; i++) {	
			if (forms[sForm].elements[i].name==sCtlName) {
				bFound=true;
			}
		}
	}
	return(bFound);
}

function isNumeric(sValue)	{
	var re=/^[0-9.]+$/;
	return(re.test(sValue));
}

function isPhone(sValue)	{
	var re=/^[0-9 ]+$/;
	return(re.test(sValue));
}

function isTime12(sValue)
{
	var re=/^[0-1][0-9]:[0-5][0-9]$/;
	var iHour;

	if ((re.test(sValue)) && (sValue.substring(0,2) <= 12))
		return true;
	return false;
}

function isTime24(sValue)	{
	var re=/^[0-2][0-9]:[0-5][0-9]$/;
	var iHour;
	if ((re.test(sValue)) && (sValue.substring(0,2) <= 23)) 
		return true;
	return false;
}

function isDate(sValue)	{
	var re=/^[0-3][0-9]\/[01][0-9]\/[2][0-9][0-9][0-9]$/;
	var bOK;

	bOK=true;
	if (re.test(sValue)) {
		if (sValue.substring(0,2) > 31) 
			bOK=false;
		if (sValue.substring(3,5) > 12) 
			bOK=false;
	} else {
		bOK=false;
	}
	return(bOK);
}

////////////////////  Processing /////////////////////////////
function ProcessForm(URL,sFrm) {
	// scan form and form a query string with <Control>T1<Value>23
	var form = document.getElementById('frmMain');
	if (form == null)
	{
		window.location = URL;
		return;
	}
	
	var sFormControls = '';
	for (i = 0; i < form.elements.length; i++) {
		element = form.elements[i];
		
		if (element.name == 'txtDataStore')
			continue;
		
		switch (element.type)
		{
			case 'text': 
				sFormControls += "[Name]"+element.name+"[Type]"+element.type+"[Value]"+element.value;
				break;
			case 'hidden':
				sFormControls += "[Name]"+element.name+"[Type]"+element.type+"[Value]"+element.value;
				break;
			case 'textarea':
				sFormControls += "[Name]"+element.name+"[Type]"+element.type+"[Value]"+element.value;
				break;
			case 'checkbox':
				sFormControls += "[Name]"+element.name+"[Type]"+element.type+"[Value]"+element.checked;
				break;
			case 'select-one':
				val = '';
				if (element.selectedIndex >= 0)
					val = element.options[element.selectedIndex].value;
				sFormControls += "[Name]"+element.name+"[Type]"+element.type+"[Value]"+val;
				break;
			case 'select-multiple':
				found = 0;
				val = '';
				for (j = 0; j < element.options.length; j++)
				{
					if (element.options[j].selected)
					{
						++found;
						if (found > 1)
							val += ',';
						val += element.options[j].value;
					}
				}
				sFormControls += "[Name]"+element.name+"[Type]"+element.type+"[Value]"+val;
				break;
			case 'radio':
				if (element.checked)
					sFormControls += "[Name]"+element.name+"[Type]"+element.type+"[Value]"+element.value;
				break;
		}
	}
	
	//copy form control to text box - unable to pass in query string as it can be very long and function bombs out
	form.elements['txtDataStore'].value = sFormControls;
	//alert(typeof(form));
	//form.elements['sMode'].value = 'ProcessForm';
	//form.elements['sURL'].value = escape(URL);
	//form.elements['sForm'].value = sFrm;

	form.action="Index.php?module=Main&action=ProcessForm&sMode=ProcessForm&sURL="+escape(URL)+"&sForm="+sFrm;
	form.submit();
}

/////////////////////// Data integrity routines ///////////

function CheckData(ctl,sType) {
	if (ctl.value != '') {
	// only check if value is not blank
		switch (sType) {
			case 'Number':if (!isNumeric(ctl.value)) {
									alert("A number is required here. \n\n You have entered '" + ctl.value +"'. \n Please correct.");
									ctl.value="";
									ctl.focus;
								}
								break;
			case 'Phone':if (!isPhone(ctl.value)) {
									alert("A phone number is required here. \n\nPhone numbers can only have numbers (0-9) and spaces. \n\nYou have entered '" + ctl.value +"'. \n Please correct.");
									ctl.value="";
									ctl.focus;
								}
								break;
			case 'Time24':if (!isTime24(ctl.value)) {
								alert("A time is required here. \n\nTime must be entered as HH:MM using the 24-hr format. \n\nYou have entered '" + ctl.value +"'. \n\n Please correct.");
								ctl.value="";
								ctl.focus;
							}
							break;
			case 'Time12':if (!isTime12(ctl.value)) {
								alert("A time is required here. \n\nTime must be entered as HH:MM using the 12-hr format. \n\nYou have entered '" + ctl.value +"'. \n Please correct. \nAlso make sure you correctly specify the 'am', 'pm' option");
								ctl.value="";
								ctl.focus;
							}
							break;
			case 'Date':if (!isDate(ctl.value)) {
								alert("A date is required here. \n\nDates must be entered as DD/MM/YYYY. \n\nYou have entered '" + ctl.value +"'. \n Please correct.");
								ctl.value="";
								ctl.focus;
							}
							break;
		}
	}
}


function ClearForm(sForm) {
	with (top.document) {
		for (i = 0; i < forms[sForm].elements.length; i++) {	
			switch (forms[sForm].elements[i].type) {
				case 'text': 
							forms[sForm].elements[i].value="";
							break;
				case 'textarea': 
							forms[sForm].elements[i].value="";
							break;
				case 'checkbox':
							forms[sForm].elements[i].checked;							
							break;
				case 'select-one': 
							forms[sForm].elements[i].selectedIndex=0;
							break;
			}
		}
	}
}

function PopUp(sURL,sSize) {
	var winVar;
	switch (sSize) {
		case 'Small':
				winVar=window.open(sURL,'PopUp','toolbar=no,status=no,width=500,height=200,resizable=yes,scrollbars=yes');
				break;
		case 'Medium':
				winVar=window.open(sURL,'PopUp','toolbar=no,status=no,width=600,height=350,resizable=yes,scrollbars=yes');
				break;
		case 'Large':
				winVar=window.open(sURL,'PopUp','toolbar=no,status=no,width=600,height=800,resizable=yes,scrollbars=yes');
				break;
	}
}

function SingleChkSelect(sCtlName,sCurrent) {
	var i;
	with (top.document) {
		for (i = 0; i < forms[0].elements.length; i++) {	
			switch (forms[0].elements[i].type) {
				case 'checkbox':
							if (forms[0].elements[i].name != sCurrent) 
								forms[0].elements[i].checked=false;		
							break;
			}
		}
	}
}

function TableItemSelected(sCtlName) {
	var i,iReturn;
	var sCtl;

	iReturn=0;
	with (top.document) {
		for (i = 0; i < forms[0].elements.length; i++) {	
			switch (forms[0].elements[i].type) {
				case 'checkbox':
							sCtl=forms[0].elements[i].name;
							if (sCtl.substring(0,sCtlName.length) == sCtlName && forms[0].elements[i].checked) 
								iReturn=sCtl.substring(sCtlName.length+1,sCtl.length);
							break;
			}
		}
	}
	return(iReturn);
}

function MenuClick(sURL) {
	ProcessForm(sURL,"");
}

function ConfirmAction(action, description)
{
	var msg = '';
	
	msg += 'Are you sure you wish to ';

	if (description != null)
		msg += description;
	else
		msg += 'perform the requested action';

	msg += '?';
	
	if (confirm(msg))
	{
		window.location = action;
	}
}

function displayHelp(messageToDisplay)
{
	//return;
	var win = window.open('', 'message', 'toolbar=no,location=no,status=no,scrollbars=yes,width=400,height=350');
	with (win.document)
	{
		open();
		write('<html>');
		write('<head>');
		write('<title>Further Information...</title>');
		write('</head>');
		write('<body>');
		write(messageToDisplay);
		write('</body>');
		write('</html>');
		close();
	}
}

function editLabel(label)
{
	window.open('Index.php?module=Administration&action=EditLabel&label=' + label, 'label', 'width=350,height=250');
}

function autoFillTextInput(controlId, message)
{
	var element = document.getElementById(controlId);
	if (element != null)
	{
		element.value = message;
		element.onfocus = function() { if (this.value == message) { this.value = ''; } };
		element.onblur = function() { if (this.value == '') { this.value = message; } };
	}
}

function externalLink(url)
{
	window.open('Index.php?module=Main&action=ExternalLink&redirect=' + url);
}

function showDisclaimer()
{
	window.open('Index.php?module=Main&action=Disclaimer', 'disclaimer', 'toolbar=no,location=no,status=no,scrollbars=yes,width=500,height=450');
}
