var new_menu_item_index = 1;
var deleted = new Array();

jQuery(function(){
	initTitleLinks();
	jQuery("#accordion").accordion({clearStyle : true, active: false });
});

function initTitleLinks(){
	var titles = jQuery("div.acc div.title:not(.title_nolink)");
	titles.bind('click', function(){
		var a = jQuery(this).children("a");
		var link = a.attr("href");
		if(link != undefined){
			window.location.href = link; 
			return false;
		}
	});
	titles.hover( 
			function(){ jQuery(this).addClass("hover"); }, 
			function(){ jQuery(this).removeClass("hover"); } 
	);
}


function refresh_menu(menu) {
	window.location.href=window.location.href;
}

function submit_menu_changes(parent_id, type) {
	var list = $(type + '_menu_list_edit');
	var list_postvars = serialize_menu_list(list);
	
	
	Droppables.remove(type + '_menu_ajax_recycle_bin');
	
	new Ajax.Request('index.php?m=Menu.edit', {
		method: 'post',
		evalScripts: true,
		parameters: '&type=' + type + '&parent_id=' + parent_id + '&action=save&' + list_postvars,
		onComplete: function(transport) {
			$('modal_container').update();
			$('modal_container').update(transport.responseText);
		}
	});
}

function serialize_menu_list(list) {
	var result = "";
	
	var elements = list.immediateDescendants();
	
	for (i=0; i<elements.size(); i++) {
		if (elements[i].id.substring(0,3) == 'new') {
			result += "item_order[" + i + "]=-1&";
			result += "item_name[-1]=" + escape($F('new_item_name')) + "&";			
		} else if (elements[i].id.substring(0,4) == 'menu'){
			id = elements[i].id.substring(10);
			result += "item_order[" + i + "]=" + id + "&";
			result += "item_name[" + id + "]=" + escape($F('item_name_' + id)) + "&";
		}
	}	
	
	for (i=0; i < deleted.length; i++) {
		result += "deleted=" + deleted + "&";
	}
	
	return result;
}

function infi_submit_content(id) {
	new Ajax.Request('index.php?m=Content.edit', {
		method: 'post',
		evalScripts: true,
		parameters: '&id=' + id + '&action=save&content_text='
				+ escape(tinyMCE.getContent('content_text_'+id))
				+ '&content_subtitle=' + escape($F('content_subtitle_'+id))
				+ '&content_wide=' + (jQuery('#content_wide_'+id).attr('checked')?1:0)
				+ '&content_modal=' + (jQuery('#content_modal_'+id).attr('checked')?1:0), 
		onComplete: function(transport) {
			window.location.href=window.location.href;
		}
	});
	return false;
}
function infi_submit_news(id) {
	new Ajax.Request('index.php?m=News.edit', {
		method: 'post',
		evalScripts: true,
		parameters: '&action=save&id=' + id
				+ '&news_title=' + escape($F('news_title_'+id))
				+ '&news_content=' + escape(tinyMCE.getContent('news_content_'+id))
				+ '&news_show_in_ticker=' + (jQuery('#news_show_in_ticker_'+id).attr('checked')?1:0), 
		onComplete: function(transport) {
			window.location.href=window.location.href;
		}
	});
		
	return false;
}
function infi_submit_inspiraties(id) {
	new Ajax.Request('index.php?m=Inspiraties.edit', {
		method: 'post',
		evalScripts: true,
		parameters: '&id=' + id +
				'&action=save&inspiratie_content=' + escape(tinyMCE.getContent('inspiratie_content_'+id)) +
				'&inspiratie_title=' + escape($F('inspiratie_title_' + id)),
		onComplete: function(transport) {
			window.location.href=window.location.href;
		}
	});
	return false;
}
function infi_submit_opdrachtgever(id) {
	new Ajax.Request('index.php?m=Opdrachtgever.edit', {
		method: 'post',
		evalScripts: true,
		parameters: '&oid=' + id +
			'&action=save&content=' + escape(tinyMCE.getContent('editor_content_'+id)) +
			'&name=' + escape($F('editor_name_'+id)),
		onComplete: function(transport) {
			window.location.href=window.location.href;
		}	
	});
}
function infi_submit_werkterrein(id) {
	new Ajax.Request('index.php?m=Werkterrein.edit', {
		method: 'post',
		evalScripts: true,
		parameters: '&id=' + id +
			'&action=save' +
			'&name=' + escape($F('editor_name_'+id)) +
			'&content=' + escape(tinyMCE.getContent('editor_content_'+id)),
		onComplete: function(transport) {
			window.location.href=window.location.href;
		}	
	});
}
function infi_submit_case(id) {
	new Ajax.Request('index.php?m=Caseview.edit', {
		method: 'post',
		evalScripts: true,
		parameters: '&case_id=' + id +
			'&action=save&content=' + escape(tinyMCE.getContent('editor_content_'+id)) +
			'&name=' + escape($F('editor_name_'+id)),
		onComplete: function(transport) {
			window.location.href=window.location.href;
		}	
	});	
}


function infi_delete_newsitem(id) {
	if (confirm('Wilt u dit nieuwsitem verwijderen?')) {
		new Ajax.Request('/index.php?m=News.edit&action=delete', {
			method: 'post',
			evalScripts: true,
			parameters: '&id=' + id,
			onComplete: function(transport) {
				window.location.href=window.location.href;
			}	
		});
	}
}



function osage_reactie(news_id_r, naam_r, reactie_r) {
	new Ajax.Request('index.php?m=reactie', {
		method: 'post',
		evalScripts: true,
		parameters: '&action=reactie&news_id_r='+ news_id_r +'&naam_r=' + escape(naam_r) + '&reactie_r=' + escape(reactie_r),
		onComplete: function(transport) {
		
			ajaxRequest3('index.php?m=News.reacties&news_id='+news_id_r+'&tellen=1','','reacties_result')
			//window.location.href=window.location.href;

		}
	});
}


function osage_reactie_delete(reactie_id, news_id_r) {
	
	new Ajax.Request('index.php?m=reactie', {
		method: 'post',
		evalScripts: true,
		parameters: '&action=delete&reactie_id='+ reactie_id,
		onComplete: function(transport) {
		
			ajaxRequest3('index.php?m=News.reacties&news_id='+news_id_r+'&tellen=1','','reacties_result')
			//window.location.href=window.location.href;

		}
	});
}


function ov2(maand,leef,jaar) {
	window.location.href="/ontslagvergoeding?p=ontslagvergoeding&m=ontslagvergoeding.result&action=bereken&maand="+maand+"&leef="+leef+"&jaar="+jaar+"&#uitkomst";
}

function osage_ontslagvergoeding(maand,leef,jaar) {
	//window.location.href="/ontslagvergoeding?p=ontslagvergoeding&m=ontslagvergoeding.result&action=bereken&maand="+maand+"&leef="+leef+"&jaar="+jaar+"&#uitkomst";
	new Ajax.Request('/?m=ontslagvergoeding.result', {
		method: 'post',
		evalScripts: true,
		parameters: '&action=bereken&maand='+ maand +'&leef=' + leef + '&jaar=' + jaar,
		onComplete: function(transport) {
		
		ajaxRequest3('/?m=Ontslagvergoeding.result','&action=bereken&maand='+ maand +'&leef=' + leef + '&jaar=' + jaar+ '','ontslagvergoeding')
	
		/*	$('ontslagvergoeding').update();
			$('ontslagvergoeding').update(transport.responseText);
		*/
		}
	});
}

function osage_wwuitkering(v1,v2,v3,v4,v5,v6) {

	//window.location.href="/wwuitkering?p=wwuitkering&m=wwuitkering.result&action=bereken&v1="+v1+"&v2="+v2+"&v3="+v3+"&v4="+v4+"&v5="+v5+"&v6="+v6+"&#uitkomst";
	new Ajax.Request('/?m=wwuitkering.result', {
		method: 'post',
		evalScripts: true,
		parameters: '&action=bereken&v1='+ v1 +'&v2=' + v2 + '&v3=' + v3 + '&v4=' + v4 + '&v5=' + v5 + '&v6=' + v6,
		onComplete: function(transport) {
	
		ajaxRequest3('/?m=Wwuitkering.result','&action=bereken&v1='+ v1 +'&v2=' + v2 + '&v3=' + v3 + '&v4=' + v4 + '&v5=' + v5 + '&v6=' + v6+ '','wwuitkering')
	
	/*	$('wwuitkering').update();
			$('wwuitkering').update(transport.responseText);
	*/
		}
	});
}

function osage_zoek(zoekwoord) {

	new Ajax.Request('index.php?m=content.zoek', {
		method: 'post',
		evalScripts: true,
		parameters: '&zoekwoord='+ zoekwoord,
		onComplete: 
		
		function(transport) {
			$('zoek_resultaat').update();
			$('zoek_resultaat').update(transport.responseText);
		}


	});
}
function osage_email(email,doelgroep) {

	new Ajax.Request('index.php?m=email', {
		method: 'post',
		evalScripts: true,
		parameters: '&action=email&email=' + escape(email) + '&doelgroep=' + doelgroep,
		onComplete: function(transport) {
			
	 	ajaxRequest3('/?m=Content.emaillijst','','lijstddd')
		$('nieuwsbrief').update();
		$('nieuwsbrief').update(transport.responseText);
		}	
	});
}


function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function osage_achtergrond(achtergrond) {
	
	new Ajax.Request('index.php?m=achtergrond', {
		method: 'post',
		evalScripts: true,
		parameters: '&action=achtergrond&achtergrond=' + achtergrond,
		onComplete: function(transport) {
			$('modal_container').update();
			$('modal_container').update(transport.responseText);
		}
	});
}

function infi_logout() {
	new Ajax.Request('index.php?m=logout', {
		method: 'post',
		evalScripts: true,
		parameters: '&action=logout',
		onComplete: function(transport) {
			$('modal_container').update();
			$('modal_container').update(transport.responseText);
		}
	});
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function mm_showhidelayers() { //v6.0
  var i,p,v,obj,args=mm_showhidelayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=MM_findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}





function echeck(str) {
		
	var kleurfout = "#f09999"
	var kleurgoed = "#00FF33"
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	document.form.email.style.background = kleurfout
		document.form.action.disabled = true
	//   alert("Invalid E-mail IDsdfsdfsdfdfdhfhghghsss")
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	 	document.form.email.style.background = kleurfout
		document.form.action.disabled = true
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		document.form.email.style.background = kleurfout
		document.form.action.disabled = true
	    return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
		document.form.email.style.background =  kleurfout
		document.form.action.disabled = true
	    return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
	    document.form.email.style.background =  kleurfout
		document.form.action.disabled = true
	    return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
		document.form.email.style.background =  kleurfout
		document.form.action.disabled = true
	    return false
	 }
	
	 if (str.indexOf(" ")!=-1){
	 	document.form.email.style.background =  kleurfout
		document.form.action.disabled = true
	    return false
	 }
	document.form.email.style.background = kleurgoed
	document.form.action.disabled = false
	 return true					
}

function ValidateForm(){
	var emailID=document.from.email
	
	if ((emailID.value==null)||(emailID.value=="")){
		document.form.email.style.background = kleurfout
		document.form.action.disabled = true
	//	emailID.focus()
		return false
	}
	if (echeck(emailID.value)==false){
		//emailID.value=""
		emailID.focus()
		return false
	}
	return true
	document.form.email.style.background = kleurgoed
	document.form.action.disabled = false
 }

function contactform_check(form) {
	if ((!jQuery('#contact_email', form).hasClass('jtextbox')) &&
		(!jQuery('#contact_vraag', form).hasClass('jtextbox'))) {
		jQuery('#contact_checked', form).attr('value', 'yes');
		return true;
	} else {
		alert('Niet alle velden zijn ingevuld');
		return false;
	}
	
}

function case_toevoegen(selector) {
	new Ajax.Request('index.php?m=Opdrachtgevers.edit', {
		method: 'post',
		evalScripts: true,
		parameters: '&action=add_case'
			+ '&case_naam=' + jQuery(selector+' .case_naam').attr('value'),
		onComplete: function(transport) {
			window.location.href='/portfolio/case?p=caseview&cid=last';
		a=0;
		}
	});
}
function case_verwijderen(case_id, opdrachtgever_id) {
	if (confirm('Wilt u deze case verwijderen? Dit werkt alleen als deze case niet aan inspiraties is verbonden.')) {
		new Ajax.Request('index.php?m=Caseview.edit', {
			method: 'post',
			evalScripts: true,
			parameters: '&action=remove_case'
				+ '&case_id=' + case_id,
			onComplete: function(transport) {
				window.location.href='/portfolio/opdrachtgevers/opdrachtgever?p=opdrachtgever&oid='+opdrachtgever_id;
			}
		});
	}
}

function tinyFCK_setup(customoptions){
	var options = {
		mode : "textareas",
		theme : "advanced",
		language : "nl",
		width : "770px",
		height : "200px",
		extended_valid_elements : "iframe[width|height|frameborder|scrolling|src],object[width|height],param[name|value],embed[src|type|wmode|width|height|id|style|quality|bgcolor|scale|wmode|s|align|FlashVars|allowfullscreen]",
		plugins : "save,preview,advimage,advlink,flash,paste,fullscreen",
		theme_advanced_buttons1 : "code,save,fullscreen,|,forecolor,bold,underline,|,cleanup,removeformat,|,justifyleft,justifycenter,bullist,numlist",
		theme_advanced_buttons2 : "image,link,unlink,flash,|,pastetext,pasteword",
		theme_advanced_buttons3 : "",
		theme_advanced_toolbar_location : "top",
		theme_advanced_toolbar_align : "left",
		theme_advanced_statusbar_location : "bottom",
		file_browser_callback : "tinyFCK_fileBrowserCallBack",
		theme_advanced_resizing : true,
		theme_advanced_resize_horizontal :false,
		apply_source_formatting : true,
		editor_selector : "mceEditor"
	};
	jQuery.extend(options, customoptions);
	tinyMCE.init(options);
}

function tinyFCK_fileBrowserCallBack(field_name, url, type, win) {
	var connector = "../../filemanager/browser.html?Connector=connectors/php/connector.php";
	var enableAutoTypeSelection = true;
	
	var cType;
	tinyfck_field = field_name;
	tinyfck = win;
	
	switch (type) {
		case "image":
			cType = "Image";
			break;
		case "flash":
			cType = "Flash";
			break;
		case "file":
			cType = "File";
			break;
	}
	
	if (enableAutoTypeSelection && cType) {
		connector += "&Type=" + cType;
	}
	
	window.open(connector, "tinyfck", "modal,width=600,height=400");
}
