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 = '<div class="pocket_server_response_content">Sending ngpay... <br>Please wait!!!</div>';
// link = document.getElementById("submit_image");
// link.target = 
}

function revertlabel1()
{
  document.getElementById("label1").innerHTML = 'Enter your mobile number and click submit to get ngpay on your handset';
}

function revertlabel2()
{
  document.getElementById("label2").innerHTML = 'Enter verification code as shown';
}

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>';
}

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();
	}

  if(message == null || message.length == 0) {
     publishErrorMobileNumber('No Response!<br>Please try again');
  } //else if(message.indexOf('Error: Verification') == 0 ){
    // publishErrorCaptcha(message);
 // }
  else if(message.indexOf('Error') == 0 ){
     publishErrorMobileNumber(message);
  } else if (message.indexOf('Thanks') != 0){  
     publishErrorMobileNumber('No Response!<br>Please try again');
  } else{
     document.getElementById("label1").innerHTML = '<div class="pocket_server_response_content">'+message+'</div>';
     clearMobileNo();
  }
  //reloadCaptcha();
// setTimeout("revertlabel1()",10000);
// link = document.getElementById("submit_image");
// link.target = 
}


function submitGetClientPush()
{
    putWait();
	var mobile=document.getElementById('mobile').value;
	//var captcha_input=document.getElementById('captcha_input').value;
	try {
			var req="http://www.ngpay.com/getClientPush.js" + '?mobile='+mobile +  "&channelCode=irctc&channel=irctc";
			bObj = new JSONscriptRequest(req);
			bObj.buildScriptTag();
			bObj.addScriptTag();
	} 
	catch(e) {
			publishServerResponse("Error: Could not connect to server. Please try again.");
	} 
}

function submit2(){
 // revertlabel2();
  submitGetClientPush();
  return;
}

function submit1(){
	var mobileNo=document.getElementById("mobile").value;
//	alert(mobileNo);
//	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('Error: Mobile number should not be less than 10 digits!');
    return;
	}
	if(!isNumeric(mobileNo)){
		publishErrorMobileNumber('Error: Mobile number should contain only digits!');
		return;
	}
	/*var captchaInput=document.getElementById("captcha_input").value;
	captchaInput=trim1(captchaInput,' ');
	if(captchaInput.length != 5){
		publishErrorCaptcha('Enter verification code as shown');
		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;
   }
