/*
Created On : 31-May-2007
Purpose    : To write all global javascript functions
*/

//------------- Code Start to prevent hacking --------------------

/*
frm - object of the form
FieldName - name of the field
Purpose - Used to validate search text
*/
function isValidChar(frm,FieldName)
{
	var ValidChars = "0123456789.abcdefghijklmnopqrstuvwxyz ABCDEFGHIJKLMNOPQRSTUVWXYZ-_,(*!@&*()/\:#"
	var IsNumber=true;
	var Char;
	var obj;

	obj = eval(frm.name + "." + FieldName)
	if (obj!="")
	{
		sText = obj.value;
		for (i = 0; i < sText.length && IsNumber == true; i++) 
		 { 
		   Char = sText.charAt(i); 
		   if (ValidChars.indexOf(Char) == -1) 
		    {
			 alert("You may have used special Characters \n Which are not allowed !!!");
			 obj.focus();
			 return false;
			}
		 }
	}
	return true;

}

//this function is used to validate password,asi,ppac,upic,ppac & sage
function isValidCode(frm,FieldName,strCodeName)
{
	var valid = "1234567890-abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	var obj;
	obj = eval(frm.name + "." + FieldName)
	var strCode = obj.value;
	if (strCode != "")
	{
		var sizeasi = strCode.length;
		for (var i=0; i<sizeasi; i++)
		 {
		  if (valid.indexOf(strCode.charAt(i)) < 0) 
		  {
			alert('Your '+ strCodeName +' contains invalid characters.');
			obj.select();
			obj.focus();
			return false;
		  }
		}
	}
}

//------------- Code End to prevent hacking --------------------

// Removes leading and ending whitespaces
function trim(value) 
{
	return LTrim(RTrim(value));
	
}

// Removes leading whitespaces
function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

function isValidEmail(frm,FieldName)
{
	var obj;
	obj = eval(frm.name + "." + FieldName)
	if(isValidMailId(obj.value)==false)
	{
		alert("Please enter valid Email Address.");
		obj.focus();
		return false;
	}
	return true
}

//This function is used to validate multiple email id
function isValidMulMailId(strEMail)
{
	var arrMailIds = new Array();
	arrMailIds = strEMail.split(",");
	for(var i=0;i<arrMailIds.length;i++)
	{
		if(isValidMailId(arrMailIds[i])==false)
		{
			alert("Invalid email id " + arrMailIds[i]);
			return false;
		}
	}

  return true;	
}
//This function is used to validate email id 
function isValidMailId(strEMail)
{
	var strInvalidChars,blnTemp,i,strTemp,intdot,IsMail,strEMailRev,intAtTheRate;
    intdot = strEMail.lastIndexOf(".");
	if (intdot == -1 )
		return false;
    
	intdot=strEMail.length-intdot-1;
	if (intdot < 2 )
		return false;
    
    // Disallowed characters
    strInvalidChars = "!#$%^&*()=+{}[]|\;:'/?>,< ";

	intAtTheRate = strEMail.indexOf("@") ;

    // Check that there is at least one '@' and a character before @
    if (intAtTheRate < 1)
		blnTemp=true;
	else
		blnTemp=false;

    if (blnTemp)
		return !blnTemp;
	
	// Check that there is at least one '.' in the form a@a.com
    blnTemp = (strEMail.indexOf(".") < 3);
   if (blnTemp)
		return !blnTemp;

	//   Check that there is at least one char between '.' and @ after @
	intdot= strEMail.indexOf(intAtTheRate+1,".");
	blnTemp = (intdot==2);
     if (blnTemp)
		return !blnTemp;

	//   Check that there is at least one char between '.' and @ before @
    blnTemp = (strEMail.substring(intAtTheRate - 1, intAtTheRate) == ".");
    if (blnTemp)
		return !blnTemp;

	//  and that the length is at least six (a@a.co)
    blnTemp = (strEMail.length < 6);
   if (blnTemp) 
		return !blnTemp;
	//  Check that there is only one '@'
    blnTemp = (intAtTheRate != strEMail.lastIndexOf("@"));
    
   if (blnTemp)
	return !blnTemp;
   //    extra checks
  //     AFTER '@' space is not allowed
	strTemp=strEMail.substring(intAtTheRate+1);
	blnTemp = (strTemp.indexOf(" ") > 0);
   if (blnTemp) 
		return !blnTemp;

  //   Check that there is one dot AFTER '@'
    blnTemp = (strTemp.indexOf(".") == 0);
   if (blnTemp)
		return !blnTemp;
    
	//    Check if there's a quote (")
    blnTemp = strEMail.indexOf("\"") > 0;
     if (blnTemp )
		return !blnTemp;
    
	//     Check if there's any other disallowed chars
	//     optimize a little if strEMail longer than strInvalidChars
	//     check the other way around
    if (strEMail.length > strInvalidChars.length)
	{
        for(var i = 0; i< strInvalidChars.length;i++)
		{
            if (strEMail.indexOf(strInvalidChars.substring(i,i+1)) > 0 )
                  blnTemp = true;
            if (blnTemp )
				break;
        } //end for
    } //end if
	else
	{
        for(var i = 0 ;i< strEMail.length;i++)
		{
            if (strInvalidChars.indexOf(strEMail.substring(i,i+1)) > 0) 
                  blnTemp = true;
            if (blnTemp )
				break;
        } //end for
    } //end else
    if (blnTemp)
		return !blnTemp;
    
	//     extra check
	 //    no two consecutive dots
    blnTemp = strEMail.indexOf("..") > 0;
    if (blnTemp)
		return !blnTemp;
    
	//    if any of the above are true, invalid e-mail
    return !blnTemp;

}

function CheckNumber(bDotReq) 
{
	event.keyCode = DisllowChars(event.keyCode,bDotReq);
	
}
function DisllowChars(keyPressed,bDotReq)
{
		
	if (keyPressed>=48 && keyPressed<=57 || keyPressed==45 || (keyPressed==46 && bDotReq==1))
	{
		
	}
	else
	{
		alert("Please enter Numbers only");
		keyPressed = 0;
	}
		return keyPressed;
}

function isValidFileFormat(frm,FieldName)
{
	var obj;
	obj = eval(frm.name + "." + FieldName)
	var arr = obj.value.split(".");
	val = arr[1];
	val = val.toLowerCase();
	if(val=="exe"||val=="vbs"||val=="dat"||val=="vbs"||val=="bak" || val=="dll")
	{
		alert("Invalid File Format !!!");
		obj.focus();
		return false;
	}
	return true;
	
}
