/*******************************************************************************
 * File Name            : JCPFrames.js
 * Author               : Keane India Pvt Ltd.,
 * Date of Creation     : 29 September 2008.
 * Description          : Created for Frames Module.
 * Version Number       : 1.0
 * Modification History :
 Date         Version  Who             Description of change
 2008-09-29   1.0      Keane India     Initial - created method for setting the
 									   different parameters into frameTop and
 									   read the XML data.
*******************************************************************************/
var queryParameters = new Array();
queryParameters[0] = "";
queryParameters[1] = "";
queryParameters[2] = "";

// Reads the parameters sent from the HOME Page through the URL.
var query = window.location.search.substring(1);
var parms = query.split('&');
var pos = 0;
var key = "";
var val = "";

for (var i = 0; i < parms.length; i++) {
    pos = parms[i].indexOf('=');

    if (pos > 0) {
        key = parms[i].substring(0, pos);
       	val = parms[i].substring(pos+1);
       	queryParameters[i] = val;
	}
}

var httpRequestSupported = true;
var langId = queryParameters[0];

if (langId == -1 || langId == -11 || langId == -10 || langId == -4) {
	langId = 0;
}

function ajaxCallback(pText) {
	top.frames[1].document.getElementById('xmlData').value = escape(pText);
}

function isHttpRequestSupported() {

   try {
      httpRequest = new XMLHttpRequest();
   }
   catch (objErr) {
      try { 
         httpRequest = new ActiveXObject("Microsoft.XMLHTTP");
      } catch(aXObjErr) {
         try {
            httpRequest = new ActiveXObject("Msxml2.XMLHTTP");
         } catch(e) {
            httpRequestSupported = false;
         }
      }
    }

   return httpRequest;
   
} 

 /**
 * Function to make a synchronous AJAX call to the url specified and register a 
 * callback function.
 *
 * @param url				String - the url to which the synchronous call is to
 *							be made.
 * @param callbackFunction  String - The callback function to be registered.
 */
function makeSyncRequest(url, callbackFunction, returnData) {

    if ((returnData) || (returnData == "yes")) {
       returnData = true;
    }
    else {
       returnData = false;
    }

	var httpRequest = false;
	
	// Exit if this function is not supported
	if (!httpRequestSupported) {
		return;
	}

	// check if supported
	httpRequest = isHttpRequestSupported();

	if (!httpRequest) {
		httpRequestSupported = false;
		return false;
	}

	// Map the response to the callback function
	httpRequest.onreadystatechange = function() {

	  	   if (httpRequest.readyState == 4) {
			   if (httpRequest.status == 200) {
				  if (returnData) {
					eval(callbackFunction + '(httpRequest.responseXML.documentElement.firstChild.data)');
				  } else {
				    eval(callbackFunction + '(httpRequest.responseText)');
			        busy=false;
			     }
			   } else {
                  if (returnData) {
                     var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
                     xmlDoc.async = false;
                     xmlDoc.load(url);
			         eval(callbackFunction + '(xmlDoc.documentElement.firstChild.data)');                   
                  }
               else {
			      eval(callbackFunction + '(httpRequest.responseText)'); 
		       }
		   }
	   }
	}

    try {
        httpRequest.open('POST', url, true);
        httpRequest.send(null);
    } catch (e) {    
        alert(eval("PAGE_NOT_DISPLAYED_PROPERLY" + languageCode));
    }
}




function updateFrameParameters() {

   // Write parameters into hidden fields to be accessed by the HTML pages constituting the Frame.
   if("JCP" == queryParameters[2]) {
       top.frames[1].document.getElementById('catalogId').value = "10051";
	   top.frames[1].document.getElementById('storeId').value = "10051";
   }
   top.frames[1].document.getElementById('languageId').value = queryParameters[0];
   top.frames[1].document.getElementById('Country').value = queryParameters[1];
   
   urlStr = "../menus/L" + langId + "/models_l_id_" + queryParameters[0] + ".xml";

   makeSyncRequest(urlStr, "ajaxCallback", "yes");   

}
