function chkPay(){
	var pays=document.getElementsByName("pay[]");
	var modes=document.getElementsByName("radpayment");
	
	//var pays=document.forms.frmUser.pay[].length;
  //  var modes=document.forms.frmUser.radpayment1.length;
	
	var pay=false;
	for(i=0;i<=pays.length-1;i++){
		if(pays[i].checked){
			pay=true;
		}
	}
	if(!pay){
		alert("Please Choose a Consumer-No to proceed")
		return pay;
	}
	var mode=false;
	for(i=0;i<=modes.length-1;i++){
		if(modes[i].checked){
			mode=true;
		}
	}
	if(!mode){
		alert("Please select a Mode of Payment")
		return mode;
	}
	if(pay && mode){
		document.forms.frmUser.submit.disabled=true;
	return true;
	
	}
	else{
	return false;
	}
	
}
   var isNN = (navigator.appName.indexOf("Netscape")!=-1);
   
function autoTab(input,len, e) {
  var keyCode = (isNN) ? e.which : e.keyCode; 
  var filter = (isNN) ? [0,8,9] : [0,8,9,16,17,18,37,38,39,40,46];
  if(input.value.length >= len && !containsElement(filter,keyCode)) {
    input.value = input.value.slice(0, len);
    input.form[(getIndex(input)+1) % input.form.length].focus();
  }

  function containsElement(arr, ele) {
    var found = false, index = 0;
    while(!found && index < arr.length)
    if(arr[index] == ele)
    found = true;
    else
    index++;
    return found;
  }

  function getIndex(input) {
    var index = -1, i = 0, found = false;
    while (i < input.form.length && index == -1)
    if (input.form[i] == input)index = i;
    else i++;
    return index;
  }
  return true;
}

function checkUsername(){
	var userName = $("txtuser");
		if(userName=="") {
		$("txtuserErr").innerHTML="<br>Missing : Username";
		$("txtuser").focus();
	} else {
		
		$("txtuserErr").innerHTML="";
		val = agent.call('',"checkUsername","",userName.value);
		
		
		if(val>0){
		
			$("txtuser").value="";
			$("txtuserErr").innerHTML="<br>Username Not Available";
			$("txtuser").focus();
			return false();
			
		} else {
			//$("txtuserErr").innerHTML="<br><span class=subHead>Username  Accepted</span>";
		}
	}
 }
function checkconsno()
{
	
	var cno=$("txtsection").value+$("txtarea").value+$("txtservice").value;
	$("txtconsnoErr").innerHTML="";
	var reg=$("drpregion");
	//var cno=$("txtconsno");
	if(cno.value == "") {
		$("txtconsnoErr").innerHTML="Missing : Consumer No";
		
	} else {
		
		$("txtconsnoErr").innerHTML="";
		val = agent.call('',"chkConsno","",cno+":"+reg.value);
		var row=eval(val);
		if(val.length >2 ){
			$("txtconsnoErr").innerHTML="Consumer No verified";
			$("cusname").value=row[0]['cname'];
			$("cusadd").value=row[0]['addres'];
		} else {
			$("txtconsnoErr").innerHTML="<span class=subHead>Invalid Consumer No / Region</span>";
			$("txtconsno").value="";
			
			return false;
		}
	}
}

function delconfirm(){
		var payCons=document.getElementsByName('operate[]');
		for(i=0;i<=payCons.length-1;i++){
			if(payCons[i].checked){
				var del=confirm("Are you sure to delete the Consumer No (s)");
				return del;
			}
		}
}
function deleteconfirm(){

		var payCons=document.getElementsByName('Delete');
		
				var del=confirm("Are you sure to delete the Consumer No ");
				return del;
			
}

function Checkpassword(){

var passwrd = $("txtpass");

if(passwrd.value.length < 6 ) 
			{ 
					alert("Password should be Min 6  characters");
					$("txtpass").value="";
					$("txtpass").focus();
					
			}	
			
			
}
function Checkaddress(){

var resaddress = $("txtresaddr");

if(resaddress.value.length > 250) 
			{ 
					alert("Address Fields allow Max 250 characters");
					$("txtresaddr").value="";
					$("txtresaddr").focus();
					
			}	
			
			
}
function Checkadd(){

var resaddress1 = $("txtaddr");

if(resaddress1.value.length > 250) 
			{ 
					alert("Address Fields allow Max 250 characters");
					$("txtaddr").value="";
					$("txtaddr").focus();
					
			}	
			
			
}


/*function Checksection(){

var section = $("txtsection");

if(section.value.length !=3 ) 
			{ 
					alert("Enter Valid Section Code");
					
					$("txtsection").focus();
					
			}	
			
			
}*/
function sumAmt(){
var pays=document.getElementsByName("pay[]");
var sum=0.0;
for(i=0;i<=pays.length-1;i++){
	if(pays[i].checked){
		var amt=pays[i].value.split(":");
		sum=sum+(parseFloat(amt[0]));
	}
}
document.getElementById('tPay').innerHTML=format(sum,2);
}

function format (expr, decplaces) {
var str = "" + Math.round (eval(expr) * Math.pow(10,decplaces));
while (str.length <= decplaces) {
str = "0" + str;
}
var decpoint = str.length - decplaces;
return str.substring(0,decpoint) + "." + str.substring(decpoint,str.length);
}
function assign(){
	var conNo=$("txtsection").value+$("txtarea").value+$("txtservice").value;
	//var conNo=$("cno").value;
	var regNo=document.getElementById("creg").value;
	var payCons=document.getElementsByName('pay[]');
	for(i=0;i<=payCons.length-1;i++){
	temp=payCons[i].value.split(":")
		if(temp[1] == (regNo+conNo))
		{
			alert("Consumer No : "+conNo+" is already in the List");
			return false;
		}
	}
	params=regNo+":"+conNo;
	agent.call('',"consumerno","allocate",params);
	$("status").style.visibility="visible";
}
function addCNo(){
 	var newcusNo=$("txtsection").value+$("txtarea").value+$("txtservice").value;
	//var newcusNo=$("cno").value;
	var regNo=document.getElementById("creg").value;
	var ecno=document.getElementById("ecusno").value;
	
	var payCons=document.getElementsByName('operate[]');
	for(i=0;i<=payCons.length-1;i++){
		if(payCons[i].value == (regNo+newcusNo))
		{
			alert("Consumer No : "+newcusNo+" is already in the List");
			return false;
		}
	}
	params=ecno+":"+newcusNo+":"+regNo;
	agent.call('',"addCno","newcusHandle",params);
	
	$("status").style.visibility="visible";
} 
function newcusHandle(obj)
{
	//alert(obj);
	
	$("status").style.visibility="hidden";
	if(obj.indexOf('Invalid')== 0){
		alert("Please provide valid Consumer No");
	}
	else{
		var newvalue=obj.split(":");
		var cmctab=document.getElementById("cmc");
		var newcmc=cmctab.insertRow(cmctab.rows.length);
		var conserialNo=newcmc.insertCell(0);
		var consumerNo=newcmc.insertCell(1);
		var conName=newcmc.insertCell(2);
		var conAddress=newcmc.insertCell(3);
		var conOption=newcmc.insertCell(4);
		
		conserialNo.innerHTML=(cmctab.rows.length) - 1;
		
		consumerNo.innerHTML=newvalue[0];
		conName.innerHTML=newvalue[1];
		conAddress.innerHTML=newvalue[2];
		conOption.innerHTML='<input type="checkbox" value="'+ newvalue[0]+'" name="operate[]" />';
		conOption.align="center";
		$("top").style.visibility="hidden";
		$("txtsection").value="";
		$("txtarea").value="";
		$("txtservice").value="";
		$('acmc').disabled=false;
		$('scmc').disabled=false;
		
	}
}
function testAjax(obj)
{
	//var id=10
	//agent.call('',"test","testJs",id)
	var coors = findPos(obj);
	$("top").style.visibility="visible";
	$("top").style.top = eval(coors[1]+ 40) + 'px';
	$("top").style.left = eval(coors[0] )+ 'px';
	$('acmc').disabled=true;
	$('scmc').disabled=true;
}
function cancelOp(){
	$('top').style.visibility='hidden';
	$('status').style.visibility='hidden';
	$('acmc').disabled=false;
	$('scmc').disabled=false;
}
function allocate(obj)
{
	
	$("status").style.visibility="hidden";
	if((obj.indexOf('Invalid')== 0) || (obj.indexOf('Bill')== 0)){
		alert("Please provide valid Consumer No");
	}
	else{
		var newvalue=obj.split(":");
		var cmctab=document.getElementById("cmc");
		var newcmc=cmctab.insertRow(cmctab.rows.length);
		var conserialNo=newcmc.insertCell(0);
		var consumerNo=newcmc.insertCell(1);
		var conName=newcmc.insertCell(2);
		var conAddress=newcmc.insertCell(3);
		var conAmt=newcmc.insertCell(4);
		var conOption=newcmc.insertCell(5);
		conserialNo.innerHTML=(cmctab.rows.length) - 1;
		consumerNo.innerHTML=newvalue[0].substr(2);
		conName.innerHTML=newvalue[1];
		conAddress.innerHTML=newvalue[2];
		conAmt.innerHTML=newvalue[3];
		conAmt.align="right";
		conOption.innerHTML='<input onclick="sumAmt();" type="radio" value="'+newvalue[3]+':'+ newvalue[0]+'" name="pay[]" />';
		conOption.align="center";
		$("top").style.visibility="hidden";
		$("cno").value="";
		$('acmc').disabled=false;
		$('scmc').disabled=false;
	}
}
function findPos(obj)
{
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		curWidth=obj.offsetTop
		while (obj == obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop,curWidth];
}
function chkPayment(){
	var pgc=document.getElementById("radpayment1").checked;
	var pgn=document.getElementById("radpayment2").checked;
	if(pgc || pgn)
	{
		return true;
	}
	else{
		alert("Please choose a Payment Mode");
		return false;		
	}
}

function recal(obj){
var msg = 'Please unselect in ascending order only, starting from the last.';
var el, prevEl, nextEl, e = -1, f = obj.form;
while (el = f.elements[++e]) if (el == obj) {
prevEl = (f.elements[e-1]) ? f.elements[e-1] : null;
nextEl = (f.elements[e+1]) ? f.elements[e+1] : null;
break;
}
if ((nextEl && nextEl.type == 'checkbox' && nextEl.checked)) {
alert(msg);
el.checked=true;
}
if ((prevEl && prevEl.type == 'checkbox' && !prevEl.checked)) {
alert(msg);
el.checked=false;
}

	var tab=document.getElementById("cmc");
	var ccharge=parseFloat(document.getElementById("cchargev").value);
	var totamt=parseFloat(document.getElementById("totamtv").value);
	var netamt=0.0;
		
	var total=ccharge;
	if(tab.rows.length>3)
	    {
		   for(i=2;i<=tab.rows.length-2;i++)
		   {
			   if(tab.rows[i].cells[2].firstChild.checked)
				{
					var temp=tab.rows[i].cells[2].firstChild.value.split("-");
					total+=parseFloat(temp[4]);	
					//temp[3]=true;
				}
	           else
			    {
				total+=0;
			    }
		    }		
        }
		
		
	 tab.rows[tab.rows.length-1].cells[1].innerHTML="Rs."+total;
	 //document.getElementById("totamtv").value=+total;
	if(document.getElementById('srvchg'))
	{
		var bcharge=(total*2.0946)/100;
		netamt=parseFloat(bcharge+total);
		var tab1=document.getElementById("cmc1");
		netamt=format(netamt,2)
		bcharge=format(bcharge,2)
		document.getElementById("srvchg").innerHTML=bcharge;
		tab1.rows[tab1.rows.length-1].cells[1].innerHTML="Rs."+netamt;
	}

return temp;
}

function confirmpay()
{
    if(document.all.frmUser.chbox.checked==true)
	{
	  document.all.frmUser.scmc.disabled=false;
	}
	if(document.all.frmUser.chbox.checked==false)
	{
	  document.all.frmUser.scmc.disabled=true;
	}

}

function js_array_to_php_array (a)
{
    var a_php = "";
    var total2 = 0;
    for (var key in a)
    {
        ++ total2;
        a_php = a_php + "s:" +
                String(key).length + ":\"" + String(key) + "\";s:" +
                String(a[key]).length + ":\"" + String(a[key]) + "\";";
    }
    a_php = "a:" + total2 + ":{" + a_php + "}";
    return a_php;
}

function disbut(val){
document.forms.frmUser.login.disabled=true;

}

function dissub(val){
document.forms.frmUser.chkout1.disabled=true;

}
function dissub1(val){
document.forms.frmUser.chkout.disabled=true;

}

function in_order(box) {
var msg = 'Please delete these records in ascending order only, starting from the last.';
var el, prevEl, nextEl, e = -1, f = box.form;
while (el = f.elements[++e]) if (el == box) {
prevEl = (f.elements[e-1]) ? f.elements[e-1] : null;
nextEl = (f.elements[e+1]) ? f.elements[e+1] : null;
break;
}
if ((prevEl && prevEl.type == 'checkbox' && prevEl.checked) ||
(nextEl && nextEl.type == 'checkbox' && !nextEl.checked)) {
alert(msg);
return false;
}
return true;
}

//////////////Light Box///////////
/*
var LightAlert = function (obj){
	this.fadetimer = null;
	this.browser = this.getBrowser();
	this.setting = this.setup(obj);
	this.body = parent.document.getElementsByTagName("body")[0];
	this.lightalert = this.createBox();
	this.overlay = {};
	this.init(obj);
}

LightAlert.prototype = {
	init: function(obj){
		THIS = this;
		this.overlay = this.displayOverlay(); //overlay
		this.displayMessage(obj); //box
		this.addEvent(window, "resize", function(){ THIS.getScroll() });
	},

	getBrowser: function(){
		b = navigator.userAgent.toLowerCase();
		if (b.indexOf('opera')!=-1) return 'opera';
		else if (b.indexOf('msie 7')!=-1) return 'ie7';
		else if (b.indexOf('msie')!=-1)  return 'ie6';
		else if (b.indexOf('safari')!=-1) return 'safari';
		else if (b.indexOf('gecko')!=-1)  return 'gecko';
	},

	setup: function(pars){
		var obj = {};
		obj.interval = 50;
		obj.show = pars.show || "0";
		obj.fade = pars.fade || "1"; 
		obj.fadein = 0; // 100 = n
		obj.fadetime = 500; // half seconds
		obj.fadestep = 25;
		obj.pause = 5000; //pause 5 seconds before fading out
		obj.overlayOpac = 65;
		obj.font =  pars.font || "bold 15px Trebuchet MS";
		obj.position = this.browser == "ie6" ? "absolute" : "fixed";		
		obj.bgcolor = pars.bgcolor || "#FFF";
		obj.border = pars.border || "1px solid #CCC";
		obj.icon = pars.icon || "./images/exclaim.gif";
		obj.w = pars.w || "300";
		obj.padding = "15px 15px 15px 59px";
		obj.bwidth = this.getBrowserWidth();
		return obj;
	},
	
	createBox: function(){
		box = document.createElement("div");
		box.style.font = this.setting.font;	
		box.style.padding = this.setting.padding;
		box.style.width = this.setting.w+"px";
		box.style.left = (this.setting.bwidth - this.setting.w) /2+"px";
		box.style.top = 0;
		box.style.position = this.setting.position;
		box.style.background = this.setting.bgcolor+" url("+this.setting.icon+") no-repeat 1em";
		box.style.border = this.setting.border;
		box.onclick = function(){THIS .destroy();}
		if (this.setting.fadein < 100){
			if (this.isIE())
				box.style.filter = "alpha(opacity="+this.setting.fadein+")";
			else
				box.style.opacity= this.setting.fadein / 100;
		}
		return box;
	},

	displayOverlay: function(){
		THIS = this;
		overlay = document.createElement("div");
		overlay.style.position = this.setting.position;
		overlay.style.backgroundColor = "#000";

		if (this.isIE())
			overlay.style.filter = "alpha(opacity="+this.setting.overlayOpac+")";
		else
			overlay.style.opacity= this.setting.overlayOpac/100;

		overlay.style.width = this.setting.bwidth+"px";
		overlay.style.height = this.getBrowserHeight()+"px";
		if (this.browser == "ie6"){
			overlay.style.height = Math.max(document.documentElement.clientHeight,document.body.scrollHeight) + 30;
		}
		overlay.style.left = 0;
		overlay.style.top = 0;
		overlay.onclick = function(){
			THIS.destroy();
		}
		this.body.appendChild(overlay);
		return overlay;
	},

	displayMessage: function(obj){
		var msg;
		if (typeof(obj) == "string")
			msg = obj;
		if (typeof(obj) == "object")
			msg = obj.msg;

		msg = typeof(msg) == "undefined" ? "//Syntax Error: msg is not defined" : msg;
		
		this.lightalert.innerHTML = msg;
		this.body.appendChild(this.lightalert);
		//realign to middle
		box.style.top = (this.getBrowserHeight() - this.lightalert.clientHeight) / 2 +"px";
		this.fadeIn();
	},
	
	fadeIn: function(){
		THIS = this;
		if (this.setting.fadein < 100 && this.setting.fade=="1"){
			this.setting.fadein += this.setting.fadestep;
			if (this.isIE()) {
				this.lightalert.style.filter = "alpha(opacity="+this.setting.fadein+")";
			}else{
				this.lightalert.style.opacity=this.setting.fadein/100;
			}
			this.fadetimer = setTimeout(function(){THIS.fadeIn()},this.setting.interval);
		}else{
			if (this.setting.fade=="0"){
				if (this.isIE())
					this.lightalert.style.filter = 100;
				else
					this.lightalert.style.opacity=1;
			}
			clearTimeout(this.fadetimer);
			if (this.setting.show == "0")
				setTimeout(function(){THIS.fadeOut()},this.setting.pause);
		}	
	},

	fadeOut: function(){
		THIS = this;
		if (this.setting.fadein > 0 && this.setting.fade=="1"){
			this.setting.fadein -= this.setting.fadestep;
			if (this.isIE()) {
				this.lightalert.style.filter = "alpha(opacity="+this.setting.fadein+")";
			}else{
				this.lightalert.style.opacity=this.setting.fadein/100;
			}
			this.fadetimer = setTimeout(function(){THIS.fadeOut()},this.setting.interval);
		}else{
			clearTimeout(this.fadetimer);
			this.destroy();
		}
	},

	destroy: function(){
		try{
			THIS = this;
			clearTimeout(this.fadetimer);
			this.body.removeChild(this.lightalert);
			this.body.removeChild(this.overlay);
			this.removeEvent(window, "resize", function(){THIS.getScroll()});
		}catch(e){}
	},
	
	isIE: function(){
		if (this.browser == "ie6" || this.browser == "ie7")
			return true;
		return false;
	},

	getScroll: function(){
		if (document.documentElement && document.documentElement.scrollTop) {
			t = document.documentElement.scrollTop;
			l = document.documentElement.scrollLeft;
			w = document.documentElement.scrollWidth;
			h = document.documentElement.scrollHeight;
		} else if (document.body) {
			t = document.body.scrollTop;
			l = document.body.scrollLeft;
			w = document.body.scrollWidth;
			h = document.body.scrollHeight;
		}
		this.lightalert.style.left = (w - this.setting.w) /2+"px";
		this.lightalert.style.top = (this.getBrowserHeight() - this.setting.h) /2+"px";

		this.overlay.style.width = this.getBrowserWidth()+"px";
		this.overlay.style.height = this.getBrowserHeight()+"px";
		if (this.browser == "ie6"){
			this.overlay.style.height = Math.max(document.documentElement.clientHeight,document.body.scrollHeight) + 30;
		}
		return t;
	},
	
	getBrowserWidth: function(){
		if (window.innerWidth){
			return window.innerWidth;
		}else if (document.documentElement && document.documentElement.clientWidth != 0){
			return document.documentElement.clientWidth;}
		else if (document.body){
			return document.body.clientWidth;
		}
		return 0;
	},

	getBrowserHeight: function(){
		if (window.innerHeight){
			return window.innerHeight;
		}else if (document.documentElement && document.documentElement.clientHeight != 0){
			return document.documentElement.clientHeight;}
		else if (document.body){
			return document.body.clientHeight;
		}
		return 0;
	},

	addEvent: function( obj, type, fn ) { 
		if ( obj.attachEvent ) { 
			obj['e'+type+fn] = fn; 
			obj[type+fn] = function(){obj['e'+type+fn]( window.event );} 
			obj.attachEvent( 'on'+type, obj[type+fn] ); 
		} else 
			obj.addEventListener( type, fn, false ); 
	},

	removeEvent: function ( obj, type, fn, funcObj ) { 
		THIS = this;
		if ( obj.detachEvent ) { 
			obj.detachEvent( 'on'+type, obj[type+fn] ); 
			obj[type+fn] = null; 
		} else {
			obj.removeEventListener( type, fn, false ); 
			
		}
	}
}

function alert(obj){
	new LightAlert(obj);
}
*/