
<!--
	/*
	=====================================================
	Filename: validation.js
	Author: Max Mulawa
	Company: Link-Infotec
	Date Created: 04/11/2005
	Approved by: Rob Antrobus
	
	Last Modified by: Max Mulawa
	Date of Last Modification: 09/11/2005
	Description: JavaScript file with validation functions
	=====================================================
	*/

//////////////////////////////////////////////////////////
/// TEXTBOX  FUNCTIONS (IsEmptyTextBox,BoxHasFloatValueGrZero,GetFloatValue,
///						BoxHasFloatValue,BoxHasFloatValRange,BoxHasContentLen)
//////////////////////////////////////////////////////////

function IsEmptyTextBox(id)
{
	var box = document.getElementById(id);
	if(box)
	{
		if(Trim(box.value) == "") 
			return true;
		else
			return false;
	}
}

function TextAreaCounter(tbId,tbCountId , maxlimit)  
{   
    //window.status = "" + tbId + " " + tbCountId;
    textArea = document.getElementById(tbId); 
    textBoxCount = document.getElementById(tbCountId)
    if(textArea == null || textBoxCount == null)
        return;
        
    if (textArea.value.length > maxlimit)
    { 
        // if too long...trim value in textarea
        textArea.value = textArea.value.substring(0, maxlimit);
        alert('You exceeded the maximum limit of ' + maxlimit + " characters")
    }
    else 
        // otherwise, update 'characters left' counter
        textBoxCount.value = maxlimit - textArea.value.length;
}


function BoxHasFloatValueGrZero(id)
{
	 box = document.getElementById(id);	
	 if(box)
	 {
			if(box.value == "")
				return false;
			else if(!IsFloat(box.value))
         	   return false;
			else if(box.value <= 0) //Enter a number greater than 0
				return false;
	}
	return true;
}

function GetFloatValue(id)
{
	var valueBox = document.getElementById(id);
	if(valueBox)
	{
	    if(IsFloat(valueBox.value))
			return valueBox.value;
		else
			return 0;
	}
	return 0;
}

function BoxHasFloatValue(id)
{
	 box = document.getElementById(id);	
	 if(box)
	 {
			if(box.value == "")
				return false;
			else if(!IsFloat(box.value))
         	   return false;
	}
	return true;
}
 
function BoxHasFloatValRange(id,lower,upper)
{
	 box = document.getElementById(id);	
	 if(box)
	 {
			if(box.value == "")
				return false;
			else if(!IsFloat(box.value))
         	   return false;
			else if(! (box.value >= lower && box.value <= upper) ) //Enter a number greater than  upper or less than lower
				return false;
	}
	return true;
}

function BoxHasContentLen(id,length)
{
	var box = document.getElementById(id);
	if(box)
	{
		if(Trim(box.value) != "") 
		{
			if(box.value.length == length)
				return true;
			else
				return false;
		}
		else
			return false;
	}
}

function BoxHasContentGrLen(id,length)
{
	var box = document.getElementById(id);
	if(box)
	{
		if(Trim(box.value) != "") 
		{
			if(box.value.length >= length)
				return true;
			else
				return false;
		}
		else
			return false;
	}
}



//////////////////////////////////////////////////////////
/// EMAIL  FUNCTIONS (IsValidEmail,IsValidEmailBox)
/////////////////////////////////////////////////////////

function IsValidEmailBox(id)
{
	var box = document.getElementById(id);
	if(box)
	{
		if(box.value == "") 
			return false;
		else if(!IsValidEmail(box.value))
			return false;
	}
	return true;
} 

 function IsValidEmail(str) 
 {
	 if(Trim(str) == "")
	 	return false;
	 //have more than one @ in string
	if(str.lastIndexOf("@") != str.indexOf("@"))
		return false;
	//@ as first charachter
	if(str.indexOf("@") <= 0)
		return false;
	if(str.indexOf("@.") != -1)
		return false;
	if(str.indexOf(".@") != -1)
		return false;
	if(str.indexOf("..") != -1)
		return false;
	if(str.indexOf(".") < 1)
		return false;
		
	var atInd;
	atInd = str.lastIndexOf("@");
 	if(atInd != -1)
	{
		if(str.lastIndexOf(".") < atInd)
			return false
	}
	else
		return false;
	str = Trim(str)
	if(str.charAt(str.length-1) == ".")
		return false;
	
	return true;
  }

//////////////////////////////////////////////////////////
/// OBJECT  FUNCTIONS (GetValue,SetValue,GetObject,SetDivHTML)
/////////////////////////////////////////////////////////

function GetValue(id)
{
	var valueBox = document.getElementById(id);
	if(valueBox)
		return valueBox.value;
	return null;
}

function SetValue(id,value)
{
	var valueBox = document.getElementById(id);
	if(valueBox)
		valueBox.value = value;

}

function GetObject(id)
{
	return document.getElementById(id);
}

function SetDivHTML(id,text)
{
	if(document.getElementById(id))
		document.getElementById(id).innerHTML  = text;
}

function ShowHideDIV(id,isVisible)  
{ 
  var item = document.getElementById(id); 
  if(item)
    item.style.display = isVisible ? "inline":"none"; 
}

function DispalyDeleteInfo()
{
	return confirm('Are you sure you want delete this item?')
}

//////////////////////////////////////////////////////////
/// DISABLE  FUNCTIONS (IsDisabled,SetDisable,SwitchDisableStatus)
//////////////////////////////////////////////////////////

function IsDisabled(id)
{
	var elem = document.getElementById(id);
	if(elem)	
		return elem.disabled;
	else 
		return false;
}

function SetDisable(id,value)
{
	var checkbox = document.getElementById(id);
	if(checkbox)	
		checkbox.disabled = value; 	
}

//disable / enable sth
function SwitchDisableStatus(id)
{
	var elem = document.getElementById(id);
	if(elem)	
		elem.disabled  = !elem.disabled ;
}

//////////////////////////////////////////////////////////
/// CHECKBOX  FUNCTIONS (IsChecked,CheckItem,IsOneOrMoreCheckBoxSel)
//////////////////////////////////////////////////////////

//checkbox
function IsChecked(id)
{
	var checkbox = document.getElementById(id);
	if(checkbox)	
		return checkbox.checked;
	else 
		return false;
}

//check item 
function CheckItem(id,value)
{
	var checkbox = document.getElementById(id);
	if(checkbox)	
		checkbox.checked = value; 
		
}


//is one checkbox group selection 
function IsOneOrMoreCheckBoxSel(checkboxName)
{
	var checkBoxColl = document.getElementsByName(checkboxName);
	
	if(checkBoxColl)
	{
		for(var i=0;i<checkBoxColl.length;i++)
		{
			if(checkBoxColl[i].checked)
				return true;
		}
	}
	return false;
}


//////////////////////////////////////////////////////////
/// RADIO BUTTON FUNCTIONS (GetSelRadioValue)
////////////////////////////////////////////////////////// 

//radio collection 
function GetSelRadioValue(radioName)
{
	var radioColl = document.getElementsByName(radioName);
	var selValue = "";
	if(radioColl)
	{
		for(var i=0;i<radioColl.length;i++)
		{
			if(radioColl[i].checked)
				return radioColl[i].value;	
		}
	}
	return selValue;
}


//////////////////////////////////////////////////////////
/// COMBOBOX FUNCTIONS (GetSelComboValue,GetSelComboText,SelComboValue)
////////////////////////////////////////////////////////// 

//combobox selection 
function GetSelComboValue(comboid)
{
	var selCombo = document.getElementById(comboid);
	if(selCombo != null) 
	{
	  if(selCombo.options != null)
	  {
		if(selCombo.options.length > 0)
			return selCombo.options[selCombo.selectedIndex].value;
	  }
	}
	return "";
}

//gets selected text instead of value
function GetSelComboText(comboid)
{
	var selCombo = document.getElementById(comboid);
	if(selCombo != null) 
	{
	  if(selCombo.options != null)
	  {
		if(selCombo.options.length > 0)
			return selCombo.options[selCombo.selectedIndex].text;
	  }
	}
	return "";
}

//selects item in combo with specified value
function SelComboValue(id,value)
{
	for (i = 0; i<document.getElementById(id).length; i++ )
	{
		if(document.getElementById(id).options[i].value == value)
		{
			document.getElementById(id).options[i].selected  =  true;
			return true;
		}
	}
	return false;
}

 function OnSelectOptionChange(comboId,divId)
 {
    var selValue = GetSelComboValue(comboId);
 
    if(selValue == "OTHER")
        ShowHideDIV(divId,true)
    else
        ShowHideDIV(divId,false)
 }

//////////////////////////////////////////////////////////
/// DATE FUNCTIONS (IsUKDateValid)
////////////////////////////////////////////////////////// 

//accepts string value of date
function IsUKDateValid(string)
{
	var checkstr = "0123456789";
	var DateField = string;
	var Datevalue = "";
	var DateTemp = "";
	var seperator = "/";
	var day;
	var month;
	var year;
	var leap = 0;
	var err = 0;
	var i;
   err = 0;
   DateValue = DateField.value;
   /* Delete all chars except 0..9 */
   for (i = 0; i < DateValue.length; i++) {
	  if (checkstr.indexOf(DateValue.substr(i,1)) >= 0) {
	     DateTemp = DateTemp + DateValue.substr(i,1);
	  }
   }
   DateValue = DateTemp;
   /* Always change date to 8 digits - string*/
   /* if year is entered as 2-digit / always assume 20xx */
   if (DateValue.length != 8) {
      err = 19;}
   /* year is wrong if year = 0000 */
   year = DateValue.substr(4,4);
   if (year == 0) {
      err = 20;
   }
   /* Validation of month*/
   month = DateValue.substr(2,2);
   if ((month < 1) || (month > 12)) {
      err = 21;
   }
   /* Validation of day*/
   day = DateValue.substr(0,2);
   if (day < 1) {
     err = 22;
   }
   /* Validation leap-year / february / day */
   if ((year % 4 == 0) || (year % 100 == 0) || (year % 400 == 0)) {
      leap = 1;
   }
   if ((month == 2) && (leap == 1) && (day > 29)) {
      err = 23;
   }
   if ((month == 2) && (leap != 1) && (day > 28)) {
      err = 24;
   }
   /* Validation of other months */
   if ((day > 31) && ((month == "01") || (month == "03") || (month == "05") || (month == "07") || (month == "08") || (month == "10") || (month == "12"))) {
      err = 25;
   }
   if ((day > 30) && ((month == "04") || (month == "06") || (month == "09") || (month == "11"))) {
      err = 26;
   }
   /* if 00 ist entered, no error, deleting the entry */
   if ((day == 0) && (month == 0) && (year == 00)) {
      err = 0; day = ""; month = ""; year = ""; seperator = "";
   }
   /* if no error, write the completed date to Input-Field (e.g. 13.12.2001) */
   if (err == 0) {
      //DateField.value = day + seperator + month + seperator + year;
	  return true;
   }
   /* Error-message if err != 0 */
   else {
      DateField.select();
	  DateField.focus();
	  
	  return false;
   }
}


//////////////////////////////////////////////////////////
/// STRING FUNCTIONS (RTrim,LTrim, Trim)
////////////////////////////////////////////////////////// 
function Trim(string)
{
	if(string.length < 1)
		return "";

	string = RTrim(string);
	string = LTrim(string);
	
	return string;
} 

function RTrim(string)
{
	var w_space = String.fromCharCode(32);
	var v_length = string.length;
	var strTemp = "";
	if(v_length < 0)
		return"";
	var iTemp = v_length -1;
	
	while(iTemp > -1)
	{
		if(string.charAt(iTemp) == w_space){
		}
		else{
		strTemp = string.substring(0,iTemp +1);
		break;
		}
		iTemp = iTemp-1;
	} //End While
	return strTemp;
	
} //End Function

function LTrim(string)
{
	var w_space = String.fromCharCode(32);
	if(v_length < 1)
		return"";

	var v_length = string.length;
	var strTemp = "";
	
	var iTemp = 0;
	
	while(iTemp < v_length)
	{
		if(string.charAt(iTemp) == w_space)
		{
		}
		else
		{
			strTemp = string.substring(iTemp,v_length);
			break;
		}
		iTemp = iTemp + 1;
	} //End While
	return strTemp;
} //End Function

//////////////////////////////////////////////////////////
/// NUMBERS FUNCTIONS (IsNumber,IsFloat,RoundNumber,FormatNumber)
////////////////////////////////////////////////////////// 
 //accepts value  and checks if value is a Integer
 function IsNumber(string)
 {
  	if(isNaN(parseInt(string)) || (string != parseInt(string)))
  		return false
  	else
  		return true
 }
  //accepts value  and checks if value is a Float
 function IsFloat(string)
 {
  	if(isNaN(parseFloat(string)) || (string != parseFloat(string)))
  		return false
  	else
  		return true
 }
 
 // The number of decimal places to round to
function RoundNumber(number,rlength )
{
	return Math.round(number*Math.pow(10,rlength))/Math.pow(10,rlength);
}

//function to change format of the number
  /* IN - num:            the number to be formatted
           decimalNum:     the number of decimals after the digit
           bolLeadingZero: true / false to use leading zero
           bolParens:      true / false to use parenthesis for - num

      RETVAL - formatted number
   */
function FormatNumber(num, decimalNum, bolLeadingZero, bolParens)
{
       var tmpNum = num;
       // Return the right number of decimal places
       tmpNum *= Math.pow(10,decimalNum);
       tmpNum = Math.floor(tmpNum);
       tmpNum /= Math.pow(10,decimalNum);

       var tmpStr = new String(tmpNum);

       // See if we need to hack off a leading zero or not
       if (!bolLeadingZero && num < 1 && num > -1 && num !=0)
           if (num > 0)
               tmpStr = tmpStr.substring(1,tmpStr.length);
           else
               // Take out the minus sign out (start at 2)
               tmpStr = "-" + tmpStr.substring(2,tmpStr.length);                        


       // See if we need to put parenthesis around the number
       if (bolParens && num < 0)
           tmpStr = "(" + tmpStr.substring(1,tmpStr.length) + ")";


       return tmpStr;
 }

//////////////////////////////////////////////////////////
///CREDIT CARD FUNCTIONS (IsSecurityNumber,IsValidCreditCard)
//////////////////////////////////////////////////////////

//accepts id of control and checks if value is number between 100 and 999
function IsSecurityNumber(id)
{
	 box = document.getElementById(id);	
	 if(box)
	 {
			if(box.value == "")
				return false;
			else if(!IsNumber(box.value))
         	   return false;
			//Enter a number greater than 99 and lower than 1000			   
			else if(parseInt(box.value) <100 && parseInt(box.value) >= 1000) 
				return false;
	}
	return true;
}

//accepts string and checks if value is valid CC
function IsValidCreditCard(string) 
{
	var v = "0123456789";
	var w = "";
	for (var i=0; i < string.length; i++) 
	{
		x = string.charAt(i);
		if (v.indexOf(x,0) != -1)
			w += x;
	}
	var j = w.length / 2;
	if (j < 6.5 || j > 8 || j == 7) 
		return false;
	var k = Math.floor(j);
	var m = Math.ceil(j) - k;
	var c = 0;
	for (var i=0; i<k; i++) 
	{
		a = w.charAt(i*2+m) * 2;
		c += a > 9 ? Math.floor(a/10 + a%10) : a;
	}
	for (var i=0; i<k+m; i++) 
		c += w.charAt(i*2+1-m) * 1;
	return (c%10 == 0);
}

//-->
 
