//loadscript();

/*ausgabe von new un updated status*/
function printStatus(createTime, updateTime, timePoint) {
			var printNew = false;
			var printUpdate = false;
			var resNew = createTime - timePoint;
			var resUpdate = updateTime - timePoint;

			if( resNew > 0) printNew = true;
			if( resUpdate > 0) printUpdate = true;
			if(printUpdate){
					document.write( " <i style=\"color:#bb0000; text-decoration: none; vertical-align: top; font-size: 100%;\"> - UPDATED - </i> " );
			}else{
				if (printNew){
					document.write( " <i style=\"color:#bb0000; text-decoration: none; vertical-align: top; font-size: 100%;\"> - NEW - </i> " );
				}
			}

		}


function loadscript(){

 alert("loading!");
}

function testAlert(){

 alert("test!")   ;
}



function popup(w,h,site) {
			x=screen.availWidth/2-w/2;
			y=screen.availHeight/2-h/2;
			var popupWindow=window.open('','','width='+w+',height='+h+',left='+x+',top='+y+',screenX='+x+',screenY='+y);
			popupWindow.document.write(site);
		}

function checkTechnicalRequirements(userRole) {
    //alert("checkTechnicalRequirements***********++");
// (1) browser vendor:
//     is_net, is_moz, is_ie, is_opera, is_safari
// (2) browser vendor and version number
//     is_net7up, is_moz1_5up
//     is_ie5up
//     is_opera7up
// (3) OS platform and version:
//     is_win
//     is_mac

    // convert all characters to lowercase to simplify testing
    var agt=navigator.userAgent.toLowerCase();
    //alert("agent = "+agt);


    // *** BROWSER VERSION ***

    // netscape 7+ ???
    var is_net7up = false;
    var is_net  = ((agt.indexOf('netscape')!=-1) && (agt.indexOf('opera')==-1));
    var index_net = agt.indexOf('netscape/');
    if (index_net != -1) {
	    var version_substring_net = agt.substring((index_net+9),(index_net+12));
	    is_net7up = (is_net && (parseInt(version_substring_net) >= 7));
    }
    //alert("is netscape 7+: "+is_net7up);


    // mozilla 1,5+ ???
    var is_moz1_5up = false;
    var is_moz  = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('opera')==-1));
    var index_moz = agt.indexOf('rv:');
    if (index_moz != -1) {
	    var version_substring_moz = agt.substring((index_moz+3),(index_moz+6));
	    is_moz1_5up = (is_moz && (parseFloat(version_substring_moz) >= 1,5) && !is_net);
    }
    //alert("is mozilla 1.5+: "+is_moz1_5up);


    // IE 5+ ???
    var is_ie5up = false;
    var is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
    var index_ie = agt.indexOf('msie');
    if (index_ie != -1) {
	    var version_substring_ie = agt.substring((index_ie+5),(index_ie+8));
	    is_ie5up  = (is_ie && (parseInt(version_substring_ie) >= 5));
    }
    //alert("is ie 5+: "+is_ie5up);


    // opera 7+ ???
    var is_opera7up = false;
    var is_opera = (agt.indexOf("opera") != -1);
    var index_opera = agt.indexOf('opera');
    if (index_opera != -1) {
	    var version_substring_opera = agt.substring((index_opera+6),(index_opera+9));
	    is_opera7up = (is_opera && (parseInt(version_substring_opera) >= 7));
    }
    //alert("is opera 7+: "+is_opera7up);


    // safari ?+ ???
    var is_safari = (agt.indexOf("safari") != -1);



    // *** OPERATING SYSTEM ***
    var is_win = (agt.indexOf("win") != -1);
    //alert("is windows: "+is_win);
    var is_mac = (agt.indexOf("mac") != -1);
    //alert("is mac: "+is_mac);



    // *** PLUG-INS ***

   // initialize variables
   var detectableWithVB = false;
   var pluginFound = false;

   // Here we write out the VBScript block for MSIE Windows
   if ((is_ie && is_win) || (is_opera && is_win && (agt.indexOf("msie") != -1))) {
	    document.writeln('<script language="VBscript">');

	    document.writeln('\'do a one-time test for a version of VBScript that can handle this code');
	    document.writeln('detectableWithVB = False');
	    document.writeln('If ScriptEngineMajorVersion >= 2 then');
	    document.writeln('  detectableWithVB = True');
	    document.writeln('End If');

	    document.writeln('\'this next function will detect most plugins');
	    document.writeln('Function detectActiveXControl(activeXControlName)');
	    document.writeln('  on error resume next');
	    document.writeln('  detectActiveXControl = False');
	    document.writeln('  If detectableWithVB Then');
	    document.writeln('     detectActiveXControl = IsObject(CreateObject(activeXControlName))');
	    document.writeln('  End If');
	    document.writeln('End Function');

	    document.writeln('\'and the following function handles QuickTime');
	    document.writeln('Function detectQuickTimeActiveXControl()');
	    document.writeln('  on error resume next');
	    document.writeln('  detectQuickTimeActiveXControl = False');
	    document.writeln('  If detectableWithVB Then');
	    document.writeln('    detectQuickTimeActiveXControl = False');
	    document.writeln('    hasQuickTimeChecker = false');
	    document.writeln('    Set hasQuickTimeChecker = CreateObject("QuickTimeCheckObject.QuickTimeCheck.1")');
	    document.writeln('    If IsObject(hasQuickTimeChecker) Then');
	    document.writeln('      If hasQuickTimeChecker.IsQuickTimeAvailable(0) Then ');
	    document.writeln('        detectQuickTimeActiveXControl = True');
	    document.writeln('      End If');
	    document.writeln('    End If');
	    document.writeln('  End If');
	    document.writeln('End Function');

	    document.writeln('</scr' + 'ipt>');
   }


    // flash plug-in
    var flashMssg = detectFlash("http://www.macromedia.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash");

    // real media plug-in
    var realMssg = detectReal("http://www.real.com/?lang=en&loc=us");




    // *** RECOMMENDED ***

    // for a student:
    if(userRole == "student" || userRole == "guest") {
    	    var mssgStudent = "";
	    if(!is_ie5up && !is_moz1_5up && !is_net7up && !is_opera7up && !is_safari) {
	    	mssgStudent = "<b>Recommended Browsers:</b><ul><li>Internet Explorer 5+</li><li>Mozilla 1.5+</li><li>Safari</li><li>Netscape 7+</li><li>Opera 7+</li></ul><br>";
	    }
	    return mssgStudent+flashMssg+realMssg;
    }
    // for an editor:
    else if(userRole == "editor") {
            var mssgEditor = "";
	    if(is_win) {
	    	if(!is_moz1_5up && !is_ie5up &&!is_net7up) {
	    		mssgEditor = "<b>For task editing with WYSIWYG the following browser is necessary: &nbsp;</b><ul><li>Internet Explorer 5+</li><li>Mozilla 1.5+</li><li>Netscape 7+</li></ul><br>";
	    	}
	    }
	    else if(is_mac) {
	    	if(!is_moz1_5up) {
	    		mssgEditor = "<b>For task editing with WYSIWYG the following browser is necessary:</b> Mozilla 1.5+<br><br>";
	    	}
	    }
	    else {
	    }
            return mssgEditor+flashMssg+realMssg;
    }


} // checkBrowserRequirements(userRole)


function downloadCheck(pluginFound, message) {
    // check for download
    if(!pluginFound) {
	return message+"<br><br><br>";
    }
    else {
    	return "";
    }
}


function detectFlash(downloadLink) {
    pluginFound = detectPlugin('Shockwave','Flash');
    // if not found, try to detect with VisualBasic
    if(!pluginFound && detectableWithVB) {
	pluginFound = detectActiveXControl('ShockwaveFlash.ShockwaveFlash.1');
    }
    // check for download
    return downloadCheck(pluginFound, "<b><a href='"+downloadLink+"' target='blank'>Download</a> the Flash plug in for viewing Resources (animations) and German courses.</b>");
}


function detectReal(downloadLink) {
    pluginFound = detectPlugin('RealPlayer');
    // if not found, try to detect with VisualBasic
    if(!pluginFound && detectableWithVB) {
	pluginFound = (detectActiveXControl('rmocx.RealPlayer G2 Control') ||
		       detectActiveXControl('RealPlayer.RealPlayer(tm) ActiveX Control (32-bit)') ||
		       detectActiveXControl('RealVideo.RealVideo(tm) ActiveX Control (32-bit)'));
    }
    // check for download
    return downloadCheck(pluginFound, "<b><a href='"+downloadLink+"' target='blank'>Download</a> the Real Media plug in for viewing Resources (videos).</b>");
}


function detectPlugin() {
    // allow for multiple checks in a single pass
    var daPlugins = detectPlugin.arguments;
    // consider pluginFound to be false until proven true
    var pluginFound = false;
    // if plugins array is there and not fake
    if (navigator.plugins && navigator.plugins.length > 0) {
	var pluginsArrayLength = navigator.plugins.length;
	// for each plugin...
	for (pluginsArrayCounter=0; pluginsArrayCounter < pluginsArrayLength; pluginsArrayCounter++ ) {
	    // loop through all desired names and check each against the current plugin name
	    var numFound = 0;
	    for(namesCounter=0; namesCounter < daPlugins.length; namesCounter++) {
		// if desired plugin name is found in either plugin name or description
		if( (navigator.plugins[pluginsArrayCounter].name.indexOf(daPlugins[namesCounter]) >= 0) ||
		    (navigator.plugins[pluginsArrayCounter].description.indexOf(daPlugins[namesCounter]) >= 0) ) {
		    // this name was found
		    numFound++;
		}
	    }
	    // now that we have checked all the required names against this one plugin,
	    // if the number we found matches the total number provided then we were successful
	    if(numFound == daPlugins.length) {
		pluginFound = true;
		// if we've found the plugin, we can stop looking through at the rest of the plugins
		break;
	    }
	}
    }
    return pluginFound;
} // detectPlugin

function google(){

	var fenster = window.open("", "neu", "width=800,height=640,top=10,left=30,status=yes,scrollbars,locationbar=yes,menubar=yes,resizable=yes");
	var qq = document.getElementById('search').value;
//		alert(qq);
	if (fenster != null){
	      with (fenster.document){
	         open();
	         write("<html><body onload=\"f.submit()\">Please wait for a sconde the request is sending.<form action=\"http://www.google.de/search\" name=f><input maxLength=256 size=55 name=q value=\"www.distributed-campus.org: "+qq+"\"><input type=hidden name=ie value=\"UTF-8\"><input type=hidden name=oe value=\"UTF-8\"><input name=hl type=hidden value=de></form></body></html>");
	         close();
	      }
	   }

	//googlefenster = window.open('http://www.google.de?q='+qq,'','width=800,height=640,top=10,left=10,status=yes,scrollbar=yes');
	//f.submit();

}


function translate(){
	var qq = document.searchTranslateForm.searchtranslate.value;

	var myurl = "http://dict.leo.org/?search="+qq;

	var fenster = window.open(myurl, "neu", "width=800,height=640,top=10,left=30,status=yes,scrollbars,locationbar=yes,menubar=yes,resizable=yes");


}

if (typeof jQuery != "undefined") {
	
jQuery.noConflict();
var current = { form : null, location : null, do_submit : false };
var pattern_img_extensions = /(.jpg|.jpeg|.gif|.png)$/i;

jQuery(function () {
	
	jQuery("table").delegate('.preview', 'click', function(e){
			// your .live code here
			var thelink = jQuery(this);	
			var position = thelink.position();
			if (!pattern_img_extensions.test(thelink.attr('href'))) 
			   return true;		
			e.preventDefault();	
			var dialog1 = jQuery('<div></div>')	
			    .dialog({
				   autoOpen: false,
				   title:  thelink.attr('href') ,
				   width: 300,
				   height: 200,
				   autoOpen: false,
				   modal: false,
				   position : [position.left, position.top - 100],
				   buttons : {
					  "Ok" : function(e){
						jQuery(this).dialog('close');
					  },
					}
			   });

			var img1 = jQuery('<img />', {
			   src: thelink.attr('href'),
			   id : thelink.text(),
			   height : 100  +"%"
			});				
			img1.css('width','auto');
			dialog1.append(img1);
			//dialog1.dialog( "option", "title", thelink.attr('title') + thelink.attr('href') +width );
			dialog1.dialog('open') ;					
			return false;
	});
	
	jQuery("form,table").delegate('.submit_dlt', 'click', function(e){
			// your .live code here
			e.preventDefault();

			var thelink = jQuery(this);			
			//current.form =thelink.parents('form');
			current.location = thelink.attr('href');
			
			var dialog1 = jQuery('<div></div>')	
			   //.load(thelink.attr('href'))
			   .dialog({
				    autoOpen: false,
				    title: thelink.attr('title'),
				    width: 300,
				    height: 200,
				    autoOpen: false,
					modal: true,
					buttons : {
					  "Confirm" : function(e){
						current.do_submit = true;
						jQuery(this).dialog('close');
					  },
					  "Cancel"  : function(e){
						 current.do_submit = false;
						 jQuery(this).dialog('close');
					  }
					}
			   }).bind('dialogbeforeclose', function(){
					if(current.do_submit){
					  //(current.form).submit();
					  //current.form = null;
					  current.do_submit = false;
					  location.href = current.location; 
					}
			   });

			dialog1.dialog('open');
			return false;
	});
	
	jQuery('a[href*=#]').click(function(event){
		var elemId = '#' + jQuery(this).attr('href').split('#')[1];
		if (!jQuery(this).attr('href').split('#')[0]) {
			//alert(location.href +elemId);
			event.preventDefault();
			location.href = location.href +elemId
		}
	});

	
/*
 *  // add class="submit_dlt into input field. e.g. taskeingabe. problem: custom js setting form action and no default form fields...
	var currentBtn = { form : null, do_submit : false };
	jQuery("form,table").delegate('.submit_dlt', 'click', function(e){
			// your .live code here
			e.preventDefault();

			var theInputField = jQuery(this);			
			currentBtn.form =theInputField.parents('form');
			
			var dialog1 = jQuery('<div></div>')	
			   .dialog({
				    autoOpen: false,
				    title: thelink.attr('alt'),
				    width: 300,
				    height: 200,
				    autoOpen: false,
					modal: true,
					buttons : {
					  "Confirm" : function(e){
						currentBtn.do_submit = true;
						jQuery(this).dialog('close');
					  },
					  "Cancel"  : function(e){
						 currentBtn.do_submit = false;
						 jQuery(this).dialog('close');
					  }
					}
			   }).bind('dialogbeforeclose', function(){
					if(currentBtn.do_submit){
					  (currentBtn.form).submit();
					  currentBtn.form = null;
					  currentBtn.do_submit = false;
					}
			   });

			dialog1.dialog('open');
			return false;
	});*/
	

});

}


// deprecated
function confirmDelete(messageText, itemName) {
	var ok = confirm(messageText+'\"'+itemName+'\"');
	if(ok)
		return true;
	else
		return false;
}
// deprecated
function confirmDelete(messageText) {

	var ok = confirm(messageText);
	if(ok)
		return true;
	else
		return false;
}


function checkUsername(username) {
  	var usernameReg = /[\s]/; //whtiespaces

  	if(username.length < 8) {
  		alert("Username length has to be 8 characters");
  		return false;
  	}
  	if(usernameReg.test(username)) {
  		alert("invalid user name");
  		return false;
  	}
  	usernameReg = /[^\w\.\@]/; // alphanumeric including underscore, dot and at
  	if(usernameReg.test(username)) {
  		alert("invalid user name");
  	  	return false;
  	}

  	return true;

}

//?berpr?ft eingabe mehrerer emailadressen getrennt durch semikolon
function checkMailAdresses(manualEMail) {
 //   alert("checkMailAdresses")

  var usr    = "([a-zA-Z0-9][a-zA-Z0-9_.-]*|\"([^\\\\\x80-\xff\015\012\"]|\\\\[^\x80-\xff])+\")";
  var domain = "([a-zA-Z0-9][a-zA-Z0-9._-]*\\.)*[a-zA-Z0-9][a-zA-Z0-9._-]*\\.[a-zA-Z]{2,5}";
  //var regex  = "^" + proto + "?" + usr + "\@" + domain + "$";
    //var regex  = "^/" + usr + "\@" + domain + "/+$";
    var regex  = "^(" + usr + "\@" + domain + "){1}(;" + usr + "\@" + domain + ";?)*$";

  var rgx    = new RegExp(regex);
  //if(!rgx.exec(manualEMail) ){   }
  //alert(rgx.exec(manualEMail) );
  return rgx.exec(manualEMail) ? true : false;

}



function checkPassword(passwordValue) {
	//var passwordReg1 = /[A-Z]/;
	//var passwordReg2 = /\W/;


  	if(passwordValue.length < 8) {
  		alert("Password length has to be 8 characters");
  		return false;
  	}
	if(passwordValue.length > 32) {
  		alert("Password length could not exceed 32 characters");
  		return false;
  	}
  	//if(!passwordReg1.test(passwordValue) && !passwordReg2.test(passwordValue)) {
    	//	alert("password has to contain an uper-case letter and a special character");
    	//	return false;
  	//}

  	pwReg = /[\s\t\v]/;  //
  	if(pwReg.test(passwordValue)) {
  		alert("invalid password: No spaces allowed.");
  		return false;
  	}
    return true;
}

function checkEmail(emailValue) {
  	var emailReg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
  	var emailReg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid

  	if (emailReg1.test(emailValue) || !emailReg2.test(emailValue)) {
  		alert("invalid email adress");
  		return false;
  	}
  	return true;
}

// Date Format: MM DD YYYY
function checkDateMMDDYYYY(month, day, year, message) {
	var monthReg = /^[0-9]{2}$/;
	var dayReg = /^[0-9]{2}$/;
	var yearReg = /^[0-9]{4}$/;

	if(!monthReg.test(month) || !dayReg.test(day) || !yearReg.test(year)) {
		alert(message);
		return false;
	}
	else {
		if((parseInt(month) > 12) || (parseInt(day) > 31)){
			alert(message);
			return false;
		}
	}
	return true;
}



// Date Format: MM.DD
function checkDateMMDD(dateValue, message) {
	var dateReg = /^[0-9]{2}\.[0-9]{2}$/;

	if(!dateReg.test(dateValue)) { // not valid
		alert(message);
		return false;
	}
	else {
		//var month = dateValue.substring(0,2);
		//if(parseInt(month) > 12) {
		//	alert(message);
		//	return false;
		//}
	}
	return true;
}

// compares two dates
// returns:
//         -1 if the second date is before the first date
//          0 if the dates are the same
//          1 if the second date is after the first date
function compareDates(month1, day1, year1, month2, day2, year2) {

	alert( month1+"  "+day1+"  "+year1);

	if(year2 < year1)

		return -1;


	else if(year2 == year1) {
		if(month2 < month1)

			return -1;
		else if(month2 == month1) {
			if(day2 < day1)
				return -1;
			else if(day2 == day1)
				return 0;
			else
				return 1;
		}
		else
			return 1;
	}

	else
		return 1;

}

// day/month/year
// compares two dates
// returns:
//         -1 if the second date is before the first date
//          0 if the dates are the same
//          1 if the second date is after the first date
function vergleicheDatum(day1, month1, year1, day2, month2, year2) {
	
	var MonthMap = {'Jan': 0, 'Feb': 1, 'Mar': 2, 'Apr':3, 'May': 4, 'Jun':5, 'Jul':6, 'Aug':7, 'Sep':8, 'Oct':9, 'Nov':10, 'Dec':11 };
	
	month1 = MonthMap[month1];
	month2 = MonthMap[month2];

	//alert( month1+"  "+day1+"  "+year1+"   2: "+month2+"  "+day2+"  "+year2 );
	if(year2 < year1)
		return -1;
	else if(year2 == year1) {
		if(month2 < month1)

			return -1;
		else if(month2 == month1) {
			if(day2 < day1)
				return -1;
			else if(day2 == day1)
				return 0;
			else
				return 1;
		}
		else
			return 1;
	}

	else
		return 1;

}


function checkImageEnding(imagePath) {
	var imageReg = /(\.jpg)$|(\.jpeg)$|(\.gif)$|(\.JPG)$|(\.JPEG)$|(\.GIF)$/;
	if(imageReg.test(imagePath))
		return true;
	else {
		alert("please select a JPEG or a GIF image");
		return false;
	}
}

/*
prueft eingabe zeitraum (start,ende) auf korrekte formatiereung und das start vor ende liegt (chronologisch)
*/
function checkPeriodEntry(start, ende){
    var returnValue = true;
    //alert(" eingeben: "+start.value+" "+ende.value)
    if (!checkDate(start.value)) returnValue = false;
    if (!checkDate(ende.value)) returnValue = false;

    if (returnValue) returnValue = liegtStartVorEndeDatum(start.value,ende.value) ;
    return returnValue;
}

// format day/month/year
function liegtStartVorEndeDatum(start,ende){

    var startA = start.split("/");
    var endA = ende.split("/");

    if ( vergleicheDatum(startA[0], startA[1], startA[2], endA[0], endA[1], endA[2]) == 1) return true;
    else { alert("The start date "+start+" has to be before the end date "+ende+" .")}
    //alert("result:"+compareDates(startA[0], startA[1], startA[2], endA[0], endA[1], endA[2]))   //return vergleicheDatum(startA[0], startA[1], startA[2], endA[0], endA[1], endA[2]);
    return false;


}

//prueft datumseingabe auf format: mm/dd/yyyy
function checkDate(dateValue)
{
    //alert("checkdate")

    var dateReg = /^\d{1,2}(\-|\/|\.)\w{1,3}\1\d{4}$/;

	if(!dateReg.test(dateValue)) { // not valid
		alert("The date format is not valid: "+dateValue);
		return false;
	}
	else {
		//var month = dateValue.substring(0,2);
		//if(parseInt(month) > 12) {
		//	alert(message);
		//	return false;
		//}
	}
	return true;
}

