function PopupController(url, loginOverlay, ContinueOverlay, UserStatus, Width, Height, NoHeightResize){

    if (NoHeightResize == undefined) {

	var sc = loginOverlay+ContinueOverlay+UserStatus;
	//check if there is query string
	var index = url.indexOf('?');
	var w = "";
	var h = "";
	if (index != -1) {
		//remove the ? 
		var query = url.substring(index + 1 );
		var vars = query.split("&");
		for (var i = 0; i < vars.length; i++) {
			var pair = vars[i].split("=");
			// Extract the value for the pairs --> pairs (key=value)
			if (pair[0] =='width') {
				w = pair[1];
			}
			if (pair[0] =='height') {
				h = pair[1];
			}
			
		}

		w = parseInt(w);
		h = parseInt(h);
    }
  
	switch (sc) {
	case "010" ://c
		
			if (w > 0 && h > 0) {
			showIFramePanel(url, w, h);
		}
			else if (Width > 0 && Height > 0) {
			showIFramePanel(url, Width, Height);
		}
		else 
			showIFramePanel(url);
		
		break;
	
	case "011" ://c
	
			if (w > 0 && h > 0) {
			showIFramePanel(url, w, h);
		}
			else if (Width > 0 && Height > 0) {
			showIFramePanel(url, Width, Height);
		}
		else 
			showIFramePanel(url);
		break;
		
	case "001" ://c
		window.top.location.href=url;
		break;
		
	case "110" ://t
	    
	    url = appendContinue(url, ContinueOverlay);
		
			if (w > 0 && h > 0) {
			showIFramePanel(url, w, h);
		}
			else if (Width > 0 && Height > 0) {
			showIFramePanel(url, Width, Height);
		}
		else 
			showIFramePanel(url);
			
		break;
		
	case "100" ://t
	    url = appendContinue(url, ContinueOverlay);
		
			if (w > 0 && h > 0) {
			showIFramePanel(url,w, h);
		}
			else if (Width > 0 && Height > 0) {
			showIFramePanel(url, Width, Height);
		}
		else 
			showIFramePanel(url);
		break;
		
	case "111" ://c
			if (w > 0 && h > 0) {
			showIFramePanel(url, w, h);
		}
			else if (Width > 0 && Height > 0) {
			showIFramePanel(url, Width, Height);
		}
		else 
			showIFramePanel(url);
		break;
		
	case "101" ://c
		window.top.location.href=url;
		break;
		
	default : window.location = url;  break; //case "000"
	}

}
    else {

	var sc = loginOverlay+ContinueOverlay+UserStatus;
	
	//check which browser is used
	var agt=navigator.userAgent.toLowerCase();
	
	//check if there is query string
	var index = url.indexOf('?');
	var w = "";
	var h = "";
	if (index != -1) {
		//remove the ? 
		var query = url.substring(index + 1 );
		var vars = query.split("&");
		for (var i = 0; i < vars.length; i++) {
			var pair = vars[i].split("=");
			// Extract the value for the pairs --> pairs (key=value)
			if (pair[0] =='width') {
				w = pair[1];
			}
			if (pair[0] =='height') {
				h = pair[1];
			}
			
		}

		w = parseInt(w);
		h = parseInt(h);
    }
  
	if (agt.indexOf("firefox") != -1) {
		switch (sc) {
	case "010" ://c
		
				if (w > 0 && h > 0) {
			showIFramePanelNoResize(url, w, h);
		}
				else if (Width > 0 && Height > 0) {
			showIFramePanelNoResize(url, Width, Height);
		}
		else 
			showIFramePanelNoResize(url);
		
		break;
	
	case "011" ://c
	
				if (w > 0 && h > 0) {
			showIFramePanelNoResize(url, w, h);
		}
				else if (Width > 0 && Height > 0) {
			showIFramePanelNoResize(url, Width, Height);
		}
		else 
			showIFramePanelNoResize(url);
		break;
		
	case "001" ://c
		window.top.location.href=url;
		break;
		
	case "110" ://t
	    
	    url = appendContinue(url, ContinueOverlay);
		
				if (w > 0 && h > 0) {
			showIFramePanelNoResize(url, w, h);
		}
				else if (Width > 0 && Height > 0) {
			showIFramePanelNoResize(url, Width, Height);
		}
		else 
			showIFramePanelNoResize(url);
			
		break;
		
	case "100" ://t
	    url = appendContinue(url, ContinueOverlay);
		
				if (w > 0 && h > 0) {
			showIFramePanelNoResize(url,w, h);
		}
				else if (Width > 0 && Height > 0) {
			showIFramePanelNoResize(url, Width, Height);
		}
		else 
			showIFramePanelNoResize(url);
		break;
		
	case "111" ://c
				if (w > 0 && h > 0) {
			showIFramePanelNoResize(url, w, h);
		}
				else if (Width > 0 && Height > 0) {
			showIFramePanelNoResize(url, Width, Height);
		}
		else 
			showIFramePanelNoResize(url);
		break;
		
	case "101" ://c
		window.top.location.href=url;
		break;
		
	default : window.location = url;  break; //case "000"
	}
	}
	else {
		switch (sc) {
	case "010" ://c
		
				if (w > 0 && h > 0) {
			showIFramePanel(url, w, h);
		}
				else if (Width > 0 && Height > 0) {
			showIFramePanel(url, Width, Height);
		}
		else 
			showIFramePanel(url);
		
		break;
	
	case "011" ://c
	
				if (w > 0 && h > 0) {
			showIFramePanel(url, w, h);
		}
				else if (Width > 0 && Height > 0) {
			showIFramePanel(url, Width, Height);
		}
		else 
			showIFramePanel(url);
		break;
		
	case "001" ://c
		window.top.location.href=url;
		break;
		
	case "110" ://t
	    
	    url = appendContinue(url, ContinueOverlay);
		
				if (w > 0 && h > 0) {
			showIFramePanel(url, w, h);
		}
				else if (Width > 0 && Height > 0) {
			showIFramePanel(url, Width, Height);
		}
		else 
			showIFramePanel(url);
			
		break;
		
	case "100" ://t
	    url = appendContinue(url, ContinueOverlay);
		
				if (w > 0 && h > 0) {
			showIFramePanel(url,w, h);
		}
				else if (Width > 0 && Height > 0) {
			showIFramePanel(url, Width, Height);
		}
		else 
			showIFramePanel(url);
		break;
		
	case "111" ://c
				if (w > 0 && h > 0) {
			showIFramePanel(url, w, h);
		}
				else if (Width > 0 && Height > 0) {
			showIFramePanel(url, Width, Height);
		}
		else 
			showIFramePanel(url);
		break;
		
	case "101" ://c
		window.top.location.href=url;
		break;
		
	default : window.location = url;  break; //case "000"
	}
	}
	

}


}

function appendContinue(url, ContinueOverlay){

	if (url.lastIndexOf("?") == -1) {
		urlContinue = url.concat('?continueoverlay=', ContinueOverlay);
    }
	else {
		urlContinue = url.concat('&continueoverlay=', ContinueOverlay);
    }
    return urlContinue;

}

function cleanUrl(url){
	var x = url.lastIndexOf("continueoverlay");
	if (x > -1) {
		cleanurl = url.substring(0,x-1);
	}
	else {
		cleanurl = url;
	}
    
    return cleanurl;
}

function getStatus(url){

	var x = url.lastIndexOf("continueoverlay");
	if(x>-1){
    var status = url.charAt(x+16);
	}
	else {
		status=0;
	}
    return status;
}

var panelContent;
var panelFrame;
var panelParams;
var panelFrameHeight;

function validateLogin(url){
    ContinueOverlay = getStatus(url);
    url = cleanUrl(url);

    if (ContinueOverlay == '1') {
        
	    $("#POPUP_LAYER .maLayerTop").hide();    
	    $("#POPUP_LAYER .maLayerBottom").hide();

		showIFramePanel(url);
		//refreshLoginView();
	}
	else {
	    window.top.location.href=url;
	}
	
	return false;
}

function showIFramePanel(src, fixedWidth, fixedHeight) {
	panelParams = { src: src, w: fixedWidth || 540, h: fixedHeight };
    
	$("#POPUP_LAYER .maLayerPad").empty().html($('<div class="loading"></div><iframe frameborder="0" />'));
	panelFrame = $("#POPUP_LAYER iframe");
	panelFrame.css({ visibility: "hidden", height: 380, width: panelParams.w - 12 });

	showPanel("POPUP_LAYER", 380);

	setTimeout(function () {
		panelFrame.attr("src", src).load(resizeFrame);
	}, 100);
	return false;
}

function resizeFrame(e) {
	var iframe = panelFrame.get(0);
	var docHeight = 520;

	if (panelParams.h) docHeight = panelParams.h;
	else {
		try {
			var doc = iframe.contentDocument || iframe.Document;
			docHeight = Math.max(100, Math.min(520, $(doc.body).height()));
		}
		catch (err) { }
	}

	var totalHeight = docHeight + 46;
	var pageTop = getScroll();
	var dims = getPageDimensions();
	var endY = pageTop + Math.max(10, (dims.height - totalHeight) / 2);

	panelFrame.css({ height: docHeight, visibility: "hidden" });
	panelContent
			.animate({ top: endY }, { duration: 600, complete: resizeEnd })
			.find(".maLayerPad").animate({ height: docHeight }, { duration: 600 });
}

function resizeEnd() {
	setTimeout(fadeIFrame, 50);
}

function fadeIFrame() {
	panelContent.find(".loading").remove();
	panelFrame.css({ visibility: "visible" });
}

function showPanel(idPanel, innerHeight) {
	var panel = $("#" + idPanel);
	panel.appendTo($("form")); // over page content

	panel.find(".maLayerBg").css({ opacity: 0.3, height: $("body").height() });
	panel.find(".closeLayer a").click(window.hidePanel);
	panel.show();

	panelContent = panel.find(".maLayerPanel");
	panelContent.css("width", panelParams.w);
	panelContent.find(".maLayerTop").css("width", panelParams.w - 16);
	if ($.browser.msie > 0 && parseInt($.browser.version) < 7) panelContent.addClass("ie6");
	if (innerHeight)
		panelContent.find(".maLayerPad").css({ height: innerHeight });
	autoCenter();
	$(window).scroll(autoCenter);
	setTimeout(fadePanel, 50);
}

function fadePanel() {
	if (panelContent)
		panelContent.fadeIn(200);
}

function hidePanel(e) {
	// For Coupon Tagging On Closing Overlay
	
    if (e) insertTag();

	if (e) e.preventDefault();
	if (panelContent) {
		panelContent.parents(".maLayer").find(".maLayerBg").css({ opacity: 0 });
		panelContent.fadeOut(200, removePanel);
		panelContent = null;
	}
	if (panelFrame) {
		panelFrame.unbind("load", resizeFrame);
		panelFrame.attr("src", "");
	}
	return false;
}

function removePanel() {
	var panel = $(this).parents(".maLayer");
	panel.hide().find(".maLayerPad").css({ height: 200 });
	panel.find("frame").attr("src", "").css({ height: 200, visibility: "hidden" });
}

function autoCenter() {
	if (panelContent) {
		var pageTop = getScroll();
		var dims = getPageDimensions();
		var endY = pageTop + Math.max(34, (dims.height - panelContent.find(".maLayerPad").height() - 95) / 2);
		panelContent.css({ top: endY });
	}
}

function getScroll() {
	if (document.all) return document.documentElement.scrollTop;
	else return document.body.scrollTop - 30;
}

function getPageDimensions() {
	var dimensions = { width: 0, height: 0 };
	if ($.browser.msie) {
		dimensions.width = document.documentElement.clientWidth;
		dimensions.height = document.documentElement.clientHeight;
	}
	else if (window.innerWidth && window.innerHeight) {
		dimensions.width = window.innerWidth;
		dimensions.height = window.innerHeight;
	}
	return dimensions;
}

