function ValidatorTrim(s) {
    var m = s.match(/^\s*(\S+(\s+\S+)*)\s*$/);
    return (m == null) ? "" : m[1];
}

function TrimFieldValue(stringToTrim) {
    return stringToTrim.replace(/^\s+|\s+$/g,"");
}

var dtCh= "/";
var minYear=01;
var maxYear=99;
function isInteger(s){
    var i;
    for (i = 0; i < s.length; i++){
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}
//following function added on 21st Nov 06 to check text box value is Number ( integer or float) or not
function checkNumber(num)
{
	if(isNaN(num.value))
	{
		num.value = "";
		alert("Enter Numeric Value.");
		num.focus();
		return false;
	}
}

function stripCharsInBag(s, bag){
    var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function checklength(obj,len)
{
    var temp = obj.value;
    if(temp.length < len)
        return false;
    return true;
}
function checkEmail_old(strng)
{
    var error = "";
    if (strng == "")
        {
        error = "You did not enter an email-address.\n";
        }
    var emailFilter=/^.+@.+\..{2,4}$/;
    if (!(emailFilter.test(strng)))
        {
        error = "Please enter a valid email address.\n";
        }
    var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/;
    if(strng.match(illegalChars))
        {
        error = "The email address contains illegal characters.\n";
        }
    return error;
}
function checkEmail(email)
{
  var error = "";
  if(email.length <= 0)
  {
    error = "You did not enter an email-address.\n";
    return error;
  }
  var splitted = email.match("^(.+)@(.+)$");
  if(splitted == null)
  {
      error = "Please enter a valid email address.\n";
      return error;
  }
  else
  {
    if(splitted[1] != null )
    {
      var regexp_user=/^\"?[\w-_\.]*\"?$/;
      if(splitted[1].match(regexp_user) == null)
      {
          error = "Please enter a valid email address.\n";
          return error;
      }
    }
    if(splitted[2] != null)
    {
      if((splitted[2]).indexOf(".")<=0)
      {
          error = "Please enter a valid email address.\n";
          return error;
      }
      var regexp_domain=/^[\w-\.]*\.[A-Za-z]{2,4}$/;
      if(splitted[2].match(regexp_domain) == null)
      {
          var regexp_ip =/^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
          if(splitted[2].match(regexp_ip) == null)
          {
              error = "Please enter a valid email address.\n";
              return error;
          }
      }
      return error;
    }
  }
  error = "Please enter a valid email address.\n";
  return error;
}
function checkEmailX(strng,fieldcaption)
{
    var error = "";
    if (strng == "")
        {
        error = "You did not enter an "+fieldcaption+".\n";
        }
    var emailFilter=/^.+@.+\..{2,4}$/;
    if (!(emailFilter.test(strng)))
        {
        error = "Please enter a valid "+fieldcaption+".\n";
        }
    var illegalChars= /[\(\)\<\>\,\;\:\\\/\"\[\]]/;
    if(strng.match(illegalChars))
        {
        error = "The "+fieldcaption+" contains illegal characters.\n";
        }
    return error;
}
function validateURL(string)
{
	var v = new RegExp();
	v.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");
    if (!v.test(string))
    	return false;
    else
    	return true;
}
//Link Rollover starts here
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.0
  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 && document.getElementById) x=document.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];}
}
//link rollover ends here

//pre load images scrip starts here
var myimages=new Array()
function preloadimages()
{
    for (i=0;i<preloadimages.arguments.length;i++)
    {
        myimages[i]=new Image()
        myimages[i].src=preloadimages.arguments[i]
    }
}
function alwaysontop()
{
    window.focus();
}
function isInt(varvalue)
{
	var regexpINT = /\D/;
	return !regexpINT.test(varvalue);
}
function isIP(varvalue)
{
	var regexpIP = /^((25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])\.){3}(25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9][0-9]|[0-9])$/;
	return regexpIP.test(varvalue);
}
function isSelected_Form_CheckBoxArray(formname,objname)
{
	var chkflg,strmessageid,url,returl;
	strmessageid="";
	chkflg=false;
	for(j=0; j<document.forms.length;j++)
	{
		if(document.forms[j].name==formname)
		{
			for (i=0;i<document.forms[j].elements.length;i++)
			{
				if (document.forms[j].elements[i].name==objname)
				{
					if (document.forms[j].elements[i].checked)
					{
						chkflg=true;
						break;
					}
				}
			}
		}
	}
	return chkflg;
}


/*******************************************************************************/
//added by chirag
//purpose: creates modaldialog for passed url
var ModalDialogWindow;
var ModalDialogInterval;
var ModalDialog = new Object;
ModalDialog.value = '';
ModalDialog.eventhandler = '';

function popupModalDialog(popupurl,width,height)
{
	if(typeof(width)=='undefined') width=500;
	if(typeof(height)=='undefined') height=500;

	if(window.showModalDialog)
	{
		window.showModalDialog(popupurl,window,'dialogWidth:'+width+'px;dialogHeight:'+height+'px;scrollbars:yes;location:no;status:no;maximize:yes;help:no;');
	}
	else
	{
		ModalDialogWindow=window.open(popupurl,window,'width='+width+',height='+height+',scrollbars=yes,location=no,status=no');
		ModalDialogInterval = window.setInterval("ModalDialogMaintainFocus()",1);
	}
}

function popupModalDialogForCandidate(popupurl,width,height)
{
	if(document.formmain.firm_id.value == "")
	{
		alert("Firm is required to select.");
		return false;
	}
	
		if(typeof(width)=='undefined') width=500;
		if(typeof(height)=='undefined') height=500;
	
		if(window.showModalDialog)
		{
			window.showModalDialog(popupurl,window,'dialogWidth:'+width+'px;dialogHeight:'+height+'px;scrollbars:yes;location:no;status:no;maximize:yes;help:no;');
		}
		else
		{
			ModalDialogWindow=window.open(popupurl,window,'width='+width+',height='+height+',scrollbars=yes,location=no,status=no');
			ModalDialogInterval = window.setInterval("ModalDialogMaintainFocus()",1);
		}
	
}

function ModalDialogMaintainFocus()
{
	try
	{
		if (ModalDialogWindow.closed)
		{
			window.clearInterval(ModalDialogInterval);
			return;
		}
		ModalDialogWindow.focus();
	}
	catch (everything) {   }
}

function setWindowSize(w,h)
{
 if (parseInt(navigator.appVersion)>3)
 {
   if (navigator.appName=="Netscape")
   {
    top.outerWidth=w;
    top.outerHeight=h;
   }
   else top.resizeTo(w,h);
 }
}
function setActiveStyleSheet(title) {
   var i, a, main;
   for(i=0; (a = document.getElementsByTagName("link")[i]); i++) {
     if(a.getAttribute("rel").indexOf("style") != -1
        && a.getAttribute("title")) {
       a.disabled = true;
       if(a.getAttribute("title") == title) a.disabled = false;
     }
   }
}

/*popup*/
function NewWindow(mypage, myname,w,h,scroll) {
	alert("test");
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;
	winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',resizable'
	win = window.open(mypage, myname, winprops)
	if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}
/**
 * Select / Unselect all checkboxes.
 *
 * @author Amit Shah
 * @param str frm formname
 */
function SetChecked(frm)
{
        var dml = eval(frm);
        if(dml.selectall.checked == true)
        	val=1;
        else
        	val=0;

        len = dml.elements.length;
        var i=0;
        for( i=0 ; i<len ; i++)
			dml.elements[i].checked=val;
}

/**
 * Validate for Checkbox selecttion
 *
 * @author Kalpak Prajapati
 * @param str frm (fromname) , chkname (checkbox id)
*/
function ChkValidate(frm,chkname){
	var validflag = false;
	for (i=0;i<document.forms[frm].elements.length;i++)
	{
		if (document.forms[frm].elements[i].id == chkname && document.forms[frm].elements[i].checked)
		{
			validflag = true;
			break;
		}
	}
	return validflag;
}
/**
 * Select / Unselecte Checkbox selecttion
 *
 * @author Kalpak Prajapati
 * @param str frm (fromname) , chkname (checkbox id), parent checkbox object
*/
function chk_uncheck(frm,chkname,chkallobj){
	if(chkallobj.checked == true){
		checkAll(frm,chkname);
	} else {
		uncheckAll(frm,chkname)
	}
	return true;
}
function checkAll(frm,chkname)
{
     var docF=document.forms[frm];
     for(var i=0;i<docF.elements.length;i++)
     {
          if(docF.elements[i].name==chkname && docF.elements[i].type=="checkbox")
               docF.elements[i].checked=true;
     }
	 return true;
}
function uncheckAll(frm,chkname)
{
     var docF=document.forms[frm];
     for(var i=0;i<docF.elements.length;i++)
     {
          if(docF.elements[i].name==chkname && docF.elements[i].type=="checkbox")
               docF.elements[i].checked=false;
     }
	 return true;
}
function checkState(frm,chkname,chkallname)
{
	var flg=true;
	var docF=document.forms[frm];
    for(var i=0;i<docF.elements.length;i++)
    {
       if(docF.elements[i].name==chkname && docF.elements[i].type=="checkbox")
		{
	  		if(docF.elements[i].checked==false)
			{
				flg=false;
				break;
			}
		 }
     }
     document.getElementById(chkallname).checked=flg;
     return true;
}


function checkFileExtension(filename)
{
	var OK = new Array ('.doc', '.odt', '.txt', '.pdf', '.docx');
	var err_file = "";

	if (filename != "")
	{
		var fileOK = 0;
		var re = /(\.doc|\.odt|\.txt|\.pdf|\.docx)$/;
		for (i = 0; i < OK.length; i++)
		{
			if (re.test(filename))
			{
				fileOK = 1; // one of the file extensions found
				break;
			}
		}

		if (fileOK !=1)
		{
			err_file +=  filename + " has an invalid file type. \r\n";
		}

	}

	return err_file;
}

function validateInputStr(inputstr)
{
 var valid=true;
 if(inputstr.length>0)
 {
  chk1 = "#$%^*\~`<>\\?";
  chk3="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
  for(j=0;j!=inputstr.length;j++)
  {
   ch1= inputstr.charAt(j);
   ch2= inputstr.charAt(0);
   rtn1=chk1.indexOf(ch1);
   rtn3=chk3.indexOf(ch2);
   if(rtn3 < 0)
   {
    valid=false;
    break;
   }
   else if(rtn1!=-1)
   {
    valid=false;
    break;
   }
  }
 }
 return valid;
}

function ValidateSelectMBlank(formvar,fld)
{
	var mselflg=false;
	for(imsel=0;imsel<document.forms[formvar].elements[fld].length;imsel++)
	{
		if(document.forms[formvar].elements[fld].options[imsel].selected && document.forms[formvar].elements[fld].options[imsel].value!="")
		{
			mselflg=true;
			break;
		}
	}
	return mselflg;
}

/*
Function added by Mihir Shah on 31-Jan-2007 to open window on click on email id.
*/
function openEmailWindow(popurl,emailTo,userType,uid)
{
	var width=700;
	var height=580;
	var win = popurl+"?email_to="+emailTo+"&usertype="+userType+"&uid="+uid;
	window.open(win,"SendEmail",'width='+width+',height='+height+',scrollbars=yes,location=no,status=yes,menubar=no,resize=yes');
	return false;
}
function openViewEmailWindow(popurl,emailID)
{
	var width=620;
	var height=500;
	var win = popurl+"?email_id="+emailID;
	window.open(win,"SendEmail",'width='+width+',height='+height+',scrollbars=yes,location=no,status=yes,menubar=no');
	return false;
}

function popupModalDialogForCandidateMultipleSubmission(popupurl,width,height)
{
	var frm = document.formmain;
	
	j=0;
	for(i=0;i< frm.length;i++)
	{
		e=frm.elements[i];
		if (e.type=='checkbox' && e.name != 'chkconfirm' && e.name != 'chkAttachCoverLetter' && e.name != 'chkAttachResume' && e.checked)
			j++;	
	}

	if(j==0)
	{
		alert("Please Select Firm Checkbox.");
		return false;
	}
	
	if(typeof(width)=='undefined') width=500;
	if(typeof(height)=='undefined') height=500;

	if(window.showModalDialog)
	{
		window.showModalDialog(popupurl,window,'dialogWidth:'+width+'px;dialogHeight:'+height+'px;scrollbars:yes;location:no;status:no;maximize:yes;help:no;');
	}
	else
	{
		ModalDialogWindow=window.open(popupurl,window,'width='+width+',height='+height+',scrollbars=yes,location=no,status=no');
		ModalDialogInterval = window.setInterval("ModalDialogMaintainFocus()",1);
	}
	
}
