var main = {
	js_url : null,
	is_ie6 : false,
	site_id: null,
	captcha: '0',
	show_loader:
	function (target,msg){
		$(target).innerHTML  = '<div class="ajaxloader" style="height:40px;">'+msg+'<img style="height:20px;display:block" src="'+this.js_url+'../images/loader.gif" style="display:block;"/></div>'; 
	},
	init:
	function(site_id,ie6,js_url) {
		this.site_id = site_id;
		this.is_ie6 = ie6;
		this.js_url = js_url;
		if($('container')) {
			this.container_height = $('container').style.height;
		}
	},
	open_window:
	function open_window(name,url,extra,titel,width,height) {
		this.write_popup();
		if(titel != null) {
			$('popup_titel').innerHTML = titel;
		}
		this.set_popup_dimensie(width,height);
		Effect.Appear('popup_bg',{to:0.5});
		if(url != '') {
			new Ajax.Updater(
			'popup_content',
			url,
				{
					asynchronous:0
				}
			);
		}
		
		if(extra != 'undefined') {
			eval(extra);
		}
		
		Effect.Appear('popup_container');
	},
	set_popup_dimensie:
	function (width,height) {
		//verbeteren
		if (parseInt(navigator.appVersion)>3) {
			if (navigator.appName=="Netscape") {
			 winW = window.innerWidth;
			 winH = window.innerHeight;
			 winHeight = winH;
			}
			if (navigator.appName.indexOf("Microsoft")!=-1) {
			 winW = document.body.offsetWidth;
			 winH = document.body.offsetHeight; 
			 winHeight = document.documentElement.clientHeight;
			}
		}
		if(parseFloat(height) > parseFloat(winH)) {
			$('popup_container').style.position = 'absolute';
		} else {
			if(!this.is_ie6) {
				$('popup_container').style.position = 'fixed';
			} else {
				$('popup_container').style.position = 'absolute';
			}
		}
		
		if(width != null && height != null) {
			if(this.is_ie6) {
				$('popup_container').style.height = (parseFloat(height)+4)+'px';
				var new_width = winW -width;
				$('popup_container').style.left = new_width/2;
				$('popup_container').style.width = width+'px';
				$('popup_container').style.marginLeft = '0';
				$('popup_content').style.height = (height-32)+'px';
				$('popup_content').style.overflow = 'hidden';
			} else {
				$('popup_container').style.height =  (parseFloat(height)+4)+'px';
				$('popup_container').style.left = (0-(width/2))+'px';
				$('popup_container').style.width = width+'px';
				$('popup_container').style.marginLeft = '50%';
				$('popup_content').style.height = (height-32)+'px';
				$('popup_content').style.overflow = 'hidden';
			}
		} else if(this.is_ie6) {
			var new_width = winW - 650;
			$('popup_container').style.left = new_width/2;
			$('popup_container').style.marginLeft = '0';
		}
		var height_container = $('popup_container').style.height;
		if(this.is_ie6) {
			$('popup_bg').style.height = winH;
			$('popup_bg').style.width = winW;
			if(height_container == '') {		
				var boven = (parseFloat(winH)-400)/2;
			} else {
				var boven = (winH-parseFloat(height_container))/2;
			}
			if(boven < 0 ) {boven = 0;}
			$('popup_container').style.marginTop = boven;
		} else{
			if(height_container == '') {
				var boven = (parseFloat(winHeight)-400)/2;
			} else {
				var boven = (parseFloat(winHeight)-parseFloat(height_container))/2;
			}
			if(boven < 0 ) {boven = 0;}
			$('popup_container').style.top = boven+'px';
		}	
	},
	write_popup:
	function() {
		if($('popup_bg') == null) {
			var popup = '<div id="popup_bg" style="display:none;" ></div>';
			popup += 	'<div id="popup_container" style="display:none;" >';
			popup +=		'<div id="popup_inner">';
			popup +=		'<div id="popup_titlebar"><div id="popup_titel" ></div><a href="javascript: close_window();" >sluiten</a></div>';
			popup +=		'<div id="popup_content" >';
			popup +=	'</div>';
			popup +=	'</div>';
			popup +=	'</div>';
			//new Insertion.Top('container',popup);
			new Insertion.Before('container',popup);
		}
	},
	popup:
	function (url,popup_name,width,height) {
		window.open(url,'name','height='+height+',width='+width);
		return false;
	},
	checkMail:
	function (x) {
		var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if (filter.test(x)) {
			 return true;
		} else{
			return false;
		}
	},
	submitform:
	function (formname,extraaction,typesubmit,sluiten,target) {
		//var url = live_site+'ajaxcallback.php?'+Form.serialize(formname);
		var url = this.js_url+'ajaxcallback.php';
		var params = Form.serialize(formname)
		switch(typesubmit) {
			case'updater':
				new Ajax.Updater(
				target,
				url, 
					{
						asynchronous:0,
						parameters:params
					}
				)
			break;
			default:
			case 'request':
				new Ajax.Request(
				url, 
					{
						asynchronous:0,
						parameters:params
					}
				)
			break;
		}
		if(extraaction != '') {
			eval(extraaction)
		}
		if(sluiten) {
			setTimeout('',2000);
			close_window ('pop_up');
		}
		return false;
	},
	HexToR:
	function (h) {
		return parseInt((this.cutHex(h)).substring(0,2),16)
	},
	HexToG:
	function (h) {
		return parseInt((this.cutHex(h)).substring(2,4),16)
	},
	HexToB:
	function (h) {
		return parseInt((this.cutHex(h)).substring(4,6),16)
	},
	cutHex:
	function (h) {
		return (h.charAt(0)=="#") ? h.substring(1,7):h
	}
}


//TODO omzetten naar main. en goed checke of alles omgaat
function get_ap_content(target,veldnr,pagina,site_id,live_site) {
	//$(target).innerHTML = '';
	new Ajax.Updater(
		target,
		live_site+'ajaxcallback.php?action=get_ap_content&veldnr='+veldnr+'&pagina='+pagina+'&site_id='+site_id,
		{
			asynchronous:1
		}
	)
}

function get_vp_content(target,veldnr,pagina,site_id,live_site) {
//	$(target).innerHTML = '';
	var url = live_site+'ajaxcallback.php?action=get_vp_content&veldnr='+veldnr+'&pagina='+pagina;
	new Ajax.Updater(
		target,
		url,
		{
			asynchronous:1
		}
	)
}

function get_nieuws_content(target,pagina,site_id,live_site) {
//	$(target).innerHTML = '';
	var url = live_site+'ajaxcallback.php?action=get_nieuws_content&pagina='+pagina;
	new Ajax.Updater(
		target,
		url,
		{
			asynchronous:1
		}
	)
}
function get_formulier(live_site,formid,siteid,target) {
	var url = live_site+'ajaxcallback.php?action=get_formulier&formid='+formid+'&site_id='+siteid;
	new Ajax.Updater(
		target,
		url,
		{
			asynchronous:1
		}
	)
	
}
function closewindow (name,opschonen) {	
	if($(name+'_inner').style.display == 'block' || $(name+'_inner').style.display == '') {
		Effect.Fade(name+'_inner');
		if(opschonen == true) {
			setTimeout( "$('"+name+"_content').innerHTML = '' ;",1000);
		}
		//$(name+'_inner').style.display = 'none';

	}
}

function openwindow (name,url,extra,breedte,hoogte,title) {	
	if(breedte == null || breedte == 0 || breedte == '' ) { breedte = '460';}
	if(hoogte == null || hoogte == 0 || hoogte == '') { hoogte = '250';}
	$(name+'_inner').style.width = breedte;
	$(name+'_inner').style.height = hoogte;
	Effect.Appear(name+'_inner', { duration: 1.5 ,asynchronous:0});
	if(url != '') {
		if($('pop_up_inner').style.opacity == 0.999999 && $('pop_up_inner').style.display != 'none') {
			new Ajax.Updater(name+'_content',url,{asynchronous:0});
		} else {
			setTimeout( "new Ajax.Updater('"+name+"_content','"+url+"',{asynchronous:0})", 1000 );
		}
	}
	if(extra != null && extra != '') {
		setTimeout("eval('"+extra+"')", 2000);
	}
	if(title == null && title != '') {
		$('pp_titlebar').innerHTML = '&nbsp;&nbsp;sleep';
	} else {
		$('pp_titlebar').innerHTML = title;
	}
	new Draggable(name+'_inner');
}

//oude popup
function write_popup() {
	if($('pop_up_inner') == null) {
		//new Insertion.Top('container','<div id="pop_up_inner" style="position:absolute;z-index:99;background:white;height:250px;width:460px;display:none;border:2px solid #ccc;margin:200px 0px 0px 250px;"><div id="pop-up-toolbar" style="width:100%;height:20px;line-height:20px;background:#cc3433;color:black;text-align:center"  " ><b><span onclick="closewindow(\'pop_up\',true)"  onMouseover="$(\'pop-up-toolbar\').style.cursor =\'pointer\' " onMouseout="$(\'pop-up-toolbar\').style.cursor =\'default\'" style="color:white" >sluiten</span></b></div><div id="pop_up_content" style="text-align:center" ></div></div>');
		new Insertion.Top('container','<div id="pop_up_inner" style="position:absolute;z-index:99;display:none;" ><div id="pop-up-toolbar"><span id="pp_titlebar" >sleep</span><span onclick="closewindow(\'pop_up\',true)" id="pp_sluit" >sluiten</span></div><div id="pop_up_content" ></div></div>');
	}
}
function writepopup(ie6) {
	//if($('popup_container') == null) {
		//new Insertion.Top('<div id="popup_bg" style="display:none;" ></div><div id="popup_container" style="display:none;" ><div id="popup_inner"><div id="popup_titlebar"><div id="popup_titel" ></div><a href="javascript: close_window();" >sluiten</a></div><div id="popup_content" ></div></div><input type="hidden" name="is_ie6" id="is_ie6" value="'+ie6+'" /></div>');
	//}
}

function submitform(live_site,formname,extraaction,typesubmit,sluiten,target,checkfunction) {
	var url = live_site+'ajaxcallback.php?'+Form.serialize(formname);
	
	checker = true;
	if(checkfunction != null ) {
		checker = checkfunction+'()';
	}
	if(eval(checker)) {
		switch(typesubmit) {
		default:
		case'updater':
			new Ajax.Updater(
			target,
			url, 
				{
					asynchronous:0
				}
			)
		break;
		case 'request':
			new Ajax.Request(
			url, 
				{
					asynchronous:0
				}
			)
		break;
		}
		if(extraaction != '') {
			eval(extraaction)
		}
		if(sluiten) {
			setTimeout('',2000);
			closewindow ('pop_up');
		}
	}
	return false;
}

function checkMail(x)
{
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(x)) {
		 return true;
	} else{
		return false;
	}
}

function print_form(live_site,formid,siteid) {
	var form_result = Form.serialize('formulier').replace('action','actionn');
	var url = live_site+'index2.php?action=print_form&formid='+formid+'&siteid='+siteid+'&'+form_result;
	window.open(url);
	return false;
}

//nieuwe popup
function open_window(name,url,extra,titel) {
	var is_ie6 = $F('is_ie6');
	if(titel != null) {
		$('popup_titel').innerHTML = titel;
	}
	set_popup_dimensie(is_ie6);
	Effect.Appear('popup_bg',{to:0.5});
	Effect.Appear('popup_container');
	if(url != '') {
		new Ajax.Updater(
		'popup_content',
		url,
			{
				asynchronous:0
			}
		);
	}
	
	if(extra != 'undefined') {
		eval(extra);
	}
}

function close_window (name,opschonen) {
	Effect.Fade('popup_bg',{from:0.5});
	Effect.Fade('popup_container');
	if(opschonen == true) {
		$('popup_content').innerHTML = '';
	}
}

function set_popup_dimensie(ie6) {
	if (parseInt(navigator.appVersion)>3) {
		if (navigator.appName=="Netscape") {
		 winW = window.innerWidth;
		 winH = window.innerHeight;
		 winHeight = winH;
		}
		if (navigator.appName.indexOf("Microsoft")!=-1) {
		 winW = document.body.offsetWidth;
		 winH = document.body.offsetHeight; 
		 winHeight = document.documentElement.clientHeight;
		}
	}
	if(ie6) {
		$('popup_bg').style.height = winH;
		$('popup_container').style.marginTop = '200px';
	} else{
		var boven = (parseFloat(winHeight)-400)/2;
		$('popup_container').style.top = boven+'px';
	}	
}

var Url = {

 	// public method for URL encoding
 	encode : function (string) {
 		 var new_string = escape(this._utf8_encode(string));
 		 new_string = new_string.replace(/@/,"%40");
 		 return new_string;
 	},

 	// public method for URL decoding
	 decode : function (string) {
 	 	return this._utf8_decode(unescape(string));
 	},

 	// 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;
	 }
}