//  File name: ClientVer.js aka ClientVerification file
//	This file holds the common JavaScript code used in Chariot Software Applications

//	Jump to a specific webpage

	function GoToThisPage(PageName)
	{
	
	//	Set new target
	
		document.forms[0].action = PageName
		
	//	Submit this form

		document.forms[0].submit();	
	}

//	Jump to a specific state

	function GoToThisState(StateValue)
	{
	
	//	Set state machine

		document.forms[0].ProgramState.value = StateValue
		
	//	Submit this form

		document.forms[0].submit();	
	}

//	Draw the html page in a specific area with a specific state

	function GoHere(DrawArea, PageName, StateValue)
	{

	//	Set new target
	
		document.forms[0].action = PageName
		document.forms[0].target = DrawArea
			
	//	Set state machine

		document.forms[0].ProgramState.value = StateValue
		
	//	Submit this form

		document.forms[0].submit();	
	}

//	Verifies that the date entered is valid.

	function CheckDate(iMonth, iDay, iYear)
	{
	
	//	Local variables
	
		var IsLeapYear = false
		
	//	Test parameters
			
		if((iYear < 1998) || (iMonth < 1) || (iDay < 1) || (iMonth > 12) || (iDay > 31) || (iYear > 2037))
		{
			return false
		}
		
	//	Leap year calculation
	
		if(!((iYear - 1972) % 4))
		{
			IsLeapYear = true
		}
		
	//	Verify that the user did not enter a day value exceeding the number of days in a specific month.
		
		if(iMonth == 2)
		{
			if(IsLeapYear)
			{
				if(iDay > 29)
				{
					return false
				}			
			}
			else
			{
				if(iDay > 28)
				{
					return false
				}			
			}		
		}
		else if((iMonth == 1) || (iMonth == 3) || (iMonth == 5) || (iMonth == 7) || (iMonth == 8) || (iMonth == 10) || (iMonth == 12))
		{
			if(iDay > 31)
			{
				return false
			}			
		}
		else if((iMonth == 4) || (iMonth == 6) || (iMonth == 9) || (iMonth == 11))
		{
			if(iDay > 30)
			{
				return false
			}
		}
		
	//	Exit function
	
		return true
	}

//	Set a default value for a radio control

	function SetRadioControlValue(Array, MaxSize, String)
	{
	
	//	Local variables
	
		var i
	
	//	Set default time zone
	
		for(i = 0; i < MaxSize; i++)
		{
			if(Array[i].value == String)
			{
				Array[i].checked = true
				
				break
			}
		}
	}

//	Set a default value for a select control

	function SetSelectControlValue(Array, MaxSize, String)
	{
	
	//	Local variables
	
		var i
	
	//	Set default time zone
	
		for(i = 0; i < MaxSize; i++)
		{
			if(Array[i].value == String)
			{
				Array[i].selected = true
				
				break
			}
		}
	}
	
//	Verify that there is some text in the string

	function IsFieldBlank(TestString)
	{
		if(TestString.length < 1)
		{
			return true
		}
		else
		{
			return false
		}	
	}
	
//	This function returns true if an invalid character is found.

	function FoundInvalidCharacter(HTMLString)
	{
		var i
		var TestChar
		
		if(IsFieldBlank(HTMLString))
		{
			return true
		}
				
		for(i = 0; i < HTMLString.length; i++)
		{
			TestChar = HTMLString.charAt(i)

		//	Found a double quote character

			if(TestChar == "\"")
			{
				return true
			}

		//	Found a single quote character

			if(TestChar == "\'")
			{
				return true
			}
		}
		
	//	No invalid characters found!
		
		return false
	}
	
//  This function returns true if the string contains a positive integer

    function IsPositiveInteger(HTMLString)
    {
		var i
		var TestChar
		
		if(IsFieldBlank(HTMLString))
		{
			return false
		}
				
		for(i = 0; i < HTMLString.length; i++)
		{
			TestChar = HTMLString.charAt(i)

		//	Found an invalid character

			if((TestChar != "0") && (TestChar != "1") && (TestChar != "2") && (TestChar != "3") && (TestChar != "4") && (TestChar != "5") && (TestChar != "6") && (TestChar != "7") && (TestChar != "8") && (TestChar != "9"))
			{
				return false
			}
		}
		
	//	Let's assume we have a positive integer
		
		return true
    }
    
//  This function is designed to set the value of a cookie.
//  The cookie will expire in one day.
//  If the user wishes to check if cookies are enabled on the user's browser then this value must be set during the OnLoad() form event
    
	function CreateCookie(szCookieKey, szCookieValue)
	{
		
	//  Local variables
		
	    var GMT = new Date()
   		var ExpirationDate
   		
   	//  Set expiration time for the cookie
    		
   		ExpirationDate = GMT.getTime() + (24 * 60 * 60 * 1000)	// One day
    		
   		GMT.setTime(ExpirationDate)
    		
   	//  Write the cookie

   		document.cookie = szCookieKey + "=" + szCookieValue + "; expires=" + GMT.toGMTString()
   	}
    	
//  This function checks the existance of a specific cookie with a specific value
//  This function returns true if the cookie has the correct value

   	function CheckCookieValue(szCookieKey, szCookieValue)
   	{
    	
	//  Local variables
		
   		var SizeOfCookieValue, SizeofCookieKey
   		var i, j, k
    		    		
   	//  Read the cookie
    	
   	    i = 0
   	    SizeofCookieKey = szCookieKey.length
   	    SizeOfCookieValue = document.cookie.length
    	    
  	    if(SizeOfCookieValue > (SizeofCookieKey + szCookieValue.length))
   	    {
       	    while(i < SizeOfCookieValue)
   	        {
   	            j = i + SizeofCookieKey
    	        
       	        if(document.cookie.substring(i, j) == szCookieKey)
       	        {
                    j = j + 1
        			k = j + szCookieValue.length

                    if(document.cookie.substring(j, k) == szCookieValue)
                    {
                        return true
                    }
                }
    	        
       	        i++
       	    }
        }
    		
    //  Exit function
    		
    	return false
    }