var showEffects = false;
var showTips = true;
var isie = navigator.appName == 'Microsoft Internet Explorer';
var isie6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
var onLoadQueue = new Array;
var objPCL = document.createElement('div');
var searchhasfocus = false;

function fadePurple(newid){
	if(typeof(newid)=='undefined')
		newid='';
	
	var pagesize = getPageSizeWithScroll();
	var windowsize = getWindowSize();
	var fadepurplebg = document.createElement('div');
	var loaderdiv = document.createElement('div');
	
	fadepurplebg.className = 'fadepurplebg';
	loaderdiv.className='loaderdiv'
	
	fadepurplebg.setAttribute('id','fadepurplebg'+newid);
	loaderdiv.setAttribute('id','loaderdiv');
	
//	loaderdiv.innerHTML='<em>Loading</em>'
	
	var divheight;
	
	if(pagesize[1]>document.body.offsetHeight)
		divheight = pagesize[1];
	else if (windowsize[1] > document.body.offsetHeight)
		divheight = windowsize[1];
	else
		divheight = document.body.offsetHeight
	
	
	fadepurplebg.style.height = divheight+'px';
	
	if (!document.getElementById('loaderdiv'))
		fadepurplebg.appendChild(loaderdiv);


	document.body.appendChild(fadepurplebg)
}

function removefadePurple(id){
	if(typeof(id)=='undefined')
		id='';
		
	if (showEffects){
		new Effect.Fade('fadepurplebg',{duration:1})
		setTimeout("document.body.removeChild(document.getElementById('fadepurplebg"+id+"'))",2001)
	} else
		document.body.removeChild(document.getElementById('fadepurplebg'+id));
}

function showWidePopup(popuptext,nobackfade,notinviewport,newdivname){
	var sidecontent='<img src="/llimages/ContactSupport.png" width="145" height="112" alt="Contact Support" /><map name="associatemap"><area coords="34,32,113,119" shape="rect" href="http://www.iia.net.au/" alt="Internet Industry Association" target="_blank" /><area coords="33,121,118,206" shape="rect" href="http://www.lawyersalliance.com.au/" alt="Australian Lawyers Allicance" target="_blank" /></map><img src="/llimages/Associates.png" width="145" height="300" usemap="associatemap" />';
	
	showPopup(popuptext,nobackfade,notinviewport,newdivname,sidecontent)
}

function showPopup (popuptext,nobackfade,notinviewport,newdivname,extendedcontent){	
	if(typeof(newdivname)=='undefined')
		newdivname=''

	if(typeof(extendedcontent)=='undefined')
		extendedcontent=''

	var divid = 'pop_'+Math.floor(Math.random()*5555)
	var handleid = 'hand_'+Math.floor(Math.random()*5555)
	
	while (document.getElementById(divid))
		divid = 'pop_'+Math.floor(Math.random()*5555)

	while (document.getElementById(handleid))
		handleid = 'hand_'+Math.floor(Math.random()*5555)
	
	popuptext=popuptext.replace(/x1x/g,divid);
	
	var containerdiv = document.createElement('div');
	var centrediv = document.createElement('div');
	var registration = document.createElement('div');
	var registrationbox = document.createElement('div');
	var contentcontainer = document.createElement('div');
	var containertext = document.createElement('div');

	var imgheader = document.createElement('img');
	var imgclose = document.createElement('img');
	var imgfooter = document.createElement('img');
	
	var lnkclose = document.createElement('a');
	
	if(extendedcontent != ''){
		var imgheaderwide = document.createElement('img');
		var sidecontent = document.createElement('div');
		var sidefooter = document.createElement('div');
		var sidecontainer = document.createElement('div');
	}
	

	containerdiv.className = 'popupcontainer';
	registrationbox.className = extendedcontent == ''?'registrationbox':'registrationboxwide';
	contentcontainer.className = extendedcontent == ''?'contentcontainer':'contentcontainerwide';
	containertext.className = 'containertext';
	containertext.style.zIndex='99';
	containertext.style.position='relative';
	
	containerdiv.setAttribute('id',divid);
	registration.setAttribute('id','registration');
	containertext.setAttribute('id','containertext');
	imgheader.setAttribute('id',handleid);
	
	containerdiv.style.top = notinviewport?'0px':f_scrollTop()+'px';
	
	centrediv.setAttribute('align','center');
	registrationbox.style.textAlign='left';
	
	lnkclose.setAttribute('href','#');
	
	//This is for compatibility with listboxes
	if(nobackfade)
		lnkclose.onclick =	function(){
								closePopup(divid)
								if(document.getElementById('MainDiv'))
									showFormElements(document.getElementById('MainDiv'));
								return false;
							}
	else
		lnkclose.onclick =	function(){
								closePopup(divid);
								if(document.getElementById('MainDiv'))
									showFormElements(document.getElementById('MainDiv'));
								removefadePurple(newdivname);
								return false;
							}
		
		imgheader.src = '/llimages/popup-header.png'
		imgheader.style.width = '540px';
		imgheader.style.height = '65px';

		imgclose.style.width = '90px';
		imgclose.style.height = '65px';
		imgclose.style.position = 'relative';
		imgclose.style.zIndex = '99';

	if(extendedcontent == ''){
		imgclose.src = '/llimages/popup-close-black.png'

	} else {		
		imgheader.src = '/llimages/popup-header-wide2.png'
		imgheader.style.width = '697px';

		imgclose.src = '/llimages/popup-close-wide2.png'
		
		imgheaderwide.src = '/llimages/popup-header-wide.png'
		imgheaderwide.style.width = '170px';
		imgheaderwide.style.height = '65px';
		
		sidecontent.className = 'sidecontent';
		sidecontent.innerHTML = extendedcontent;
		
		sidefooter.className = 'sidefooter';
		sidefooter.innerHTML='&nbsp;';
		
		sidecontainer.className = isie?'sidecontainer':'sidecontainerff';
	}
	
	imgfooter.src = '/llimages/popup-footer.png'
	imgfooter.style.width = '630px';
	imgfooter.style.height = '25px';
	if (!isie)
		imgfooter.style.position = 'relative';
	imgfooter.style.float = 'left';

//	imgfooter.setAttribute('align','left');

	containertext.innerHTML = popuptext;
	
	lnkclose.appendChild(imgclose);

	contentcontainer.appendChild(containertext);
	registrationbox.appendChild(imgheader);
	registrationbox.appendChild(lnkclose);
	
	if(extendedcontent != '' && !isie){
		contentcontainer.style.position='absolute';

		sidecontainer.appendChild(sidecontent);
		sidecontainer.appendChild(sidefooter);
		
//		registrationbox.appendChild(imgheaderwide);
		registrationbox.appendChild(sidecontainer);
	}

	if(extendedcontent != '' && isie){
		containertext.style.zIndex='99';
		sidecontainer.style.zIndex='90';
		
		sidecontainer.appendChild(sidecontent);
		sidecontainer.appendChild(sidefooter);

//		registrationbox.appendChild(imgheaderwide);
		contentcontainer.appendChild(sidecontainer);
	}


	registrationbox.appendChild(contentcontainer);
	registrationbox.appendChild(imgfooter);
	registration.appendChild(registrationbox);
	centrediv.appendChild(registration);
	containerdiv.appendChild(centrediv);
	
	
	//Background should already be here by now, but just in case it's not, put it there.
	if(!document.getElementById('fadepurplebg') && !nobackfade)
		fadePurple();
	
	if (document.getElementById('loaderdiv'))
		document.getElementById('fadepurplebg'+newdivname).removeChild(document.getElementById('loaderdiv'));
	
	if (showEffects){
		containerdiv.style.display = 'none';
		
		document.body.appendChild(containerdiv);
		new Effect.SlideDown(divid,{duration:1})
	} else {
		if(extendedcontent != '' && isie){
			//IE Fix, we need to hide the div, reposition it, and then put it back
			//For some reason IE and FF treat position:absolute different
			containerdiv.style.visibility='hidden';
			document.body.appendChild(containerdiv);
			contentcontainer.style.left = findPosX(document.getElementsByClassName('registrationboxwide')[0])+'px';
			contentcontainer.style.top  = (findPosY(document.getElementsByClassName('registrationboxwide')[0])+65)+'px';
			sidecontainer.style.left = findPosX(document.getElementById(handleid))+589+'px';
			containerdiv.style.visibility='visible';
		} else {
			document.body.appendChild(containerdiv);
		}
	}
	
	if(extendedcontent != ''){
		//By now our objects have offset heights, and we need to move the footer, lest it float
		//in the wrong place.
		imgfooter.style.top=contentcontainer.offsetHeight - sidecontent.offsetHeight-35+'px';
	}

	
	hideElements();
	
	showFormElements(document.getElementById(divid));

	var scriptelements = containertext.getElementsByTagName('script');
	for (i=0;i<scriptelements.length;i++)
		eval(scriptelements[i].innerHTML);

	if(notinviewport)
		document.scrollTo(0,0);
		
	fixPNG();
	
	//Inside try as sometimes it fails for the list windows
	try {
		new Draggable(divid,{handle: handleid});
	} catch (e){
		// do nothing
	}
	
	//Stretch grey to cover screen now that the new div is there
	if(!nobackfade)
		fixStretchMarks(newdivname, divid);
//		setTimeout("fixStretchMarks('"+newdivname+"','"+divid+"')",2000);

}

function closePopup(divid){
	if (showEffects){
		new Effect.SlideUp(divid,{duration:1})
		setTimeout("document.body.removeChild(document.getElementById('"+divid+"'));",2001)
	} else
		document.body.removeChild(document.getElementById(divid));
		
	showElements();
}

function fixStretchMarks(yamum, divid){
	if(typeof(yamum)=='undefined')
		yamum='';

		
	if(document.getElementById('fadepurplebg'+yamum)){
		var pagesize = getPageSizeWithScroll();
		var windowsize = getWindowSize();
		
		if(pagesize[1]>document.body.offsetHeight)
			var divheight = pagesize[1];
		else if (windowsize[1] > document.body.offsetHeight)
			var divheight = windowsize[1];
		else
			var divheight = document.body.offsetHeight
		
		var popheight = 0;

		/*Its bad practice I know, but it IS possible for there to be more than one div
		  with the the ID containertext, however if there is it should have been created
		  without the create background flag, and thus this won't run. Anyway, this screws
		  up IE. So here's a bandaid. Dry your eyes.
		  
		  Also, I know that if this height is used (as it will in IE when the div is longer than
		  the existing page, the grey div will actually be extended past the bottom of the div by its
		  existing height. And you know what? The general consensus is that this is OK. So don't complain.
		*/
		var containers = document.getElementsByClassName('containertext');
		for (var x=0; x<containers.length; x++)
			if (containers[x].offsetHeight > popheight)
				popheight=containers[x].offsetHeight;

		if(popheight > divheight)
			divheight += popheight		//This is addition because the div doesn't sit at the top of the page and if we just added
										//then it would be too short because of the top-of-page-offset. I could fix this but the JS
										//overhead isn't worth it, and I can't be bothered.

		document.getElementById('fadepurplebg'+yamum).style.height = divheight+'px';
	}
}

function getPageSizeWithScroll(){
	if (window.innerHeight && window.scrollMaxY) {
		// Firefox
		yWithScroll = window.innerHeight + window.scrollMaxY;
		xWithScroll = window.innerWidth + window.scrollMaxX;
	} else if (document.body.scrollHeight > document.body.offsetHeight){
		// all but Explorer Mac
		yWithScroll = document.body.scrollHeight;
		xWithScroll = document.body.scrollWidth;
	} else {
		// works in Explorer 6 Strict, Mozilla (not FF) and Safari
		yWithScroll = document.body.offsetHeight;
		xWithScroll = document.body.offsetWidth;
	}     arrayPageSizeWithScroll = new Array(xWithScroll,yWithScroll);
	return arrayPageSizeWithScroll;
} 

function gup(name){
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var tmpURL = document.location;
	var results = regex.exec( tmpURL );
	if( results == null )
		return "";
	else
		return results[1];
}

function getWindowSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return new Array(myWidth,myHeight);

}

function f_clientWidth() {
	return f_filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}
function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}
function f_scrollLeft() {
	return f_filterResults (
		window.pageXOffset ? window.pageXOffset : 0,
		document.documentElement ? document.documentElement.scrollLeft : 0,
		document.body ? document.body.scrollLeft : 0
	);
}
function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}
function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

function pngFix(){
	fixPNG();
	//For compatibility
}

function fixPNG(){
	var arVersion = navigator.appVersion.split("MSIE")
	var version = parseFloat(arVersion[1])
	
	if ((version >= 5.5) && (document.body.filters)) 
	{
	   for(var i=0; i<document.images.length; i++)
	   {
		  var img = document.images[i]
		  var imgName = img.src.toUpperCase()
		  if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
		  {
			 var imgID = (img.id) ? "id='" + img.id + "' " : ""
			 var imgClass = (img.className) ? "class='" + img.className + "' " : ""
			 var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
			 var imgStyle = "display:inline-block;" + img.style.cssText 
			 if (img.align == "left") imgStyle = "float:left;" + imgStyle
			 if (img.align == "right") imgStyle = "float:right;" + imgStyle
			 if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
			 var strNewHTML = "<span " + imgID + imgClass + imgTitle
			 + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
			 + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
			 + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
			 img.outerHTML = strNewHTML
			 i = i-1
		  }
	   }
	}
}

function launchCourse(cid){
	if (cid == 'intro')
		var opener=window.open('/videos/presenter/introlaunch.html','new','left=0,top=0,scrollbars=no,toolbar=no,menubar=no,location=no,width=1000,height=685');
	else
		var opener=window.open('/uniScripts/mgrqispi015.dll?APPNAME=FullFaceGlobal&PRGNAME=launchCourse&ARGUMENTS=-N'+cid,'new','left=0,top=0,scrollbars=no,toolbar=no,menubar=no,location=no,width=1000,height=685');
}

function loadFlash(swfpath, height, width, targetid){
	var requiredMajorVersion = 9;
	var requiredMinorVersion = 0;
	var requiredRevision = 0;
	
	var hasReqestedVersion = DetectFlashVer(requiredMajorVersion, requiredMinorVersion, requiredRevision);

	if (hasReqestedVersion) {
		flashtargetid = targetid
		// if we've detected an acceptable version
		// embed the Flash Content SWF when all tests are passed
		AC_FL_RunContent(
					"src", swfpath,
					"width", width,
					"height", height,
					"id", "flashbanner",
					"quality", "high",
					"name", "flashbanner",
					"allowScriptAccess","sameDomain",
					"type", "application/x-shockwave-flash",
					'codebase', 'https://fpdownload.macromedia.com/get/flashplayer/current/swflash.cab',
					"pluginspage", "https://www.adobe.com/go/getflashplayer"
		);
	} else {  // flash is too old or we can't detect the plugin
		var alternateContent = '<a href="https://www.adobe.com/go/getflashplayer" target="_blank"><img src="/images/flashupgrade.jpg" style="border:none;"></a>';
		document.getElementById(targetid).innerHTML = alternateContent;  // insert non-flash content
	}
}
