var runQAS = false;

/**
Method Added by MohmedShoaib Shaikh to override behavior of checkit(pFormName) method.
*/

function verifyit(pFormName)
{
	if(runQAS)
	{
		if(checkit(pFormName))
		{
			var pcont = document.getElementById('pCountry').value;
			if(pcont == "USA" || pcont == "CAN")
			{
				
				//All of these return false because the ajax call is async...
				if(!verifyAddress(pFormName))
				{
					//alert("top block");
					return false;
				}
				else
				{
					//alert("bottom block");
					return false;							
				}
				
			}
			else
			{
				return true;
			}
			
		}
		else
		{
			return false;
		}
	}
	else
	{
		return checkit(pFormName);
	}
	
}

var QASFormName = "";

/**
Method added by MohmedShoaib Shaikh.
Method will return true, if verifyAddress succeeds.
Method will return false, if verifyAddress fails.
*/

function verifyAddress(pFormName) {
	
	QASFormName = pFormName;
	
	//alert("hello dolly");
	
  url1 = '/store/common/qas/addressverification.jsp';
  data1 = 'address1=' + document.getElementById('pAddress1').value;
  data1 += '&address2=' + document.getElementById('pAddress2').value;
  data1 += '&city=' + document.getElementById('pCity').value;
  data1 += '&country=' + document.getElementById('pCountry').value;
  data1 += '&state=' + document.getElementById('pState').value;
  data1 += '&postalcode=' + document.getElementById('pPostalCode').value;
  /*alert('URL:'+url1+'?'+data1);*/
$.ajax({
   type: "GET",
   url: url1,
   data: data1,
   async: "true",
   dataType: "html",
   timeout: 2000,
   success: function(msg){ 
      /*alert('msg:'+msg);*/
      if(startsWith(msg, "ERROR")) {
         /* QAS Service is down. 
            TO DO: Add js to submit main form in this block                                    
         */
		
		runQAS = false;
		document.getElementById("test123").click();
		
		//document.forms[pFormName].submit();
         return false;
      }
	  else if(startsWith(msg, "SUPERVERIFIED"))
	  {
	  	runQAS = false;
		document.getElementById("test123").click();
		//document.forms[pFormName].submit();
	  	return false;
	  }
	  else{
	  	
		//alert(msg.substring(0, 13));
		
         /* QAS is up. Open the modal with the qas response inserted into the content div */
		$('#qasWindow').jqm(qas_jqm_init);
		$('#qasWindow').jqmShow(this).find('div.modalContent').html(msg);                                 
		return true;
      } 
   }
 });
  //return false;
}

function startsWith(string, str)
{
	return (string.match("^"+str) == str);
}

var qas_jqm_init = {
    defaultWidth: 700,
    defaultHeight: 500,
    closeClass: 'modalClose'
}

$().ready(function() {
   $("<div class='modalWindow' id='qasWindow'><table border='0' cellspacing='0' cellpadding='0'><tr class='modalControls'><td class='modalCaption'>Address Validation</td><td align='right'><a class='modalClose'></a></td></tr><tr><td colspan='2'><div class='modalContent'></div></td></tr></table></div>").appendTo("body"); 
    /*initializes qasWindow as a modal window
    {
                defaultWidth: 700,
                //defaultHeight: "auto",
                closeClass: 'modalClose'
    }); */   
});


function getHttpConnect(){
var xmlhttp=false;
 try {
    xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
 } catch (e) {
    try {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } catch (E) {
    xmlhttp = false;
   }
 }

if (!xmlhttp && typeof XMLHttpRequest!='undefined') {
	try {
		xmlhttp = new XMLHttpRequest();
	} catch (e) {
		xmlhttp=false;
	}
}
if (!xmlhttp && window.createRequest) {
	try {
		xmlhttp = window.createRequest();
	} catch (e) {
		xmlhttp=false;
	}
}
return xmlhttp;
}

function parseQASResponse(jsonString){
/*alert('JSON.qas_ADDRESS1:'+jsonString.qas_address1);*/
strQASHTML = '<TABLE>';
if(jsonString.qas_address1 !=undefined) { 
   strQASHTML+='<TR><TD>'+ jsonString.qas_address1 + '</TD></TR>';
}
if(jsonString.qas_address2 !=undefined) { 
   strQASHTML+='<TR><TD>'+ jsonString.qas_address2 + '</TD></TR>';
}
if(jsonString.qas_address3 !=undefined) { 
   strQASHTML+='<TR><TD>'+ jsonString.qas_address3 + '</TD></TR>';
}
if(jsonString.qas_city !=undefined) { 
   strQASHTML+='<TR><TD>'+ jsonString.qas_city + '</TD></TR>';
}
if(jsonString.qas_state !=undefined) { 
   strQASHTML+='<TR><TD>'+ jsonString.qas_state + '</TD></TR>';
}
if(jsonString.qas_postalcode !=undefined) { 
   strQASHTML+='<TR><TD>'+ jsonString.qas_postalcode + '</TD></TR>';
}
if(jsonString.qas_country !=undefined) { 
   strQASHTML+='<TR><TD>'+ jsonString.qas_country + '</TD></TR>';
}
   strQASHTML+='</TABLE>';
/*alert('INNERHTML:'+strQASHTML);*/
document.getElementById('qas_address').innerHTML = strQASHTML;
}
