/* 
Page Name: CommonFuncs.js
Description: This page holds common Javascrip functions that are executed at the browser side

Created by Javna Web applications development team. October 2001
 */

//*******************************************************************************************// 
// Function to check for empty fields //
function TextFilled(Obj,ErrorMessage)
{
// obj = provided field									//
// ErrorMessage = message to be shown if check failed.	//
// This function uses the Trim() function				//
// If the ErrorMessage was "null" then no alert for it  //
// This function returns true or false					//
	var OK=true;
	if (Trim(Obj.value)=='') 
		{
			if(ErrorMessage != null)
				{
					alert (ErrorMessage);
					Obj.focus();
				}
			OK=false;
			
		}
	return OK
}
//*******************************************************************************************//
// Function to trim provided string //
function Trim(strString) {
// strString = Provided string //
// This function returns trimmed value //

	var St=new String(strString);
	var arrS;
	arrS=St.split(" ");

	var sTrim= new String();
	var sp=new String();
	sp="";
	for (i=0;i<=arrS.length-1;i++)
		{
			if (arrS[i]!="")
			{ 
				sTrim=sTrim + sp +arrS[i];
				sp=" ";
			} 
		}
		return(sTrim);
}

//*******************************************************************************************//

//function provided to check that the submitted date is withen the correct format
function SingleVaildateDate(date,ErrorMessage,flag)
{
	
	var DateObj = new Date();
	var m = DateObj.getMonth()+1;
	var d = DateObj.getDate();
	var Y = DateObj.getYear();
	
	var localDate = String(date.value);
	FirstIndex = localDate.indexOf("/")       //holds the position of the first "/" in the  date
	LastIndex  = localDate.lastIndexOf("/")   //holds the position of the last "/"  in the  date
	
	Mounth     = parseInt(localDate.substring(0,FirstIndex))
	Day        = parseInt(localDate.substring(FirstIndex+1,LastIndex))
	Year       = parseInt(localDate.substring(LastIndex+1,localDate.length))
	
	if ((Year < 1900) || (Year > 9999)|| (Mounth>12) || ( (Day > 28) && (Mounth==2)) || ( Day >31 )) 
	{
		alert(ErrorMessage)
		date.focus();
		return false;
	}
	
	var DateFlag  = isNum(Day,0) && isNum(Mounth,0) && isNum(Year,0)
	if (DateFlag != 1)
	{
		alert(ErrorMessage)
		date.focus();
		return false;
	}
	if (flag == 1)
	{
		if (Y > Year)
		{
			alert("Please enter a start date that does not precede the current date.");
			return false;
		}
		if ((Mounth < m) && (Y == Year) )
		{
			
			alert("Please enter a start date that does not precede the current date.");
			return false;
		}
		if ((Day < d ) && (m == Mounth) && (Y == Year))
		{

			alert("Please enter a start date that does not precede the current date.");
			return false;
		}
	}	
		
	
	date.value = Mounth +"/" +Day +"/" +Year;
	return true;
}

//*******************************************************************************************//
// check if the value is integer/float or not //

function isNum(obj,type,ErrorMessage)  
{
// obj :  item in question
// if type=0 then it check the value if it is integer
// if type=1 then it check the value if it is float
        var point=0
		var cc=''
        var dd=''
        var bstr = ''
        var isit=0  
        var nstr = '1234567890'
        //force number to a string
        val = '' + obj
		if (val.substring(0,1)=='+')
		 val=val.substring(1,val.length);
    // begin loop which cycles through all of the characters in val
        for (var i = 0 ; i < val.length ; i++)
        {
         cc=val.substring(i, i+1)
         // begin loop which cycles through all of the characters in nstr
         for (var j =0 ; j< nstr.length ; j++) 
         {
          dd = nstr.substring(j, j + 1)
          isit = 0
          if (cc == dd)
          {
           isit = 1
		   if(type && cc==".") {point++;}
           break       
          }
		 }
		if (isit == 0 || point>1)
         {isit=0; break;}
        else continue
   	}
  if(isit==0)
	{
		if(ErrorMessage != null) {
				alert(ErrorMessage);
		}
	}
  return isit
}

//*******************************************************************************************//
//function provided to check for the two dates together//
function VaildateDate(sDate,eDate,ErrorMessage)
{
	
	var returnFlag = true;
	var localSDate = String(sDate.value);
	var loaclEDate = String(eDate.value);
	
	SFirstIndex = localSDate.indexOf("/")       //holds the position of the first "/" in the start date
	SLastIndex  = localSDate.lastIndexOf("/")   //holds the position of the last "/"  in the start date
	
	sMounth     = parseInt(localSDate.substring(0,SFirstIndex))
	sDay        = parseInt(localSDate.substring(SFirstIndex+1,SLastIndex))
	sYear       = parseInt(localSDate.substring(SLastIndex+1,localSDate.length))
		
	EFirstIndex = loaclEDate.indexOf("/")       //holds the position of the first "/" in the start date
	ELastIndex  = loaclEDate.lastIndexOf("/")   //holds the position of the last "/"  in the start date
	
	eMounth     = parseInt(loaclEDate.substring(0,EFirstIndex))
	eDay        = parseInt(loaclEDate.substring(EFirstIndex+1,ELastIndex))
	eYear       = parseInt(loaclEDate.substring(ELastIndex+1,loaclEDate.length))
	
				
	if (eYear < sYear)
	{
		alert(ErrorMessage);
		returnFlag = false;
		return returnFlag;
	}
	if ((eMounth < sMounth) && (eYear == sYear) )
	{
		alert(ErrorMessage);
		returnFlag = false;
		return returnFlag;
	}
	if ((eDay < sDay ) && (eMounth == sMounth) && (eYear == sYear))
	{
		alert(ErrorMessage);
		returnFlag = false;
		return returnFlag;
	}
return returnFlag;
}

//*******************************************************************************************//

//function provided to check all the checkboxes of a given checkbox object//
function SelectAll(obj){
	
	len = obj.length;
	Status = window.event.srcElement.checked;
	if (Status){
		for (i=0; i<len; i++){
			obj[i].checked = true;
		}
	}
	else{
		for (i=0; i<len; i++){
			obj[i].checked = false;
		}
	}
	
}

//*******************************************************************************************//

//Adds selected option(s) from a source list object to destination list objectand removes them
//from the source list object
function AddToList(srcObj,destObj){

	//check if any option selected
	if (srcObj.selectedIndex != -1){
		//loop and add all selected objects to destObj
		for (i=0; i<srcObj.options.length; i++){
			
			if (srcObj.options[i].selected == true){
				SText = srcObj.options[i].text;
				SValue = srcObj.options[i].value;
				destObj.options[destObj.options.length] = new Option(SText, SValue)
			}
		}
		//loop and remove all selected options from srcObj
		for(i=srcObj.options.length-1; i > -1 ;i--){	
			if (srcObj.options[i].selected == true)
				srcObj.remove(i);
		}
		
	}
}

//*******************************************************************************************//
//checks if the given input object's value is a valid telephone number or not
function isTelephoneNumber(ValueString){
var notNumber
	notNumber=0;
	telNumber2String=new String(ValueString.value);
for(i=0;i<=telNumber2String.length-1;i++)
{
  if (!((telNumber2String.charAt(i)>='0' && telNumber2String.charAt(i)<='9')||(telNumber2String.charAt(i)=='(') || (telNumber2String.charAt(i)==')') || ( telNumber2String.charAt(i)=='-') || (telNumber2String.charAt(i)==' ') || (telNumber2String.charAt(i)=='*') || (telNumber2String.charAt(i)=='#')))
  {
    notNumber=1
    break;
  }
}
   if (notNumber==1){
     
	 return false;
 }
 else 
	 return true;
}

//*******************************************************************************************//
//if there is no data changed this function navigates to the destination, else it confirms the user
function NavigateTo(Destination)
{
	if (window.changed.value != 0 ){
		var flag = window.confirm("Any unsaved data will be lost. Are you sure?")
		if(flag)
			document.location.href = Destination;
		
	}				
	else
		document.location.href = Destination;
}

//*******************************************************************************************//

//function provided to uncheck the select all checkboxes of a given checkbox object//
function UnSelectAll(obj){
	
	len = obj.length;
	Status = window.event.srcElement.checked;
	if (!Status){
		obj[0].checked = false;
		obj[len-1].checked = false;
	}
	else{
		selFlagUnSelectAll = false;
		for(i=0; i<obj.length; i++)
			if ((i != 0) && (i != (len-1)) && (!obj[i].checked))
				selFlagUnSelectAll = true;
		if (!selFlagUnSelectAll) {
			obj[0].checked = true;
			obj[len-1].checked = true;
		}
				
	}
		
}

//*******************************************************************************************//
//function provided to prent the text in a textarea from exceeding a given length//

function maxLen(obj, len){
	if (obj.innerText.length > len){
		obj.innerText = obj.innerText.substring(0,len)
	}
}

//*******************************************************************************************//
//same as NavigateTo function but is used for help pages because it opens them in a different window

function NavigateHelpTo(Destination)
{
	if (window.changed.value != 0 ){
		var flag = window.confirm("Any unsaved data will be lost. Are you sure?")
		if(flag)
			window.open(Destination, 'EpicenterHelpWindow');
		
	}				
	else
		window.open(Destination, 'EpicenterHelpWindow');
}

//*******************************************************************************************//
//to check if email is valid or not

function isEmailValid(checkThisEmail, Message)
{
var myEMailIsValid = true
var myAtSymbolAt = checkThisEmail.indexOf('@')
var myLastDotAt = checkThisEmail.lastIndexOf('.')
var mySpaceAt = checkThisEmail.indexOf(' ')
var myLength = checkThisEmail.length


// at least one @ must be present and not before position 2
// @yellow.com : NOT valid
// x@yellow.com : VALID

if (myAtSymbolAt < 1 ) 
{myEMailIsValid = false}


// at least one . (dot) after the @ is required
// x@yellow : NOT valid
// x.y@yellow : NOT valid
// x@yellow.org : VALID

if (myLastDotAt < myAtSymbolAt) 
{myEMailIsValid = false}

// at least two characters [com, uk, fr, ...] must occur after the last . (dot)
// x.y@yellow. : NOT valid
// x.y@yellow.a : NOT valid
// x.y@yellow.ca : VALID

if (myLength - myLastDotAt <= 2) 
{myEMailIsValid = false}


// no empty space " " is permitted (one may trim the email)
// x.y@yell ow.com : NOT valid

if ((checkThisEmail.indexOf(',') != -1) || (checkThisEmail.indexOf(';') != -1)  || (checkThisEmail.indexOf(':') != -1) || (checkThisEmail.indexOf('"') != -1) || (checkThisEmail.indexOf('/') != -1) || (checkThisEmail.indexOf('?') != -1)|| (checkThisEmail.indexOf('|') != -1)|| (checkThisEmail.indexOf(']') != -1) || (checkThisEmail.indexOf('[') != -1) || (checkThisEmail.indexOf('{') != -1) || (checkThisEmail.indexOf('}') != -1) || (checkThisEmail.indexOf(';') != -1) || (checkThisEmail.indexOf('<') != -1) || (checkThisEmail.indexOf('>') != -1) || (checkThisEmail.indexOf('=') != -1) || (checkThisEmail.indexOf('+') != -1) || (checkThisEmail.indexOf(')') != -1) ||  (checkThisEmail.indexOf('(') != -1) || (checkThisEmail.indexOf('*') != -1) || (checkThisEmail.indexOf('%') != -1) || (checkThisEmail.indexOf('^') != -1) || (checkThisEmail.indexOf('$') != -1) || (checkThisEmail.indexOf('#') != -1) || (checkThisEmail.indexOf('!') != -1) || (checkThisEmail.indexOf('`') != -1) || (checkThisEmail.indexOf('~') != -1)  || (checkThisEmail.indexOf('\\') != -1)  || (checkThisEmail.indexOf("'") != -1))
	{myEMailIsValid = false}

if (mySpaceAt != -1) 
{myEMailIsValid = false}


if (myEMailIsValid != true)
{
alert(Message);
}

return myEMailIsValid
}

//*******************************************************************************************//
//checks if the given input object's value is a valid telephone number or not
function isMobileNumber(ValueString){
var notNumber
	notNumber=0;
	telNumber2String=new String(ValueString.value);
for(i=0;i<=telNumber2String.length-1;i++)
{
  if (!(telNumber2String.charAt(i)>='0' && telNumber2String.charAt(i)<='9'))
  {
    notNumber=1
    break;
  }
}
   if (notNumber==1){
     
	 return false;
 }
 else 
	 return true;
}
