/******************************************************/
/*  Merck COP global JavaScript file   				  */
/******************************************************/


// PRELOADING IMAGES
if (document.images) {
 	imgHide =new Image();  imgHide.src ="/usacontent/productservices/images/arrowRight.gif";
 	imgShow=new Image();  imgShow.src="/usacontent/productservices/images/arrowDown.gif";
}

function handleOver(divid) {

	var imgName='img'+divid;

	if (document.getElementById(divid).className=="hidepaneltog") {

		document.getElementById(divid).style.fontWeight="bold";
	} else {

		document.getElementById(divid).style.fontWeight="bold";
		document.getElementById(divid).style.textDecoration="underline";
		document.getElementById(divid).style.backgroundImage="url('/usacontent/productservices/images/rolloverControl.gif')";
		document.getElementById(imgName).src="/usacontent/productservices/images/arrowDown.gif";
	}
}

function handleOut(divid) {
	var imgName='img'+divid;
	if (document.getElementById(divid).className=="hidepaneltog") {
		document.getElementById(divid).style.fontWeight="bold";
		document.getElementById(divid).style.textDecoration="none";
		document.getElementById(divid).style.backgroundImage="url('/usacontent/productservices/images/rolloverControl.gif')";
	}
	else
	{
		document.getElementById(divid).style.fontWeight="normal";
		document.getElementById(divid).style.textDecoration="none";
		document.getElementById(divid).style.backgroundImage="url('/usacontent/productservices/images/regularControl.gif')";
		document.getElementById(imgName).src="/usacontent/productservices/images/arrowRight.gif";
	}
}

//function handleOver(divid) {
//
//	var imgName='img'+divid;
//
//	if (document.getElementById(divid).className=="hidepaneltog") {
//
//		document.getElementById(divid).style.fontWeight="bold";
//		document.getElementById(divid).style.backgroundColor="#A7C6E3";
//
//	} else {
//
//		document.getElementById(divid).style.fontWeight="bold";
//		document.getElementById(divid).style.backgroundColor="#616D9D";
//		document.getElementById(imgName).src="/usacontent/productservices/images/cop_arrow_on.jpg";
//	}
//}
//
//function handleOut(divid) {
//	var imgName='img'+divid;
//	if (document.getElementById(divid).className=="hidepaneltog") {
//		document.getElementById(divid).style.fontWeight="bold";
//		document.getElementById(divid).style.backgroundColor="#A7C6E3";
//	}
//	else
//	{
//		document.getElementById(divid).style.fontWeight="normal";
//		document.getElementById(divid).style.backgroundColor="#8B96C3";
//		//var imgName='img'+divid;
//		document.getElementById(imgName).src="/usacontent/productservices/images/cop_arrow_off.jpg";
//	}
//}


function toggleHideDIV(url,target,divid) {

	//alert("toggleHide: Target: " + target);
	var imgName='img'+divid;
	hidden = document.getElementById(divid).className;
	if(hidden == "hidepanel"){
		document.getElementById(divid).className = 'hidepaneltog';
		document.getElementById(divid).style.backgroundColor="#A7C6E3";
		if (document.images) document.getElementById(imgName).src=imgShow.src;

	}else{
		document.getElementById(divid).className = 'hidepanel';
		document.getElementById(imgName).src=imgHide.src;
	}
	document.getElementById(target).style.display = (document.getElementById(target).style.display == 'none') ? 'block' : 'none';
	document.getElementById(target).innerHTML = 'loading...';

    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
		req.onreadystatechange = function() {toggleHideDIVDone(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() {toggleHideDIVDone(target);};
            req.open("GET", url, true);
            req.send();
        }
    }
}

function toggleHideDIVDone(target) {
    //alert("toggleHide: req.readyState = " + req.readyState);
    //alert("toggleHide: req.status = " + req.status);

    // only if req is "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
            results = req.responseText;
            //alert("results : " + results);

            if (results.indexOf("ERRORSTO", 0) != -1 ||
            	results.indexOf("authorized to view", 0) != -1
            )
            {
            	swapIndexDIVForSession("/portal/site/merckproductservices/?front_door=true&epi-content=ERRORSTO", "gridcontent")
			}
			else
			{
				document.getElementById(target).innerHTML = results;
			}

        } else {
            document.getElementById(target).innerHTML="jah error:\n" +
                req.statusText;
        }
    }
}



function hoverOver(divid) {
	var imgName='img'+divid;
	if (document.getElementById(divid).className=="viewpaneltog") {
		document.getElementById(divid).style.fontWeight="bold";
		document.getElementById(divid).style.textDecoration="underline";
		document.getElementById(divid).style.backgroundColor="#616D9D";
		document.getElementById(imgName).src="/usacontent/productservices/images/arrowDown.gif";
	} else	{
		document.getElementById(imgName).src="/usacontent/productservices/images/arrowDown.gif";
		document.getElementById(divid).style.textDecoration="none";
	}

}

function hoverOut(divid) {
	var imgName='img'+divid;
	if (document.getElementById(divid).className=="viewpaneltog") {
		document.getElementById(divid).style.fontWeight="normal";
		document.getElementById(divid).style.textDecoration="none";
		document.getElementById(divid).style.backgroundColor="#8B96C3";
		document.getElementById(imgName).src="/usacontent/productservices/images/arrowRight.gif";
	}
	else {
		document.getElementById(divid).style.backgroundColor="#A7C6E3";
		document.getElementById(divid).style.textDecoration="none";
		document.getElementById(imgName).src="/usacontent/productservices/images/arrowDown.gif";
	}
}


function toggleShowDIV(url,target,divid) {
	//alert("toggleShow: Target : " + target);
	var imgName='img'+divid;
	show = document.getElementById(divid).className;

	if(show == "viewpanel") {
		document.getElementById(divid).className = 'viewpaneltog';
		document.getElementById(divid).style.fontWeight="normal";
		document.getElementById(divid).style.textDecoration="none";
		document.getElementById(divid).style.backgroundColor="#8B96C3";
		document.getElementById(imgName).src="/usacontent/productservices/images/arrowRight.gif";
	}else {
		document.getElementById(divid).className = 'viewpanel';
	}

	if ( document.getElementById ) {
    	target1 = document.getElementById( target );
	    if ( target1.style.display == "none" ) {
     		target1.style.display = "";
    	} else {
     		target1.style.display = "none";
    	}
   }

   document.getElementById(target).innerHTML = 'loading...';
    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
		req.onreadystatechange = function() {toggleShowDIVDone(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() {toggleShowDIVDone(target);};
            req.open("GET", url, true);
            req.send();
        }
    }
}

function toggleShowDIVDone(target) {
    //alert("toggleShow: req.readyState = " + req.readyState);
    //alert("toggleShow: req.status = " + req.status);
	// only if req is "loaded"
    if (req.readyState == 4) {
		//alert("req.status = " + req.status);
        // only if "OK"
        if (req.status == 200) {
            results = req.responseText;

            //alert("results : " + results);
            if (results.indexOf("ERRORSTO", 0) != -1 ||
            	results.indexOf("authorized to view", 0) != -1
            )
            {
            	swapIndexDIVForSession("/portal/site/merckproductservices/?front_door=true&epi-content=ERRORSTO", "gridcontent")
			}
			else
			{
				document.getElementById(target).innerHTML = results;
			}

	    } else {
            document.getElementById(target).innerHTML="jah error:\n" +
                req.statusText;
        }
    }
}

function swapIndexDIV(url,target) {

    // native XMLHttpRequest object
    document.getElementById(target).innerHTML = 'loading...';

    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
		req.onreadystatechange = function() {swapIndexDIVDone(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() {swapIndexDIVDone(target);};
            req.open("GET", url, true);
            req.send();
        }
    }
}

function swapIndexDIVDone(target) {

    // only if req is "loaded"
    if (req.readyState == 4) {
        // only if "OK"
        if (req.status == 200) {
            results = req.responseText;
            if (results.indexOf("ERRORSTO", 0) != -1 ||
            	results.indexOf("authorized to view", 0) != -1
            )
            {
            	swapIndexDIVForSession("/portal/site/merckproductservices/?front_door=true&epi-content=ERRORSTO", "gridcontent")
			}
			else
			{
				document.getElementById(target).innerHTML = results;
			}

        } else {
            document.getElementById(target).innerHTML="jah error:\n" +
                req.statusText;
        }
    }
}

function swapIndexDIVForSession(url,target) {

    // native XMLHttpRequest object
    document.getElementById(target).innerHTML = 'loading...';

    if (window.XMLHttpRequest) {
        req = new XMLHttpRequest();
		req.onreadystatechange = function() {swapIndexDIVDone(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() {swapIndexDIVDone(target);};
            req.open("GET", url, true);
            req.send();
        }
    }
}

function swapIndexDIVForSessionDone(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;
        }
    }
}
function copPopUp(url){
	//Check for session timeout
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
		req.onreadystatechange = function() {checkTimeout(url);};
		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() {checkTimeout(url);};
			req.open("GET", url, true);
			req.send();
		}
	}
}

function checkTimeout(URL){
	if (req.readyState == 4) {
		// only if "OK"
		if (req.status == 200) {
			results = req.responseText;
			if (results.indexOf("ERRORSTO", 0) != -1 ||
				results.indexOf("authorized to view", 0) != -1)	{
				swapIndexDIVForSession("/portal/site/merckproductservices/?front_door=true&epi-content=ERRORSTO", "gridcontent")
			}
			else
			{
				var newwin = window.open(URL, "copPopUp", "height=685,width=810,location=no,scrollbars=yes,menubars=no,toolbar=no,status=yes,resizable=yes");
				newwin.moveTo(90,20);
				newwin.focus();
			}
		}
	}
}



