
//
// IUR3 Remote API
// (c) www.disney.com 2005
//
//

//  function IURCommand(reg_url, task, promotion_id, skin, next_url, site_code, member_name,
//	                     password, ageBand, fullScreen, q1, q2, q3, q4, q5, q6, q7, q8, q9, q10,...) {


//  SzP: 06/10/2005 - IUR3 version.
//
//
//  TCW: 26/2/2001 - Rewrote this function to:
//		1.  allow for more than 10 answers to be submitted
//		2.  allow for a total length of more than 255 characters to be submitted as answers
//
//  TCW: 18/4/2001 - Changed declaration of answers array to ensure no error in the case when there are less than 8 arguments

 var openwin= null;

// this is for vbackward compatibility with IUR2 sites. simply calls launchIUR() with the appropriate parameters
function IURCommand() {
  var answers
  if (arguments.length > 8) {
    answers		= new Array(arguments.length - 8);
  } else {
    answers		= null;
  }

  // Retrieve arguments 0 - 7
  var reg_url		= arguments[0];
  
  
  
  var task			= arguments[1];
  var promotion_id	= escape(arguments[2]);
  var skin_name		= escape(arguments[3]); // was bu in old IUR
  var next_url		= escape(arguments[4]);
  var site_code		= escape(arguments[5]); // was country in old IUR
  var member_name	= escape(arguments[6]);
  var password		= escape(arguments[7]);

  // Loop through and retrieve the answers - arguments 8 to n.  (Will retrieve all answers regardless of how many).
  for (var i = 8; i < arguments.length; i++) {
    answers[i - 8] = escape(arguments[i]);
    //alert(answers[i - 8])
  }
  
  _launchIURInternal(
			/* target_url       */  reg_url, 
			/* task             */  task, 
			/* promotion_id     */  promotion_id, 
			/* skin_name        */  skin_name, 
			/* next_url         */  next_url, 
			/* site_code        */  site_code, 
			/* member_name      */  member_name, 
			/* password         */  password,
			/* ageBand          */  "",
			/* fullScreen       */  "",			
			/* swid	            */  "",			
			/* answers          */  answers);  
			  
}			


function launchIUR() {

  var answers = null;

  // Retrieve arguments 0 - 9
  var reg_url      = arguments[0];
  var task         = arguments[1];
  var promotion_id = escape(arguments[2]);
  var skin_name	   = escape(arguments[3]); // was bu in old IUR
  var next_url     = escape(arguments[4]);
  var site_code    = escape(arguments[5]); // was country in old IUR
  var member_name  = escape(arguments[6]);
  var password     = escape(arguments[7]);
  var ageBand      = escape(arguments[8]);
  var fullScreen   = escape(arguments[9]);
  
  if (arguments[10] instanceof Array) {
    answers = arguments[10]
  } else if (arguments.length > 10 && arguments[10] != null) {
    answers = new Array(arguments.length - 10);
    // Loop through and retrieve the answers - arguments 10 to n.  (Will retrieve all answers regardless of how many).
    for (var i = 10; i < arguments.length; i++) {
      answers[i - 10] = escape(arguments[i]);
    }
  }
  
  _launchIURInternal(
			/* target_url       */  reg_url, 
			/* task             */  task, 
			/* promotion_id     */  promotion_id, 
			/* skin_name        */  skin_name, 
			/* next_url         */  next_url, 
			/* site_code        */  site_code, 
			/* member_name      */  member_name, 
			/* password         */  password,
			/* ageBand          */  ageBand,
			/* fullScreen       */  fullScreen,
			/* swid	            */  "",			
			/* answers          */  answers);

}

//compared to launchIUR this method has an additional SWID parameter 
function launchIUR_v2() {
	var answers = null;

	  // Retrieve arguments 0 - 10
	  var reg_url      = arguments[0];
	  var task         = arguments[1];
	  var promotion_id = escape(arguments[2]);
	  var skin_name	   = escape(arguments[3]); // was bu in old IUR
	  var next_url     = escape(arguments[4]);
	  var site_code    = escape(arguments[5]); // was country in old IUR
	  var member_name  = escape(arguments[6]);
	  var password     = escape(arguments[7]);
	  var ageBand      = escape(arguments[8]);
	  var fullScreen   = escape(arguments[9]);
	  var swid		   = escape(arguments[10]);
	  
	  if (arguments[11] instanceof Array) {
	    answers = arguments[11]
	  } else if(arguments.length > 11) {
	    answers = new Array(arguments.length - 11);
	    // Loop through and retrieve the answers - arguments 11 to n.  (Will retrieve all answers regardless of how many).
	    for (var i = 11; i < arguments.length; i++) {
	      answers[i - 11] = escape(arguments[i]);
	    }
	  }
	  
	  _launchIURInternal(
				/* target_url       */  reg_url, 
				/* task             */  task, 
				/* promotion_id     */  promotion_id, 
				/* skin_name        */  skin_name, 
				/* next_url         */  next_url, 
				/* site_code        */  site_code, 
				/* member_name      */  member_name, 
				/* password         */  password,
				/* ageBand          */  ageBand,
				/* fullScreen       */  fullScreen,
				/* swid	            */  swid,			
				/* answers          */  answers);	
}

//This method is for internal use only, it can change any time 
//without notice so never call it directly
function _launchIURInternal() {

  // Declare variables
  htmlString		= "";
  var width			= 460;
  var height		= 420;
  var scrollbars	= 0;
  var resizable		= 0;
  var features		= "";

  // Retrieve arguments 0 - 10
  var reg_url      = arguments[0];
  var task         = arguments[1];
  var promotion_id = escape(arguments[2]);
  var skin_name	   = escape(arguments[3]); // was bu in old IUR
  var next_url     = escape(arguments[4]);
  var site_code    = escape(arguments[5]); // was country in old IUR
  var member_name  = escape(arguments[6]);
  var password     = escape(arguments[7]);
  var ageBand      = escape(arguments[8]);
  var layout       = escape(arguments[9]); //this parameter used to be called fullScreen
  var swid		   = escape(arguments[10]);
  
  var fullScreen;
  //true is for backward compatibility when the layout parameter was called fullScreen 
  if (layout == 'true' || layout == 'fullScreen') {
	  fullScreen = 'true';
  }
  
  //alert("swid as argument[11]: " + swid);

  var lochref = location.href; 
  if ((lochref.substr(7,3) == "dev" || lochref.substr(7,7) == "staging") && (reg_url.substr(7,4) != "test") && (reg_url.substr(7,7) != "staging")) {
      // we should handle both http and https 
      if(reg_url.indexOf("//") != -1) {
 	  	protocol = reg_url.substr(0, reg_url.indexOf("//") + 2);
 	  }
 	  target = reg_url.substr(reg_url.indexOf("//") + 2);
      reg_url = protocol + "test" + target; 
  }
  
  //alert(reg_url);

  var answers;
  
  if (arguments[11] instanceof Array) {
    answers = arguments[11]
  } else if (arguments[11] == null) {
    answers = new Array()	  
  } else if(arguments.length > 11) {
    answers = new Array(arguments.length - 11);
    // Loop through and retrieve the answers - arguments 11 to n.  (Will retrieve all answers regardless of how many).
    for (var i = 11; i < arguments.length; i++) {
      answers[i - 11] = escape(arguments[i]);
    }
  }


/* Debugging parameters - disabled. 

	var msg = "Arguments:\n";
	for (var i = 0; i < 15; i++) {
		msg = msg + "\n   -  arguments["+i+"]: " + escape(arguments[i]);
	}

	alert(msg);
*/

  if (task!='homepage') {
  	features = "width=460,height=420,scrollbars=0,resizable=0,menubar=0,toolbar=0,location=0,directories=0,status=0,copyhistory=0";
  }

  //conversion from old IUR tasks
  if (task == 'sc_modify') {
    task = 'modify'
  }
  if (task == 'guardian_modify') {
    task = 'modify'
  }

  target_url = "";
  // TB
//  target_url += reg_url + "/regservice?task=" + task;
//alert('task = [' + task + ']');
  if (task=='homepage') {  
    target_url += reg_url + "/homepage.htm?task=" + task;
  } else if (task=='invite') {
    target_url += reg_url + "/invite.htm?task=" + task;
  } else if (task=='password_forgot'
           ||task=='hint') {
    target_url += reg_url + "/hint.htm?task=" + task;  
  } else if (task=='submit_promotion'
          || task=='member_login'
          || task=='login'
          || task=='modify'
          || task=='modify_details_only') {
    target_url += reg_url + "/login.htm?task=" + task;
  } else if (task=='member_logout' || task=='logout'){
    target_url += reg_url + "/logout.htm?task=" + task;
  } else if (task=='register_full_screen'){
    target_url += reg_url + "/fullscreeninfo.htm?task=" + task;
  } else {
    target_url += reg_url + "/RegService?task=" + task;
  }
  //alert('target_url = [' + target_url + ']');


  //-------------------------------------------------------------------------------------------------------------------------------
  // Add paramater pairs if defined and non-zero length
  function AddParam (name, value) {
    if (value != null && value.toLowerCase()!= 'null' && value.length != 0 && value != 'undefined') {
      target_url += "&" + name + "=" + value;
		}
  }
  //-------------------------------------------------------------------------------------------------------------------------------

  // TB
//  AddParam("promotion_id",	promotion_id);         // See above for AddParam() function.
  AddParam("p",	promotion_id);         // See above for AddParam() function.
  AddParam("skin_name",		skin_name);
  AddParam("next_url",		next_url);
  AddParam("site_code",		site_code);
  AddParam("member_name",	member_name);
  AddParam("password",		password);
  AddParam("ageBand",	    ageBand);
  AddParam("fullScreen",  fullScreen); 
  AddParam("swid",	swid);
  
  //alert("swid after addparam: " + swid);
  
  // Build up HTML string to write to the new window
  htmlString += "<html><head></head><body>";
  htmlString += "<form name='form0' method='post' action='" + target_url + "'>\n";
  
  //alert(target_url);  

  //-------------------------------------------------------------------------------------------------------------------------------
  // Add paramater pairs if defined and non-zero length
  function AddFormParam (name, value) {  
    if (value != null && value.toLowerCase() != 'null' && value.length != 0) {
      htmlString += "<input type='hidden' name='" + name + "' value='" + value + "'>\n";
    } else {
      htmlString += "<input type='hidden' name='" + name + "' value=''>\n";	
    }
  }
  //-------------------------------------------------------------------------------------------------------------------------------

  // Add the rest of the answer params
  for (var j = 0; j < answers.length; j++) {
    AddFormParam("q" + (j + 1), answers[j]);
  }
  //for debugging htmlString += "<input type=submit value='X' ></input></form></body></html>\n";
  //note that the hidden input must be present for netscape to work correctly
  htmlString += "<input type=hidden> </input></form></body></html>\n";
  
  
  var newdocument;
  if(fullScreen == 'true' || layout == 'iframe'){ 
    newdocument = this.document;
  }else{                                // popup handling
    // if openwin and openwin.open exist, and openwin.closed isn't true, then close it
    if (openwin && openwin.open && !openwin.closed) openwin.close();
  
    // Open a temporary window on the target URL, create the form and immediately submit it to the new target URL.
    if (features=="")
    { //this stops netscape from opening the homepage in a featureless window (no nav bar no menus etc)
      openwin = this.open("", task);
    } else {
	  openwin = this.open("", task, features);
    }
    newdocument = openwin.document;
  }
  newdocument.write(htmlString);

  
  // the following can not be:  newdocument.form0.submit();  because IE 4.5 on Mac does not like it
  newdocument.forms[0].submit();  
}