var DHTML = (document.getElementById || document.all || document.layers);

function sendToSite(){
	var siteURL = document.forms['sitesform'].site.value;

	if(siteURL == "" || siteURL == null){
		alert("Please select a site");
		return false;
	}
	else{
		var newWin = window.open(siteURL, "Popup");
		newWin.focus();
	}
}

function highlight(td) {
	if (DHTML)
		td.style.background = "#FFFFFF";
}

function unHighlight(td) {
	if (DHTML)
		td.style.background = "#DFE0EC";
}

function popUpHelp(URL){
	var newwin = window.open(URL, "Help", "height=530,width=668,location=no,scrollbars=yes,menubars=no,toolbar=no,resizable=yes,status=yes");
	newwin.focus();
}

/***** Jump Menu on Site Help ******/
function goToFaq() {
	var form = document.topic_form;
	url = form.topic.options[form.topic.selectedIndex].value;

	document.location.href = url;
	return true;
}
/*********** Connect With Merck button on MPS Site Help **************/
function helpConnectMerck(){
	opener.location='../../../portal/site/merckproductservices/connectwithmerck/';
	opener.focus();
	return false;
}


function popUpProduct(URL){
	var newwin = window.open(URL, "popup", "height=530,width=668,location=no,scrollbars=yes,menubars=no,toolbar=no,status=yes,resizable=yes");
	newwin.focus();
}

function popUpMediumLarge(URL) {
	var newWin = window.open(URL, "popupMedLg", "height=285,width=265,location=no,scrollbars=no,menubars=no,toolbar=no,resizable=no");
	newWin.focus();
}

function popUpCookies(URL) {
	var newWin = window.open(URL, "Cookies", "height=450,width=540,location=no,scrollbars=no,menubars=no,toolbar=no,resizable=no");
	newWin.focus();
}

function popUpPrivacyPolicy(svlparam) {
	var newWin = window.open("index.jsp?menuItem=Merck%20Privacy%20Policy&pathInfo=external", "PopupPrivacy", "height=440,width=518,location=no,scrollbars=yes,menubars=no,toolbar=no,resizable=yes");
	newWin.focus();
}
function popUpPrivacyPolicy() {
	var newWin = window.open("index.jsp?menuItem=Merck%20Privacy%20Policy&pathInfo=external", "PopupPrivacy", "height=440,width=518,location=no,scrollbars=yes,menubars=no,toolbar=no,resizable=yes");
	newWin.focus();
}


function popUpPrivacyPolicyMSP() {
	var newWin = window.open("index.jsp?menuItem=Merck%20Privacy%20Policy&pathInfo=external", "PopupPrivacy", "height=400,width=630,location=no,scrollbars=yes,menubars=no,toolbar=no,status=yes,resizable=yes");
	newWin.focus();
}

function popUpMSPPAP(URL) {
	var newWin = window.open(URL, "MSP_PAP", "height=400,width=630,location=no,scrollbars=yes,menubars=no,toolbar=no,status=yes,resizable=yes");
	newWin.focus();
}

function popUpExit(URL) {
	var newWin = window.open(URL, "MPS_EXIT", "height=520,width=790,location=yes,scrollbars=yes,menubars=yes,toolbar=yes,status=yes,resizable=yes");
	newWin.focus();
}

function popUpMS(selectObj) {
	var item = selectObj.options[selectObj.selectedIndex].value;
	if(item == "Medical Student")
	{
		alert("For medical students, please use your Medical Education number\n"+
	 	"in lieu of your state license number");
	}
		else if(item == "D.O. Resident" || item == "M.D. Resident")
		{
			alert("For Residents, please enter your Medical Education number or state license number.\n"+
		 	"If these are not available, please contact our helpdesk at 1-800-489-5119.");

		}

}

function setFocusFirst(){
	var bolFocusSet=false;
	var formObj;
	var j;
	var i;
	try{
	for(i=0;i>-1;i--){
		formObj = document.forms[i];
		if(formObj){
			j = 0;

			while(formObj.elements[j]){
				if(formObj.elements[j].type == "text"){
					formObj.elements[j].focus();
					bolFocusSet = true;
					break;
				}
				j++;
			}
		}

		if(bolFocusSet==true)
			break;
	}
	}catch(exc)
	{

	}
}

function disableButton(strSubmitBtnId){
	var objSubmitBtn = document.getElementById(strSubmitBtnId);
	objSubmitBtn.disabled = !(objSubmitBtn.disabled);
}

function hlthis(rowObj){rowObj.className="leftNavProdListHigh";}
function hlsthis(rowObj){rowObj.className="leftNavProdListHigh";}
function llthis(rowObj){rowObj.className="leftNavProdListDef";}
function llathis(rowObj){rowObj.className="selectednav";}

function hlsthis_locked(rowObj){rowObj.className="leftNavProdListHighlocked";}
function llathis_locked(rowObj){rowObj.className="selectednavlocked";}

function hlthis_locked(rowObj){rowObj.className="leftNavProdListHighlocked";}
function llthis_locked(rowObj){rowObj.className="leftNavProdListDeflocked";}

function pophlthis(rowObj){rowObj.className="leftNavProdListPopHigh";}
function popllthis(rowObj){rowObj.className="leftNavProdListPop";}

function rollOn(imgID){
	var objImage = document.getElementById(imgID);
	var i;
	var newSrc;
	i = objImage.src.indexOf("_o");

	if(i > 0){
		newSrc = objImage.src.substring(0, i+1) + "over.gif";
		objImage.src = newSrc;
	}
}

function rollOut(imgID){
	var objImage = document.getElementById(imgID);
	var i;
	var newSrc;
	i = objImage.src.indexOf("_o");

	if(i > 0){
		newSrc = objImage.src.substring(0, i+1) + "on.gif";
		objImage.src = newSrc;
	}
}

function rollOut2(imgID){
	var objImage = document.getElementById(imgID);
	var i;
	var newSrc;
	i = objImage.src.indexOf("_o");

	if(i > 0){
		newSrc = objImage.src.substring(0, i+1) + "on2.gif";
		objImage.src = newSrc;
	}
}

function popUpProd(URL){
	var newwin = window.open(URL, "popup", "height=440,width=518,location=no,scrollbars=yes,menubars=no,toolbar=no,resizable=yes");
	newwin.focus();
}

function openPage(optionObj) {
	if ((optionObj.selectedIndex!=null && optionObj.selectedIndex>=0) &&
		(optionObj[optionObj.selectedIndex].value.indexOf("http:")!=-1 ||
			optionObj[optionObj.selectedIndex].value.indexOf(".pdf")!=-1 ||
				optionObj[optionObj.selectedIndex].value.indexOf("Product Pop-Up")!=-1)) {
		if (optionObj[optionObj.selectedIndex].value.indexOf("Product Pop-Up")!=-1) {
			popUpProd(optionObj[optionObj.selectedIndex].value) ;
			return false;
		} else {
			window.open(optionObj[optionObj.selectedIndex].value);
		}
		return false;
	} else {
		return true;
	}
}

function limitChars(formField,charLimit){//arguments are: the field to limit & the number of characters allowed
	if(formField.value.length > charLimit){
		formField.value = formField.value.substr(0,charLimit);
	}
}

function sizeWindow(newx,newy){
	//detect the inner dimensions of the window

	if (self.innerHeight) // all except Explorer
	{
		if(self.innerWidth!=newx||self.innerHeight!=newy){resizeTo(newx,newy);}
	}
	else if (document.documentElement && document.documentElement.clientHeight)
		// Explorer 6 Strict Mode
	{
		if(document.documentElement.clientWidth!=newx||document.documentElement.clientHeight!=newy){resizeTo(newx,newy);}
	}
	else if (document.body) // other Explorers
	{
		if(document.body.clientWidth!=newx||document.body.clientHeight!=newy){resizeTo(newx,newy);}
	}
}

function closeThisToRegister(server){
	if (window.opener && !window.opener.closed)
	{
		window.opener.location.href = server + "/portal/site/merckproductservices/index.jsp?epi-content=REGISTRATION";
	}
	else
	{
		homeWin = window.open(server + "/portal/site/merckproductservices/index.jsp?epi-content=REGISTRATION","");
		if(window.focus){homeWin.focus();}

	}
	self.close();
}

//Contact Us Validation

// Regular expressions to validate data format
	var regExpName=/^[A-Za-z0-9 '-]{1,20}$/;
	var regExpZip=/^(\d{5})$/;
	var regExpPhone=/^\(?([2-9])(\d{2})\)?[- .]?(\d{3})[- .]?(\d{4})$/;

	function validateField(strId, objRegExp, strError){
		var objField = document.getElementById(strId);
		var objErrorMsg = document.getElementById(strId+"Error");
		var objFieldRow = document.getElementById(strId+"Row");

		if(objField.value.length<1){
			objErrorMsg.innerHTML=strError;
			objFieldRow.className="row error";
			return true;
		}
		else{
			if(!(objRegExp.test(objField.value))){
				objErrorMsg.innerHTML=strError;
				objFieldRow.className="row error";
				return true;
			}
			else{
				objFieldRow.className="row";
			}
		}
		return false;
	}

	// Validate the fields for signed in form
	function validateFieldsSigned(){
		// If any of the fields are invalid indicate so by highlighted the "form box" and displaying a appropriate message.
		var bolPhone = validateField("homephone", regExpPhone, "Please enter a valid Phone Number.");

		if (bolPhone){
			document.getElementById("userMessage").className="errorFound";
			document.getElementById("formBox").className="highlight";

			return false;
		}
		else{
			document.getElementById("userMessage").className="noError";
			document.getElementById("formBox").className="default";
		}

		disableForm(document.getElementById("callbackform"));
		return true;
	}

	function validateMOCFields(){
		// If any of the fields are invalid indicate so by highlighted the "form box" and displaying a appropriate message.
		var bolPhone = validateField("homephone", regExpPhone, "Please enter a valid Phone Number.");

		if (bolPhone){
			document.getElementById("userMessage").className="errorFound";
			document.getElementById("startSessionRow").className="mocError";
			return false;
		}
		else{
			document.getElementById("userMessage").className="noError";
			document.getElementById("startSessionRow").className="mocRow";
		}

		disableForm(document.getElementById("callbackform"));
		return true;
	}


	// Validate the fields for anonymous form
	function validateFieldsAnon(){
		// If any of the fields are invalid indicate so by highlighted the "form box" and displaying a appropriate message.
		var bolFname = validateField("firstname", regExpName, "Please enter your First Name.");
		var bolLname = validateField("lastname", regExpName, "Please enter your Last Name.");
		var bolZip = validateField("zip", regExpZip, "Please enter a valid Zip Code.");
		var bolPhone = validateField("homephone", regExpPhone, "Please enter a valid Phone Number.");

		if (bolFname || bolLname || bolZip || bolPhone){
			document.getElementById("userMessage").className="errorFound";
			document.getElementById("formBox").className="highlight";
			return false;
		}
		else{
			document.getElementById("userMessage").className="noError";
			document.getElementById("formBox").className="default";
		}

		disableForm(document.getElementById("callbackform"));
		return true;
	}

	function disableForm(theform){
		for (i = 0; i < theform.length; i++){
			var tempobj = theform.elements[i];
			if (tempobj.type.toLowerCase() == "submit")
			tempobj.disabled = true;
		}
	}
//Contact Us Validation
//Moved to Grid
//onload=setFocusFirst;

//emf Home Locked tabs
	function emfWebcastsLockedTabs2()
     {

			document.getElementById("emfScheduledWebcastDetailLocked").className="emfHide";
			document.getElementById("emfRegisterWebcastDetailLocked").className="emfShow";


     }
	 function emfWebcastsLockedTabs1()
     {
	 		document.getElementById("emfScheduledWebcastDetailLocked").className="emfShow";
			document.getElementById("emfRegisterWebcastDetailLocked").className="emfHide";

     }

	function emfWebcastsProductLockedTabs2()
     {

			document.getElementById("emfScheduledWebcastDetailProductLocked").className="emfHide";
			document.getElementById("emfRegisterWebcastDetailProductLocked").className="emfShow";


     }
	 function emfWebcastsProductLockedTabs1()
     {
	 		document.getElementById("emfScheduledWebcastDetailProductLocked").className="emfShow";
			document.getElementById("emfRegisterWebcastDetailProductLocked").className="emfHide";

     }

// emf Product Page and Home Page tabs
	 function emfWebcastsTabs1()
     {

	 		document.getElementById("ajaxcontentarea").className="emfShow";
			document.getElementById("defaultcontentarea").className="emfHide";
     }
	function emfWebcastsTabs2()
     {

			document.getElementById("ajaxcontentarea").className="emfHide";
			document.getElementById("defaultcontentarea").className="emfShow";

     }
function swapDIV(url,target) {
    // native XMLHttpRequest object
    document.getElementById(target).innerHTML = 'loading...';

    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
		req.onreadystatechange = function() {swapDIVDone(target);};
		req.open("GET", url, true);
        req.send(null);
    // IE/Windows ActiveX version
    } else if (window.ActiveXObject) {
        req = new ActiveXObject("Microsoft.XMLHTTP");
        if (req) {
            req.onreadystatechange = function() {swapDIVDone(target);};
            req.open("GET", url, true);
            req.send();
        }
    }
}

function swapDIVDone(target) {
    // only if req is "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
            results = req.responseText;
	        document.getElementById(target).innerHTML = results;
        } else {
            document.getElementById(target).innerHTML="jah error:\n" +
                req.statusText;
        }
    }
}

/* Help pop-up functions*/

function popHelp(linkEl){
	var mpshelpurl = linkEl;
	// Detect existence of the popup
	if(typeof mpshelppopup!="object"){ // check if the pop-up was created
		mpshelppopup = window.open(mpshelpurl,"mpshelpwin","width=650,height=500,top=0,left=0,resizable=yes,scrollbars=yes,toolbar=no,menubar=no,location=no,status=yes,directories=no");
		// detect if popup window obj created
		if(mpshelppopup!=null){// blocked
		}
		else {// not blocked
		}
	}
	else if(typeof mpshelppopup=="object"){ // check if mpshelppopup was created, but the window is closed
		if(mpshelppopup.closed){
			mpshelppopup = window.open(mpshelpurl,"mpshelpwin","width=650,height=500,top=0,left=0,resizable=yes,scrollbars=yes,toolbar=no,menubar=no,location=no,status=yes,directories=no");
		}
		else{
			//alert(mpshelpurl);

			var path = mpshelppopup.location.pathname;
			//alert(getFilename(path)+', '+getFilename(mpshelpurl.pathname.toString()));
			mpshelppopup.location.href=mpshelpurl;
			if(getFilename(path)==getFilename(mpshelpurl.pathname.toString())){//if same page, must reload page for hash to function
				mpshelppopup.location.reload(false);
			}
		}
	}
	mpshelppopup.focus();
}

function getFilename(filepath){
	var filename = "";
	if(filepath.lastIndexOf("/")>-1){
		filename = filepath.substring(filepath.lastIndexOf("/")+1,filepath.length);
	}
	return filename;
}

/* end Help pop-up functions*/

/* My Items flyout */
var timeoutID = null;
function toggleMyItems(action){
	if(document.getElementById){
		var myitemsmenu = document.getElementById("myitemmenu");
		var myitemslink = document.getElementById("myitemslink");
	}
	if(myitemsmenu){
		switch (action){
			case "open":
				if(timeoutID)clearTimeout(timeoutID);
				myitemsmenu.style.display = "block";
				myitemslink.className = "selected";
				break;
			case "close":
				timeoutID = setTimeout(
								function(){
									myitemsmenu.style.display = "none"
									myitemslink.className = "";
								}
							,1000);
				break;
		}
	}
}
/* end My Items flyout */

/* fix for background image flicker in IE6 */
try {
  document.execCommand("BackgroundImageCache", false, true);
}
catch(err) {}

function submitonce(theform)
{
	if (document.all||document.getElementById)
	{
		for (i=0;i<theform.length;i++)
		{
			var tempobj=theform.elements[i];
			if(tempobj.type.toLowerCase()=="submit"||tempobj.type.toLowerCase()=="reset")
			tempobj.disabled=true;
		}
	}
	else
	{
		return false;
	}
}

/* remove curved footer from eMedical Forums module on product pages that have Additional Services and Resources */
function changeEmedicalForumsFooter() {
	document.getElementById('emedical-forums').style.background = '#F3F3F3';
	document.getElementById('emedical-forums').style.paddingBottom = '0';
}
