// Remember the current position.
function storeCaret(text)
{
	// Only bother if it will be useful.
	if (typeof(text.createTextRange) != 'undefined')
		text.caretPos = document.selection.createRange().duplicate();
}

function replaceText(text, textarea)
{
	// Attempt to create a text range (IE).
	if (typeof(textarea.caretPos) != "undefined" && textarea.createTextRange)
	{
		var caretPos = textarea.caretPos;

		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
		caretPos.select();
	}
	// Mozilla text range replace.
	else if (typeof(textarea.selectionStart) != "undefined")
	{
		var begin = textarea.value.substr(0, textarea.selectionStart);
		var end = textarea.value.substr(textarea.selectionEnd);
		var scrollPos = textarea.scrollTop;

		textarea.value = begin + text + end;

		if (textarea.setSelectionRange)
		{
			textarea.focus();
			textarea.setSelectionRange(begin.length + text.length, begin.length + text.length);
		}
		textarea.scrollTop = scrollPos;
	}
	// Just put it on the end.
	else
	{
		textarea.value += text;
		textarea.focus(textarea.value.length - 1);
	}
}

// Surrounds the selected text with text1 and text2.
function surroundText(text1, text2, textarea)
{
	// Can a text range be created?
	if (typeof(textarea.caretPos) != "undefined" && textarea.createTextRange)
	{
		var caretPos = textarea.caretPos;

		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text1 + caretPos.text + text2 + ' ' : text1 + caretPos.text + text2;
		caretPos.select();
	}
	// Mozilla text range wrap.
	else if (typeof(textarea.selectionStart) != "undefined")
	{
		var begin = textarea.value.substr(0, textarea.selectionStart);
		var selection = textarea.value.substr(textarea.selectionStart, textarea.selectionEnd - textarea.selectionStart);
		var end = textarea.value.substr(textarea.selectionEnd);
		var newCursorPos = textarea.selectionStart;
		var scrollPos = textarea.scrollTop;

		textarea.value = begin + text1 + selection + text2 + end;

		if (textarea.setSelectionRange)
		{
			if (selection.length == 0)
				textarea.setSelectionRange(newCursorPos + text1.length, newCursorPos + text1.length);
			else
				textarea.setSelectionRange(newCursorPos, newCursorPos + text1.length + selection.length + text2.length);
			textarea.focus();
		}
		textarea.scrollTop = scrollPos;
	}
	// Just put them on the end, then.
	else
	{
		textarea.value += text1 + text2;
		textarea.focus(textarea.value.length - 1);
	}
}

function isDate(DateToCheck){
if(DateToCheck==""){return true;}
						
	var m_strDate = FormatDate(DateToCheck);
	if(m_strDate==""){
		return false;
	}
	var m_arrDate = m_strDate.split("/");
	var m_DAY = m_arrDate[0];
	var m_MONTH = m_arrDate[1];
	var m_YEAR = m_arrDate[2];
		
	if(m_YEAR.length > 4){return false;}
					
	m_strDate = m_MONTH + "/" + m_DAY + "/" + m_YEAR;
	var testDate=new Date(m_strDate);
	if(testDate.getMonth()+1==m_MONTH){
		return true;
	} 
	else{
		return false;
	}
}
				
function FormatDate(DateToFormat,FormatAs){
	if(DateToFormat==""){return"";}
	if(!FormatAs){FormatAs="dd/mm/yyyy";}

	var strReturnDate;
	FormatAs = FormatAs.toLowerCase();
	DateToFormat = DateToFormat.toLowerCase();
	var arrDate
	var arrMonths = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
	var strMONTH;
	var Separator;

	while(DateToFormat.indexOf("st")>-1){
		DateToFormat = DateToFormat.replace("st","");
	}

	while(DateToFormat.indexOf("nd")>-1){
		DateToFormat = DateToFormat.replace("nd","");
	}

	while(DateToFormat.indexOf("rd")>-1){
		DateToFormat = DateToFormat.replace("rd","");
	}

	while(DateToFormat.indexOf("th")>-1){
		DateToFormat = DateToFormat.replace("th","");
	}

	if(DateToFormat.indexOf(".")>-1){
		Separator = ".";
	}

	if(DateToFormat.indexOf("-")>-1){
		Separator = "-";
	}


	if(DateToFormat.indexOf("/")>-1){
		Separator = "/";
	}

	if(DateToFormat.indexOf(" ")>-1){
		Separator = " ";
	}

	arrDate = DateToFormat.split(Separator);
	DateToFormat = "";
	for(var iSD = 0;iSD < arrDate.length;iSD++){
		if(arrDate[iSD]!=""){
			DateToFormat += arrDate[iSD] + Separator;
		}
	}
	DateToFormat = DateToFormat.substring(0,DateToFormat.length-1);
	arrDate = DateToFormat.split(Separator);

	if(arrDate.length < 3){
		return "";
	}

	var DAY = arrDate[0];
	var MONTH = arrDate[1];
	var YEAR = arrDate[2];




	if(parseFloat(arrDate[1]) > 12){
		DAY = arrDate[1];
		MONTH = arrDate[0];
	}

	if(parseFloat(DAY) && DAY.toString().length==4){
		YEAR = arrDate[0];
		DAY = arrDate[2];
		MONTH = arrDate[1];
	}


	for(var iSD = 0;iSD < arrMonths.length;iSD++){
		var ShortMonth = arrMonths[iSD].substring(0,3).toLowerCase();
		var MonthPosition = DateToFormat.indexOf(ShortMonth);
		if(MonthPosition > -1){
			MONTH = iSD + 1;
			if(MonthPosition == 0){
				DAY = arrDate[1];
				YEAR = arrDate[2];
			}
			break;
		}
	}

	var strTemp = YEAR.toString();
	if(strTemp.length==2){

		if(parseFloat(YEAR)>40){
			YEAR = "19" + YEAR;
		}
		else{
			YEAR = "20" + YEAR;
		}

	}


	if(parseInt(MONTH)< 10 && MONTH.toString().length < 2){
		MONTH = "0" + MONTH;
	}
	if(parseInt(DAY)< 10 && DAY.toString().length < 2){
		DAY = "0" + DAY;
	}
	switch (FormatAs){
		case "dd/mm/yyyy":
			return DAY + "/" + MONTH + "/" + YEAR;
		case "dd-mm-yyyy":
			return DAY + "-" + MONTH + "-" + YEAR;
		case "mm/dd/yyyy":
			return MONTH + "/" + DAY + "/" + YEAR;
		case "dd/mmm/yyyy":
			return DAY + " " + arrMonths[MONTH -1].substring(0,3) + " " + YEAR;
		case "mmm/dd/yyyy":
			return arrMonths[MONTH -1].substring(0,3) + " " + DAY + " " + YEAR;
		case "dd/mmmm/yyyy":
			return DAY + " " + arrMonths[MONTH -1] + " " + YEAR;	
		case "mmmm/dd/yyyy":
			return arrMonths[MONTH -1] + " " + DAY + " " + YEAR;
		case "yyyymmdd":
			return YEAR + "" + MONTH + "" + DAY;
		}
		

		return DAY + "/" + strMONTH + "/" + YEAR;;

} //End Function

//test if the value is empty 
function isEmpty(s){ 
	if(s==null || s.length==0){ 
		return true; 
	} 
	else{ 
		return false; 
	}
} 
 
//test if the value only contains whitespace 
function isWhiteSpace(s){ 
	var whiteSpace ="\t\n\r "; 
	var i; 
	
	if (isEmpty(s)){
		return true; 
	}
	
	for(i=0;i<s.length;i++){ 
		var c = s.charAt(i); 
		
		if(whiteSpace.indexOf(c)==-1){
			return false; 
		}
	} 
	return true; 
} 
 
function validateEditor(naam){ 
	var oFCKeditor = FCKeditorAPI.GetInstance(naam); 
	var myText = oFCKeditor.GetXHTML(false); 
 
	if(isWhiteSpace(myText) || myText == "&nbsp;"){ 
		return false; 
	} 
	else{ 
		return true; 
	} 
}

function changeSty(classpassed, id){
	document.getElementById(id).className = classpassed;
}

function canDelete(args, vraag){
	if(window.confirm (vraag) ){
		document.location = args;
	}
}
