
// This function is for select / deselect checkbox
function selectDeselectAll(theElement)
{	             
	var theForm = theElement.form, z = 0;
    var all1=0;
		
	while (z<theForm.elements.length)
	{
		if(theForm[z].type == 'checkbox' &&  theForm[z].name == 'chkall[]')
		{
			theForm[z].checked=theElement.checked;
		}			
		z++;
	}
}


function selectDeselectAlertAll(theElement)
{	             
	var theForm = theElement.form, z = 0;
    var all1=0;
		
	while (z<theForm.elements.length)
	{
		if(theForm[z].type == 'checkbox' &&  theForm[z].name == 'chkall[]')
		{
			theForm[z].checked=theElement.checked;
			if(theElement.checked)
			{
				document.getElementById("divtxt"+theForm[z].value+"").style.display='none';
				document.getElementById("divtextbox"+theForm[z].value+"").style.display='block';
			}
			else
			{
				document.getElementById("divtxt"+theForm[z].value+"").style.display='block';
				document.getElementById("divtextbox"+theForm[z].value+"").style.display='none';
			}
		}			
		z++;
	}
}
// this function to check if all checkbox are selected then select/deselect main checkbox
function checkAllCheckbox(theElement)
{
	var theForm = theElement.form, z = 0;
    var all1=0;
		
	while (z<theForm.elements.length)
	{
		if(theForm[z].type == 'checkbox' &&  theForm[z].name == 'chkall[]')
		{
			if(theForm[z].checked!=theElement.checked)
			{	all1=1;
				break;
			}
		}			
		z++;
	}

	if(all1)
		document.getElementById('checkAll').checked = false;
	else
		document.getElementById('checkAll').checked = theElement.checked; 
}

function checkAllAlertCheckbox(theElement)
{
	var theForm = theElement.form, z = 0;
    var all1=0;
	
	if(theElement.checked)
	{
	document.getElementById("divtxt"+theElement.value+"").style.display='none';
	document.getElementById("divtextbox"+theElement.value+"").style.display='block';
	}
	else
	{
	document.getElementById("divtextbox"+theElement.value+"").style.display='none';
	document.getElementById("divtxt"+theElement.value+"").style.display='block';
	}

	while (z<theForm.elements.length)
	{
		if(theForm[z].type == 'checkbox' &&  theForm[z].name == 'chkall[]')
		{
			if(theForm[z].checked!=theElement.checked)
			{	all1=1;
				
				
				


//				theForm.elementid.style.display='none';
//				theForm.elementid1.style.display='block';

			  //alert(theElement.value);
				break;
			}
		}			
		z++;
	}

	if(all1)
		document.getElementById('checkAll').checked = false;
	else
		document.getElementById('checkAll').checked = theElement.checked; 
}





function select_chk_mb(theElement)  
{	             
	var theForm = theElement.form, z = 0;
	var all1=0;
	
	if(theElement.value=="Select All")
	{
		while (z<theForm.elements.length)
		{
			if(theForm[z].type == 'checkbox' &&  theForm[z].name == 'chk_mb[]')
			{
				theForm[z].checked=true;
				if(!(theForm[z].checked))
					all1=1;
			}			
			z++;
		}
		theElement.value="  Clear All";					
	}
	else
	{
  		var all1=0,z=0;
		while (z<theForm.elements.length)
	 	{
			if(theForm[z].type == 'checkbox' && theForm[z].name == 'chk_mb[]' )
			{
				theForm[z].checked=false;
				if(!(theForm[z].checked))
					all1=1;
		  	}			
		 	z++;
	 	}
	 	theElement.value="Select All";	
	}		  
}
	
	  
function confirmDelete(msg)
{			
	if(msg != "")				
		var agree=confirm(msg + "\nAre you sure you wish to delete this record?");
	else
		var agree=confirm("Are you sure you wish to delete this record?");
	
	if (agree)
		return true;
	else
		return false;
}
	  
function displaypage(theElement)
{

	var NoofPages = document.getElementById('noofpages').value;


	var formAction = document.getElementById(theElement.form.id).action;





	
	if(formAction!="")
	document.getElementById(theElement.form.id).action = formAction +"&noofpages="+NoofPages;
	else
		document.getElementById(theElement.form.id).action = "index.php?page="+theElement.form.id+"&noofpages="+NoofPages;
	document.getElementById(theElement.form.id).submit();
}

function displaypagecmp(theElement)
{
	var NoofPages = document.getElementById('noofpages').value;


	var formAction = document.getElementById(theElement.form.id).action;





	
	if(formAction!="")
	document.getElementById(theElement.form.id).action = formAction +"&noofpages="+NoofPages;
	else
		document.getElementById(theElement.form.id).action = "sysindex.php?page="+theElement.form.id+"&noofpages="+NoofPages;
	document.getElementById(theElement.form.id).submit();
}

function hdisplaypage(theElement)
{

	var NoofPages = document.getElementById('noofpages').value;
	var orderby = document.getElementById('hdnorderby').value;
	var ordertype = document.getElementById('hdnordertype').value;
	var searchstr = document.getElementById('hdnsearchstr').value;


	var formAction = document.getElementById(theElement.form.id).action;	
	if(formAction!="")
	{
	if(ordertype!="n")
	document.getElementById(theElement.form.id).action = formAction +"&noofpages="+NoofPages+"&orderby="+orderby+"&ordertype="+ordertype+"";
	else
	document.getElementById(theElement.form.id).action = formAction +"&noofpages="+NoofPages;
	if(searchstr!="n")
	document.getElementById(theElement.form.id).action=document.getElementById(theElement.form.id).action + "&"+searchstr;
	}
	else
		document.getElementById(theElement.form.id).action = "index.php?page="+theElement.form.id+"&noofpages="+NoofPages;
	document.getElementById(theElement.form.id).submit();
}


function new_hdisplaypage(theElement,url_param)
{

	var NoofPages = document.getElementById('noofpages').value;
	var orderby = document.getElementById('hdnorderby').value;
	var ordertype = document.getElementById('hdnordertype').value;
	var searchstr = document.getElementById('hdnsearchstr').value;


	var formAction = document.getElementById(theElement.form.id).action;	
	if(formAction!="")
	document.getElementById(theElement.form.id).action =formAction+"&"+url_param+"&noofpages="+NoofPages;
/*	{
	if(ordertype!="n")
	document.getElementById(theElement.form.id).action = formAction+url_param+"&noofpages="+NoofPages;
	else
	document.getElementById(theElement.form.id).action = formAction +"&noofpages="+NoofPages;
	if(searchstr!="n")
	document.getElementById(theElement.form.id).action=document.getElementById(theElement.form.id).action + "&"+searchstr;
	}
	else
		document.getElementById(theElement.form.id).action = "index.php?page="+theElement.form.id+"&noofpages="+NoofPages;*/
	document.getElementById(theElement.form.id).submit();
}
function checkFormAction(buttonid,msg,confirmmsg)
{
var frmobj=buttonid.form;
for(i=0;i<frmobj.elements.length;i++)
{
	if(frmobj[i].type=="checkbox" && frmobj[i].checked)
	{
		var agree=confirm(confirmmsg);	
		if(agree)
			return true;
		else
		{
			frmobj[i].checked=false;
			selectDeselectAll(frmobj[i]);		
			return false;
		}

	}
}
alert(msg);
return false;
}
function pdisplaypage(theElement)
{

		

	var NoofPages = document.getElementById('noofpages').value;



	var formAction = document.forms["pagfrm"].action;




	
	if(formAction!="")
	document.forms["pagfrm"].action = formAction +"&noofpages="+NoofPages;
	else
		document.forms["pagfrm"].action = "index.php?page="+theElement.form.id+"&noofpages="+NoofPages;
	document.forms["pagfrm"].submit();

}

	  

	  
function chkCheckbox(theElement)
{
	var theForm = theElement.form, z = 0,all1=0;
	while (z<theForm.elements.length)
	{
		if(theForm[z].type == 'checkbox' &&  theForm[z].name == 'chkall[]')
		{
			if(theForm[z].checked)
				all1=1;
		}			
		z++;
	}
	if(!all1)
	{    					 	  
		alert('Please select at least one checkbox');
		return false;					 
	}
	else
	{ 
		return true; 
	}
}
		
				
		
// submit listing form for DELETE operation
function Delete(theElement,msg)
{
	if(chkCheckbox(theElement) == true)
	{				
		if(confirmDelete(msg) == true)
		{
			if(confirm("Are you sure you want to delete the selected agent company?")==true){
				document.getElementById(theElement.form.id).action = 'index.php?page='+theElement.form.id+'&action=Delete';
				document.getElementById(theElement.form.id).submit();
			}
		}
	}
}
		
// submit listing form for ACTIVATE operation
function Activate(theElement)
{
	if(chkCheckbox(theElement) == true)
	{	
		if(confirm("Are you sure you want to activate the selected company?")==true){
			var noofpages = document.getElementById('noofpages').value;
			document.getElementById(theElement.form.id).action = 'index.php?page='+theElement.form.id+'&status=1&noofpages='+noofpages;
			document.getElementById(theElement.form.id).submit();
		}
	}
}

//submit listing for RESET PHONE ID
function ResetPhone(theElement)
{
	if(chkCheckbox(theElement) == true)
	{				
		document.getElementById(theElement.form.id).action = 'index.php?page='+theElement.form.id+'&action=reset';
		document.getElementById(theElement.form.id).submit();				
	}
}

		// submit listing form for DEACTIVATE operation
function Deactivate(theElement)
{		
	
	if(chkCheckbox(theElement) == true)
	{	
		if(confirm("Are you sure you want to deactivate the selected company?")==true){
			var noofpages = document.getElementById('noofpages').value;
			document.getElementById(theElement.form.id).action = 'index.php?page='+theElement.form.id+'&status=0&noofpages='+noofpages;
			document.getElementById(theElement.form.id).submit();
		}
	}
}
		
		// submit listing form for VIEW DELETED operation
function ViewDeleted(theElement)
{			
	document.getElementById("show_usr").value = "";
	if(theElement.value=="View Deleted")
	{
		//theElement.value="Cancel";				
		document.getElementById(theElement.form.id).action = 'index.php?page='+theElement.form.id+'&mode=viewDeleted';
		//alert(document.getElementById(theElement.form.id).action);
		//location.href='index.php?page='+theElement.form.id+'&mode=viewDeleted';
	}
	else if(theElement.value=="Cancel")
	{
		//theElement.value="View Deleted";
		document.getElementById(theElement.form.id).action = location.href='index.php?page='+theElement.form.id;
		
		//location.href='index.php?page='+theElement.form.id;
	}
	document.getElementById(theElement.form.id).submit();
	//document.getElementById('dstr_listing').submit();	
}
		
// submit listing form for RESTORE operation
function RestoreData(theElement)
{
	if(chkCheckbox(theElement) == true)
	{				
		document.getElementById(theElement.form.id).action = 'index.php?page='+theElement.form.id+'&action=restore';
		document.getElementById(theElement.form.id).submit();				
	}
}
		
		
		
/* This function is to validate the user status while landing it on login page  
   Parameters : 
				u_id 	  -> User ID of current login user
				chg_id    -> User ID of user to whom we want to show as login
				u_status  -> Status of the user who is going to show as login
				
*/
	
function checkSwap(u_id,chg_id,u_status)
{
	
	if(parseInt(u_status) == 0)
	{	alert("User status is Inactive.");
		return false;
	}
	
		
	if(parseInt(chg_id)==0)
	{
		alert("No Login record is present for the User. User can not login.");
		return false;
	}
	return true;
}
	
function checkSingleSelect()
{
	var e=document.getElementsByName("chkall[]");

	var len= e.length;
	var flag=0;
	var total=0;
	var chk='';
	for(var i=0; i < len; i++ )
	{
	
			 if (e[i].checked==true)
			{	
				 var flag=1; 
				  total=total+1;
				   if(chk=='')
					chk=e[i].value;
			   else
					chk=chk+","+e[i].value;
	
		   } 
	}
	
	if(total>=2 || flag=="0" )
	{
		alert("Please select only one check box."); 
		return false;
	}
	return chk;
}