/* ********************************* */
/* FUNCTIONS TO CREATE A SILO DIALOG */
/* ********************************* */
/*
* - check if we have the request param "request_type" that indicates a form call
* - check if the "request_type" param is valid
* - get all the additional params and decide which to transfer to the form
* - get the related jsp and add the params
* - check if a silo dialog exists already, if not create one
* - assign jsp as src of the iframe
* - show the dialog 
*/

function getWcmsPrefix(){
  var prefix = "";
  if (self.location.href.indexOf('/bmw_edit/') != -1) {
    prefix = "/bmw_edit";
  } else if (self.location.href.indexOf('/bmw_qa/') != -1) {
    prefix = "/bmw_qa";
  } else if (self.location.href.indexOf('/bmw_prod/') != -1) {
    prefix = "/bmw_prod";
  }
  return prefix;
}

var requestTypes = new Array();

requestTypes["contact"] = new Object();
requestTypes["contact"].jsp = getWcmsPrefix() + "/de/de/_common/silo/contact/contact.jsp";
requestTypes["contact"].width = 700;
requestTypes["contact"].height = 900;
requestTypes["contact"].top = 140;
requestTypes["contact"].left = 170;
requestTypes["contact"].allowedParams = new Array("transfer", "lead_context", "subscription_lead_context", "pg_title", "pg_path", "req_comment");
requestTypes.push(requestTypes['contact']);

requestTypes["rfi"] = new Object();
requestTypes["rfi"].jsp = getWcmsPrefix() + "/de/de/_common/silo/rfi/rfi.jsp";
requestTypes["rfi"].width = 700;
requestTypes["rfi"].height = 900;
requestTypes["rfi"].top = 140;
requestTypes["rfi"].left = 170;
requestTypes["rfi"].allowedParams = new Array("transfer", "lead_context", "subscription_lead_context", "pg_title", "pg_path");
requestTypes.push(requestTypes['rfi']);

requestTypes["tda"] = new Object();
requestTypes["tda"].jsp = getWcmsPrefix() + "/de/de/_common/silo/tda/tda.jsp";
requestTypes["tda"].width = 700;
requestTypes["tda"].height = 1050;
requestTypes["tda"].top = 140;
requestTypes["tda"].left = 170;
requestTypes["tda"].allowedParams = new Array("transfer", "lead_context", "subscription_lead_context", "pg_title", "pg_path", "series", "bodytype");
requestTypes.push(requestTypes['tda']);

requestTypes["tda_preselected"] = new Object();
requestTypes["tda_preselected"].jsp = getWcmsPrefix() + "/de/de/_common/silo/tda/tda_preselected.jsp";
requestTypes["tda_preselected"].width = 700;
requestTypes["tda_preselected"].height = 900;
requestTypes["tda_preselected"].top = 140;
requestTypes["tda_preselected"].left = 170;
requestTypes["tda_preselected"].allowedParams = new Array("transfer", "lead_context", "subscription_lead_context", "pg_title", "pg_path", "series", "bodytype");
requestTypes.push(requestTypes['tda_preselected']);

requestTypes["reminder"] = new Object();
requestTypes["reminder"].jsp = getWcmsPrefix() + "/de/de/_common/silo/rfi/rfi.jsp";
requestTypes["reminder"].width = 700;
requestTypes["reminder"].height = 900;
requestTypes["reminder"].top = 140;
requestTypes["reminder"].left = 170;
requestTypes["reminder"].allowedParams = new Array("transfer", "lead_context", "subscription_lead_context", "pg_title", "pg_path");
requestTypes.push(requestTypes['reminder']);

requestTypes["sweepstake"] = new Object();
requestTypes["sweepstake"].jsp = getWcmsPrefix() + "/de/de/_common/silo/sweepstake/sweepstake.jsp";
requestTypes["sweepstake"].width = 700;
requestTypes["sweepstake"].height = 800;
requestTypes["sweepstake"].top = 140;
requestTypes["sweepstake"].left = 170;
requestTypes["sweepstake"].allowedParams = new Array("transfer", "lead_context", "subscription_lead_context", "pg_title", "pg_path");
requestTypes.push(requestTypes['sweepstake']);

requestTypes["login"] = new Object();
requestTypes["login"].jsp = getWcmsPrefix() + "/de/de/_common/silo/login/login.jsp";
requestTypes["login"].width = 700;
requestTypes["login"].height = 950;
requestTypes["login"].top = 140;
requestTypes["login"].left = 170;
requestTypes["login"].allowedParams = new Array("transfer", "lead_context", "subscription_lead_context", "pg_title", "pg_path");
requestTypes.push(requestTypes['login']);

requestTypes["profile_create"] = new Object();
requestTypes["profile_create"].jsp = getWcmsPrefix() + "/de/de/_common/silo/profile/profile_create.jsp";
requestTypes["profile_create"].width = 700;
requestTypes["profile_create"].height = 950;
requestTypes["profile_create"].top = 140;
requestTypes["profile_create"].left = 170;
requestTypes["profile_create"].allowedParams = new Array("transfer", "lead_context", "subscription_lead_context", "pg_title", "pg_path");
requestTypes.push(requestTypes['profile_create']);


// onload check if we have an external request to a form
$(document).ready(function(){
	checkExternalRequest();
});


function checkExternalRequest() {
	splitSearchString();
	var requestType = query.request_type;	// request types are e.g. rfi, tda, reminder...
	
	// check if request type param ist given and allowed
	if(typeof requestType != "undefined" && requestType != null && requestType != "" && isValidRequestType(requestType)) {
		// get all the allowed params from request
		var allowedRequestParams = getAllowedRequestParams(requestType);
		
		createSiloDialog(requestType, allowedRequestParams);
	}
}

/* Gets the related jsp, the params and creates an iframe. */
/* This function can be used for internal form requests. */
function createSiloDialog(requestType, requestParams) {
	var siloLayerId = "siloDialog";
	var siloIframeId = "siloDialogIFrame";
	
	// get the jsp of the related request type
	var siloJsp = requestTypes[requestType].jsp;
	
	// add request params to silo jsp
	siloJsp += "?border_status=true&" + requestParams;
	
	// check if a silo dialog (existent of layer and iframe) already exists
	if(!checkSiloLayer(siloLayerId, siloIframeId)) {
		// create a new silo layer + iframe
		createSiloLayerAndIframe(requestType);
	}
	
	// resize layer
	setSiloWidthHeight(siloLayerId, requestType);
	setSiloTopLeft(siloLayerId, requestType);
	setSiloWidthHeight(siloIframeId, requestType);
	
	// add silo jsp and params to iframe
	document.getElementById(siloIframeId).src = siloJsp;
	setVisibility(siloLayerId, 1, "block");
}


/* Returns if the external request is ok or not.
   An external request is ok, if the request type exists
	 and we have a jsp to display the form
	 and we have a transfer parameter in request */
function isValidRequestType(requestType) {
	if(typeof requestTypes[requestType] != "undefined" && requestTypes[requestType].jsp != null &&
		requestTypes[requestType].jsp != "" && typeof query.transfer != "undefined" && query.transfer != "")
		return true;
	else
		return false;
}


// check if silo layer and iframe already exists
function checkSiloLayer(siloLayerId, siloIframeId) {
	if(document.getElementById(siloLayerId) && document.getElementById(siloIframeId))
		return true;
	else
		return false
}


/* adds a layer and an iframe to body element */
function createSiloLayerAndIframe(requestType) {
	var layerWidth = requestTypes[requestType].width;
	var layerHeight = requestTypes[requestType].height;
	var topPos = requestTypes[requestType].top;
	var leftPos = requestTypes[requestType].left;
  var siloCode =  "";
	
	siloCode += "<div id=\"siloDialog\" style=\"position:absolute; top:" + topPos + "px; left:" + leftPos + "px; width:" + layerWidth + "px; height:" + layerHeight + "px; z-index:850; overflow:hidden;\">";
  siloCode += "<iframe id=\"siloDialogIFrame\" name=\"siloDialogIFrame\"  style=\"background:transparent; width:" + layerWidth + "px; height:" + layerHeight + "px; border: none;\" allowtransparency=\"true\" src=\"";
  siloCode += getWcmsPrefix() + "/_common/shared/transparent_frame.html";
  siloCode += "\" frameborder=\"0\" scrolling=\"no\"></iframe></div>";
  //$("#siloDialog").remove();
  $("body").append(siloCode);
}


/* returns an String with all the allowed params for silo form */
function getAllowedRequestParams(requestType) {
	var allowedRequestParams = "";
	
	for(i = 0; i < requestTypes[requestType].allowedParams.length; i++) {
		var tmpParam = requestTypes[requestType].allowedParams[i];
		
		if(typeof query[tmpParam] != "undefined" && query[tmpParam] != "") {
			allowedRequestParams += ("&" + tmpParam + "=" + query[tmpParam]);
		}
	}
	
	return allowedRequestParams;
}


function setSiloTopLeft(elementId, requestType){
  $("#" + elementId).css("top", requestTypes[requestType].top);
  $("#" + elementId).css("left", requestTypes[requestType].left);
}


function setSiloWidthHeight(elementId, requestType){
  $("#" + elementId).css("width", requestTypes[requestType].width);
  $("#" + elementId).css("height", requestTypes[requestType].height);
}
