//------------------------------------------------------------------------
//
//	Copyright 2001 Danish Probe.  All Rights Reserved.
//	ABC products
//	Add-on: E-Learning Portal
//
//	$Workfile: Common.js $ 
//	$Author: Sune $
//	$JustDate: 14-01-04 $ 
//	Description:
//		- objectError
//		- openCourse
//		- mimeDetect
//		- getPath
//		- InStrR
//		- mouseoverHandler
//		- mouseoutHandler
//		- changeCheckBoxes
//		- onclickflipcheckboxes
//		- showTooltip
//		- hideTooltip
//		- onMouseOutABCButton
//		- onMouseOverABCButton
//		- onClickABCTab
//		- checkDateFormat
//		- onKeyPressTextAreaAsInput
//		- updateFilenameFromPopup
//
//------------------------------------------------------------------------


var lTemp = 1 / 2;
var fractionSeparator = lTemp.toString().substring( 1, 2 );
var lTemp2 = navigator.appVersion.substring( navigator.appVersion.indexOf( 'E' ) + 1, navigator.appVersion.indexOf( ';', navigator.appVersion.indexOf( 'E' ) + 1 ) );
var lPos = lTemp2.indexOf( '.', 0 );
if ( lPos > -1 ) { lTemp2 = lTemp2.substring( 0, lPos ) + fractionSeparator + lTemp2.substring( lPos + 1 ) }
var bV = parseFloat( lTemp2 );
var NS4		= (document.layers) ? true : false;
var IE4		= ((document.all)&&(bV>=4))?true:false;

var bObjectError = false;

function objectError(){
	bObjectError = true;
	return true;
}

function openCourse(sServerAddress, sCourseTitle, sAlias, sMode){
	var sLocation = '';
	var sConnectAddress = '';
	var sPath = '';
	var sFile = '';

	if (window.navigator.userAgent.indexOf('MSIE') <= 0){
		if (mimeDetect() == false){
			if ( sMode == 'win32' ){
				window.location = r_download_asp;
				return true;
			}
		}
	}else{
		if (bObjectError == true){
			if ( sMode == 'win32' ){
				window.location = r_download_asp;
				return true;
			}
		}
	}
	if ( sMode == 'win32' || ( sMode == 'mixed' && bObjectError == false ) ){
		sConnectAddress = window.location.protocol + '//' + window.location.hostname + '/' + getPath(window.location.pathname);
		sConnectAddress = sConnectAddress + r_start_asp+'?serveraddress=' + escape(window.location.protocol + '//' + window.location.hostname ) + '&coursetitle=' + escape(sCourseTitle);
		window.location = sConnectAddress;
	}else{
		oWin = window.open('/ABC Catalog/' + sAlias + '/Index.htm','_blank','top=0,left=0,width='+(screen.availWidth-4)+',height='+(screen.availHeight-43)+',toolbar=no,status=yes,resizable=yes');
		oWin.moveTo(-4,-4);
	}
}

function mimeDetect(){
	var bFound;
	var i;
	for (i=0; i < navigator.mimeTypes.length; i++){   
		if (navigator.mimeTypes[i].suffixes == 'abc'){
			return true;
		}
	}
	return false;
}

function getPath( sPath ){
	var lPosBackSlash = 0;
	var lPosSlash = 0;

	lPosBackSlash = InStrR(sPath, unescape('%5C'));
	lPosSlash = InStrR(sPath, unescape('%2F'));

	if (lPosBackSlash < 0 && lPosSlash < 0){
		return sPath;
	}

	if (lPosBackSlash > lPosSlash && lPosBackSlash > 0){
		sTemp = sPath.substring( lPosSlash, sPath.length );
		if ( sTemp.indexOf( '.', 0 ) >= 0 ){
			return sPath.substring(1, lPosBackSlash + 1);
		}else{
			return sPath;
		}
	}else{
		if (lPosSlash > lPosBackSlash && lPosSlash > 0){
			sTemp = sPath.substring( lPosSlash, sPath.length );
			if ( sTemp.indexOf( '.', 0 ) >= 0 ){
				return sPath.substring(1, lPosSlash + 1);
			}else{
				return sPath;
			}
		}
	}
}

function InStrR(sTarget, sFind){

	var lPos = 0;
	var lLastP = 0;
	var lStart = 0;

	lPos = sTarget.indexOf( sFind, 1 );
	do{
       	lLastP = lPos;
		lPos = sTarget.indexOf( sFind, lLastP + 1 );
	}while ( lPos > 0 );
	return lLastP;
}

function mouseoverHandler( elItem ){
	elItem.src= elItem.src.substring(0, elItem.src.lastIndexOf( '/', elItem.src.length ) + 1 ) + elItem.id+'mouseover.gif';
}

function mouseoutHandler( elItem ){
	elItem.src= elItem.src.substring(0, elItem.src.lastIndexOf( '/', elItem.src.length ) + 1 ) + elItem.id+'.gif';
}

function changeCheckBoxes( sRangeName, bCheckValue ){
	for(i=0;i<document.forms[0].length;i++){
		curItem = document.forms[0][i];
		if(curItem.name.indexOf(sRangeName,0)!=-1){
			if(curItem.checked!=bCheckValue && curItem.disabled==false){
				curItem.checked=bCheckValue;
			}
		}
	}
}


function onclickflipcheckboxes(sflipCheckbox,sCheckboxes){
	if (document.forms[0][sflipCheckbox].checked == true){
		changeCheckBoxes(sCheckboxes,true);
	}else{
		changeCheckBoxes(sCheckboxes,false);
	}
}

if(!IE4){
	var abcClientX = 0
	var abcClientY = 0

	function abcSetAbcClientX(e){
		abcClientX = e.clientX
	}

	function abcSetAbcClientY(e){
		abcClientY = e.clientY
	}
	document.addEventListener("mousemove", abcSetAbcClientX, true);
	document.addEventListener("mousemove", abcSetAbcClientY, true);
}

function abcGetClientX(){
	if(IE4){
		return window.event.clientX
	}else{
		return abcClientX 
	}	
}

function abcGetClientY(){
	if(IE4){
		return window.event.clientY
	}else{
		return abcClientY
	}	
}

function showTooltip(fArg){
	var tooltipOBJ = eval("document.all['" + fArg + "']");
	
	if('' + tooltipOBJ.innerHTML + '' != 'undefined'){
		if(tooltipOBJ.innerHTML.length > 0){
		   
			var tooltipOffsetTop = tooltipOBJ.scrollHeight + 15;
			var testTop = (document.body.scrollTop + event.clientY) - tooltipOffsetTop;
			var testLeft = event.clientX - 150;
			var tooltipAbsLft = (testLeft < 0) ? 10 : testLeft;
			var tooltipAbsTop = (testTop < document.body.scrollTop) ? document.body.scrollTop + 10 : testTop;
			if(bABCUsePopupTooltip==true){
				tooltipAbsTop = event.clientY - tooltipOffsetTop;
				oABCTooltipPopup.document.body.innerHTML = '<div class="TooltipPopup">' + tooltipOBJ.innerHTML + '</div>';
				oABCTooltipPopup.show(tooltipAbsLft,tooltipAbsTop,tooltipOBJ.offsetWidth,tooltipOBJ.offsetHeight,document.body);
			}else{
				tooltipOBJ.style.posLeft = tooltipAbsLft;
				tooltipOBJ.style.posTop = tooltipAbsTop;
				tooltipOBJ.style.visibility = "visible";
			}	
		}
	}	
}
	
function hideTooltip(fArg){
	if(bABCUsePopupTooltip==true){
		oABCTooltipPopup.hide();
	}else{
		var tooltipOBJ = eval("document.all['" + fArg + "']");
		tooltipOBJ.style.visibility = "hidden";
	}	
}

function onMouseOutABCButton(sID){
	oSelect = document.all.item(sID+'_Select');
	oNotSelect = document.all.item(sID+'_NotSelect');
	oSelect.style.zIndex = 1000;
	oNotSelect.style.zIndex = 1001;
}


function onMouseOverABCButton(sID){
	oSelect = document.all.item(sID+'_Select');
	oNotSelect = document.all.item(sID+'_NotSelect');
	oSelect.style.zIndex = 1001;
	oNotSelect.style.zIndex = 1000;
}

var sSelectedABCTabID
function onClickABCTab(sID){
	oSelect_Select = document.all.item(sID+'_Select');
	oSelect_NotSelect = document.all.item(sID+'_NotSelect');
	oNotSelect_Select = document.all.item(sSelectedABCTab+'_Select');
	oNotSelect_NotSelect = document.all.item(sSelectedABCTab+'_NotSelect');
	oNotSelect_Select.style.zIndex = 1000;
	oNotSelect_NotSelect.style.zIndex = 1001;	
	oSelect_Select.style.zIndex = 1001;
	oSelect_NotSelect.style.zIndex = 1000;
	sSelectedABCTab = sID;
}

function checkDateFormat(sDate){
	var regXDate = new RegExp(/^(\d{1,2})(\/|-)(\d{1,2})(\/|-)(\d{4})$/);
	var matchArray = sDate.match(regXDate);
	
	if (matchArray == null){
		return false;
	}else{
		day		= matchArray[1];
		month	= matchArray[3]; 
		year	= matchArray[5];
		if (month < 1 || month > 12){ 			
			return false;
		}
		if (day < 1 || day > 31){
			return false;
		}
		if ((month == 4 || month == 6 || month == 9 || month==11) && day == 31){
			return false;
		}
		if (month == 2){ 
			var isleap = (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0));
			if (day > 29 || (day==29 && !isleap)){
				return false;
			}
		}
		return true;
	}
}

function onKeyPressTextAreaAsInput(sFunction){
	if (window.event.keyCode == 13){
		window.event.returnValue = false;
		eval(sFunction);
	}
}

function updateFilenameFromPopup(oElement,sURL,lHeight,lWidth){
	var sReturnValue
	
	sReturnValue = '' + window.showModalDialog(sURL,'','dialogHeight:'+lHeight+'px;dialogWidth:'+lWidth+'px;center:yes;scroll:no;status:no;help:no')
	//sReturnValue = '' + window.open(sURL,'')
	if(sReturnValue!='' && sReturnValue!='undefined'){
		oElement.value = sReturnValue
	}
}

function updateFilenameFromPopupTemp(oElement,sURL,lHeight,lWidth){
	var sReturnValue
	
	sReturnValue = '' + window.showModalDialog(sURL,'','dialogHeight:'+lHeight+'px;dialogWidth:'+lWidth+'px;center:yes;scroll:yes;status:no;help:no')
	//sReturnValue = '' + window.open(sURL,'')
	if(sReturnValue!='' && sReturnValue!='undefined'){
		oElement.value = sReturnValue
	}
}

function abcShowDialog(sDialogID,sDialogURL){
	var lHeight
	var lWidth
	
	switch (sDialogID) {
		case 'updatefromcompetencetemplates' :
			lHeight = 360;
			lWidth = 400;
			break;
		
		case 'selectcompetencetemplateandquestiongroup':
			lHeight = 180;
			lWidth = 400;
			break;
	}		
	
	return window.showModalDialog(sDialogURL+'?DID='+sDialogID,'','dialogHeight:'+lHeight+'px;dialogWidth:'+lWidth+'px;center:yes;scroll:auto;status:no;help:no')
}

function checkIntegerInterval(lValue,lMin,lMax,sErrorText){
	if(lValue!='' ) {
		var anum = new RegExp( '^(\\d+)$' );
		if (anum.test(lValue)==false){
			alert(sErrorText);
			return false;
		}else{
			if(lValue<lMin || lValue>lMax){
				alert(sErrorText);
				return false;
			}else{
				return true;
			}
					
		}
				
	}
}

function showABCObjectPropertiesDialog(sURL){
	var sDialogProp = 'dialogHeight:340px;dialogWidth:550px;center:yes;scroll:auto;status:no;help:no';
	var sReturnvalue;
	
	sReturnvalue = window.showModalDialog(sURL,'',sDialogProp);
	
	if (''+ sReturnvalue + '' == 'reload'){
		document.location.reload(true);
	}
}

function abcGetCookie( sCookie ){
	var aCookie = document.cookie.split(";");

	for ( var i=0; i < aCookie.length; i++ ) {
		var aCrumb = aCookie[i].split( "=" );
		if ( aCrumb[0].indexOf(' ', 0) == 0 ) {
			 aCrumb[0] = aCrumb[0].substring( 1, aCrumb[0].length ); 
		}
		if ( sCookie == aCrumb[0] ){
			return unescape( aCrumb[1] );
		}	
	}
	return null;
}

function abcGetElementById(aID){ 
	var element = null; 

	element = document.getElementById(aID) 

	return element; 
}

function abcCheckElementIsObject(oElement){
	var sString
	var bReturnValue

	sString = '' + oElement + '';
	
	if(IE4){
		if(sString=='[object]' || sString=='[object Object]'){
			bReturnValue = true;
		}else{ 	
			bReturnValue = false;
		}	
	}else{
		if(sString.substring(0,8)=='[object '){
			bReturnValue = true;
		}else{ 	
			bReturnValue = false;
		}	
	}	
	return bReturnValue;
}

function abcGetabcpageReadyState(oWindow){
	var lReturnvalue = -1
	var sReturnvalue = ''

	if( abcCheckElementIsObject(oWindow) ){
		try {
			var abcpageProperty = new oWindow.abcpageProperty()
		}	
		catch (e) {
			lReturnvalue = 0
		}
		if( abcCheckElementIsObject(abcpageProperty) && lReturnvalue == -1){
			lReturnvalue =  abcpageProperty.readyState
		}else{
			lReturnvalue = 0
		}	
	}else{
		lReturnvalue = 0
	}
	
	switch (lReturnvalue) {
		case 0 :
			sReturnvalue = 'uninitialized'
			break;
		
		case 4 :
			sReturnvalue = 'complete'
			break;
			
		default :
			sReturnvalue = 'uninitialized'
			
	}		

	return sReturnvalue
}	

function abcCheckPopupBlocker(sUrl){
	var bPopupStatus = false
	
	var	objWinPopUp = window.open(sUrl,'_blank','resizable=no, scrollbars=no, toolbar=no, location=no, directories=no, status=yes, left='+(screen.availWidth-0)+',top='+(screen.availHeight-0)+',width=10,height=10')
	
	if(abcCheckElementIsObject(objWinPopUp)){
		bPopupStatus = true;
		objWinPopUp.close();
	}
	return bPopupStatus
}

function abcEncodeFilter(sFilterValue){
	var sReturnValue = ''
	
	sReturnValue = sFilterValue
	sReturnValue = escape(sReturnValue)
	sReturnValue = sReturnValue.replace(/\+/gi,'%2B')
	
	return sReturnValue 	
}
		