//validate the recharge form
function validaterechargeform() {
	var errorfound = 0;
	/* Validate the mobile no*/
	if(!validatedata("ezmobno", "valmono", false))
		errorfound = 1;
	/* Validate the serpovider*/
	if(!validatedata("ezservprov", "valpro", false))
		errorfound = 1;
	/*validate the amount*/
	if(!validatedata("ezamount", "valamt", false))
	{
		errorfound = 1;
		var valamt = 1;
	}
	/* Validate the location*/
	if(!validatedata("ezlocation", "valloc", false))
		errorfound = 1;	
		
	/*validate the mobile number length*/	
	if (!checkfldlength("ezmobno", 10)) {
		showerror("valmono");
		errorfound = 1;
	} else {
		shownoerror("valmono");
	}
	if (checkMobileNo(document.getElementById("ezmobno").value) == false) {
		showerror("valmono");
		errorfound = 1;
	} else {
		shownoerror("valmono");
	}
	var stramount = getvalue("ezamount");
	var amount = parseInt(stramount);
	if (amount < 10 ) {
		errorfound = 1;
		showerror("valamt");
		alert ("Amount cannot be less than Rs.10");
	} else if(valamt !=1) {
		shownoerror("valamt");
	}

	/*this return the true if no error and vice versa*/	
	if(errorfound  == 0)
		return true;

	return false;
}
//validate form and show the confirm message 
function ConfirmPay() {
	if(validaterechargeform()) 
	{
		//get the internetcharges
		var rc = getvalue("ezamount");
        var sp = getvalue("ezservprov");
        var loc = getvalue("ezlocation");

		//hide the form and visible the confirm form 
		document.getElementById("idrechargeform").style.visibility="hidden";
		document.getElementById("loadalab").style.visibility="visible";
        document.getElementById("ezservprov").style.visibility = "hidden";
        document.getElementById("ezlocation").style.visibility = "hidden";
		
		getslabval(rc);
	}
}
function resetRadio()
{
	document.getElementById("normal").checked = false;
	document.getElementById("spl").checked = false;	
}
function valButton(btn) {
    var cnt = -1;
    for (var i=btn.length-1; i > -1; i--) {
        if (btn[i].checked) {cnt = i; i = -1;}
    }
    if (cnt > -1) return btn[cnt].value;
    else return null;
}

//get internet charges from database
function getslabval(amt) {
	var address = "amt=" +encodeURI(amt);
	ajaxpost('php/getslab.php',address,getslabval_callback);
}
var internetcharges;
function getslabval_callback() {
	if (xmlobject.readyState==4 || xmlobject.readyState=="complete")
	{
		//get the return value from php
		internetcharges = xmlobject.responseText;
		alertDetails(getvalue("ezservprov"),getvalue("ezlocation"),getvalue("ezamount"));
		//this function fills the values in the confirmation box
		fillvalues();
		//HIK document.getElementById("confirmrecharge").focus();
	}
}

//get recharge tariff details
function alertDetails(p_sprov,p_loc,p_amt) 
{
	var address = "sprov="+encodeURI(p_sprov)+"&location="+escape(p_loc)+"&amount="+encodeURI(p_amt);
	ajaxpost('php/checkrecharge.php',address,alertDetails_callback);
}
function alertDetails_callback()
{
	if (xmlobject.readyState==4 || xmlobject.readyState=="complete")
	{
		//get the return value from php
		var getinfo = xmlobject.responseText;
		if(getinfo != "1")
		{
			document.getElementById("hintbox").style.visibility="visible";
			if(getinfo == "")	
			{
				document.getElementById("loadinf").innerHTML="Looks to be an \"Invalid Amount\"";	
			}
			else
			{				
				document.getElementById("loadinf").innerHTML=getinfo;	
			}
		}
	}
}

function fillvalues() {
	document.getElementById("loadalab").style.visibility="hidden";
	document.getElementById("idreconfirmform").style.visibility="visible";
	//get the value and set value
	setvalue("conmobno", getvalue("ezmobno"));
	
	/*Code changed by Deepu*/
	var rctype = getvalue("rctype");
	var sp = getvalue("ezservprov");
	if(sp == "TataDocomo" || sp == "TataDocomo")
	{
		if(rctype == "normal")
			sp = "TataDocomo";
		else
		if(rctype == "special")
			sp = "DocomoSpecial";
		else
			sp = "TataDocomo";			
	}
	if(sp == "Videocon" || sp == "videocon")
	{
		if(rctype == "normal")
			sp = "Videocon";
		else
		if(rctype == "special")
			sp = "VideoconSpecial";
		else
			sp = "Videocon";			
	}
	
	setvalue("conserpro",sp);
	setvalue("conloc", getvalue("ezlocation"));
	//calculate the grand total 
	var amount = getvalue("ezamount");
	var finamt=parseFloat(amount);
	var finintcha=parseFloat(internetcharges);
	var grandtotal = finamt + finintcha;
	//concat with the INR and with RS
	var embamt = "Rs. "+amount+" (INR)";
	var embintchg = "Rs. "+internetcharges+" (INR)";
	var embgdtot = "Rs. "+grandtotal+" (INR)";
	//set value after the calulation and concat 
	setvalue("conamt",embamt);
	setvalue("conintchg",embintchg);
	setvalue("contotal",embgdtot);
}
//this redo ConfirmPay function for user to edit
function goback() {
	document.getElementById("idrechargeform").style.visibility="visible";
	document.getElementById("payingmode").style.visibility="hidden";
	document.getElementById("idreconfirmform").style.visibility="hidden";
	document.getElementById("hintbox").style.visibility="hidden";
	document.getElementById("ezservprov").style.visibility = "visible";
	document.getElementById("ezlocation").style.visibility = "visible";
}
// show the paying mode
function payopt() {
	document.getElementById("payingmode").style.visibility="visible";
	document.getElementById("idreconfirmform").style.visibility="hidden";
	document.getElementById("hintbox").style.visibility="hidden";
}
//gets banks from database coressponding to pay mode
function getbank(opt) {
	var address = "option=" +encodeURI(opt);
	ajaxpost('php/getbank.php',address,getbank_callback);
}
//call back of getbank function
function getbank_callback()
{
	if (xmlobject.readyState==4 || xmlobject.readyState=="complete")
	{
		var combobox = xmlobject.responseText;
		document.getElementById("idbanks").innerHTML = combobox;
	}
}
//validate payment options and terms and conditions
function valpayment(){

	//var comval = document.getElementById("bankscombo").value;
	var termscheck = document.getElementById("termscondition").checked;
	var mobilecheck = document.getElementById("mobilecondition").checked;
	var locationcheck = document.getElementById("amountcondition").checked;
	var sprovcheck = document.getElementById("sprovcondition").checked;
	//if (comval=="0") {
	//	alert("Invalid Payment Option")
	//	return false;
	//}
	if (termscheck) 
	{
		if ((!mobilecheck) || (!locationcheck) || (!sprovcheck))
		{
			alert("Please select the Mandatory options");
			return false;
		} else {
			return true;
		}
	} else {
		alert("Select Terms and Condition");
		return false;
	}
}
//enable rest of the check box
function enablechkbox(){
	var chkterms = document.getElementById("termscondition").checked;
	document.getElementById("mobilecondition").disabled=!chkterms;
	document.getElementById("amountcondition").disabled=!chkterms;
	document.getElementById("sprovcondition").disabled=!chkterms;
}
//checks terms & condition on text click
function selectchk(fldid) {
	var fld = document.getElementById(fldid)
	if (fld.checked){
		fld.checked=false;
		document.getElementById("mobilecondition").disabled=true;
		document.getElementById("amountcondition").disabled=true;
		document.getElementById("sprovcondition").disabled=true;
	} else {
		fld.checked=true;
		enablechkbox();
	}
}
//select radio button on the bank
function chkbank(fldid) {
	document.getElementById(fldid).checked=true;
	getbank(fldid);
}
function selectsubchk(fldid){
	var chkterms = document.getElementById("termscondition").checked;
	if(chkterms){
		var fld = document.getElementById(fldid)
		if (fld.checked){
			fld.checked=false;
		} else {
			fld.checked=true;
		}
	}
}
//Function will validate and Submit the form
function valgetaddr()
{
	var payname = document.getElementById('getcrename').value;
	var payaddr = document.getElementById('getcreaddr').value;
	var paycity = document.getElementById('getcrecity').value;
	payaddr=payaddr.split(" ");
	var errorfound = 0;
	// Validate the address
	if(!validatedata("getcrename", "valgetname", false))
		errorfound = 1;
	if(!validatedata("getcreaddr", "valgetaddress", false))
		errorfound = 1;
	if(!validatedata("getcrecity", "valgetcity", false))
		errorfound = 1;
	if(!validatedata("paystate", "valgetcrestate", false))
		errorfound = 1;
	if(!validatedata("getcrezip", "valgetcrezip", false))
		errorfound = 1;
	if(!validatedata("paycountry", "valgetcrecountry", false))
		errorfound = 1;
	if(!validatedata("getcremail", "valgetcremail", true))
	{
		errorfound = 1;
	}
	if(payname.length<3)
	{
		showerror("valgetname");
		errorfound = 1;
	}
	if(paycity.length<4)
	{
		showerror("valgetcity");
		errorfound = 1;
	}
	if(payaddr.length<2)
	{
		showerror("valgetaddress");
		errorfound = 1;
	}
	if(!document.getElementById("termschk").checked)
	{
		alert("Please select the acknowledgement");
		errorfound = 1;
	}
	//this return the true if no error and vice versa

	if(errorfound  == 1)
		return false;
	return true;
} 
function validateEmail()
{
		var eml = document.getElementById('getcremail').value;
		var address = "className=NOCLASS" + "&functionName=checkEmailEX" + "&eml=" + encodeURI(eml);
		document.getElementById("valgetcremail").innerHTML = "";
		document.getElementById("valgetcremail").innerHTML = "<img src='../images/load.gif' width='17px'/>";
		ajaxpost('php/callback.php',address,emlchk_callback);
}
function emlchk_callback()
{
	if (xmlobject.readyState==4 || xmlobject.readyState=="complete")
	{
		var response = xmlobject.responseText;
		if(response == "INVALIDEMAIL")
		{
			alert("Invalid E-Mail ID. Please enter again.");
			
			document.getElementById("termschk").checked = false;
			document.getElementById('getcremail').value = "";
	
			document.getElementById("valgetcremail").innerHTML = "";
			showerror("valgetcremail");
			return false;
		}else{
			document.getElementById("valgetcremail").innerHTML = "";
			document.getElementById("valgetcremail").innerHTML = "<img src='images/tick.png' width='17px'/>";
			return true;
		}
	}
}
function uncheckterms()
{
	document.getElementById("termschk").checked = false;
}
function IsNumeric(sText)
{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
}
/* Function to validate Mobile Number entered in Recharge Box */
function checkMobileNo(mobNo)
{
	var mobNoLen = mobNo.length;
	if(mobNoLen <= 0)
	{
		return false;
	}
	if(IsNumeric(mobNo) == false)
	{
		return false;
	}	
//	if(mobNoLen <= 9 || mobNo.charAt(0) == '0' || mobNo.charAt(0) == '1'  || mobNo.charAt(0) == '2' || mobNo.charAt(0) <= '3' || mobNo.charAt(0) == '4' || mobNo.charAt(0) == '5' || mobNo.charAt(0) == '6')
	if(mobNoLen <= 9 || mobNo.charAt(0) == '0')
	{
		return false;
	}
	return true;
}

