var gl_el; //global element


// trim() function defined to remove spaces
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };


// get element by id 
function getObject(val){
	return document.getElementById(val);
}


function trim(b)
{
	var i=0;
	while(b.charAt(i)==" ")
	{
	i++;
	}
	b=b.substring(i,b.length);
	len=b.length-1;
	while(b.charAt(len)==" ")
	{
	len--;
	}
	b=b.substring(0,len+1);
	return b;
}

function checkAtLeastOneBox(name) {
	
	count = 0;
	elements = document.getElementsByName(name);
	len = elements.length;

	for (var i = 0; i < len; i++) {
		var e = elements[i];
		if (e.checked) {
			count=count+1;
		}
	}

	if (count == 0){
		return false;
	}
	else {
		return true;
	}
} 


function isCharsInBag (s, bag)
  {
    var i;
    // Search through string's characters one by one.
    // If character is in bag, append to returnString.

    for (i = 0; i < s.length; i++)
    {
        // Check that current character isn't whitespace.
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) return false;
    }
    return true;
 }

//function to check valid zip code
function isZIP(s) 
  {
    return isAlphaNumeric(s);
 }
 
 

//function to check valid Telephone,. Fax no. etc
function isPhone(s)
  {
	return isCharsInBag (s, "0123456789-+(). ");//simple test
	
	var PNum = new String(s);
	
	//	555-555-5555
	//	(555)555-5555
	//	(555) 555-5555
	//	555-5555

    // NOTE: COMBINE THE FOLLOWING FOUR LINES ONTO ONE LINE.
	
	var regex = /^[0-9]{3,3}\-[0-9]{3,3}\-[0-9]{4,4}$|^\([0-9]{3,3}\) [0-9]{3,3}\-[0-9]{4,4}$|^\([0-9]{3,3}\)[0-9]{3,3}\-[0-9]{4,4}$|^[0-9]{3,3}\-[0-9]{4,4}$/;
	
//	var regex = /^\([1-9]\d{2}\)\s?\d{3}\-\d{4}$/; //(999) 999-9999 or (999)999-9999
	if( regex.test(PNum))
		return true;
	else
		return false;

}

function isAlphaNumeric(s){
    return isCharsInBag (s, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 ");
}

function isUserName(s){
    return isCharsInBag (s, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789");
}

function isEmpty(s)
{
		 s=trim(s); 
		  return ((s == null) || (s.length == 0));
}

//function to check valid email id
function isEmail(s)
{
	
	var regex = /(^[a-z]([a-z_\.]*)@([a-z_\.]*)([.][a-z]{3})$)|(^[a-z]([a-z_\.]*)@([a-z_\.]*)(\.[a-z]{3})(\.[a-z]{2})*$)/i
	var regex =	/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
	return regex.test(s);

}


//format MM-DD-YYYY
function isValidFormatDate(strdate) { 
 // alert(strdate)
  var datedelimiter = '/';
  var datesplit = strdate.split(datedelimiter)
  if (datesplit.length > 3) {return false;}
  var month = 0; 
  month = datesplit[0];
  if (month < 1 || month >12 ) {return false;}
  if (isNaN(datesplit[0])) {return false;}
  else if (isNaN(datesplit[1])) {return false;}
  else if (isNaN(datesplit[2])) {return false;}
  else {
    //var year = parseInt(datesplit[2],10);
    var yearLn = (datesplit[2].length);
    var year= datesplit[2];
    	
    if (yearLn==1){return false;}
    if (yearLn==3){return false;}
    if (year<1){return false;}
    if (yearLn==2){
         year = '20'+ year
    }

    var day = parseInt(datesplit[0],10);
     if(day<0){return false;}
	if (day>31){return false;}
    if ((day > 30) && ((month == 4) || (month == 6) || (month == 9) || (month == 11))) {return false;}
    if (month == 2) {  // This calculates the basic leap year no matter the format, i.e. 2000 or 00. 
		var leap = ((year/4) == parseInt(year/4))
		if (leap) {if (day > 29) {return false;}
		}else {if (day > 28) {return false;}
      }
    }
  } 
  return true;
}



//format YYYY-MM-DD
function isValidDate(strdate) { 
 // alert(strdate)
  var datedelimiter = '-';
  var datesplit = strdate.split(datedelimiter)
  if (datesplit.length > 3) {return false;}
  var month = 0; 
  month = datesplit[1];
  if (month < 1 || month >12 ) {return false;}
  if (isNaN(datesplit[0])) {return false;}
  else if (isNaN(datesplit[1])) {return false;}
  else if (isNaN(datesplit[2])) {return false;}
  else {
    //var year = parseInt(datesplit[2],10);
    var yearLn = (datesplit[0].length);
    var year= datesplit[0];
    	
    if (yearLn==1){return false;}
    if (yearLn==3){return false;}
    if (year<1){return false;}
    if (yearLn==2){
         year = '20'+ year
    }

   //var year = year;
   // alert(year)
    // var year = (datesplit[2],10);

    var day = parseInt(datesplit[2],10);
     if(day<0){return false;}
	if (day>31){return false;}
    if ((day > 30) && ((month == 4) || (month == 6) || (month == 9) || (month == 11))) {return false;}
    if (month == 2) {  // This calculates the basic leap year no matter the format, i.e. 2000 or 00. 
		var leap = ((year/4) == parseInt(year/4))
		if (leap) {if (day > 29) {return false;}
		}else {if (day > 28) {return false;}
      }
    }
  } 
  return true;
}

function isbigdateTime(StDate, StTime, EdDate, EdTime)
 {
//alert('start' + StTime + 'end' + EdTime)
	//var DTDelimiter = ' ';
	var DateDelimiter='/';
	var TimeDelimiter=':';
	
	//Split start date and time.
	//var StDTSplit = StDateTime.split(DTDelimiter);
	//if (StDTSplit.length>2){return false;}
	//var StDate=StDTSplit[0];
	//var StTime=StDTSplit[1];
	var StDSplit = StDate.split(DateDelimiter);//Splite date into MM/DD/YYYY
	//alert(StDSplit.length)
	if (StDSplit.length>3){return false;}
	var StMM=parseInt(StDSplit[0]);
	var StDD=parseInt(StDSplit[1]);
	var StYY=parseInt(StDSplit[2]);
	var StTSplit = StTime.split(TimeDelimiter);//Splite time into H:M
	if (StTSplit.length>2){return false;}
	var StH=StTSplit[0];
	var StM=StTSplit[1];
	//alert('StMM' + StMM + '  StDD' + StDD + '  StYY' + StYY + '  StH' + StH + '  StM' 

//+ StM);
	
	//Split end date and time.
	//var EdDTSplit = EdDateTime.split(DTDelimiter);
	//if (EdDTSplit.length>2){return false;}
	//var EdDate=EdDTSplit[0];
	//var EdTime=EdDTSplit[1];
	var EdDSplit = EdDate.split(DateDelimiter);//Splite date into MM/DD/YYYY
	if (EdDSplit.length>3){return false;}
	
	var EdMM=parseInt(EdDSplit[0]);
	var EdDD=parseInt(EdDSplit[1]);
	var EdYY=parseInt(EdDSplit[2]);
	var EdTSplit = EdTime.split(TimeDelimiter);//Splite time into H:M
	//alert(EdTSplit.length)
	if (EdTSplit.length>2){return false;}
	var EdH=EdTSplit[0];
	var EdM=EdTSplit[1];
	//alert('time'+ EdH)
	//alert('EdMM' + EdMM + '  EdDD' + EdDD + '  EdYY' + EdYY + '  EdH' + EdH + '  EdM' 

//+ EdM);
	
	if(StYY>EdYY){
	    form1.txtDateEnd.focus()
		return false;
	}
	else if(StYY==EdYY && StMM>EdMM){return false;}	
	else if(StYY==EdYY && StMM==EdMM && StDD>EdDD){return false;}	
	else if(StYY==EdYY && StMM==EdMM && StDD==EdDD && StH>EdH){
		//alert("time HH")
		form1.selEndTime.focus(); 
		return false;
	}else if(StYY==EdYY && StMM==EdMM && StDD==EdDD && StH==EdH && StM>=EdM){
		//alert("time MM")
		form1.selEndTime.focus(); 
		//form.selEndTime.focus();
		return false;
	}	return true;	
}


//Validation for numeric fields
function isInteger(var1){
	str = var1;
	return isCharsInBag (str, "0123456789");
}


// validate radio button
function validateRadio(obj){
  var result = 0;
  for(var i=0; i<obj.length; i++){
    if(obj[i].checked==true && obj[i].value==correct) result = 1;
  }
  /*if(!result && obj.value == correct) result = 1*/
  return result;
}



//Validation for percentage
function validatePercentage(var1, var2){
	var fld = "";
	fld = var1.value;
	if (fld > 100 && fld != ""){
		return false;
	}
	return true;
}

function isValidateUrl(strUrl) {
   
   var v = new RegExp();
    v.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");
    if (!v.test(strUrl)) { 
        return false;
    }
	return true;
} 

function isValidUrl(strUrl) {
   
   var v = new RegExp();
    v.compile("[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");
    if (!v.test(strUrl)) { 
        return false;
    }
	return true;
} 

function isName(s){
	s=trim(s);
	return isCharsInBag (s, "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ- ");
}

function isPassword(s)
{
	
	s=trim(s);
	return isCharsInBag (s, "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_");
}

function isNumeric(s){
	s=trim(s);
	return isCharsInBag (s, "0123456789");
}

  // This function accepts a string variable and verifies if it is a
  // proper date or not. It validates format matching either
  // mm-dd-yyyy or mm/dd/yyyy. Then it checks to make sure the month
  // has the proper number of days, based on which month it is.	 
  // The function returns true if a valid date, false if not.
  // ******************************************************************	 
  function isDate(dateStr) 
  {

	   var datePat = /^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{2})$/;
	   var matchArray = dateStr.match(datePat); // is the format ok?
	   months= new Array(12);
	   months[0]="Jan";
	   months[1]="Feb";
	   months[2]="Mar";
	   months[3]="Apr";
	   months[4]="May";
	   months[5]="Jun";
	   months[6]="Jul";
	   months[7]="Aug";
	   months[8]="Sep";
	   months[9]="Oct";
	   months[10]="Nov";
	   months[11]="Dec"; 
	  if (matchArray == null) 
	  {
		 ("Please enter date as either mm/dd/yy or mm-dd-yy.");
		  return false;
	  }
 
	  month = matchArray[1]; // parse date into variables
	  day = matchArray[3];
	  year = matchArray[5];

	  if (month < 1 || month > 12) // check month range
	  { 
		  alert("Month must be between 1 and 12.");
		  return false;
	  }
 
	  if (day < 1 || day > 31) 
	  {
		  alert("Day must be between 1 and 31.");
		  return false;
	  }
 
	  if ((month==4 || month==6 || month==9 || month==11) && day==31) 
	  {
		  alert("Month "+ months[month-1]+" doesn't have 31 days!")
		  return false;
	  }
 
	  if (month == 2)  // check for february 29th
	  {
		  var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
		  if (day > 29 || (day==29 && !isleap)) 
		  {
			  alert("February " + year + " doesn't have " + day + " days!");
			  return false;
		  }
	  }
	  return true; // date is valid
  }


  function selval(sellist, selvalue){
  	for(iVar=0;iVar<sellist.options.length;iVar++){
		if (selvalue == ""){
			sellist.selectedIndex = 0;
			return;
		} else if (sellist.options[iVar].value == selvalue){
			sellist.selectedIndex = iVar;
			return;
		}
	}
	return;
  }


  //set focus on el or global variable gl_el;
  function setFocus(el){
	if(el != "undefined"){
		document.getElementById(el).focus();
		setAlertStyle(el);
	//	addEvent(document.getElementById(el),'onblur',function(){ alert('hello!'); })
	}else if(typeof gl_el != "undefined"){
		gl_el.focus();	
	}
	return true;
	
  }
  
  function setAlertStyle(el){
	document.getElementById(el).style.border = '2px solid';
	document.getElementById(el).style.borderColor = '000';
  } 
  
  function unsetAlertStyle(el){
	document.getElementById(el).style.border = '1px solid';
	document.getElementById(el).style.borderColor = '#ff9149';
  }
