/*			INFO 				

Who created this?	-> Alfred Tan
When was it?		-> 28-05-2003
Updated on?		-> 17th March 2004
With what?		-> Javascript

Purpose			-> contains general javascript functions

*/


/* 	+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	+ This function will loop through all the elements and check if the ALT of the object is NOT empty, then
	+ validate that object. If the validation fails, it will use the ALT value and display it as an ALERT msg.
	+ TO USE: pass the FORM object
	+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */
	
function checkForm()
{
	// -- VARIABLES --
	var formObj = arguments[ 0 ];	// assign the form object
	var intCounter;				// a counter to loop through all elements in the FORM object
	var intTotalElements;			// to hold all the elements in the FORM object;
	
	// since radio buttons in the same group have the same name, we need to keep track of it so that
	// one radio group will only be validated once...
	var strRadioName = '';			// will hold the last radio group validated...
	// -- END OF VARIABLES --
	
	intTotalElements = formObj.length;	// get total elements in the FORM object
	
	// go through each element in the FORM
	intCounter = 0;
	// mainLoop:
	for ( ; intCounter < intTotalElements; intCounter++ )
	{

		// check the ALT tag 
		// - must NOT be UNDEFINED and,
		// - must NOT be empty

		if ( ( typeof( formObj.elements[ intCounter ].alt ) !== "undefined" ) && ( whiteSpaceOnly(formObj.elements[ intCounter ].alt ) == false ) )
		{

			 // IF can reach here, meaning the ALT tag is present... so, validate it according to the type

            // IF the element is disabled, skip it
            if ( formObj.elements[ intCounter ].disabled == true )
            {
                continue;
            }

			// if the object is a TEXTBOX or TEXTAREA, send in for validation
			//if ( ( formObj.elements[ intCounter ].type == "text" ) || ( formObj.elements[ intCounter ].type == "textarea" ) || ( formObj.elements[ intCounter ].type == "password" ) ) 
			if ( ( formObj.elements[ intCounter ].type == "text" ) || 
					( formObj.elements[ intCounter ].type == "textarea" ) || 
					( formObj.elements[ intCounter ].type == "file" ) || 
					( formObj.elements[ intCounter ].type == "password" ) ) 
			{
				// uses "validateText" function to validate text fields...
				// it will return 1 if the validation fails.
				if ( validateText( formObj.elements[ intCounter ] ) == false)
				{
					alert( formObj.elements[ intCounter ].alt);		// give an alert msg
					return false;	// exit function
				}
			}
			// if the object is a DROP DOWN LIST, sned in for validation
			else if ( formObj.elements[ intCounter ].type == "select-one" )
			{

				// uses "validateSelect" function validate dropdown list.
				// will return 1 if the validation fails.
				if ( validateSelect( formObj.elements[ intCounter ] ) == false )
				{
					alert( formObj.elements[ intCounter ].alt );		// give an alert msg
					return false;	// exit function
				}
			} // end if		
			// if the object is a RADIO, send in for validation
			else if ( formObj.elements[ intCounter ].type == "radio" )
			{
				// if the current radio name is not same as the previous one
				if ( formObj.elements[ intCounter ].name != strRadioName )
				{
					// assign the current radio name to var
					strRadioName = formObj.elements[ intCounter ].name;
					// validate that at least one of the radio group is selected.
					// if it return false, meaning none is selected
					if ( validateRadio( formObj.elements[ intCounter ] ) == false )
					{
						alert( formObj.elements[ intCounter ].alt );		// give an alert msg
						formObj.elements[ intCounter ].focus();
						return false;	// exit function
					}					
				}
			} // end if						
		} // end if
	} // end for
	
	// if the function can reach here, means all the elements are validated!
	return true;
	
} // end function

// ------------------------------------ end of checkForm -------------------------------------------------------------------


/* 	+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	+ This function will check if a textbox or textarea is empty or contain whitespaces only
	+ TO USE: pass the textbox OR textarea object
	+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */


function validateText()
{
	// -- VARIABLES --
	var textObj = arguments[ 0 ];	// assign text object (can be textbox or textarea)
	var strTextType;				// to hold the textObj type that will be used for the Alert msg.
	// -- END OF VARIABLES --

	// make sure textbox is not empty, if it is, give a msg
	if ( textObj.value.length == 0 )
	{
		textObj.focus();	// set focus
		return false;			// exit function
	}

	// check if textbox contains white spaces only, if it does, give a msg
	if ( whiteSpaceOnly( textObj.value ) == true )
	{
		textObj.value = "";	// remove all whitespaces
		textObj.focus();	// set focus
		return false;			// exit function
	}
} // end function
// ------------------------------------ end of validateText -------------------------------------------------------------------


/* 	+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	+ This function will check if an item of a SELECT object is selected. This function will only validate if 
	+ the first OPTION VALUE - which is the default one - has an empty value.
	+ TO USE: pass a the select object
	+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */


function validateSelect()
{
	// -- VARIABLES --	
	var selectObj = arguments[ 0 ];	// assign SELECT object 
	// -- END OF VARIABLES --

	// if the current selected value is empty... this means that, 
	// that is the default option OR something that should not be selected, eg. a spacer....
	if ( selectObj.options[ selectObj.selectedIndex ].value == '' )
	{
		selectObj.focus();	// set focus
		return false;			// exit function
	}
} // end function

// ------------------------------------ end of validateSelect -------------------------------------------------------------------



/* 	+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	+ This function will check if an item of a SELECT object is selected. This function will only validate if 
	+ the first OPTION VALUE - which is the default one - has an empty value.
	+ TO USE: pass a the select object
	+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */


function validateRadio()
{
	// -- VARIABLES --	
	var radioObj = arguments[ 0 ];		// assign SELECT object 
	var intRadioCounter = 0;		// counter for FOR loop
	var boolCheckedFound = 0;		// a bool to know if at least one radio is selected
	var strRadioName = radioObj.name;	// the radio object name
	var strForm = radioObj.form.name;	// the form name
	
	// create the object.length string, will look like this:
	// document.<form name>.<radio name>.length
	var strEval = 'document.' + strForm + '.' + strRadioName + '.length';
	
	// eval the string to get length...
	var intRadioLength = eval( strEval );
	
	// create the object string, will look like this:
	// document.<form name>.<radio name>
	strEval = 'document.' + strForm + '.' + strRadioName;
	
	// eval the string to get the radio object
	var radioObj = eval(strEval);
	
	// -- END OF VARIABLES --
	
	if (intRadioLength > 0)
	{
		// go through each buttons of the radio group
		for ( intRadioCounter = 0; intRadioCounter < intRadioLength; intRadioCounter++ )
		{	
			//if the current button is checked,
			if ( radioObj[ intRadioCounter ].checked )
			{
				// change the bool to 1, meaning one button of the group is selected already, which will pass the validation
				boolCheckedFound = 1;
				break;
			}
		}
	}
	else
	{
		// go through each buttons of the radio group
		//if the current button is checked,
		if ( radioObj.checked == true )
		{
			// change the bool to 1, meaning one button of the group is selected already, which will pass the validation
			boolCheckedFound = 1;
		}
	}

	// if the boolean value is still zero, meaning none of the buttons are checked... so return false
	if ( boolCheckedFound == 0 )
	{
		return false;
	}

} // end function

// ------------------------------------ end of validateRadio -------------------------------------------------------------------




/* 	+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	+ This function is used to check whether a value passed contains white spaces only. 
	+ If it DOES, it will return "true" and vice-versa.
	+ TO USE: pass a value
	+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */

function whiteSpaceOnly()
{
	// -- VARIABLES --
	var strValue= arguments[ 0 ];		// assign the value to a variable
	var intLenOfText = strValue.length;	// get the length of the value
	var intCharPosition;				// will be used to go through each character of the value
	var intWhiteSpaces = 0;			// used to keep a count one white spaces found
	// -- END OF VARIABLES --

	// cycle through the characters of the value passed
	for ( intCharPosition = 0; intCharPosition < intLenOfText ; intCharPosition++ )
	{
		// check if the current character is a white space.
		// by giving an INDEX to the charAt method, we can get the character for that INDEX
		if ( strValue.charAt( intCharPosition ) == " " )
		{
			// if it is a white space, increment the count...
			intWhiteSpaces++;
		}
	} // end for

	// check the whitespace count against the length of the value passed. if both of it is the same,
	// this means that the whole value is a whitespace.
	if ( intWhiteSpaces == intLenOfText )
	{
		return true;	// indicates whitespace only or empty
	}
	else
	{
		return false;	 
	} // end if
	
} // end of function whiteSpacesOnly

// ------------------------------------ end of whiteSpaceOnly -------------------------------------------------------------------



/* 	+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	+ This function will be used in a situation where there's a main checkbox and a set of other child checkboxes. When
	+ checking the main checkbox, the other set will be automatically checked.
	+
	+ NOTE: this code will only work where there is ONE main checkbox and a set of child checkboxes related to it.
	+	Other than this situation will still work, but it will check EVERYTHING...
		For example, there are 2 main checkboxes with their own chilld checboxes... by using this code to check
		ONE of the 2 main checkboxes, EVERYTHING will be checked...
	+
	+ TO USE: pass the form object, and the main checkbox object that triggers the check all thingy
	+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */


function checkAll()
{
	// -- VARIABLES --
	var formObj = arguments[ 0 ];		// assign the form object
	var checkObj = arguments[ 1 ];		// assign the checkbox object 
	var intCounter;					// a counter to cycle through all the elements;
	// -- END OF VARIABLES --

	// go through all the elements in the form
	for ( intCounter = 0; intCounter < formObj.elements.length; intCounter++ )
	{
		// if current element is a checkbox
		if ( formObj.elements[ intCounter ].type == "checkbox" && formObj.elements[ intCounter ].disabled == false )
		{
			// set its check status according to the main checkbox...
			formObj.elements[ intCounter ].checked = checkObj.checked;
		}
	} // end for
} // end function

// ------------------------------------ end of checkAll -------------------------------------------------------------------




/*
	+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
	+ use this function to block unwanted keyboard characters on a textbox. 
	+ it currently supports blocking non-integer chracters and integer characters.
	+ this function should be compatible with IE and non-IE browsers...
	+ TO USE (on a textbox) :
	+	onkeypress="javascript:return blockKey(event, this, 1)">
	+	event 	- the event
	+	this		- the textbox object
	+	1 or 2 or 3	- the type of blocking, see the code for info
	+
	+	NOTE: 
	+		- the RETURN keyword must be there
	+		- this function works for 'onkeypress' only, other event will be buggy...
	+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++	
*/	
	
function blockKey()
{	
	// ARGUMENTS
	// 1 - the EVENT object
	// 2 - the textbox object
	// 3 - the block type:
	//	1 : block non-numbers
	//	2 : block numbers

	var eventObj = arguments[ 0 ];		// the event object
	var textObj = arguments[ 1 ];		// the textbox object
	var intType = arguments[ 2 ];		// type of block
	// END OF ARGUMENTS
	
	// VARIABLES
	var intCharacterCode;				// will hold the character code ( ASCII )
	var strKey;						// will hold the actual character
	var testValues;					// will hold the regExp to be tested against
	// END OF VARIABLES
	
	// GET THE BROWSER
	var strUserAgent = navigator.userAgent.toLowerCase(); 
	var isIE = strUserAgent.indexOf("msie") > -1; 
	var isNS6 = strUserAgent.indexOf("netscape6") > -1; 
	var isNS4 = !isIE && !isNS6  && parseFloat(navigator.appVersion) < 5; 	
	// END BROWSER


	// if browser is IE   
	if (isIE) 
     	{
     		// get the character code
     		intCharacterCode = eventObj.keyCode;
     	} 
     	// other browsers
     	else 
     	{
     		// get the character code
		intCharacterCode = eventObj.which; 
     	}

	// block non-integer characters...
	if ( intType == 1 )
	{
		testValues = /\D/;		// set regExp to match non-integers
	}
	// block integer characters
	else if( intType == 2 )
	{
		testValues = /\d/;		// set regExp to match integers
	}
	else if( intType == 3 )
	{
		testValues = /\d\.,/;		// set regExp to match integers, dot, and comma
	}
	else if( intType == 4 )
	{
		testValues = /[^\d\.]/;		// set regExp to ALLOW integers and dot
	}
     	// get the character from the character code
	strKey = String.fromCharCode( intCharacterCode );


	// test the character against the regExp, it will return true if the character is valid.
	// if the test returns true, that means the user pressed an invalid character...so, return false
	if( testValues.test( strKey ) )
	{
		return false;
	}
	
	return true;
}


// ------------------------------------ end of blockKey -------------------------------------------------------------------




/*
    this is a simple function to get the occurence of a certain character or word
    in a given value
    // to use : pass the value, and the character/word to find
    // return value : 0 if no occurences found,
                      else, the number of occurences
*/
function getOccurence()
{
    // VARIABLES
    var strValue = arguments[ 0 ];      // the value
    var strOccurValue = arguments[ 1 ]; // the value that will be matched  
    var reTest = new RegExp( strOccurValue, "g" ); // set up regular expression
    var result = '';
    // - VARIABLES -
   
    // match the value with the regular expression, will return all matched values in an array
    var result = strValue.match( reTest );   
    
    //alert(result);
    // if nothing is being matched, it will return null...
    if ( result == null )
    {
        return 0;
    }
    else
    {
        return result.length;    
    }
}
// --------------------------------------------------------------------------------------------------------

/*
    use this function to disable/enable a certain element
    
    TO USE : 
        1. pass the element object
        2. pass the status ( true of false )
        3. pass 'true' to clear a textbox
*/
function setDisabled()
{
    objElement = arguments[ 0 ];
    boolStatus = arguments[ 1 ];
    boolEmpty  = arguments[ 2 ];
    
    objElement.disabled = boolStatus;
    
    if ( boolEmpty === true )
    {
        if ( objElement.type == "text" )
        {
            objElement.value = "";
        }
    }
    
}


//-----------------------------------------------------------------------

function findForm()
{
	var objForm = arguments[ 0 ];
	
	if( checkForm( objForm ) == false )
	{
	    return false;
	}
	
	objForm.submit();
}



function getDistrict()
{
    var objForm = arguments[ 0 ];
	if( checkForm( objForm ) == false )
	{
	    return false;
	}
	
	objForm.submit();
    
}

// Paging
function pagingSubmit (obj) 
{
    var objForm = arguments[ 0 ];
    document.forms[0].action = objForm;
    document.forms[0].submit();   
}

function CheckFloat()
{
    objEvent = arguments[ 0 ];
    objText  = arguments[ 1 ];
    intLength = objText.value.length;
    // VARIABLES
	var intCharacterCode;				// will hold the character code ( ASCII )
	var strKey;						// will hold the actual character
	var testValues;					// will hold the regExp to be tested against
	// END OF VARIABLES
	
	// GET THE BROWSER
	var strUserAgent = navigator.userAgent.toLowerCase(); 
	var isIE = strUserAgent.indexOf("msie") > -1; 
	var isNS6 = strUserAgent.indexOf("netscape6") > -1; 
	var isNS4 = !isIE && !isNS6  && parseFloat(navigator.appVersion) < 5; 	
	// END BROWSER
	
    if (isIE) 
 	{
 		// get the character code
 		intCharacterCode = objEvent.keyCode;
 	} 
 	// other browsers
 	else 
 	{
 		// get the character code
	intCharacterCode = objEvent.which; 
 	}
 	strKey = String.fromCharCode( intCharacterCode );
 	testValues = /[^\d\.]/;	
 	if ( testValues.test( strKey ) == true )
 	{
 	    return false;
 	}
 	testValues = /[\.,]/;	
 	
 	
 	if ( intLength == 0 && strKey == '.' )
 	{
 	    return false;
 	}
    else if ( intLength > 0 && testValues.test( objText.value ) == true && strKey == '.' )
    {
        return false;
    }
    else if ( intLength > 0 && testValues.test( objText.value ) == true )
    {
        //alert('in');
        arrNumbers = objText.value.split( '.' );
        
        if ( typeof( arrNumbers[ 1 ] ) !== "undefined" && arrNumbers[ 1 ].length > 1 )
        {
            return false;
        }
    }
    
}