//-----------------------------------------------------------------------------
function CenterWindow(width,height){
	var x,y,pos
	x= (screen.width-width)/2
	y= (screen.height-height)/2
  pos='width='+width+',height='+height+',top='+y+',left='+x
  return (pos)
}
//-----------------------------------------------------------------------------
function PopupWin(Url, name, width, height) {
	var w	
	w=window.open(Url,name,'scrollbars=no,'+CenterWindow(width,height)+',status=no,menubar=no,location=no,resizable=no')
	if (w != null) {
	  w.focus()
  }
	return(w) 	  
}
//-----------------------------------------------------------------------------
function PopupWinWin(objWin, Url, name, width, height) {
	var w	
	w=objWin.open(Url,name,'scrollbars=no,'+CenterWindow(width,height)+',status=no,menubar=no,location=no,resizable=no')
	if (w != null) {
	  w.focus()
  }
	return(w)
}
//-----------------------------------------------------------------------------
function PopupWinF(Url, name, width, height, features) {
	var w	
	if(features=='')
		w=window.open(Url,name,'menubar=no,'+CenterWindow(width,height)+',status=no,location=no')
	else	
		w=window.open(Url,name,'menubar=no,'+CenterWindow(width,height)+',status=no,location=no,'+features)
	if (w != null) {
	  w.focus()
  }
	return(w)
}
//-----------------------------------------------------------------------------
function IsValidIPAddress(ipaddr, outformattype) {
//returns empty string if IP is invalid else a formatted IP depending on outformattype
//outformattype : 0 - the user entered ipaddr
//							: 1 - leading zeros(0) removed from ip address
//							: 2 - leading zeros(0) added to ip address
	var outip='', tmp
	var re = /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}$/;
	if (re.test(ipaddr)) {
		var parts = ipaddr.split(".");
		if (parseInt(parseFloat(parts[0])) == 0) { return ''; }
		for (var i=0; i<parts.length; i++) {
		   if (parseInt(parseFloat(parts[i])) > 255) { return ''; }
		}
		if (outformattype==0)
			 outip=ipaddr
		else {
			if (outformattype==1) {
				outip=parseInt(parseFloat(parts[0]))+'.'			
				outip+=parseInt(parseFloat(parts[1]))+'.'			
				outip+=parseInt(parseFloat(parts[2]))+'.'			
				outip+=parseInt(parseFloat(parts[3]))
			}	
			else {
				for (var i=0; i<parts.length; i++) {
					if (parts[i].length==1) 
						tmp='00'+parts[i]
					else
						if (parts[i].length==2) 	
							tmp='0'+parts[i]
						else
							tmp=parts[i]	
					outip+= tmp+'.'
				}
				outip= outip.substr (0,15)  //remove trailing dot (.)
			}	
		}			
	  return outip;
	} else {
	   return '';
	}
}
//---------------------------------------------------------------------------------------------------------------------
function IsValidEmail(emailAddress) {
//	var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
	var re = new RegExp("^(([^<>()[\\]\\\\.,;:\\s@\\\"]+(\\.[^<>()[\\]\\\\.,;:\\s@\\\"]+)*)|(\\\".+\\\"))@((\\[[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\.[0-9]{1,3}\\])|(([a-zA-Z\\-0-9]+\\.)+[a-zA-Z]{2,}))$");
	re.global=true 
	re.ignoreCase=true 
	return re.test(emailAddress);
}

//----------------------------------------------------------------------------------------------------------------------
function formatdateDMYtoMDY(indate){

	var strDay="";
	var strMonth="";
	var strYear="";
	var dateMDY="";
	var strSeparatorArray = new Array("/");
	var intElementNr;
	var err = 0;
	var strDate = indate;
	
	if (strDate.length < 1) return '';

	for (intElementNr = 0; intElementNr < strSeparatorArray.length; intElementNr++) {
		if (strDate.indexOf(strSeparatorArray[intElementNr]) != -1) {
			strDateArray = strDate.split(strSeparatorArray[intElementNr]);
			if (strDateArray.length != 3) {
				return '';
			}
			else {
				strDay = strDateArray[0];
				strMonth = strDateArray[1];
				strYear = strDateArray[2];
			}
		}
	}
	dateMDY=strMonth+"/"+strDay+"/"+strYear;
	return dateMDY;
}
//----------------------------------------------------------------------------------------------------------------------
function IsDigit(str) {
	var re = /\D+/;
  
	if (str=='')
		return false
	else	
		return ! re.test(str);
}
//----------------------------------------------------------------------------------------------------------------------
function IsAlphaDigit(str)	{
	var re = /[^A-Za-z0-9]+/;

	if (str=='')
		return false
	else	trim
	
		return ! re.test(str);
}
//----------------------------------------------------------------------------------------------------------------------
function ltrim ( s )
{
	return s.replace( /^\s+/, '')
}
//----------------------------------------------------------------------------------------------------------------------
function rtrim ( s )
{
	return s.replace( /\s+$/, '');
}
//----------------------------------------------------------------------------------------------------------------------
function trim ( s )
{
	return rtrim(ltrim(s));
}
//----------------------------------------------------------------------------------------------------------------------
function calcHeight()
{
  var ua = navigator.userAgent.toLowerCase();
  if (ua.indexOf('opera') == -1) {
		//Not Opera
		var	minHeight=440;
		var zeroHeight=650;
		var the_height=document.getElementById('the_iframe').contentWindow.document.body.scrollHeight;//find the height of the internal page
			
				}
	else {
		//this is for OPERA
		var the_height=document.getElementById('the_iframe').document.body.scrollHeight;//find the height of the internal page
	    
	}
	if(the_height==0){
		the_height=zeroHeight}
	else {
			if(the_height<minHeight){
				the_height=minHeight }}
	the_height+=15;
	document.getElementById('the_iframe').style.height=the_height;  //change the height of the iframe			
}
//-------------------------------------------------------------------------------------------------------------------
function SelectAllCheckboxes(spanChk,chkTitle){
 //Select all the checkboxes in a GridView 

 // Added as ASPX uses SPAN for checkbox
 var oItem = spanChk.children;
 var theBox= (spanChk.type=="checkbox") ? spanChk : spanChk.children.item[0];
/*
 var strTmpElm = "";
 var strTmpTheBox = "";
*/ 
 xState=theBox.checked;
 elm=theBox.form.elements;

//elm[i].title==chkTitle &&  ".substring indexOf 


 for(i=0;i<elm.length;i++)
   /* strTmpElm = elm[i].id.substring(elm[i].id.indexOf("_")+1)
    strTmpElm = strTmpElm.substring(strTmpElm.indexOf("_")+1)
    
    strTmpTheBox = theBox.id.substring(theBox.id.indexOf("_")+1)
    strTmpTheBox = strTmpTheBox.substring(strTmpTheBox.indexOf("_")+1)
  */  
   if(elm[i].type=="checkbox" && elm[i].id!=theBox.id ) //&& strTmpTheBox= strTmpElm)
   {
     //elm[i].click();
     if(elm[i].checked!=xState)
       elm[i].click();
     //elm[i].checked=xState;
   }
}
//-------------------------------------------------------------------------------------------------------------------
function CheckCheckBoxesNyName(CheckBoxNamePattern, checkVal)
 {
  re = new RegExp('^' + CheckBoxNamePattern + '$')  //generated control name starts with a colon
  for(i = 0; i < document.form1.elements.length; i++)
  {
   elm = document.form1.elements[i]
   if (elm.type == 'checkbox')
   {
    if (re.test(elm.name))
     elm.checked = checkVal
   }
  }
 }
//-------------------------------------------------------------------------------------------------------------------
function CheckCheckBoxesByID(CheckBoxIdPattern, checkVal)
 {
  re = new RegExp('^' + CheckBoxIdPattern + '$')  //generated control name starts with a colon
  for(i = 0; i < document.form1.elements.length; i++)
  {
   elm = document.form1.elements[i]
   if (elm.type == 'checkbox')
   {
    if (re.test(elm.id))
     elm.checked = checkVal
   }
  }
 }       
//-------------------------------------------------------------------------------------------------------------------
function GetCheckBoxesByID(CheckBoxIdPattern, OnlySelected) {
  var elms = new Array ;

  re = new RegExp('^' + CheckBoxIdPattern + '$')  //generated control name starts with a colon
  for(i = 0; i < document.form1.elements.length; i++) {
    elm = document.form1.elements[i]
    if (elm.type == 'checkbox') {
      if (re.test(elm.id)) {
        if (OnlySelected) { 
          if (elm.checked) {
            elms.push (elm)
          }  
        }  
        else {
          elms.push (elm)      
        }  
      }  
    }  
  }          
  return elms 
}
//-------------------------------------------------------------------------------------------------------------------
function CountCheckBoxesByID(CheckBoxIdPattern, OnlySelected) {
  var elms = new Array ;
  var count=0
  
  re = new RegExp('^' + CheckBoxIdPattern + '$')  //generated control name starts with a colon
  for(i = 0; i < document.form1.elements.length; i++) {
    elm = document.form1.elements[i]
    if (elm.type == 'checkbox') {
      if (re.test(elm.id)) {
        if (OnlySelected) { 
          if (elm.checked) {
            count++
          }  
        }  
        else {
          count++     
        }  
      }  
    }  
  }          
  return count    
}

//---------------------------------------------------------------------------------------------------------------
  function isValidDate(dateStr,ErrorMsg) {

    var isValid = false;

    if (dateStr == "" )
      isValid= true;
    else
    {

        // Checks for the following valid date formats:
        // MM/DD/YY   MM/DD/YYYY   MM-DD-YY   MM-DD-YYYY
        var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{4})$/; // requires 4 digit year

        isValid = true;
        
        var matchArray = dateStr.match(datePat); // is the format ok?
        if (matchArray == null) {
          isValid=  false;
        }

        day  = matchArray[1]; // parse date into variables
        month= matchArray[3];
        year = matchArray[4];
       
       
      
        if  ((isValid== true) && (month < 1 || month > 12)) { // check month range
          isValid=  false;
        }

        if ((isValid== true) && (day < 1 || day > 31)) {
          isValid=  false;
        }

        if ((isValid== true)&&((month==4 || month==6 || month==9 || month==11) && day==31)) {
          isValid=  false;
        }

        if ((isValid== true)&&(month == 2) ){ // check for february 29th
          var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
          if (day>29 || (day==29 && !isleap))
            isValid=  false;
          
        }
        
        if (isValid==false)
        {
          if (ErrorMsg!="")
            alert(ErrorMsg);
        }
    
    }  
    
    return isValid;
  }
  
//-->    