pageDirty=false;
uploadTarget="";
currentEditor="";

document.onclick = function(e) {  }

String.prototype.endsWith = function(str) 
{return (this.match(str+"$")==str)}

var ppurl=document.location.href;
if(document.location.hostname!='www.yunicc.ws' && document.location.hostname!='localhost')
{
	if(document.location.hostname=='127.0.0.1')
	{
		document.location=ppurl.replace(document.location.hostname,'localhost');
	}
	else
	{
		document.location=ppurl.replace(document.location.hostname,'www.yunicc.ws');
	}
}

if(typeof $ != 'function') 
{ 
	$ = function(id) { return document.getElementById(id) }
}
/*function $(id)
{
	return document.getElementById(id);
}*/

function makeDirty()
{
	pageDirty=true;
}

function checkDirty(url)
{
	if(pageDirty)
	{
		res=confirm("Er zijn een of meerdere niet opgeslagen wijzigingen op deze pagina. Weet je zeker dat je door wilt gaan zonder op te slaan?");
		if(res)
		{
			document.location=url;
		}
	}
	else
	{
		document.location=url;
	}
}

function checkDelete(url)
{
	if(confirm("Weet je zeker dat je deze module wilt verwijderen?")) 
	{
		document.location=url;
	}
}

function askConfirmation(message,url)
{
	if(confirm(message))
	{
		navigate("#"+url);
	}
}

function askConfirmationLegacy(message,url)
{
	if(confirm(message))
	{
		navigate(url);
	}
}

function getOrder(id)
{
	var str = '';
	var inputs = id.getElementsByTagName("span");
	for(var i in inputs)
	{
		var inp = inputs[i];
		//alert(inp.name);
		//if(inp.=="hidden")
		//{
			val = inp.innerHTML;
			if(isNaN(val)==false)
			{
				str+=val+";";
			}
		//}
	}
	return str;
}

function saveOrder(clubid)
{
	var obj=newAjaxObject();
	obj.onreadystatechange=function()
	{
		if(obj.readyState==4)
		{
			returnResult(obj.responseText);
		}
	}
	var c1 = document.getElementById('dragdropcolumn1');
	var c2 = document.getElementById('dragdropcolumn2');
	obj.open("GET","prc.php?m=reorder&c1="+getOrder(c1)+"&c2="+getOrder(c2)+"&id="+clubid,true);
	obj.send(null);
}

function returnResult(obj)
{
	if(obj=="OK")
	{
		pageDirty=false;
		alert("De nieuwe indeling is succesvol opgeslagen!");
	}
	else if(obj=="RIGHTS")
	{
		alert("Je hebt niet genoeg rechten om deze bewerking uit te voeren. ");
	}
	else if(obj=="ERR")
	{
		alert("Er is een fout opgetreden: "+obj);
	}
	else if(obj=="ID")
	{
		alert("Er kon geen toegang verkregen worden.");
	}
	else
	{
		alert("Er is een onbekende fout opgetreden: "+obj);
	}
}

function resizeImages()
{
	var viewportwidth;
	 if (typeof window.innerWidth != 'undefined')
	 {
		  viewportwidth = window.innerWidth
	 }
	 else if (typeof document.documentElement != 'undefined'
		 && typeof document.documentElement.clientWidth !=
		 'undefined' && document.documentElement.clientWidth != 0)
	 {
		   viewportwidth = document.documentElement.clientWidth
	 }
	 else
	 {
		   viewportwidth = document.getElementsByTagName('body')[0].clientWidth
	 }
	 
  var maxwidth=viewportwidth*0.4;
  //var imgs = document.getElementsByTagName("img");
  var imgs = getElementsByClassName("resize");
  for ( var p = 0; p < imgs.length; p++ )
  {
    //if ( imgs[p].className == "resize" )
    //{
      var w = parseInt( imgs[p].width );
      var h = parseInt( imgs[p].height );
      if ( w > maxwidth )
      {
        imgs[p].style.cursor = "pointer";
        imgs[p].onclick = function( )
        {
          var iw = window.open ( this.src, 'Afbeelding','resizable=1' );
          iw.focus();
        };
        h = ( maxwidth / w ) * h;
        w = maxwidth;
        imgs[p].height = h;
        imgs[p].width = w;
      }
    //}
  }
}

function changeColor(row, change)
{
	if (change)
	{
	  row.style.backgroundColor = '#BFBFBF';
	}
	else
	{
	  row.style.backgroundColor = 'transparent';
	}
}

function navigate(url)
{
	window.location=url;
}

function newAjaxObject()
{
	if (window.XMLHttpRequest)
	{
		// code for IE7+, Firefox, Chrome, Opera, Safari
		return new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{
		// code for IE6, IE5
		return new ActiveXObject("Microsoft.XMLHTTP");
	}
	else
	{
		return null;
	}
}

function returnHash(a_sLink){
  return unescape(self.document.location.hash.substring(1))
}

function slideInNotification(element)
{
	var effect = new Fx.Style(element, 'bottom', {duration:1200});
	effect.options.transition = Fx.Transitions.bounceOut;
	effect.start(40);
}

function slideOutNotification(element)
{
	var effect = new Fx.Style(element, 'bottom', {duration:1200});
	effect.options.transition = Fx.Transitions.backIn;
	effect.start(-65);
}

function fadePage()
{
	var element = document.getElementById('pagecontents');
	var effect = new Fx.Style(element, 'opacity', {duration:400});
	effect.options.transition = Fx.Transitions.quadOut;
	effect.start(1);
}

function goNext() 
{
	window.location=uploadTarget;
}

function flashResize(ele, height, width) {
	var uploader = document.getElementById(ele);
	
	if(height != 0)
		uploader.style.height = height+"px";
	
	if(width != 0)
		uploader.style.width = width+"px";
}

function parseScript(_source,_target) {
	var source = _source;
	var scripts = new Array();
	
	/*var pattern = /<!--POST-LOAD-->(.*?)<!--xPOST-LOAD-->/;
	var matches = source.match(pattern);
	var postscript = matches[1];
	source=source.replace(pattern,'');

	document.write(the_string);*/
	
	// Strip out tags
	while(source.indexOf("<script") > -1 || source.indexOf("</script") > -1) {
		var s = source.indexOf("<script");
		var s_e = source.indexOf(">", s);
		var e = source.indexOf("</script", s);
		var e_e = source.indexOf(">", e);
		
		// Add to scripts array
		scripts.push(source.substring(s_e+1, e));
		// Strip from source
		source = source.substring(0, s) + source.substring(e_e+1);
	}
	
	_target.innerHTML=source;
	
	for(var i=0; i<scripts.length; i++) {
		//try {
			eval(scripts[i]);
		//}
		//catch(ex) {
			// do what you want here when a script fails
		//}
	}
	// Return the cleaned source
	return true;
}

function catchForm(form)
{
	//alert(implodeForm(form));
	act=form.action.substring(form.action.lastIndexOf('/')+1);
	switchPage(act,undefined,implodeForm(form));
	 return false;
}

function fixForms()
{
	for(form in document.forms)
	{
		addListener(document.forms[form],"submit","catchForm(this);");
	}
}

function addListener(obj,event,func)
{
	//try {
	//  obj.addEventListener(event, func);
	//} catch(e) {
	//  obj.attachEvent(event, func); //Internet Explorer
	//}
	obj.onsubmit=function() {
		eval(func);
		return false;
	}
}

function showdiv(divid)
{
	var b = getElementsByClassName('hidebox',document.getElementById('pagecontents'));
	for(cl in b)
	{
		try
		{
			b[cl].style.display='none';
		}
		catch(err)
		{
		}
	}
	document.getElementById(divid).style.display='block';
}

function insertTag(formField, openTag, closeTag) {

  if (document.selection) {

    formField.focus();

    sel = document.selection.createRange();

    sel.text = openTag + sel.text + closeTag;

  }


  else if (formField.selectionStart || formField.selectionStart == '0') {

    var startPos = formField.selectionStart;

    var endPos = formField.selectionEnd;

    formField.value = formField.value.substring(0, startPos)

                  + openTag
				  
				  + formField.value.substring(startPos, endPos)
				  
				  + closeTag

                  + formField.value.substring(endPos, formField.value.length);

  } else {

    formField.value += openTag + closeTag;

  }

}

function nextObject(p) 
{
	var n = p;
	do n = n.nextSibling;
	while (n && n.nodeType != 1);
	return n;
}

function previousObject(p) 
{
	var n = p;
	do n = n.previousSibling;
	while (n && n.nodeType != 1);
	return n;
}

function getContentBox(titleBox)
{
	var ps = titleBox;
	while(ps.className != "boxtop")
	{
		ps = ps.parentNode;
	}
	ps = nextObject(ps);
	return ps;
}

function selectAllPictures(p)
{
	// TODO: SELECT ALL PICTURES
	var l = getContentBox(p.parentNode).getElementsByTagName("div");
	for(lc = 0; lc < l.length; lc++)
	{
		var elc = l[lc];
		if(elc.className=="imagebart")
		{
			var iCol = elc.getElementsByTagName("input");
			if(iCol[0]!=undefined)
			{
				iCol[0].checked=true;
				updateBarColor(iCol[0]);
			}
		}
	}
}

function toggleCollapsePictures(p)
{
	if(p.title == "Uitklappen")
	{
		// make pictures larger
		p.src="images/icon/up.png";
		p.title="Inklappen";
		var l = getContentBox(p.parentNode).getElementsByTagName("img");
		for(lc = 0; lc < l.length; lc++)
		{
			var elc = l[lc];
			if(elc.src.indexOf("/edit.png")==-1 && elc.src.indexOf("/bin.png")==-1)
			{
				elc.width=(elc.clientWidth*3);
			}
		}
		var l = getContentBox(p.parentNode).getElementsByTagName("div");
		for(lc = 0; lc < l.length; lc++)
		{
			var elc = l[lc];
			if(elc.className=="imagebox")
			{
				elc.style.width="180px";
				elc.style.height="207px";
				elc.style.backgroundColor="transparent";
			}
			if(elc.className=="inner_imagebox")
			{
				elc.style.bottom="27px";
			}
			if(elc.className=="imagebart" || elc.className=="imagebarb")
			{
				elc.style.visibility="visible";
			}
		}
	}
	else
	{
		// make pictures smaller
		p.src="images/icon/down.png";
		p.title="Uitklappen";
		var l = getContentBox(p.parentNode).getElementsByTagName("img");
		for(lc = 0; lc < l.length; lc++)
		{
			var elc = l[lc];
			if(elc.src.indexOf("/edit.png")==-1 && elc.src.indexOf("/bin.png")==-1)
			{
				elc.width=(elc.clientWidth/3);
			}
		}
		var l = getContentBox(p.parentNode).getElementsByTagName("div");
		for(lc = 0; lc < l.length; lc++)
		{
			var elc = l[lc];
			if(elc.className=="imagebox")
			{
				elc.style.width="60px";
				elc.style.height="60px";
				elc.style.backgroundColor="transparent";
			}
			if(elc.className=="inner_imagebox")
			{
				elc.style.bottom="0px";
			}
			if(elc.className=="imagebart" || elc.className=="imagebarb")
			{
				elc.style.visibility="hidden";
			}
		}
	}
}

function confirmPictureRemoval(ref,id)
{
	var picDiv = document.createElement("div");
	//picDiv.style.backgroundColor="#E0E0E0";
	picDiv.className="hidebox";
	document.body.appendChild(picDiv);
	var curSrc = "";
	var l = ref.parentNode.parentNode.parentNode.getElementsByTagName("img");
	for(lc = 0; lc < l.length; lc++)
	{
		var elc = l[lc];
		if(elc.src.indexOf("/edit.png")==-1 && elc.src.indexOf("/bin.png")==-1)
		{
			curSrc = elc.src;
		}
	}
	picDiv.innerHTML = '<div style="padding: 9px; text-align: center;"><img src="'+curSrc+'"><br><br>Weet je zeker dat je deze afbeelding wilt verwijderen?<br><button type="button" onclick="ybox_hidediv(); navigate(\'#pictures&mode=content&act=delete&pid='+id+'\');">Doorgaan</button>&nbsp;<button type="button" onclick="ybox_hidediv();">Annuleren</button></div><span class=\"ybox-ie-fix\"></span>';
	ybox_showdiv(picDiv,false);
	//askConfirmation('Weet je zeker dat je deze afbeelding wilt verwijderen?','pictures&mode=content&act=delete&pid='+id);
}

// image bars

function setOpacity(obj,value) {
	obj.style.opacity = value/10;
	obj.style.filter = 'alpha(opacity=' + value*10 + ')';
}

function showBars(iid)
{
	document.getElementById('imagebart'+iid).style.display='block'; 
	document.getElementById('imagebarb'+iid).style.display='block';
}

function hideBars(iid)
{
	var el = document.getElementById('imagebart'+iid).getElementsByTagName('input');
	if(el[0])
	{
		var c = el[0].checked;
	}
	else
	{
		var c = false;
	}
	if(c == false)
	{
		document.getElementById('imagebart'+iid).style.display='none'; 
		//document.getElementById('imagebarb'+iid).style.display='none';
	}
}

function flareBars(iid)
{
	setOpacity(document.getElementById('imagebart'+iid),10);
	setOpacity(document.getElementById('imagebarb'+iid),10);
}

function unflareBars(iid)
{
	var el = document.getElementById('imagebart'+iid).getElementsByTagName('input');
	if(el[0])
	{
		var c = el[0].checked;
	}
	else
	{
		var c = false;
	}
	if(c == true)
	{
		setOpacity(document.getElementById('imagebart'+iid),9);
		setOpacity(document.getElementById('imagebarb'+iid),9);
	}
	else
	{
		setOpacity(document.getElementById('imagebart'+iid),6);
		setOpacity(document.getElementById('imagebarb'+iid),6);
	}
}

function updateBarColor(iid)
{
	if(typeof(iid)=="string")
	{
		var el = document.getElementById('imagebart'+iid).getElementsByTagName('input');
	}
	else
	{
		//alert(iid);
		var el = iid.parentNode.parentNode;
	}
	if(el[0])
	{
		var c = el[0].checked;
	}
	else
	{
		var c = false;
	}
	if(c == true)
	{
		document.getElementById('imagebart'+iid).style.backgroundColor = "#FFFFCC";
		document.getElementById('imagebarb'+iid).style.backgroundColor = "#FFFFCC";
	}
	else
	{
		document.getElementById('imagebart'+iid).style.backgroundColor = "white";
		document.getElementById('imagebarb'+iid).style.backgroundColor = "white";
	}
}

function check(e)
{ 
	var target = (e && e.target) || (event && event.srcElement); 
	var obj = document.getElementById('mydiv'); 
	var obj2 = document.getElementById('sho'); 
	checkParent(target)?obj.style.display='none':null; 
	target==obj2?obj.style.display='block':null; 
} 

function checkParent(t)
{ 
	while(t.parentNode)
	{ 
		if(t==document.getElementById('mydiv'))
		{ 
			return false 
		} 
		t=t.parentNode 
	} 
	return true 
} 

function showPreview(filename)
{
	var base = document.getElementById('navbox');
	var previmg = document.createElement('img');
	previmg.setAttribute('src',filename);
	previmg.className='preview-image';
	previmg.style.position='absolute';
	var curp = getlocalpos(base); 
	previmg.style.left=(curp['x']+275)+'px';
	previmg.style.top=curp['y']+'px';
	base.parentNode.appendChild(previmg);
}

function hidePreview()
{
	var col = document.getElementsByTagName("img");
	for(var c in col)
	{
		var imge = col[c];
		if(imge.className=="preview-image")
		{
			imge.style.display="none";
		}
	}
}

function toggleUsergroups()
{
	//alert($('gr1').checked);
	if($('gr1').checked==true)
	{
		//alert('darp');
		$('gr2').disabled=true;
		$('gr7').disabled=true;
		$('gr4').disabled=true;
		$('gr5').disabled=true;
		$('gr6').disabled=true;
		$('lbl2').className="disabled";
		$('lbl7').className="disabled";
		$('lbl4').className="disabled";
		$('lbl5').className="disabled";
		$('lbl6').className="disabled";
	}
	else
	{
		$('gr2').disabled=false;
		$('gr7').disabled=false;
		$('gr4').disabled=false;
		$('gr5').disabled=false;
		$('gr6').disabled=false;
		$('lbl2').className="";
		$('lbl7').className="";
		$('lbl4').className="";
		$('lbl5').className="";
		$('lbl6').className="";
	}
}

function boolInt(bool)
{
	if(bool==true)
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

