BASEURL='http://www.wegenwerken.be/';
DEVIATIONICONSSTEPS = 0.1; //10 points per 1 KM - zoom 15 0.1 - zoom 15->20   0.6 - zoom 12 - 15    

function debug(object){
    try{
        if(console.debug){
            console.debug(object);
        }
    }
    catch(ex){}
}


function ValidatePeriod(fromDate, toDate)
{
	if(fromDate == '' || toDate == '')
	{
		return true;
	}
	
	var fromArray=fromDate.split("-");
	var toArray=toDate.split("-");
	
	var from = new Date(fromArray[0],fromArray[1],fromArray[2]);
	var to = new Date(toArray[0],toArray[1],toArray[2]);
	
	if(from > to)
	{
		alert(MSG_START_DATE_BIGGER_THAN_END_DATE);
		return false;
	}
	return true;		
}

function isValidURL(url)
{
	var RegExp=/^[\w]+:\/\/[\w0-9-_]+[.]+[\w0-9-_%&\?\/.=]+$/;

	return RegExp.test(url);
}

var domTT_styleClass = 'domTTClassic';
function initColorPicker(){
	
	var myCP1 = dhtmlXColorPickerInput('dhtmlxColorPicker1');
	myCP1.setImagePath(BASEURL+"fw_headers/js/dhtmlx/dhtmlxColorPicker/dhtmlxColorPicker/codebase/imgs/");
	myCP1.init();
	
}
function checkInt(el)
{
	if(el.value.toString()!='')
	{
		if(el.value.toString().search(/^[0-9]+$/) != 0)
		{
			el.value='';
			el.focus();
			return 1;
		}
		else
		{
			return 0;
		}
	}
}
function ExistItemInComboBox(gridComboBox,item)
{
	var inCombo=false;
	var combo= gridComboBox;
	var options_label=combo.values;
	var options_value=combo.keys;
	for (i=0;i<combo.size();i++)
	{
		if (item==options_label[i] || item==options_value[i])
		{
			inCombo=true;
			break;
		}
	}
	return inCombo;
}
function submitForm(frm, action) {
	var id = 0;
	if (action != 'new') {
		if( !frm.selected_item.length ){
			id = frm.selected_item.value;
		}
		else{
			for ( i=0; i<frm.selected_item.length; i++ ) {
				if ( frm.selected_item[i].checked ) {
					id = frm.selected_item[i].value;
				}
			}
		}
	}
	frm.id.value=id;
	if(action=='edit'){
		if(!id){
			alert("Gelieve een selectie te maken");
			return;
		}
		frm.action=BASEURL+'users/edit/';
		frm.submit();
	}
	if(action=='delete'){
		if(!id){
			alert("Gelieve een selectie te maken");
			return;
		}
		frm.action=BASEURL+'users/del-'+id+'/';
		frm.submit();
	}
}

function addLoadEvent(func) {
	var oldonload = (typeof window.onload == 'function')?window.onload:null;
	window.onload = function() {
		if(oldonload != null && typeof oldonload == 'function') oldonload();
		eval(func);
	}
}

function checkRegister(form){
	msg="Gelieve alle verplichte velden in te vullen. :\n";
	flag=false;


	if(form.naam.value==''){
		msg+="naam\n";
		flag=true;
	}

	if(form.email.value==''){
		msg+="email\n";
		flag=true;
	}

	if(flag){
		alert(msg);
		return false;
	}
	if(!flag){
		if(!isValidEmail(form.email.value)){
			alert("Gelieve een juist email adres in te geven.");
			return false;
		}
	}
}

function isValidEmail(s){
	var m = s.match(/\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*.?/);  
	return (m != null && s == m[0]);
}

function display(id){
	for(i=1;i<4;i++){
		if(i==id){
			if(id==1){
				eval('document.getElementById("selection66").style.display=""');
			}else{
				eval('document.getElementById("selection66").style.display="none"');
			}
			eval('document.getElementById("selection'+id+'").style.display=""');
		}else{
			eval('document.getElementById("selection'+i+'").style.display="none"');
		}
		//eval('document.getElementById("selection2'+i+'").style.display="none"');
		//document.getElementById("status").value='';
	}
	if(!id){
		eval('document.getElementById("selection66").style.display="none"');
	}
}

function display2(id){
	for(i=1;i<4;i++){
		if(i==id){
			eval('document.getElementById("selection2'+id+'").style.display=""');
		}else{
			eval('document.getElementById("selection2'+i+'").style.display="none"');

			document.getElementById('selection22').style.display="";
		}
	}
}


function showHistInputs(id){
	text='<table width="100%" align="left"><tr>';
	text+='<td valign="top"><b>'+DATUM+'</b></td><td valign="top"><input type="text" id="hist_2" style="width:75px" name="instantie"> <a style=\'padding-left:5px;\' href="javascript:popUpCalendar(document.getElementById(\'hist_2\'),document.getElementById(\'hist_2\'),\'dd-mm-yyyy\');"><img src="'+BASEURL+'fw_headers/js/pop_calendar/images/calendar.gif" border="0" /></a>';
	text+='</td><td valign="top"><b>'+TEKST+'</b></td><td align="right"> <textarea name="adres" style="width:520px;" id="hist_3" ></textarea></td></tr>';
	text+='<tr><td colspan="6" align="right"><input type="button" onclick="if(!document.getElementById(\'hist_2\').value){alert(\'Please select a date\')}else{xajax_saveHist('+id+',document.getElementById(\'hist_2\').value,document.getElementById(\'hist_3\').value);}" name="save" value="'+BEWAAR+'" class="grid_button" style="height:20px;width:78px;" /> <input style="height:20px;width:78px;" type="button" name="save" value="'+VERWIJDEREN+'" onclick="xajax_deleteHistField(\''+id+'\')" class="grid_button" /> <input type="button" name="save" value="'+ANNULEREN+'" onclick="document.getElementById(\'hist_inputs\').innerHTML=\'\';" class="grid_button" style="height:20px;width:78px;" /></td></tr>';
	text+='</table>';

	document.getElementById('hist_inputs').innerHTML=text;
	document.getElementById('hist_2').focus();
	xajax_fillHistFields(id);
}


function addContactRow(id){
	tabel=document.getElementById('workForm_contacts');
	rand=document.createElement('tr');
	rand.className='grid_line';
	rand.id='contact_rand'+id;
	rand.onclick=function (){ showContactInputs(id) };
	for(i=1;i<7;i++){
		col=document.createElement('td');
		col.id='contact_'+id+i;
		col.innerHTML='&nbsp';
		rand.appendChild(col);
	}
	tabel.appendChild(rand);
	increment++;
}



function addHistRow(id){
	tabel=document.getElementById('workForm_hist');
	rand=document.createElement('tr');
	rand.className='grid_line';
	rand.id='hist_rand'+id;
	rand.onclick=function (){ showHistInputs(id) };
	for(i=2;i<4;i++){
		col=document.createElement('td');
		col.id='hist_'+id+i;
		col.innerHTML='&nbsp';
		rand.appendChild(col);
	}
	tabel.appendChild(rand);
	increment2++;
}

function checkContact(form){
	flag=true;
	if(form.email.value=='' || form.subject.value=='' || form.message.value==''){
		alert("Gelieve alle velden in te vullen!");
		return false;
	}
	if(!isValidEmail(form.email.value)){
		alert("Gelieve een juist email adres in te geven.");
		return false;
	}
	return true;
}

function checkWorkForm(form){

	try{
		form.streetName.value=document.getElementById('select_searchStreet').options[document.getElementById('select_searchStreet').selectedIndex].text;
		form.streetId.value=document.getElementById('select_searchStreet').value;
	}catch(e){
	}

	try{
		form.regStreetName.value=document.getElementById('select_searchStreetRegional').options[document.getElementById('select_searchStreetRegional').selectedIndex].text;
	}catch(e){
	}

	try{
		form.point1.value=top.frames['areaMap'].document.getElementById('selected_segments_sign_point').value;
	}catch(e){

	}

	try{
		form.segments.value=top.frames['areaMap'].document.getElementById('selected_segments_work').value;
	}catch(e){

	}
	try{
		form.segments2.value=top.frames['areaMap'].document.getElementById('selected_segments_deviation').value;
	}catch(e){

	}
	try{
		form.streets.value=top.frames['areaMap'].document.getElementById('selected_segment_streets_work').value;
	}catch(e){

	}

	if(!top.frames['areaMap'].document.getElementById('selected_segments_sign_point').value){
		alert("Gelieve een weg aan te duiden");
		return false;
	}

	try{
		fx=top.frames['areaMap'].document.getElementById('fx').value;
		fy=top.frames['areaMap'].document.getElementById('fy').value;
		if(fx && fy){
			form.coordX.value=fx;
			form.coordY.value=fy;
		}
	}catch(e){}

	var objRegExp = /^\d{2}\-\d{2}\-\d{4}$/
	if (form.geplande_begindatum.value!='dd-mm-yyyy' && !objRegExp.test(form.geplande_begindatum.value) && form.geplande_begindatum.value!='' ){
		alert("Incorrect date");
		form.geplande_begindatum.focus();
		return false;
	}

	if (form.geplande_einddatum.value!='dd-mm-yyyy' && !objRegExp.test(form.geplande_einddatum.value)  && form.geplande_einddatum.value!='' ){
		alert("Incorrect date");
		form.geplande_einddatum.focus();
		return false;
	}

	if (form.effective_begindatum.value!='dd-mm-yyyy' && !objRegExp.test(form.effective_begindatum.value) &&form.effective_begindatum.value !=''  ){
		alert("Incorrect date");
		form.effective_begindatum.focus();
		return false;
	}

	if (form.effective_einddatum.value!='dd-mm-yyyy' && !objRegExp.test(form.effective_einddatum.value) &&  form.effective_einddatum.value!='' ){
		alert("Incorrect date");
		form.effective_einddatum.focus();
		return false;
	}
	if (document.getElementById('workForm_contacts').rows.length<5)
	{
		alert("Gelieve een contactpersoon toe te voegen");
		return false;
	}

	form.submit();
}

function checkWorkForm2(form){

	try{
		form.streetName.value=document.getElementById('select_searchStreet').options[document.getElementById('select_searchStreet').selectedIndex].text;
		form.streetId.value=document.getElementById('select_searchStreet').value;
	}catch(e){
	}

	try{
		form.regStreetName.value=document.getElementById('select_searchStreetRegional').options[document.getElementById('select_searchStreetRegional').selectedIndex].text;
	}catch(e){
	}

	try{
		form.segments.value=top.frames['areaMap'].document.getElementById('selected_segments_work').value;
	}catch(e){

	}
	try{
		form.segments2.value=top.frames['areaMap'].document.getElementById('selected_segments_deviation').value;
	}catch(e){

	}
	try{
		form.streets.value=top.frames['areaMap'].document.getElementById('selected_segment_streets_work').value;
	}catch(e){

	}


	try{
		form.point1.value=top.frames['areaMap'].document.getElementById('selected_segments_sign_point').value;
	}catch(e){

	}
	var objRegExp = /^\d{2}\-\d{2}\-\d{4}$/
	if (form.geplande_begindatum.value!='dd-mm-yyyy' && !objRegExp.test(form.geplande_begindatum.value) && form.geplande_begindatum.value!='' ){
		alert("Incorrect date");
		form.geplande_begindatum.focus();
		return false;
	}

	if (form.geplande_einddatum.value!='dd-mm-yyyy' && !objRegExp.test(form.geplande_einddatum.value)  && form.geplande_einddatum.value!='' ){
		alert("Incorrect date");
		form.geplande_einddatum.focus();
		return false;
	}

	if (form.effective_begindatum.value!='dd-mm-yyyy' && !objRegExp.test(form.effective_begindatum.value) &&form.effective_begindatum.value !=''  ){
		alert("Incorrect date");
		form.effective_begindatum.focus();
		return false;
	}

	if (form.effective_einddatum.value!='dd-mm-yyyy' && !objRegExp.test(form.effective_einddatum.value) &&  form.effective_einddatum.value!='' ){
		alert("Incorrect date");
		form.effective_einddatum.focus();
		return false;
	}
	if (document.getElementById('workForm_contacts').rows.length<5)
	{
		alert("Gelieve een contactpersoon toe te voegen");
		return false;
	}
	form.submit();
}

function edCheck(elem){
	dml=document.forms['mainForm'];
	len = dml.elements.length;
	ind=elem.id.indexOf('-');
	ide=elem.id.substr(ind+1);
	var i=0;
	if(!elem.checked){
		for( i=0 ; i<len ; i++) {
			if (dml.elements[i].id.indexOf(ide)>0 && dml.elements[i].id!=elem.id) {
				dml.elements[i].disabled=false;
			}
		}
	}else{
		for( i=0 ; i<len ; i++) {
			if (dml.elements[i].id.indexOf(ide)>0 && dml.elements[i].id!=elem.id) {
				dml.elements[i].disabled=true;
			}
		}
	}
}

function LinkPopUp(link) {
	window.open(link,'link','resizable=1,menubar=1,toolbar=1,location=1,directories=1,status=1,scrollbars=1');
}

function trim(str) {
  return str.replace(/^\s+|\s+$/g,"");
}

/**
*
*  Base64 encode / decode
*  http://www.webtoolkit.info/
*
**/

var Base64 = {

	// private property
	_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

	// public method for encoding
	encode : function (input) {
		var output = "";
		var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		var i = 0;

		input = Base64._utf8_encode(input);

		while (i < input.length) {

			chr1 = input.charCodeAt(i++);
			chr2 = input.charCodeAt(i++);
			chr3 = input.charCodeAt(i++);

			enc1 = chr1 >> 2;
			enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			enc4 = chr3 & 63;

			if (isNaN(chr2)) {
				enc3 = enc4 = 64;
			} else if (isNaN(chr3)) {
				enc4 = 64;
			}

			output = output +
			this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
			this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

		}

		return output;
	},

	// public method for decoding
	decode : function (input) {
		var output = "";
		var chr1, chr2, chr3;
		var enc1, enc2, enc3, enc4;
		var i = 0;

		input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

		while (i < input.length) {

			enc1 = this._keyStr.indexOf(input.charAt(i++));
			enc2 = this._keyStr.indexOf(input.charAt(i++));
			enc3 = this._keyStr.indexOf(input.charAt(i++));
			enc4 = this._keyStr.indexOf(input.charAt(i++));

			chr1 = (enc1 << 2) | (enc2 >> 4);
			chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			chr3 = ((enc3 & 3) << 6) | enc4;

			output = output + String.fromCharCode(chr1);

			if (enc3 != 64) {
				output = output + String.fromCharCode(chr2);
			}
			if (enc4 != 64) {
				output = output + String.fromCharCode(chr3);
			}

		}

		output = Base64._utf8_decode(output);

		return output;

	},

	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++) {

			var c = string.charCodeAt(n);

			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}

		}

		return utftext;
	},

	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while ( i < utftext.length ) {

			c = utftext.charCodeAt(i);

			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}

		}

		return string;
	}

}

