var all=0;

function CheckAll(list,checkall) {
	if (checkall.checked==true) {
			all=1;
	} else {
			all==0?all=1:all=0;
	}
		
	 if (checkall.checked==true) {
		for (i=0; i<list.length; i++) {
			 list[i].checked=true;
		}
	 } else {
		 for (i=0; i<list.length; i++) {
			 list[i].checked=false;
		}
	 }
}
 
function CheckList(list,checkall) {
	var count=0;
	for (j=0; j<list.length; j++) {
		if(list[j].checked==true) {
			count++;
		}
	}
	if ((list.length-1==count) && (all==0)) {
		checkall.checked=true;
	} else {
		if (list.length-1<count) {
			checkall.checked=true;
		} else {
			checkall.checked=false;
		}
	}
}

function AllChecked()
{
	ml = document.forms[0];
	len = ml.elements.length;
	if (len)
	{
		for(var i = 0 ; i < len ; i++) {
			if (ml.elements[i].name == "SP_deleteId" && !ml.elements[i].checked && !ml.elements[i].disabled) {
				return false;
			}
		}
		return true;
    }
	else {
		if (ml.checkbox.checked && !ml.checkbox.disabled)
		{
			return false;
		} else return true;
	}
}

function hasItems(frm,radio_name){ 
	//alert("frm = "+frm.elements.length+", name = "+radio_name);
	var counter = 0;
	for (var index = 0; index < frm.elements.length; ++index)
	{
		var el = frm.elements[index];
		//alert(el.type+","+el.name+","+el.checked);
		if (("checkbox" == el.type) && (radio_name == el.name) && el.checked)
			++counter;
	}
	if (0 == counter)
	{
		return false;
	}else {
		return true;
	}
}

function emailCheck (emailStr) {	
	if (emailStr!=""){
		var checkTLD=1;
		var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
		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];
		for (i=0; i<user.length; i++) {
			if (user.charCodeAt(i)>127) {
				alert("Ths username contains invalid characters.");
				return false;
			}
		}
		
		for (i=0; i<domain.length; i++) {
			if (domain.charCodeAt(i)>127) {
				alert("Ths domain name contains invalid characters.");
				return false;
			}
		}
		
		// See if "user" is valid 
		if (user.match(userPat)==null) {
			
			// user is not valid
			alert("The username doesn't seem to be valid.");
			return false;
		}
		
		var IPArray=domain.match(ipDomainPat);
		if (IPArray!=null) {
		
		// this is an IP address
		for (var i=1;i<=4;i++) {
			if (IPArray[i]>255) {
				alert("Destination IP address is invalid!");
				return false;
		   	}
		}
		return true;
		}
		
		// Domain is symbolic name.  Check if it's valid. 
		var atomPat=new RegExp("^" + atom + "$");
		var domArr=domain.split(".");
		var len=domArr.length;
		for (i=0;i<len;i++) {
			if (domArr[i].search(atomPat)==-1) {
				alert("The domain name does not seem to be valid.");
				return false;
		   	}
		}
		
		if (checkTLD && domArr[domArr.length-1].length!=2 && 
			domArr[domArr.length-1].search(knownDomsPat)==-1) {
			alert("The address must end in a well-known domain or two letter " + "country.");
			return false;
		}
		
		// Make sure there's a host name preceding the domain.
		if (len<2) {
			alert("This address is missing a hostname!");
			return false;
		}
		
		// If we've gotten this far, everything's valid!
		return true;
	}else {
		return true;
	}
}
function echeck(str) {

		var at="@";
		var dot=".";
		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);
		if (str.indexOf(at)==-1){
		   alert("Invalid E-mail ID");
		   return false;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert("Invalid E-mail ID");
		   return false;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert("Invalid E-mail ID");
		    return false;
		}

		 if (str.indexOf(at,(lat+1))!=-1){
		    alert("Invalid E-mail ID");
		    return false;
		 }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert("Invalid E-mail ID");
		    return false;
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert("Invalid E-mail ID");
		    return false;
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert("Invalid E-mail ID");
		    return false;
		 }

 		 return true;				
	}

	
function SearchAndReplace(str,replacewith,regex)
{
	var myregex=new RegExp("test");
	var replaceWith=replacewith;
	var argstring="g";
	//argstring = "g" //global
	//argstring = "i" //caseinsentitive

	myregex.compile(regex,argstring);

	teststr=str;

	return teststr.replace(myregex,replaceWith);
}

function trim(str) {
	return SearchAndReplace(str,""," ");
}

//number function
//function same as is Number
function isInteger(string1){
	for (var i = 0; i < string1.length; i++) {
		var c = string1.charAt(i);
		if (!((c >= "0") && (c <= "9"))) {
			return false;
		}
	}
	return true;
}

//check double number
function isDouble(number1,rdigit )
{
	var part_num = 0;
	var spt =  number1.split(".");
	if(spt.lenght>2){
		return false;
	}
	while (part_num < spt.length)
	{
	  //alert(spt[part_num]);
	  if(part_num===1){
		if(rdigit!="" && spt[part_num].length>rdigit){
			return false
		}
	  }
	  if(!isInteger(spt[part_num])){
		return false;
	  }
	  part_num+=1;
	}
	return true;
}	
	
function OnlyOneDot1(str){
	var Result1="";
	var firstdot = true;
	var firstnum = false;
	for (Count=0; Count<str.length; Count++) {
		Char=str.substring (Count, Count+1);
		if(Char=="."){
			if(firstdot&&firstnum){
				Result1 = Result1+Char;
				firstdot = false;
			}
		}else{
			Result1 = Result1+Char;
			firstnum = true;
		}
	}
	//alert(Result1);
	return Result1;
}

function RemoveNotNum(str){
	return OnlyOneDot1(SearchAndReplace(str,"","[^0-9.]"));
}