//var winReference = null;
var intPopDetectorCount = 0;

// utility function to retrieve an expiration date in proper
// format; pass three integer parameters for the number of days, hours,
// and minutes from now you want the cookie to expire (or negative
// values for a past date); all three parameters are required,
// so use zeros where appropriate
function getExpDate(days, hours, minutes) {
    var expDate = new Date( );
    if (typeof days == "number" && typeof hours == "number" && 
        typeof hours == "number") {
        expDate.setDate(expDate.getDate( ) + parseInt(days));
        expDate.setHours(expDate.getHours( ) + parseInt(hours));
        expDate.setMinutes(expDate.getMinutes( ) + parseInt(minutes));
        return expDate.toGMTString( );
    }
}
   
// utility function called by getCookie( )
function getCookieVal(offset) {
    var endstr = document.cookie.indexOf (";", offset);
    if (endstr == -1) {
        endstr = document.cookie.length;
    }
    return unescape(document.cookie.substring(offset, endstr));
}
   
// primary function to retrieve cookie by name
function getCookie(name) {
    var arg = name + "=";
    var alen = arg.length;
    var clen = document.cookie.length;
    var i = 0;
    while (i < clen) {
        var j = i + alen;
        if (document.cookie.substring(i, j) == arg) {
            return getCookieVal(j);
        }
        i = document.cookie.indexOf(" ", i) + 1;
        if (i == 0) break; 
    }
    return "";
}
   
// store cookie value with optional details as needed
function setCookie(name, value, expires, path, domain, secure) {
    document.cookie = name + "=" + escape (value) +
        ((expires) ? "; expires=" + expires : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}
   
// remove the cookie by setting ancient expiration date
function deleteCookie(name,path,domain) {
    if (getCookie(name)) {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}



function popmain(url){
		if(opener == undefined){
			mainWin = window.open(url,"","");
		}else if(opener.closed){
			mainWin = window.open(url,"","");			
		}else{
			/*
			opener.location = url;
			opener.window.focus();
			*/
			mainWin = window.open(url,"","");	
		}
		//window.close();
}

function refreshPage(){
	top.window.location = top.window.location;
}

function openPositionedWindow(url, name, width, height, x, y, status, scrollbars, moreProperties, openerName) {
	var agent = navigator.userAgent.toLowerCase();
	if (agent.indexOf("mac")!=-1 && agent.indexOf("msie") != -1 && agent.indexOf("msie 5.0")==-1) {
		height += 2;
		if (status) height += 15;
	}

	width += (scrollbars != '' && scrollbars != null && agent.indexOf("mac") == -1) ? 16 : 0;        
        if (!moreProperties || moreProperties == ''){ moreProperties = 'toolbar=1, status=1, resizable=1'; }
	var properties = 'width=' + width + ',height=' + height +  ((status) ? ',status' : '') + ',scrollbars' + ((scrollbars) ? '' : '=no') + ((moreProperties) ? ',' + moreProperties : '');
	var reference = openWindow(url, name, properties, openerName);
	return reference;
}

function popout(url, width, height, name, scrollbars, status, moreProperties, openerName) {
	var x = 0;
	var y = 0;
	if (screen) x = (screen.availWidth - width) / 2;
	if (screen) y = (screen.availHeight - height) / 2;
	if (!status) status == '';
	if (!openerName) openerName == '';	
	var reference = openPositionedWindow(url, name, width, height, x, y, status, scrollbars, moreProperties, openerName);
	chkPopUpBlocker(reference,url);
}	

function openMaxedWindow(url, name, scrollbars, openerName) {
	var width  = 600;
	var height = 800;
	if (screen) width  = screen.width - 10;
	if (screen) height = screen.height - 30;
	var reference = openPositionedWindow(url, name, width, height, 0, 0, false, scrollbars, openerName, moreProperties);
	chkPopUpBlocker(reference,url);
	return reference;
}

function openFullChromeWindow(url, name, openerName) {
	return openWindow(url, name, 'directories,location,menubar,resizable,scrollbars,status,toolbar');
}

function openWindow(url, name, properties, openerName) {

	var agent = navigator.userAgent.toLowerCase();
	if (agent.indexOf("msie") != -1 && parseInt(navigator.appVersion) == 4 && agent.indexOf("msie 5") == -1 && agent.indexOf("msie5") == -1 && agent.indexOf("win") != -1 && url.indexOf('http://') == 0) {
		winReference = window.open('about:blank', name, properties);
		
		setTimeout('if (winReference && !winReference.closed) winReference.location.replace("' + url + '")', 300);
	}
	else {
		winReference = window.open(url, name, properties);
	}
		chkPopUpBlocker(winReference,url);

		setTimeout('if (winReference && !winReference.closed) winReference.focus()', 200);
	
		if (openerName) self.name = openerName;
		return winReference;
	
}

function chkPopUpBlocker(winref,url){
	if(intPopDetectorCount == 0){
		if(winref == null && getCookie("IGNORE_BLOCKER")!='true'){
			var urlReferer = location.href;
			intPopDetectorCount = intPopDetectorCount + 1; 

			alert('A pop-up window was prevented from appearing on your screen.\nTo find out what you can do about it, click the button below.');
			location.href = "/general/pop_blocker/index.cfm?http_referer="+ escape(urlReferer);
		}
	}
}

function closeDialog(dialog) {
	if (dialog && dialog.closed != true) dialog.close();
}

function closeParentDialog() {
	if (top.opener && isWindowPopup(top.opener)) {
		root = top.opener.top.opener;
		top.opener.close();
		top.opener = root;
	}
}

function isWindowPopup(win) {
	return ((win.opener) ? true : false);
}

function openerRedirect(url,openerWinName,properties,closeself){
	if(opener == null || opener.closed){
		opener=window.open(url,openerWinName,properties);
	}else{
		opener.location.href=url;
	}
	opener.focus();
	if(closeself){
		window.close();
	}

}

// JavaScript Document
function launchCenter(url, name, height, width,moreProperties) {
	var str = "height=" + height + ",innerHeight=" + height;
	str += ",width=" + width + ",innerWidth=" + width;

	if (window.screen) {
		var ah = screen.availHeight - 30;
		var aw = screen.availWidth - 10;
		var xc = (aw - width) / 2;
		var yc = (ah - height) / 2;
		str += ",left=" + xc + ",screenX=" + xc;
		str += ",top=" + yc + ",screenY=" + yc;
	}

	var winReference = window.open(url, name, moreProperties+str);
	chkPopUpBlocker(winReference,url);
	//setTimeout('if (winReference && !winReference.closed) winReference.focus()', 200);
	return winReference

} 


function confirmLink(link){
  if( confirm( "This link will take you out of meadjohnsonasia.com website and to the website of an entity that is not related to Mead Johnson Nutrition and/or its affiliates. Mead Johnson Nutrition and/or its affiliates does not have any control over nor responsibility for such other entity or the content of the website. Providing a link to the other site does not necessarily imply endorsement of some content on the site." ) )
  {
      window.open( link, "_blank", "menubar=1,status=1,location=1,scrollbars=1,resizable=1" );
  }
}


