// JavaScript Document

//Check Active, Archive and Delete function 
function actarchdel(page,value1) {
var chk=confirm("Are you sure to "+value1+" selected message")
if (chk==true)
{
	document.form1.action=(page+'?value='+value1); 
	document.form1.submit();
	//return false;
}
	return false;
}

// Check Active Flag
function activeflag(value1) {
var boxvalue
boxvalue=eval("document.Form1."+value1+"");
	if (boxvalue.checked)
	{
			boxvalue.value="1"
	}

	else
	{
			boxvalue.value="0"
	}

}

// Check Blank Space

function hasWhiteSpace(variablename,Value) 
{
  var reWhiteSpace = new RegExp(/^\s+$/);
     // Check for white space
     if (reWhiteSpace.test(Value)) {
          alert("Please Check Your Fields For Spaces");
		  variablename.focus();
          return;
     }
}


function checkspace(variablename,Message)
{
	var str="";
	str=variablename.value;
	var len=str.length;
	var i;
	for(i=0;i<len;++i)
	{
		if(str.charAt(i)!=" ")
		{
			return false;
		}
	}
	alert(Message+" to continue...");
	variablename.focus();
	return true;
}


//Check E-mail 
function checkemail(variablename,displayName) 
{ 
	var email=variablename.value;
	var whitespace = " \t\n\r";
	
	var i;
    
	for (i = 0; i < email.length; i++)
    	{   
        var c = email.charAt(i);
        if (whitespace.indexOf(c) == 0) 
        {
			alert("No spaces please."); 
			variablename.focus();
			return false;
        }
    	}

		 var i2 = 1;
		 var sLength = email.length;

   	 // look for @
  	  while ((i2 < sLength) && (email.charAt(i2) != "@"))
   	 { i2++
  	  }

   	 if ((i2 >= sLength) || (email.charAt(i2) != "@")) 
   	 {
		 alert("Please enter a valid email address."); 
		 variablename.focus();
		 return false;
   	 }
  	  else i2 += 2;

   	 // look for .
   	 while ((i2 < sLength) && (email.charAt(i2) != "."))
   	 { i2++
   	 }

   	 // there must be at least one character after the .
   	 if ((i2 >= sLength) || (email.charAt(i2) != ".")) 
   	 {
		alert("Please enter a valid new email address."); 
		variablename.focus();
		return false;
   	 }

}

// Check  Trim
function checktrim(s)
{
  while (s.substring(0,1) == ' ') {
    s = s.substring(1,s.length);
  }
  while (s.substring(s.length-1,s.length) == ' ') {
    s = s.substring(0,s.length-1);
  }
  return s;
}

//Check Numeric value
function checknumeric(variablename,displayName)
{
var sText=checktrim(variablename.value);
if(isNaN(sText))
{
			alert(displayName+" should be numeric.");
			variablename.focus();
			return false;

}
/*
var ValidChars = "0123456789.";
var IsNumber12=true;
var Char;
   for (i = 0; i < sText.length && IsNumber12 == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
		 	IsNumber12=false;
			alert(displayName+" should be numeric.");
			variablename.focus();
			return false;
         }
      }
IsNumber12=true;
return true;
*/
} 

// Check Date 
function checkdate(obj,dateStr)
{
var datePat = /^(\d{1,2})(\/|-)(\d{1,2})\2(\d{2}|\d{4})$/;

var matchArray = dateStr.match(datePat); // is the format ok?
if (matchArray == null) {
	alert("Invalid date.");
	obj.focus();
	return false;
}
month = matchArray[1]; // parse date into variables
day = matchArray[3];
year = matchArray[4];
if ((month==4 || month==6 || month==9 || month==11) && day==31) {
	alert("Invalid date.");
	obj.focus();
	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("Invalid date.");
	obj.focus();
	return false;
   }
}
return true;  // date is valid

}

// Check Positive Value
function isPositive(obj,displayName)
{
	var str="";
	str=lTrim(obj.value);
	if(str.length<=0)
		return true;
	if(!isNaN(obj.value))
	{
		if(parseFloat(obj.value)>=0)
			return true;
		else
		{
			alert(displayName+" should be a positive number.");
			obj.focus();
			return false;
		}
	}
	else
	{
		alert(displayName+" should be numeric");
		obj.focus();
		return false;
	}
	return true;
}

// Check Phone Number
function PhoneNoCheck(obj,displayName)
	{
		
		var s="";
		s=lTrim(obj.value);
		splChars=new Array("@","$","%","^","&","*","!");
		for(i=0;i<s.length;i++)
			for(j=0;j<splChars.length;j++)
			{
				if(s.charAt(i)==splChars[j])
				{
					alert(displayName+" cannot have special characters like @,$,% etc.");
					obj.focus();
					return false;
				}
			}
		return true;
	}	
// Check Length	
function maxLength(obj,displayname,num)
{

var str="";
var  num=num;
str=lTrim(obj.value);

if(str.length > num)

{
	alert(displayname+" should not have more than "+num+" characters.");
	obj.focus();
	return false;
}
	return true;

}

// Check Decimal Value
function isFloatNumber(obj,displayName)
{
	var str=lTrim(obj.value);
	var firstIndex,lastIndex;
	firstIndex=str.indexOf(".");
	lastIndex=str.lastIndexOf(".");
	if(firstIndex!=lastIndex)
	{
		alert(displayName+" should be numeric.");
		obj.focus();
		return false;
	}
	for(i=0;i<str.length;i++)
	{
		if((str.charAt(i)<'0'||str.charAt(i)>'9')&&str.charAt(i)!=".")
		{	
			if((str.charAt(0))=="-")
			{
				i=i+1;
					continue;
			}
			alert(displayName+" should be numeric.");
			obj.focus();
			return false;
		}
	}
	return true;
}  	
		
// Open Popup Window
function openpopup(pagename,width1,height1,scrollbar1,resizeable) {
window.open(pagename,'EANITHING','toolbar=no,location=no,directories=no,status=nomenubar=no,resizable='+resizeable+',copyhistory=no,scrollbars='+scrollbar1+',width='+width1+',height='+height1+',top=300,left=250');
}

// Open Calendar
function openCalendar(sDate,sCallback,sCallbackField,nTop,nLeft,page)
{
	var oWindow;
oWindow = window.open("calendar.asp?date=" + sDate + "&callback=" + sCallback + "" + "&callbackfield=" + sCallbackField + "&page="+page+"","calendar","top=" + nTop + ",left=" + nLeft + ",height=200,width=180,status=no,resizable=no,toolbar=no,menubar=no,scrollbars=no,location=no");
	oWindow.focus();
	return true;
}

// Counter
function chkmeonceplz(i) {
var section;
section="";
//for (i=0;i<document.Form1.section.length;i++) {
	if (document.Form1.section[i].checked)
	{
if (document.Form1.counter.value=="" ) {section=""+document.Form1.section[i].value+"" ;}
	if (document.Form1.counter.value!="" ) {section=document.Form1.counter.value+"," +""+document.Form1.section[i].value+"" ;}
	document.Form1.counter.value=section;
	}
	
	if (document.Form1.section[i].checked==false)
	{
	counter=document.Form1.counter.value.split(","); 
	for (j=0;j<counter.length;j++) {
	if (counter[j]==document.Form1.section[i].value) {
	//counter[j]+","=counter[j]+","+.replace(counter[j]+",","");
	counter[j]="";
	}
		if (counter[j]!="") {section=section + "," +counter[j] ; }
	//alert(counter[j]);
	}
	document.Form1.counter.value=section;
		}

}

function Left(str, n){
	if (n <= 0)
	    return "";
	else if (n > String(str).length)
	    return str;
	else
	    return String(str).substring(0,n);
}

function Right(str, n){
    if (n <= 0)
       return "";
    else if (n > String(str).length)
       return str;
    else {
       var iLen = String(str).length;
       return String(str).substring(iLen, iLen - n);
    }
}
