﻿// TODO: setattr werkend maken voor IE
function setAttributeCustom(element,attribute,value){

	
	if (attribute == "class")
	{
	element.className = value;
	}
	else if (attribute == "style")
	{
	element.style.cssText = value;
	}
	else
	{
	element.setAttribute(attribute, value)
	}

}



// laat de resultaten van de ajax opdracht zien
function show_ajax_res(z) {
	// PROBLEEM: hoe weet je welke vars terug komen,
	// onderscheid maken tussen save en iets anders?
	
	try{
	
	var result_array = eval('(' + z + ')');

	var vars = new Object;

	for (var i in result_array){
		vars[i] = result_array[i];	
	}

	// OOK DIT IS LELIJK, TODO, verzin wat moois
	try{
	document.getElementById(vars['auto']).value= id;
	}
	catch(ee){}
	item_name = vars['item_name'];
	if (!vars['errors'] && vars['show_result'] == 1)
	{
		
		if (vars['action'] == 'save')
		{
		set_result_div('green',item_name+" was saved in "+vars['linkto']+" under "+vars['auto']+" "+vars[vars['auto']])
				}
		else if (vars['action'] == 'remove')
		{
		set_result_div('green',item_name+" was removed from "+vars['linkto'])
		}
	}	
	else if(vars['errors'])
	{
	set_result_div('red',"not all fields have been filled in correctly");
	temp = vars['errors'];
		
	for (var i in temp){
	setAttributeCustom(document.getElementById(i),'class', 'rood');
	toelichting = i+"_tl";
	document.getElementById(toelichting).innerHTML = result_array['errors'][i];
	}
	
	
	}
		if (vars['is_removed'] == 1){
		set_result_div('green',"div is verwijderd!");
		}
	
	if (typeof(vars['redir']) != 'undefined'){		
		document.location = vars['redir'];
		}

	
}
catch(err){alert("mainlus: "+err)
}
	
	}
	
	// BELANGRIJKSTE FUNCTIE, VEJSOND EN STUURT NAAR PHP
function submit_to_ajax(form_id,div_id,update){
	try{	
	thisform = document.forms[form_id];
	old_class = thisform.parentNode.getAttribute('class');
	j= thisform.length;
	var post1 = new Object;
		
		// TODO: Lelijk, is ff voor de demo, waar ik geen tiny gebruik		
	try{
	tinyMCE.triggerSave();
	}catch(E){}	
		
	for (i=0;i<j;i++){           
        //document.write(thisform[i].id);
        if (thisform[i].id != "" && thisform[i].id.substr(0,3) != "mce" && thisform[i].id != " " && thisform[i].id.substr(0,7) != "content")
        {
        post1[thisform[i].id]  = thisform[i].value;   	
      	}   	
      	// Als het een content ding is, uit het cms dus, dan moet je daar content van maken ipv content12315325254, en die id opslaan
      	else if (thisform[i].id.substr(0,7) == "content"){
      	mceedit = thisform[i].id;
      	post1["content"]  = thisform[i].value;  
      	}
      	
      	// als het een password is, kijken of je moet encrypten
        if (thisform[i].id == "password"){
      		if (thisform["encrypt"].value == 1){
      		 post1[thisform[i].id] = calcSHA1((thisform['uniqid'].value+calcSHA1(thisform[i].value)));
      		
      		}
       	}
      	
      	
      	if (thisform[i].getAttribute('class') == "rood")
      	{
      	setAttributeCustom(thisform[i],'class', '');
 		}
 		
 		
 		
 		// TODO: LELIJK!!
 				try {
 				toelichting = thisform[i].getAttribute('name')+"_tl";
 				if (document.getElementById(toelichting).innerHTML != "")
      				{
      				document.getElementById(toelichting).innerHTML = "";
 						}
 		
 					}catch(err){}
 					
 					
 			
 		 }

 	if (typeof( window[ 'mceedit' ] ) != "undefined"){
      		//tinyMCE.removeMCEControl(tinyMCE.getEditorId(mceedit));
       } 
    
 	if (!post1['item_name'])
	{
	post1['item_name'] = thisform.getAttribute('name');
	} 
 	
	var strPOST = JSON.stringify(post1);  
	
	//
	if (post1['class']){
	if (post1['class'] == ""){post1['class'] = old_class;}
		if (update)
		{
		update_div(div_id,post1['class'],post1['item_name']);
		}
	}
	set_result_div('red','Checking data - '+post1['item_name']+"...");
	

	
}
catch(error){alert(error)}
	// SCHRIJF WEG NAAR PHP
	x_doajax(strPOST, show_ajax_res);   
	}
	
	// PLAATS DE NIEUWE FORM
function insert_new_form(z) {
//alert(z);
//	document.getElementById("output").innerHTML = z;
 	var result_array = eval('(' + z + ')');
	
  var ni = document.getElementById('append_div');
  var newdiv = document.createElement('div');
  var divIdName = 'new_div';
  setAttributeCustom(newdiv,'id',divIdName);
 
  setAttributeCustom(newdiv,'class',result_array['class']);
 
  newdiv.innerHTML = result_array['form_src'];
  ni.appendChild(newdiv);
 
 /// TODO: DIT IS HEEL VERROT 
/*  if (result_array['linkto'] == "content")
  {
  // IK WEET NOG NIET HOE IK DE TEXT AREA DYNAMISCH KAN BENADEREN
  // kan wel, moet ik elke form item een uniek id geven, maar dan krijg je probs
  // met json post, dus dan moet je daar eerst weer substrigns van nemen
  setTextareaToTinyMCE(document.forms[1][4].getAttribute("id"));
	}*/
	

	}
	
	// maak van de textarea een tinyding
	function setTextareaToTinyMCE(sEditorID) {
		var oEditor = document.getElementById(sEditorID);
		tinyMCE.execCommand('mceAddControl', true, sEditorID);
		bTextareaWasTinyfied = true;

		return;
	}
	
	//    BEGIN NEW FORM SHIT
	function req_new_form(mid,content,is_removed,type) {
	x_add_empty_form(mid, content,is_removed,type, insert_new_form)
	}

	
	// WORDT GEBRUIKT OM NIEUWE FORMS TE PLAATSEN
function addElement() {
  var ni = document.getElementById('top_div');
  var newdiv = document.createElement('div');
  var divIdName = 'new_div';
  setAttributeCustom(newdiv,'id',divIdName);
  ni.appendChild(newdiv);
}
	//roep de PHP remove procedure en en verwijder de div van het scherm
function req_remove_div(div_id,auto_id,form_name){
	set_result_div('red','BEZIG MET VERWIJDEREN VAN '+div_id);
	//remove_div(div_id);	
	x_remove_div(auto_id, form_name, show_ajax_res)
}
// maak van de new_div een normale div
function update_div(div_id,class_name,new_id){
	
	var d = document.getElementById(div_id);
	setAttributeCustom(d,'class',class_name);
	setAttributeCustom(d,'id',new_id);
}
//open_upload_window
var newwindow = '';
function open_upload_window(form_name,input)
{
	url = "../mod/upload.php?form="+form_name+"&input="+input;
	if (!newwindow.closed && newwindow.location)
	{
		newwindow.location.href = url;
	}
	else
	{
		newwindow = window.open(url,"upload","height=150,width=370");
		if (!newwindow.opener) newwindow.opener = self;
	}
	if (window.focus) {newwindow.focus()}
	return false;
}

function pop_up(url)
{
	if (!newwindow.closed && newwindow.location)
	{
		newwindow.location.href = url;
	}
	else
	{
		newwindow = window.open(url,"popup","height=600,width=800");
		if (!newwindow.opener) newwindow.opener = self;
	}
	if (window.focus) {newwindow.focus()}
	return false;
}


// VERWIJDER DIV
function remove_div(div_id){
	var olddiv = document.getElementById(div_id);
	var p = olddiv.parentNode;
 	p.removeChild(olddiv);
 	}
 	
 	function display(div_id){
 		if (document.getElementById(div_id).style.display == "none"){
 		document.getElementById(div_id).style.display = "block"
 		}
 		else
 		{
 		document.getElementById(div_id).style.display = "none"
 		}
 	}
 	

	function set_result_div(state,msg)
	{
		switch (state){
	case "red":
	setAttributeCustom(document.getElementById('result'),"style","color: #fff; font-family: arial; font-weight: bold; background: url(../img/message_red.png); ");
	document.getElementById('result').innerHTML = msg;
	break;
	case "green":
	setAttributeCustom(document.getElementById('result'),"style","font-family: arial; font-weight: bold; background: url(../img/message_green.png); border: solid 1px #fff;");
	document.getElementById('result').innerHTML = msg;
	break;
		}
	}
	
