//***************************************************************************************************************************
/**
*	file: root/js/popup.js
*
*	purpose: Draw the policy page
*	
*
*	@example
*		//files to include
*		<script language="javascript" type="text/javascript" src="js/popup.js" ></script>
*
*		include this page before any other js pages
*		if you have other js pages with (window)onload events, add popupWindows(); first to that list 
*		add the rel element to your link to define the size/location/options of your popup window
8			popup|600|400|0|yes 
*				popup to define it is a pop up window 600x400 is the height and width, zero for false on some attributes and yes or no to display/hide toolbars
*		
*	@author Mark Henderson mah323@psu.edu 
* 	@access private 
*  	@copyright Penn State Justice and Safety Institute (JASI)
*/
//***************************************************************************************************************************
function popupWin(winName, mylink, attribs, form) {
	var popupWin = null;
	//randomString() is in common.js
	//var winName = randomString(5); 
	popupWin = window.open(mylink, winName, attribs);  
	if(winName == 'email') {
		var a = window.setTimeout("submitForm();",500); 
	} else if(winName == 'form') {
		var a = window.setTimeout("submitForm('"+form+"');",500); 
	}
}


function popupWindows() {
    if(!document.getElementsByTagName) {
         return;
    }
    var scrW = screen.availWidth;
    var scrH = screen.availHeight;
    var anchors = document.getElementsByTagName("a");
    for (var i = 0; i < anchors.length; i++) {
         var anchor = anchors[i];
         var linkDest = anchor.getAttribute("href");
		  if(anchor.getAttribute("rel")){
			 var relIndex = anchor.getAttribute("rel");
			 //alert (anchors.length);
			 var relSplit = relIndex.split("|");
			 var windowAttributes = "";
			 			 
			 if(relSplit[0] == "popup" || relSplit[0] == "email" || (relSplit[0].search(/form/gi) > -1)) {
			 	  

				  if (relSplit[1] > scrW) {
					 pW = scrW - 10;
				  }
				  else {
					  if(relSplit[1] > 0){
						  pW = relSplit[1];
					  }else{
						  pW = 544;
					  }
				  }
				  if (relSplit[2] > scrH) {
					 pH = scrH - 40;
				  }
				  else {
					  if(relSplit[2] > 0){
						  pH = relSplit[2];
					  }else{
						  pH = 450;
					  }
				  }
				  scrX = (scrW - pW - 10) * .5;
				  scrY = (scrH - pH - 30) * .5;
				  var windowAttributes = "width=" + pW + ",height=" + pH + ",left=" + scrX + ",top=" + scrY + ",screenX=" + scrX + ",screenY=" + scrY;
				  windowAttributes += ",resizable=no,scrollbars=yes,toolbar=no,location=no,directories=no,status=no,menubar=no";
				  winName = 'popup';			
				  if(relSplit[0] == "email") {
				  	winName = 'email';
				  } else if(relSplit[0].search(/form/gi) > -1) {
				  	winName = "form";
				  }
				  				  
				 linkDest = linkDest.replace(/@/i, '%40');
				 anchor.setAttribute("href", "javascript:popupWin('" + winName + "','" + linkDest + "','" + windowAttributes + "','" + relSplit[0] +"');");
			 } else if(relSplit[0] == "erl") {
				  var windowAttributes = "width=790,height=543,left=0,top=0,screenX=0,screenY=0";
				  windowAttributes += ",resizable=no,scrollbars=no,toolbar=no,location=no,directories=no,status=no,menubar=no";
				  linkDest += "&studentID="+studentID;
				  var winName = "popup";
				  anchor.setAttribute("href", "javascript:popupWin('" + winName + "','" + linkDest + "','" + windowAttributes + "'); ");
			 } else if(relSplit[0] == "full") {	
			 	  var windowAttributes = "left=0,top=0,screenX=0,screenY=0";
				  windowAttributes += ",resizable=yes,scrollbars=yes,toolbar=yes,location=yes,directories=yes,status=yes,menubar=yes";
				  var winName = "full";
				  anchor.setAttribute("href", "javascript:popupWin('" + winName + "','" + linkDest + "','" + windowAttributes + "'); ");
			 }	else if(relSplit[0] == "scorm") {	
			 	  var windowAttributes = "left=0,top=0,screenX=0,screenY=0";
				  windowAttributes += ",resizable=yes,scrollbars=no,toolbar=no,location=no,directories=no,status=no,menubar=no";
				  var winName = "scorm";
				  anchor.setAttribute("href", "javascript:popupWin('" + winName + "','" + linkDest + "','" + windowAttributes + "'); ");
			 }			 
		}
    }
	
} 