
// whitespace characters
var whitespace = " \t\n\r";

function requireText(ctlArray)
{
	var isValid = true;
	for (var i=0; i<ctlArray.length; i++ )
	{
		var txtCtl = document.getElementById(ctlArray[i]);

		if (txtCtl.value.length <= 0)
		{
			//txtCtl.focus();
			isValid = false;
			break;
		}
	}

	return isValid;
}

function requireTextbox(ctlName)
{
	var isValid = true;
	var txtCtl = document.getElementById(ctlName);
	if (txtCtl.value.length <= 0)
	{
		isValid = false;
	}

	return isValid;
}

function requireCheckbox(ctl)
{
	if (ctl.checked == false)
	{
		//ctl.focus();
		return false;
	}

	return true;
}

function requireRadioGroup(ctl)
{
	var radioGroup = ctl;
	var itemchecked = false;

	for(var j = 0 ; j < radioGroup.length ; ++j) 
	{
		if(radioGroup[j].checked) 
		{
			itemchecked = true;
			break;
		}
	}

	if(!itemchecked) 
	{ 
		//if(radioGroup.focus)
		//	radioGroup.focus();
		return false;
   }

   return true;
}

function getCheckedValue(ctl)
{
	var checkedValue = "";

	for(var j = 0 ; j < ctl.length ; ++j) 
	{
		if(ctl[j].checked) 
		{
			checkedValue = ctl[j].value;
			break;
		}
	}

	return checkedValue;
}

function checkDropdown(ctlName)
{
	var drpCtl = document.getElementById(ctlName);
	if (drpCtl.selectedIndex == 0)
	{
		//drpCtl.focus();
		return false;
	}

	return true;
}

// Check whether string s is empty.

function isEmpty(s)
{   return ((s == null) || (s.length == 0))
}

function IsNumeric(sText)
{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   }


// Returns true if string s is empty or 
// whitespace characters only.

function isWhitespace (s)

{   var i;

    // Is s empty?
    if (isEmpty(s)) return true;

    // Search through string's characters one by one
    // until we find a non-whitespace character.
    // When we do, return false; if we don't, return true.

    for (i = 0; i < s.length; i++)
    {   
        // Check that current character isn't whitespace.
        var c = s.charAt(i);

        if (whitespace.indexOf(c) == -1) return false;
    }

    // All characters are whitespace.
    return true;
}

// isEmail (STRING s [, BOOLEAN emptyOK])
// 
// Email address must be of form a@b.c -- in other words:
// * there must be at least one character before the @
// * there must be at least one character before and after the .
// * the characters @ and . are both required
//
// For explanation of optional argument emptyOK,
// see comments of function isInteger.
function isEmail (s)
{   if (isEmpty(s)) 
       if (isEmail.arguments.length == 1) return false;
       else return (isEmail.arguments[1] == true);
   
    // is s whitespace?
    if (isWhitespace(s)) return false;
    
    // there must be >= 1 character before @, so we
    // start looking at character position 1 
    // (i.e. second character)
    var i = 1;
    var sLength = s.length;

    // look for @
    while ((i < sLength) && (s.charAt(i) != "@"))
    { i++
    }

    if ((i >= sLength) || (s.charAt(i) != "@")) return false;
    else i += 2;

    // look for .
    while ((i < sLength) && (s.charAt(i) != "."))
    { i++
    }

    // there must be at least one character after the .
    if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false;
    else return true;
}

function isEqual(string1,string2)
{
	if (string1 == string2)
	{
		return true;
	}
	else
	{
		return false;
	}
}


function writeErroMessage(isRequire,field)
	{
		var divMessage = document.getElementById("errorMessage");
		var divErrorDetail = document.getElementById("errorDetails");

		divMessage.style.display = "block";

		if (isRequire)
		{
			divMessage.innerHTML = "Please enter the following details:";
			divErrorDetail.style.display = "block";
		}
		else
		{
			divMessage.innerHTML = "Sorry, you seem to have entered some details incorrectly. Please check and try again.";
			divErrorDetail.style.display = "none";
		}
		window.scrollTo(0,0);
	}

// show the error message.
function writeValidationMessage(invalidType, invalidArray, invalidErrorArray)
{
	var divMessage = document.getElementById("errorMessage");
	var divErrorDetail = document.getElementById("errorDetails");
	var strError =  "<ul>";

	divMessage.style.display = "block";
	divErrorDetail.style.display = "block";

	if (invalidType == -1)	//require
	{
		divMessage.innerHTML = "Please enter the following details:";
	}
	else								//wrong format
	{
		divMessage.innerHTML = "Sorry, you seem to have entered some details incorrectly. Please check and try again.";
	}

	for (var i=0; i<invalidArray.length; i++)
	{
		if (invalidArray[i])
		{
			strError += invalidErrorArray[i];
		}
	}
	strError += "</ul>";
	divErrorDetail.innerHTML = strError;


	window.scrollTo(0,0);
}