// JavaScript Document

/********************************************
	Project Name   : Birbal
	File Name      : General Javascript Page 
	Purpose        : this file contains Common Javascript function in the site 
	Developer      : Krishna Kanthi M
	Created Date   : 20th April 2007
	Changes        :
	Modified Date  :
	Modified By    :
	
	********************************************/
function LTrim(str)
{
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(0)) != -1) {
      // We have a string with leading blank(s)...

      var j=0, i = s.length;

      // Iterate from the far left of string until we
      // don't have any more whitespace...
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;

      // Get the substring from the first non-whitespace
      // character to the end of the string...
      s = s.substring(j, i);
   }
   return s;
}

function RTrim(str)
{
   // We don't want to trip JUST spaces, but also tabs,
   // line feeds, etc.  Add anything else you want to
   // "trim" here in Whitespace
   var whitespace = new String(" \t\n\r");

   var s = new String(str);

   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      // We have a string with trailing blank(s)...
     var i = s.length - 1;       // Get length of string
      // Iterate from the far right of string until we
      // don't have any more whitespace...
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;
      // Get the substring from the front of the string to
      // where the last non-whitespace character is...
      s = s.substring(0, i+1);
   }

   return s;
}


function trim(str)
{
   return RTrim(LTrim(str));
}
//Email validation
function checkEmail(oObject)
{	var emailStr=oObject;
		
var emailPat=/^(.+)@(.+)$/;
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
var validChars="\[^\\s" + specialChars + "\]";
var quotedUser="(\"[^\"]*\")";
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
var atom=validChars + '+';
var word="(" + atom + "|" + quotedUser + ")";
var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

var matchArray=emailStr.match(emailPat)
if (matchArray==null) {
	//alert("Email address seems incorrect (check @ and .'s)");
	return false;
}
var user=matchArray[1];
var domain=matchArray[2];

if (user.match(userPat)==null) {
   // alert("The username doesn't seem to be valid. Please check or register fresh");
    return false;
	}
var IPArray=domain.match(ipDomainPat);
if (IPArray!=null) {
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	       // alert("Destination IP address is invalid!");
		return false;
	    }
    }
    return true;
}

var domainArray=domain.match(domainPat);
if (domainArray==null) {
	//alert("The domain name doesn't seem to be valid.");
    return false;
}
var atomPat=new RegExp(atom,"g");
var domArr=domain.match(atomPat);
var len=domArr.length;
if (domArr[domArr.length-1].length<2 || 
    domArr[domArr.length-1].length>3) {
  // alert("The address must end in a three-letter domain, or two letter country.");
   return false;
}

if (len<2) {
  // var errStr="This address is missing a hostname!";
   alert(errStr);
   return false;
}
return true;
} //end of email validation

function checkNum(str) 
{

	 for(i=0;i<str.length;i++) { 

		 c=str.charAt(i);

		  b=(((c >= "0") && (c <= "9")) || (c==" ") || (c=="(")|| (c==")")|| (c=="-") || (c=="/")|| (c==",")) 

		  if(b==true) 

		  continue; 

		  else 

		  return false; 

	  } 

	  return true; 

}

//to find if the field contains alphabets only
function CheckAlpha(Text)
{
	var i=0;
	var ValidData="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz._- ";
	
	var Data=Text.value;
	for(i=0;i<Data.length;i++)
	{
		if(ValidData.indexOf(Data.charAt(i))==-1)
		{
			//alert("Please enter Non-Numeric Data Only");
			Text.focus();
			return false;
		}
	}
   	return true;
}
	
//to check if the field contains both alphabets and numbers 
function CheckAlphaNumeric(Text)
{
	var i=0;
	var ValidData=". ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890/_- ";
	var Data=Text.value;
	
	for(i=0;i<Data.length;i++)
	{
		if(ValidData.indexOf(Data.charAt(i))==-1)
		{
			//alert("Invalid entry.\t\t");
			Text.focus();
			return false;
		}
	}
	return true;
}
function listboxItemSelected(oList1,oList2)
	{
		id="";
		if (oList2!=null){
		clearComboOrList(oList2);
		if (oList1.selectedIndex == -1){
		oList2.options[oList2.options.length] = new Option('Select', '');
		} else {
		//alert(oList1.name + '=' + oList1.options[oList1.selectedIndex].value)
		fillListbox(oList2, oList1.name + '=' + oList1.options[oList1.selectedIndex].value);
		//alert(getvalue(oList1,k));
		}
		}
	}
		
	function clearComboOrList(oList)
	{
		//alert('1')
		for (var i = oList.options.length - 1; i >= 0; i--)
		{
			oList.options[i] = null;
			alert('t')
		}
		oList.selectedIndex = -1;
		if (oList.onchange)	
			oList.onchange();
		//alert('2')
	}
		
	function fillListbox(oList, vValue)
	{
		if (vValue != '') {
		if (assocArray[vValue]){
		var arrX = assocArray[vValue];
		for (var i = 0; i < arrX.length; i = i + 2){
		if (arrX[i] != 'EOF') 
		{
			s1=arrX[i + 1];
			oList.options[oList.options.length] = new Option(s1.replace("&#39","'"), arrX[i]);
		}
		}
		//alert (oList.options.length)
		if (oList.options.length == 1){
		//oList.selectedIndex=0;
		if (oList.onchange)	oList.onchange();
		}
		} else {
			oList.options[0] = new Option('All', '');
		}
		}
	}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
