﻿// form.js
// Copyright © 2003-2004, Intellecta Systems AB.
// Encoded as UTF-8.


// ===================================================================
// Functions for working document items in general.

// -------------------------------------------------------------------
// Gets a document element by its id.
// ARG: elementId - the unique id of the element.
// RET: the element or null if not found.
// -------------------------------------------------------------------
function GetElement(elementId)
{
	if( document.getElementById )
		return document.getElementById(elementId);
	else if( document.all )
		return document.all[elementId];
	else
	{
		// This is bad. No implementation in browser.
		return null;
	}
}

// -------------------------------------------------------------------
// Shows a hidden control on the page.
// ARG: control - the control itself or it's id.
// ARG: doShow [opt] - optional true/false value if the control
//  shoud be show or hidden.
// -------------------------------------------------------------------
function ShowControl(control, doShow)
{
	if( typeof(control)=="string" )
		control = GetElement(control);
	
	if( control!=null )
	{
		if( doShow==false )
			HideControl(control);
		else
		{
			//control.style.visibility = "visible";
			control.style.display = "";//"inline";
		}
	}
}

// -------------------------------------------------------------------
// Hides a visible control on the page.
// ARG: control - the control itself or it's id.
// -------------------------------------------------------------------
function HideControl(control)
{
	if( typeof(control)=="string" )
		control = GetElement(control);

	if( control!=null )
	{
		//control.style.visibility = "hidden";
		control.style.display = "none";
	}
}

// -------------------------------------------------------------------
// Disable a control on the page.
// ARG: control - the control itself or it's id.
// -------------------------------------------------------------------
function DisableControl(control)
{
	if(typeof (control) == "string")
		control = GetElement(control);

	if(control != null)
	{
		control.disabled = "disabled";
	}
}


// ===================================================================
// Functions for working with form input fields.

// -------------------------------------------------------------------
// Returns the value of a html form input as a string.
// ARG: item - html form input. Such as.
// RET: a string with the value of the input object.
// -------------------------------------------------------------------
function FormGetValue(item)
{
	// NOTE: "select-one" is the type name of 
	// select boxes in Netscape up to v4.7

	if( item.type=="checkbox" )
		return item.checked?'1':'0';
	else if( item.type=="select" || item.type=="select-one" ) 
		return item.options[item.selectedIndex].value;
	else if( item.type=="radio" )
		return item.checked?item.value:'';
	else
		return item.value;
}

// -------------------------------------------------------------------
// Sets the value of a html form input from a string. Follows the 
// same rules of values as FormGetValue().
// ARG: item - html form input. Such as.
// ARG: val - a string with the new value of the input object.
// RET: true - if changing value succeded.
// -------------------------------------------------------------------
function FormSetValue(item, val)
{
	// NOTE: "select-one" is the type name of 
	// select boxes in Netscape up to v4.7

	if( item.type=="checkbox" )
	{
		if( val=="1" )
			item.checked = true;
		else
			item.checked = false;
	}
	else if( item.type=="select" || item.type=="select-one" )
	{
		var count = item.options.length;
		for( var i=0; i<count; i++ )
		{
			if( item.options[i].value==val )
			{
				item.selectedIndex = i;
				return true;
			}
		}
		return false;
	}
	else if( item.type=="radio" )
	{
		var form = item.form;
		var name = item.name;
		var count = form.elements.length;
		var found = false;
		for( var i=0; i<count; i++ )
		{
			item = form.elements[i];
			if( item.type=="radio" && item.name==name )
			{
				item.checked = (item.value==val);
				if( item.checked )
					found = true;
			}
		}
		return found;
	}
	else
	{
		item.value = val;
	}

	return true;
}


// ===================================================================
// Functions for working with PHF coding.

// -------------------------------------------------------------------
// Generates a phf encoded string from a html form.
// ARG: form - html form object.
// ARG: prefix - a name prefix for form input fields. Only inputs 
//      whose name begins with <prefix> are included int the phf 
//      string. The prefix is removed from the name during processing.
// RET: a phf encoded string.
// -------------------------------------------------------------------
function PhfEncodeForm(form,prefix)
{
	var item;
	var phf = "";
	var n = form.length;
	if( !prefix )
		prefix = "";
	for( i=0; i<n; i++ )
	{
		item = form.elements[i];
		if( item.name.indexOf(prefix)!=0 )
			continue; // Does not start with prefix.
		
		if( item.type=="radio" && !item.checked )
			continue;

		phf += PhfArg(
			form.elements[i].name.substr(prefix.length),
			FormGetValue(form.elements[i])
		);
	}
	return phf;
}

// -------------------------------------------------------------------
// Encodes a name-value-pair into a phf string. The string can be
// concatenated with additional phf strings.
// ARG: name - name of the name-value-pair.
// ARG: value - value of the name-value-pair. Can be null.
// RET: a string with the phf encoded name-value-pair.
// -------------------------------------------------------------------
function PhfArg(name, value)
{
	name = String(name);
	name = name.replace(/\\/g,"\\b");
	name = name.replace(/&/g,"\\a");
	name = name.replace(/=/g,"\\e");

	if( value==null )
		return "&"+name;

	value = String(value);
	value = value.replace(/\\/g,"\\b");
	value = value.replace(/&/g,"\\a");
	value = value.replace(/=/g,"\\e");

	return "&"+name+"="+value;
}

// -------------------------------------------------------------------
// Decodes a value from a phf string. 
// ARG: phf - a phf encoded string.
// ARG: name - name of the name-value-pair.
// RET: a value of name-value-pair. A string, null or undefined.
// -------------------------------------------------------------------
function PhfValue(phf, name)
{
	var pos = phf.indexOf("&"+name);
	if( pos==-1 ) return undefined;
	else pos++;

	var sep = phf.indexOf("=");
	if( sep==-1 ) sep = phf.length;

	var next = phf.indexOf("&");
	if( next==-1 ) next = phf.length;

	if( sep>=phf )
		return null;

	var value = phf.substring(sep+1,next);
	value = value.replace(/\\a/g,"&");
	value = value.replace(/\\e/g,"=");
	value = value.replace(/\\b/g,"\\");
	return value;
}



// ===================================================================
// Functions for working with select boxes.

// -------------------------------------------------------------------
// Deletes all selected items in a select box.
// ARG: sel - select box whose selected items should be deleted.
// RET: the number of items that were deleted.
// -------------------------------------------------------------------
function DeleteSelectedItems(sel)
{
	var count = 0;
	for( var i=0; i<src.options.length; i++ )
	{
		if( src.options[i].selected )
		{
			dst.options[dst.options.length] =
				new Option(src.options[i].text, src.options[i].value);
			count++;
		}
	}
	return count;	
}

// -------------------------------------------------------------------
// Copies all selected items in a select box to another select box.
// ARG: src - select box, whose selected items are to be copied.
// ARG: dst - select box, where new items are inserted.
// RET: the number of copied items.
// -------------------------------------------------------------------
function CopySelectedItems(src, dst)
{
	var count = 0;
	for( var i=0; i<src.options.length; i++ )
	{
		if( src.options[i].selected )
		{
			dst.options[dst.options.length] =
				new Option(src.options[i].text, src.options[i].value);
			count++;
		}
	}
	return count;	
}

// -------------------------------------------------------------------
// Counts the number of selected items in a select box.
// ARG: sel	-	select box whose selected items to count.
// RET: The number of selected items in the select box.
// -------------------------------------------------------------------
function CountSelectedItems(sel)
{
	var count = 0;
	for( var i=0; i<sel.length; i++ )
	{
		if( sel.options[i].selected )
			count++;
	}
	return count;
}

// -------------------------------------------------------------------
// Creates a list of values for all selected items in a select box.
// ARG: sel	-	select box to create list from.
// RET: Semicolon separated list of selected values.
// -------------------------------------------------------------------
function GetSelectedItemValueList(sel)
{
	var list = "";
	var first = true;
	
	for( var i=0; i<sel.length; i++ )
	{
		if( sel.options[i].selected )
		{
			if( first )
			{
				first = false;
				list += sel.options[i].value;
			}
			else
			{
				list += ";" + sel.options[i].value;
			}
		}
	}
	return list;
}


// ===================================================================
// Functions for working with ASP.NET client-side validation.
// NOTE: Client-side validation is only supported for MS IE.
// All functions behave as if all is valid for all other browsers. 

// -------------------------------------------------------------------
// Validates a html control using a client side ASP.NET validator.
// ARG: control - the html control to validate or its id.
// RET: true if all validators connected to the control was valid
//  or false if at least one was invalid.
// -------------------------------------------------------------------
function ValidateControl(control)
{
	if( !ClientSideValidationSupported() )
		return true;

	// Check if user used id of the control instead of control itself.
	if( typeof(control)=="string" )
	{
		control = GetElement(control);
		if( control==null )
			return null;
	}
		
	// Validate validators for control.
	var valid = true;
	for( var i=0; i<Page_Validators.length; i++ ) 
	{
		var v = Page_Validators[i];
		if( v.controltovalidate==control.id )
		{
			ValidatorValidate(v);
			if( !v.isvalid )
				valid = false;
		}
	}
	return valid;
}

// -------------------------------------------------------------------
// Validate html controls using client side ASP.NET validators.
// ARG: controlprefix - the prefix of the html controls to validate.
// RET: true if matching controls are valid, otherwise false.
// -------------------------------------------------------------------
function ValidateControls(controlprefix)
{
	if( !ClientSideValidationSupported() )
		return true;

	var valid = true;
	for( var i=0; i<Page_Validators.length; i++ )
	{
		var val = Page_Validators[i];
		if(val.controltovalidate.indexOf(controlprefix) >= 0)
		{
			ValidatorValidate(val);
			if(!val.isvalid)
				valid = false;
		}
	}
	return valid;
}

// -------------------------------------------------------------------
// Validate html controls using client side ASP.NET validators.
// ARG: idprefix - the prefix of the validator identifiers to validate.
// RET: true if matching validators are valid, otherwise false.
// -------------------------------------------------------------------
function ValidateValidators(idprefix)
{
	if( !ClientSideValidationSupported() )
		return true;

	var valid = true;
	for( var i=0; i<Page_Validators.length; i++ )
	{
		var v = Page_Validators[i];
		if( v.id.indexOf(idprefix)>=0 )
		{
			ValidatorValidate(v);
			if( !v.isvalid )
				valid = false;
		}
	}
	return valid;
}

// -------------------------------------------------------------------
// Sets focus to the first control on the page whose ASP.NET validator
// is not valid. This function does NOT do validation but assumes that 
// validation has already been called.
// RET: Reference to the control to which focus was moved or null
//  if there was no invalid control.
// -------------------------------------------------------------------
function FocusOnFirstInvalidControl()
{
	if( !ClientSideValidationSupported() )
		return null;
		
	for( var i=0; i<Page_Validators.length; i++ ) 
	{
		var v = Page_Validators[i];
		if( !v.isvalid )
		{
			var control = GetElement(v.controltovalidate);
			if( control!=null )
				control.focus();
			return control; 
		}
	}
	return null;
}


// -------------------------------------------------------------------
// Checks if client-side validation is supported in this browser.
// RET: True if supported.
// -------------------------------------------------------------------
function ClientSideValidationSupported()
{
	return typeof(Page_Validators)!="undefined";
}

// -------------------------------------------------------------------
// Event function for validating a custom date.
// This function requires that the page includes /js/tims.js
// ARG: source - validator sending the event.
// ARG: args - validation event arguments.
// -------------------------------------------------------------------
function ValidateCustomDate(source, args)
{
	args.IsValid = CheckDate(args.Value);
	
	if( args.IsValid )
	{
		var id = source.controltovalidate;
		var c = GetElement(id);
		var date = FormatDate(c.value);
		date = date.substr(0,4)+"-"+date.substr(4,2)+"-"+date.substr(6,2);
		if( c.value!=date )
			c.value = date;
	}
}


