function getObject(x) {
	if (typeof x == 'string') {
		el = document.getElementById(x);
	}
	return el;
}
function addClass(x, classStr) {
	el = getObject(x);

	if (!el.className) {
		el.className = classStr;
	}
	else if (!containsClass(el, classStr)){
		el.className += (" " + classStr);
	}
	return el;
}

function removeClass(x, classStr) {
	el = getObject(x);

	if (!!el.className) {
		var re = new RegExp(classStr);
		el.className = el.className.replace(re, "");
	}
	return el;
}

function containsClass(x, classStr) {
	el = getObject(x);

	var regex = new RegExp(classStr);
	if (!!el && !!el.className && el.className.match(regex)) {
		return true;
	}
	return false;
}

function toggleClass(el, classStr) {
	if (containsClass(el, classStr)) {
		removeClass(el, classStr);
	}
	else {
		addClass(el, classStr);
	}
	return el;
}

function emailPage() {
  var url = "/emailapp/SendToFriend?url=";
  var title = replaceSpace(document.title);
  url += window.location.href;
  url += "&title=";
  url += title;
  var features = "toolbar=no,status=no,scrollbars=no,resizable=no,width=425,height=480";
  return window.open(url,'emailPop',features);
}


function replaceSpace(str) {
  var buffer = str;
  var space = " ";
  var token = "%20";
  while (buffer.indexOf(space) != -1) {
    buffer = buffer.replace(space, token);
  }
  return buffer;
}


function printPage() {
    window.print();
}

function getElementStyle(elemID, IEStyleProp, CSSStyleProp) {
    var elem = document.getElementById(elemID);
    if (elem.currentStyle) {
        return elem.currentStyle[IEStyleProp];
    }
    else if (window.getComputedStyle) {
        var compStyle = window.getComputedStyle(elem, "");
        return compStyle.getPropertyValue(CSSStyleProp);
    }
    return "";
}

function addOnload(fn) { //appends javascript code contained in string fn to the onload handler of the body tag
    if (window.onload) {
        var oldOnload = window.onload;
        window.onload = function() {
            oldOnload();
            eval(fn);
        }
    }
    else {
        window.onload = function()
        {
            eval(fn);
        }
    }
}

addOnload("hideZoom()");

function hideZoom() {                                           //hide zoom if unsupported
    if (!document.body.currentStyle && !window.getComputedStyle && document.getElementById) {
        document.getElementById("text_zoom").style.display = "none";
    }
}

function getZoom() {                                            //get zoom value from cookie
    if (getCookie("zoom")) {
        document.body.style.fontSize = getCookie("zoom");
    }
}


function zoom(inout) {
    var fontSize;
    var now;
    var weekFromNow;

    if (document.body.currentStyle) {
        fontSize = document.body.currentStyle["fontSize"];
    }
    else if (window.getComputedStyle) {
        var compStyle = window.getComputedStyle(document.body, "");
        fontSize = compStyle.getPropertyValue("font-size");
    }


    var num = fontSize.match(/[\d\.]+/);
    var type = fontSize.match(/[^\d\.]+/);
    num = num * ((inout == "in") ? 1.1 : .909);
    var newSize = num + type;

    document.body.style.fontSize = newSize;

    now = new Date();                                           //set cookie, expires in 1 week
    weekFromNow = new Date(now.getTime() + 86400000);
    setCookie("zoom", newSize, weekFromNow, "/");
}






//external link functions

function MM_openCorporateWindow(theURL,winName,features) { //v2.0
    alert('By clicking on the link for our press releases you will be leaving the MiniMed.com website and going to our corporate site Medtronic.com.  The Terms of Use and Privacy Statement that are posted on MiniMed.com may not apply to when visiting Medtronic.com.');
    window.open(theURL,winName,features);
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
    alert('You have just clicked on a link.  If you continue, you will leave the Medtronic Diabetes Site and look at a web site run by someone else.  We do not review or monitor the content on the linked web site.  Medtronic Diabetes is not responsible for any business dealings or transactions you may have with them, such as buying a product.  Your use of the linked web site is subject to the terms and conditions of that site. ');
    window.open(theURL,winName,features);
}

function MM_openNonUS(theURL,winName,features) { //v2.0
    alert('You have just clicked on a link.  If you continue, you will leave the MiniMed.com web site and look at web site run by a Medtronic Diabetes affiliate in a '
            +'different country.  The Terms of Use and Privacy Statement that are posted on www.minimed.com do not apply when you visit the Medtronic affiliate '
            +'web site.  Your use of the Medtronic affiliate web site is subject to any terms of use or privacy statements displayed on the affiliate web site.'
            +'\n\n'
            +'In addition, the information that may appear on this affiliate site is intended for use by customers, patients and health care professionals outside '
            +'the U.S.  The U.S. may have regulatory, legal requirements or medical practices which are different than those in the foreign country and may require '
            +'different or additional information.  Therefore, this information may not be appropriate for use within the U.S.');
    window.open(theURL,winName,features);
}



//popup image script

    //really not important (the first two should be small for Opera's sake)
PositionX = 10;
PositionY = 10;
defaultWidth  = 600;
defaultHeight = 400;

    //kinda important
var AutoClose = true;

    //don't touch
function popImage(imageURL,imageTitle){
    var imgWin = window.open('','_blank','scrollbars=no,resizable=1,width='+defaultWidth+',height='+defaultHeight+',left='+PositionX+',top='+PositionY);
    if( !imgWin ) { return true; } //popup blockers should not cause errors
    imgWin.document.write('<html><head><title>'+imageTitle+'<\/title><script type="text\/javascript">\n'+
        'function resizeWinTo() {\n'+
        'if( !document.images.length ) { document.images[0] = document.layers[0].images[0]; }'+
        'var oH = document.images[0].height, oW = document.images[0].width;\n'+
        'if( !oH || window.doneAlready ) { return; }\n'+ //in case images are disabled
        'window.doneAlready = true;\n'+ //for Safari and Opera
        'var x = window; x.resizeTo( oW + 200, oH + 200 );\n'+
        'var myW = 0, myH = 0, d = x.document.documentElement, b = x.document.body;\n'+
        'if( x.innerWidth ) { myW = x.innerWidth; myH = x.innerHeight; }\n'+
        'else if( d && d.clientWidth ) { myW = d.clientWidth; myH = d.clientHeight; }\n'+
        'else if( b && b.clientWidth ) { myW = b.clientWidth; myH = b.clientHeight; }\n'+
        'if( window.opera && !document.childNodes ) { myW += 16; }\n'+
        'x.resizeTo( oW = oW + ( ( oW + 200 ) - myW ), oH = oH + ( (oH + 200 ) - myH ) );\n'+
        'var scW = screen.availWidth ? screen.availWidth : screen.width;\n'+
        'var scH = screen.availHeight ? screen.availHeight : screen.height;\n'+
        'if( !window.opera ) { x.moveTo(Math.round((scW-oW)/2),Math.round((scH-oH)/2)); }\n'+
        '}\n'+
        '<\/script>'+
        '<\/head><body onload="resizeWinTo();"'+(AutoClose?' onblur="self.close();"':'')+'>'+
        (document.layers?('<layer left="0" top="0">'):('<div style="position:absolute;left:0px;top:0px;display:table;">'))+
        '<img src='+imageURL+' alt="Loading image ..." title="" onload="resizeWinTo();">'+
        (document.layers?'<\/layer>':'<\/div>')+'<\/body><\/html>');
    imgWin.document.close();
    if( imgWin.focus ) { imgWin.focus(); }
    return false;
}

var minimed = {
    /* SITEWIDE JAVASCRIPT CONTENT */
    content : {
        warnings : {
            nonUsAffiliate : "You have just clicked on a link. If you continue, " +
                "you will leave the MiniMed.com web site and look at web site " +
                "run by a Medtronic Diabetes affiliate in a different country. " +
                "The Terms of Use and Privacy Statement that are posted on " +
                "www.minimed.com do not apply when you visit the Medtronic " +
                "affiliate web site. Your use of the Medtronic affiliate web " +
                "site is subject to any terms of use or privacy statements " +
                "displayed on the affiliate web site. \n\n " +
                "In addition, the information that may appear on this affiliate " +
                "site is intended for use by customers, patients and health care " +
                "professionals outside the U.S. The U.S. may have regulatory, " +
                "legal requirements or medical practices which are different " +
                "than those in the foreign country and may require different or " +
                "additional information. Therefore, this information may not be " +
                "appropriate for use within the U.S.",
            offsite : "You have just clicked on a link. If you continue, you " +
                "will leave the Medtronic Diabetes Site and look at a web site " +
                "run by someone else.  We do not review or monitor the content " +
                "on the linked web site. Medtronic Diabetes is not responsible " +
                "for any business dealings or transactions you may have with " +
                "them, such as buying a product. Your use of the linked web " +
                "site is subject to the terms and conditions of that site."
        }
    },
    /* SITEWIDE JAVASCRIPT PARAMETERS */
    parameters : {
        popup : "toolbar=yes,location=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes",
		noToolPopup : "toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,resizable=yes,width=750,height=550",
        emailPagePopup : "toolbar=no,status=no,scrollbars=no,resizable=no,width=425,height=480",
        emailPageUrlStub : "/emailapp/SendToFriend?url=",
        imagePopup : {
            positionX : 10,
            positionY : 10,
            defaultWidth : 400,
            defaultHeight : 400,
            autoClose : true
        }
    }
};
function otherPopup(href) {
	var openOtherParams = "toolbar=yes,location=yes,status=yes,menubar=yes,scrollbars=yes,resizable=yes";


    MM_openBrWindow(href,'',openOtherParams);
    return false;
}
function offsitePopup (anchor) { //optional 2nd argument, if TRUE, indicates non-US affiliate link
    if (typeof anchor == "object" && !!anchor.href) {
        href = anchor.href;
    }
    else if (typeof anchor == "string") {
        href = anchor;
    }
    else {
        return true; //allow the ling to behave normally
    }
    var warningStr = (!!arguments[1]) ? minimed.content.warnings.nonUsAffiliate : minimed.content.warnings.offsite;
    alert(warningStr);
    window.open(href, "", minimed.parameters.popup);
    return false;
}
function legalCheck(formObj) {
    if (!formObj.over13.checked) {
        alert("For privacy purposes, you must verify that you are 13 years or older and understand our legal terms. Please click the age verfication checkbox.");
        return false;
    }
    return true;
}

//new jQuery external link functionality
$(function() {
	$('a[rel]').click(function() {
		var link = $(this).attr('href')
		var rel = $(this).attr('rel')
		$('<div id="' + rel + '" />').load('/includes/dialog/' + rel + '.html').dialog({
			modal:true,
			title:'WARNING: External Link',
			buttons: {
				'Continue': function() {
					$('#' + rel + '').dialog('close');
					$('#' + rel + '').remove();
					location.replace(link);
				},
				'Cancel': function() {
					$('#' + rel + '').dialog('close');
					$('#' + rel + '').remove();
					return false;
				}
			}
		});
		$('.ui-dialog-buttonpane button:first').addClass('expected');
		return false;
	});
})