function JSONscriptRequest(fullUrl) {
    // REST request path
    this.fullUrl = fullUrl; 
    // Keep IE from caching requests
    this.noCacheIE = '&noCacheIE=' + (new Date()).getTime();
    // Get the DOM location to put the script tag
    this.headLoc = document.getElementsByTagName("head").item(0);
    // Generate a unique script tag id
    this.scriptId = 'JscriptId' + JSONscriptRequest.scriptCounter++;
}

// Static script ID counter
JSONscriptRequest.scriptCounter = 1;

// buildScriptTag method
//
JSONscriptRequest.prototype.buildScriptTag = function () {
    // Create the script tag
    this.scriptObj = document.createElement("script");
    
    // Add script object attributes
    this.scriptObj.setAttribute("type", "text/javascript");
    this.scriptObj.setAttribute("charset", "utf-8");
    this.scriptObj.setAttribute("src", this.fullUrl + this.noCacheIE);
    this.scriptObj.setAttribute("id", this.scriptId);
}
 
// removeScriptTag method
// 
JSONscriptRequest.prototype.removeScriptTag = function () {
    // Destroy the script tag
    this.headLoc.removeChild(this.scriptObj);  
}

// addScriptTag method
//
JSONscriptRequest.prototype.addScriptTag = function () {
    // Create the script tag
    this.headLoc.appendChild(this.scriptObj);
}

function clearMobileNo(){
    document.getElementById('mobile').focus();
    document.getElementById('mobile').value='+91';
	//document.getElementById('captcha_input').value='';
}
function trim1(str, chars) {
    return ltrim1(rtrim1(str, chars), chars);
}

function ltrim1(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim1(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function putWait()
{
   document.getElementById("label1").innerHTML = '<img src="images/SendingNgpay.png" alt="SendingNgpay" />';
// link = document.getElementById("submit_image");
// link.target = 
}

function revertlabel1()
{
  document.getElementById("label1").innerHTML = '<img src="images/EnterValidNumber.png" alt="Valid No."/>';
}

//function revertlabel2()
//{
  //document.getElementById("label2").innerHTML = 'Enter verification code';
//}

//function reloadCaptcha()
//{
//     var image = document.getElementById("captcha_image");
    // image.src= "http://www.ngpay.com/getCaptcha.do?id="+Math.random();
//}

function publishErrorMobileNumber(message)
{
 // revertlabel2();
//  document.getElementById("label1").innerHTML = '<div class="pocket_error_content">'+message+'</div>';
  document.getElementById("label1").innerHTML = '<img src="images/' + message + '" alt="Error" />';
}

function publishErrorCaptcha(message)
{
  revertlabel1();
  document.getElementById("label2").innerHTML = '<div class="pocket_error_content">'+message+'</div>';
}


// Global variable
var bObj;

function removeWaitAndPublishServerResponse(message)
{
  publishServerResponse(message);
// link = document.getElementById("submit_image");
// link.target = 
}

function publishServerResponse(message)
{
	if(bObj != null){
		bObj.removeScriptTag();
	}
	//alert(message);
 // if(message == null || message.length == 0) {
    // publishErrorMobileNumber('NoResponse.png');
 // } else if(message.indexOf('Error: Verification') == 0 ){
   //  publishErrorCaptcha('PleaseTryAgain.png');
 // } else if(message.indexOf('Error') == 0 ){
   //  publishErrorMobileNumber('PleaseTryAgain.png');
 // } else if (message.indexOf('Thanks') != 0){  
    // publishErrorMobileNumber('NoResponse.png');
//  } else{
     document.getElementById("label1").innerHTML = 'You will shortly receive a link to download ngpay. If you do not get it within a minute, try one of the options below';
     clearMobileNo();
 // }
  //reloadCaptcha();
// setTimeout("revertlabel1()",10000);
// link = document.getElementById("submit_image");
// link.target = 
}


function submitGetClientPush()
{
    putWait();
	var mobile=document.getElementById('mobile').value;
	//var channelval=document.getElementById('channel').value;
	//var channelval=<?php echo $_GET["channel"]; ?>;
	var channelval="irctcChannel";
	alert(mobile);
	alert(channelval);
	//var captcha_input=document.getElementById('captcha_input').value;
	try {
			var req="http://www.ngpay.com/getClientPush.js" + '?mobile='+mobile 
					+"&channel="+channelval;
			alert(req);
			bObj = new JSONscriptRequest(req);
			bObj.buildScriptTag();
			bObj.addScriptTag();
	} 
	catch(e) {
		
			//alert(e);
			
			publishServerResponse("PleaseTryAgain.png");
	} 
}

function submit2(){
 // revertlabel2();
  submitGetClientPush();
  return;
}

function submit1(){
	var mobileNo=document.getElementById("mobile").value;
	alert(mobileNo);
	alert(channel);
//	alert(mobileNo.length);
	mobileNo=trim1(mobileNo,' ');
//	alert(mobileNo.length);
	if(mobileNo.indexOf('+')==0){
		mobileNo=mobileNo.substring(1);
	}
	if(mobileNo.indexOf('91')==0){
		mobileNo=mobileNo.substring(2);
	}
//	alert(mobileNo);
//	alert(mobileNo.length);
	if(mobileNo.length 
           < 10){
		publishErrorMobileNumber('EnterDigits.png');
    return;
	}
	if(!isNumeric(mobileNo)){
		publishErrorMobileNumber('EnterOnlyDigits.png');
		return;
	}
	//var captchaInput=document.getElementById("captcha_input").value;
	//captchaInput=trim1(captchaInput,' ');
	//if(captchaInput.length != 5){
		//publishErrorCaptcha('Enter verification code');
		//return;
	//}
  submit2();
}

function isNumeric(strString)   {
   var strValidChars = "0123456789";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;
   for (var i = 0; i 
            < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   }

function myOnload(){
	var location = window.location+"";
var index = location.lastIndexOf("?");
if(index < 0)
{
	return;
}
var queryParams= location.substring(index,location.length);
if(queryParams == null){
	return;
}
var arr = new Array();
arr = document.getElementsByTagName('a');
for(var i = 0; i < arr.length; i++){
	var hrefAttribute=arr[i].getAttribute("href");
	var modifiedUrl=hrefAttribute+queryParams;	
	arr[i].setAttribute('href',modifiedUrl);
}
//for options
 arr = new Array();
arr = document.getElementsByTagName('option');
for(var i = 0; i < arr.length; i++){
	var hrefAttribute=arr[i].value;
	if(value.indexOf("html") >0) {
	var modifiedUrl=hrefAttribute+queryParams;	
	arr[i].value =modifiedUrl;
	}
}
	
}
if (window.addEventListener) {
	window.addEventListener( "load", myOnload, false );
}
else if (window.attachEvent) {
	window.attachEvent( "onload", myOnload );
}

