window.onload = function()
{
  initCheckBehavior();
}
function showpic(id,imgno){
	for(i=1;i<=imgno;i++){
		if(i!=id){
    		document.getElementById(i+'pic').style.display='none'; 
		} else {
			document.getElementById(i+'pic').style.display='block';
		}
	}
}

function seeifanyoneischecked(){
	var elems=document.getElementById('form1').elements.length;
	var str='';
	 for(i=0; i<elems; i++)
	 	if (document.getElementById('form1').elements[i].id=='checkbox' && document.getElementById('form1').elements[i].checked)
		 	str+=document.getElementById('form1').elements[i].value+',';
	if(str==''){
		alert('Please select the records');
		return false;
	}
	if(confirm('Are you sure ?')==true){
		return true;
	}
	else return false;
} 

function initCheckBehavior()
{
  var i,a;
  for (i = 0; i < document.links.length; ++i) {
    a = document.links[i];
    if (a.id.indexOf('UncheckAll_') != -1) {
      a.onclick = doCheckBehavior;
      a._CBNAME_ = a.id.substr(11) + '[]';
      a._CBCHECKED_ = false;
    }
    else if (a.id.indexOf('CheckAll_') != -1) {
      a.onclick = doCheckBehavior;
      a._CBNAME_ = a.id.substr(9) + '[]';
      a._CBCHECKED_ = true;
    }
  }
}
function doCheckBehavior()
{
  var i, cb = document.getElementsByName(this._CBNAME_);
  for (i = 0; i < cb.length; ++i) {
    cb[i].checked = this._CBCHECKED_;
  }
  return false;
}




function popUp(URL, $width, $height) {
	day = new Date();
	id = day.getTime();
	eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=no,location=0,statusbar=0,menubar=0,resizable=1,width="+ $width +",height="+$height+",left = 400,top = 150');");
}

function taketootherpage(page){
	document.location=page;
}

function IsNumeric(sText)
{
	if(sText=='')return false;
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;
   for (j = 0; j < sText.length && IsNumber == true; j++) 
      { 
      Char = sText.charAt(j); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
   
   
   }
   
function ValidEmail(email){
	var valide= /^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
	var myemail = trimAll(email);
	if(!valide.test(myemail))return false;
	else return true;
}

function IsNotEmpty(value){
	var myvalue=trimAll(value);
	if(myvalue=='')return false;
	else return true;
}

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

function ValidText(text){
	var validText= /^([a-zA-Z])+/;
	var myText = trimAll(text);
	if(!validText.test(myText))return false;
	else return true;
}


function trimAll( strValue ) {
	var objRegExp = /^(\s*)$/;
		if(objRegExp.test(strValue)) {
		   strValue = strValue.replace(objRegExp, '');
		   if( strValue.length == 0)
			  return strValue;
		}
	   objRegExp = /^(\s*)([\W\w]*)(\b\s*$)/;
	   if(objRegExp.test(strValue)) {
		   strValue = strValue.replace(objRegExp, '$2');
		}
	  return strValue;
}


function MakeValidations(validateArray){
	var msg= new Array();
	for (i in validateArray){
		var testValue=trimAll(document.getElementById(i).value);
		if(validateArray[i]=="IsNumeric"){
			mytest=IsNumeric(testValue);
			if(mytest==false)msg.push('Please enter numeric '+i);
		}
		if(validateArray[i]=="ValidEmail"){
			mytest=ValidEmail(testValue);
			if(mytest==false)msg.push('Please enter correct '+i);
		}
		if(validateArray[i]=="IsNotEmpty"){
			mytest=IsNotEmpty(testValue);
			if(mytest==false)msg.push('Please enter a value in '+i);
		}
		if(validateArray[i]=="ValidUrl"){
			mytest=ValidUrl(testValue);
			if(mytest==false)msg.push('Please enter correct url');
		}
		if(validateArray[i]=="ValidText"){
			mytest=ValidText(testValue);
			if(mytest==false)msg.push('Please enter text in '+i);
		}
	}
	if(msg.length>0){
		var display='';
		for(j=0;j<msg.length;j++){
			display+='<span class="hint-length">'+msg[j]+'</span>';
		}
		display+='<br style="clear:both;"/>';
		$('#jserror-message').show('slow');
		document.getElementById('jserror-message').innerHTML=display;
		return false;
	}else{
		return true;
	}
}



function MakeClientValidations(validateArray){
	var msg= new Array();
	var cnt = 0;
	for (i in validateArray){
		//alert(i);
		var testValue=trimAll(document.getElementById(i).value);
		//alert(testValue);
		document.getElementById(i+'-error').innerHTML='';
		
		if(validateArray[i]=="IsNumeric"){
			mytest=IsNumeric(testValue);
			if(mytest==false){
				document.getElementById(i+'-error').innerHTML='*';
				cnt++;
			}
			
		}
		if(validateArray[i]=="ValidEmail"){
			mytest=ValidEmail(testValue);
			if(mytest==false){
				document.getElementById(i+'-error').innerHTML='*';
				cnt++;
			}
		}
		if(validateArray[i]=="IsNotEmpty"){
			mytest=IsNotEmpty(testValue);
			if(mytest==false){
				document.getElementById(i+'-error').innerHTML='*';
				cnt++;
			}
		}
		if(validateArray[i]=="ValidUrl"){
			mytest=ValidUrl(testValue);
			if(mytest==false){
				document.getElementById(i+'-error').innerHTML='*';
				cnt++;
			}
		}
		if(validateArray[i]=="ValidText"){
			mytest=ValidText(testValue);
			if(mytest==false){
				document.getElementById(i+'-error').innerHTML='*';
				cnt++;
			}
		}
	}
	
	if(cnt>0){
		var display='';
		document.getElementById('error-message').innerHTML='Please fill valid entry in the fields marked with (*)';
		return false;
	}else{
		return true;
	}
	return false;
}
