// util.js
function pad_with_zeros(rounded_value, decimal_places) {
    var value_string = rounded_value.toString()
    var decimal_location = value_string.indexOf(".")
    if (decimal_location == -1) {
        decimal_part_length = 0
        value_string += decimal_places > 0 ? "." : ""
    }
    else {
        decimal_part_length = value_string.length - decimal_location - 1
    }
    var pad_total = decimal_places - decimal_part_length
    if (pad_total > 0) {
       for (var counter = 1; counter <= pad_total; counter++) 
            value_string += "0"
        }
    else
     {
      var pad_total = decimal_part_length - decimal_places
      for (var counter = 1; counter <= pad_total; counter++) 
            value_string = value_string.substring(0,value_string.length-1);
     }
    return value_string
}
//convart amount in words
function amt_convert(val){
	val = val.replace(/\,/g,"");
	//alert("val>>"+val);
	valFloor=Math.floor(val);
	//alert("valFloor>>"+valFloor);
	var varFloorStr=new String(valFloor);
	var valLength = varFloorStr.length;
	var crr = new String('');
	var crrString = new String('');
	var valString = new String('');
	//alert(valLength);
	if(valLength>7){
		crr = varFloorStr.substr(0,valLength - 7);
		val = val.substr(valLength - 7);
		//alert("crr>>"+crr+"<<val>>"+val);
		crrString = amt_in_words(crr,false);
		crrString = crrString + " Crore" ;
	}
	valString = amt_in_words(val,true);
	//alert("valString>>"+valString+"<<crrString>>"+crrString);
	var finalStr = crrString + ' ' + valString;
	return finalStr;
}
function amt_in_words(junkVal,addOnly)
{
	//alert("BIG--"+junkVal);
	var iWords=["Zero", " One", " Two", " Three", " Four", " Five", " Six", " Seven", " Eight", " Nine"];
	var ePlace=['Ten', ' Eleven', ' Twelve', ' Thirteen', ' Fourteen', ' Fifteen', ' Sixteen', ' Seventeen', ' Eighteen', ' Nineteen'];
	var tensPlace=['dummy', ' Ten', ' Twenty', ' Thirty', ' Forty', ' Fifty', ' Sixty', ' Seventy', ' Eighty', ' Ninety' ];

	junkVal = junkVal.replace(/\,/g,"")
	//alert(junkVal);
	var dotInd = junkVal.indexOf(".");
	var pointStr = -1;
	if(dotInd > 0){
		pointStr = junkVal.substr(dotInd+1,junkVal.length - 1);
	}
	//alert("<<dotInd>>"+dotInd+"<<pointStr>>"+pointStr);
	junkVal=Math.floor(junkVal);
	//alert(junkVal);
	if(Number(junkVal) >=0)
	{
		//do nothing
	}
	else
	{
		//alert('wrong Number cannot be converted');
		return false;
	}
	if(Number(junkVal)==0)
	{		
		return 'Zero Only';
	}
	var pointApplicable = true;
	if(pointStr > 0){
		pointApplicable = true;
	}else{
		pointApplicable = false;
	}

	var obStr=new String(junkVal);
	//var crrString = new String('');

	numReversed=obStr.split("");
	actnumber=numReversed.reverse();
	var pointString = new String('');
		
	if(pointApplicable){
		pointRev = pointStr.split("");
		pointNum = pointRev.reverse();
		//alert("pointRev>>"+pointRev);
		//alert("pointNum>>"+pointNum);
		var pointlength = pointRev.length;

		for(x=0; x<pointlength; x++){
		 	var val = pointRev[x];
			var word = iWords[val]
			pointString = word + ' ' + pointString   ;
		 }
	 	pointString = 'Point ' + pointString;
 	}
//	alert("obStr.length>>" + obStr.length);
//	if(obStr.length >= 8 ){
//		crrString = obStr.substr(0,obStr.length - 7);
//		alert("crrString>>"+crrString);
//	}
	

	if(actnumber.length>7)
	{
		alert('The Number is too big');
		return false;
	}
 
 
	var iWordsLength=numReversed.length;

	var totalWords="";
	var inWords=new Array();
	var finalWord="";
	var j=0;
	//alert("iWordsLength>>"+iWordsLength);
	for(i=0; i<iWordsLength; i++)
	{
		//alert("i--"+i+"j--"+j);
		switch(i)
		{
		case 0:
			if(actnumber[i]==0 || actnumber[i+1]==1 )
			{
				inWords[j]='';
			}
			else
			{
				inWords[j]=iWords[actnumber[i]];
			}
			inWords[j]=inWords[j]+ ' ' + pointString ;
			if(addOnly){
				inWords[j]=inWords[j]+' Only';
			}
			break;
		case 1:
			tens_complication();
			break;
		case 2:
			if(actnumber[i]==0)
			{
				inWords[j]='';
			}
			else if(actnumber[i-1]!=0 && actnumber[i-2]!=0)
			{
				inWords[j]=iWords[actnumber[i]]+' Hundred and';
			}
			else
			{
				inWords[j]=iWords[actnumber[i]]+' Hundred';
			}
			break;
		case 3:
			if(actnumber[i]==0 || actnumber[i+1]==1)
			{
				inWords[j]='';
			}
			else
			{
				inWords[j]=iWords[actnumber[i]];
			}
			/* Modified by Amit */
			/*	inWords[j]=inWords[j]+" Thousand"; */
			if(actnumber[i]!=0 || actnumber[i+1]!=0){
				inWords[j]=inWords[j]+' Thousand';
			}	
			break;
		case 4:
			tens_complication();
			break;
		case 5:
			if(actnumber[i]==0 || actnumber[i+1]==1 )
			{
				inWords[j]='';
			}
			else
			{
				inWords[j]=iWords[actnumber[i]];
			}
			/* Modified by Amit */
			/*	inWords[j]=inWords[j]+" Lakh"; */
			if(actnumber[i]!=0 || actnumber[i+1]!=0){
				inWords[j]=inWords[j]+' Lakh ';
			}	
			break;
		case 6:
			tens_complication();
			break;
//		case 7:
//			alert("7");
//			inWords[j] = amt_in_words(crrString) + " Crore";
//			break;
//		case 8:
//			tens_complication();
//			break;
		default:
			break;
		}
		//alert("i>>" + i +"<<WORD>>"+inWords[j]);
		j++;
	}
 
	function tens_complication()
	{
		if(actnumber[i]==0)
		{
			inWords[j]='';
		}
		else if(actnumber[i]==1)
		{
			inWords[j]=ePlace[actnumber[i-1]];
		}
		else
		{
			inWords[j]=tensPlace[actnumber[i]];
		}
		//alert(inWords[j]);
	}
	inWords.reverse();
	for(i=0; i<inWords.length; i++)
	{
		finalWord+=inWords[i];
	}
	
	return finalWord;
}

//Validate Pan Number
function panCheck(/*String*/ panEntered){
	var match=false;
	if(panEntered.length==10){
		var match = /[A-Z][A-Z][A-Z][A-Z][A-Z][0-9][0-9][0-9][0-9][A-Z]/.test(panEntered);
	}
	return match;
}

//Blank Ajax Request after a specific time delay 
var c=0; //counter
var t;   //var that capture setTimeout id
var timer_is_on=0; //status for timeoutfunction

function timedCount(csrfToken) {
	document.getElementById('txt').value=c;
	c=c+1;
	t=setTimeout("timedCount('"+csrfToken+"')",300000);
	retrieveURL('/QEPS/security/getServerResponce.do?count='+c + "&" + csrfToken);
	if(c >= 5){
		stopCount();
	}
}
function doTimer(csrfToken){
	if (!timer_is_on){
  		timer_is_on=1;
  		timedCount(csrfToken);
  	}
	$("#timeLeftId").html("");
}
function stopCount()
{
	clearTimeout(t);
	timer_is_on=0;
	c = 0;
}
function retrieveURL(url) {
	if (window.XMLHttpRequest) { 
		req = new XMLHttpRequest();
    	req.onreadystatechange = processStateChange;
    	try {
          req.open("GET", url, true); 
    	} catch (e) {
      		alert("Server Communication Problem\n"+e);
    	}
		req.send(null);
  	} else if (window.ActiveXObject) {
    	req = new ActiveXObject("Microsoft.XMLHTTP");
    	if (req) {
      		req.onreadystatechange=processStateChange;
      		req.open("GET", url, true);
      		req.send();
    	}
  	}
}
function processStateChange() {  
  if (req.readyState == 4) {
    if (req.status == 200) {
   		//DO NOTHING
    } else {
      alert("Problem with server response:\n " + req.statusText);
    }
  }
}
function timedCountOrder(csrfToken) {
	document.getElementById('txt').value=c;
	c=c+1;
	retrieveURL('/QEPS/security/getServerResponce.do?count='+c + "&" + csrfToken);
	if(c == 14){
		showAlertTime();
	}
	if(c >= 15){
		stopCountOrder();
	}
}
function doTimerOrder(csrfToken){
	if (!timer_is_on){
  		timer_is_on=1;
  		//timedCountOrder(csrfToken);
  		t1=setInterval(function() {timedCountOrder(csrfToken);}, 120000);
  	}
}
function stopCountOrder()
{
	clearTimeout(t1);
	timer_is_on=0;
	c = 0;
	/*window.location.href='/QEPS/security/signOutAction.do';*/
	window.location.href ='/QEPS/business/security.action?methodName=signOutMethod';
}
function showAlertTime()
{
	$(document).ready(function() {	

		var id = '#dialogOrder';
		//$(id).hide();
		//Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
	
		//Set heigth and width to mask to fill up the whole screen
		$('#maskOrder').css({'width':maskWidth,'height':maskHeight});
		
		//transition effect		
		$('#maskOrder').fadeIn(1000);	
		$('#maskOrder').fadeTo("slow",0.8);	
	
		//Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width();
              
		//Set the popup window to center
		$(id).css('top',  winH/2-$(id).height()/2);
		$(id).css('left', winW/2-$(id).width()/2);
	
		//transition effect
		$(id).show();
		$(id).fadeIn(2000); 	
		$('html, body').animate({ scrollTop: $(id).offset().top }, 'slow');
		//if close button is clicked
		$('.windowOrder .closeOrder').click(function (e) {
			//Cancel the link behavior
			e.preventDefault();
			
			$('#maskOrder').hide();
			$('.windowOrder').hide();
		});		
		
		//if mask is clicked
		$('#maskOrder').click(function () {
			$(this).hide();
			$('.windowOrder').hide();
		});		
	
	});
}