$(document).ready(function() {
	// Click handler for image removal.
  $('.removeImg').click(function(){
  	if (confirm('Are you sure you want to remove this image?')) {
  		return true;
  	}
  	return false;
  });
  
  // disable or enable the templates generation button
  if (($.trim($("#title_3").val()) == '') || ($.trim($("#title_2").val()) == '') || ($.trim($("#urlComponent_3").val()) == '') || ($.trim($("#urlComponent_2").val()) == '')) {
  	$('#templateButton').attr('disabled','disabled');
  } else {
  	$("#templateButton").removeAttr("disabled");
  }
  $("#title_3, #title_2, #urlComponent_3, #urlComponent_2").keyup(function () {
		if (($.trim($("#title_3").val()) == '') || ($.trim($("#title_2").val()) == '') || ($.trim($("#urlComponent_3").val()) == '') || ($.trim($("#urlComponent_2").val()) == '')) {
  		$('#templateButton').attr('disabled','disabled');
  	} else {
  		$("#templateButton").removeAttr("disabled");
  	}
	});
	
  if($('#newImage').length) {
	   new AjaxUpload('newImage', {
	    action: 'main.php?action=uploadImage&id=' + $('input#cmsid').val(),
	    name: 'image',
	    onSubmit: function(file, ext) {
	    	if (!ext || !/^(jpg|JPG|png|jpeg|gif)$/.test(ext)){
	    		alert('Please only upload images.');
	    		// cancel upload
	    		return FALSE;
	      }

	      $('#newFile').attr('disabled','disabled');
	      $('img.loader').css('display','inline');
	    },
	    onComplete: function(file, response) {
	      var odd = '';
	      if($("#images tr:last").attr('class') != 'odd') {
	        odd = ' class="odd"';
	      }

	      var resp = response.split(':');
	      var url = resp[0];
	      var filename = resp[1];
	      var fid = resp[2];
	      var dis = resp[3];

	      $("#newFile").removeAttr("disabled");
	      $('img.loader').css('display','none');

	      if(url == 'error') {
	        alert(response);
	        return false;
	      }
	      
	      var disabled = '';
        /*if(dis < 795) {
          disabled = ' disabled="disabled"';
        }*/

	      var html = '<tr' + odd + '><td align="center" class="image"><img src="'+url+'" alt="'+filename+'"/></td><td class="desc"><!--<label class="caption">Description NL:</label> <input type="text" class="caption" name="caption_1_'+fid+'" maxlength="255"/><br/>--><label class="caption">Description FR:</label><input type="text" class="caption" name="caption_2_'+fid+'" maxlength="255"/><br/><label class="caption">Description EN:</label><input type="text" class="caption" name="caption_3_'+fid+'" maxlength="255"/><br/><!--<label class="caption">Description 2 NL:</label> <input type="text" class="caption" name="caption_2_1_'+fid+'" maxlength="255"/><br/>--><label class="caption">Description 2 FR:</label><input type="text" class="caption" name="caption_2_2_'+fid+'" maxlength="255"/><br/><label class="caption">Description 2 EN:</label><input type="text" class="caption" name="caption_2_3_'+fid+'" maxlength="255"/><br/></td><td align="center"><input type="radio" name="main_image" value="'+fid+'" '+disabled+'/></td><td align="center"><input type="checkbox" name="delete_'+fid+'" value="1"/></td></tr>';
	      
	    	$("#images tr:last").after(html);
	    }
	   });
	 }  
	 
	if($('#newFile').length) {
	   new AjaxUpload('newFile', {
	    action: 'main.php?action=uploadFile&id=' + $('input#cmsid').val(),
	    name: 'file',
	    onSubmit: function(file, ext) {
	    	if (!ext || !/^(pdf)$/.test(ext)){
	    		alert('Please only upload PDF\'s.');
	    		// cancel upload
	    		return FALSE;
	      }

	      $('#newFile').attr('disabled','disabled');
	      $('img.loader').css('display','inline');
	    },
	    onComplete: function(file, response) {
	      var odd = '';
	      if($("#files tr:last").attr('class') != 'odd') {
	        odd = ' class="odd"';
	      }

	      var resp = response.split(':');
	      var url = resp[0];
	      var filename = resp[1];
	      var fid = resp[2];
	      var dis = resp[3];

	      $("#newFile").removeAttr("disabled");
	      $('img.loader').css('display','none');

	      if(url == 'error') {
	        alert(response);
	        return false;
	      }
	      
	      var disabled = '';
        /*if(dis < 795) {
          disabled = ' disabled="disabled"';
        }*/

	      var html = '<tr'+odd+'><td align="left" class="file"><a href="'+url+'">'+filename+'</a></td><td align="center"><input type="checkbox" name="delete_'+fid+'" value="1"/></td></tr>';
	    	$("#files tr:last").after(html);
	    }
	   });
	 }
	 
	 //var altField = $("#actual-start-date").datepicker( "option", "altField" );
	 $("#start-date").datepicker({altFormat: 'yy-mm-dd 00:00:00', altField: '#actual-start-date' });
	 //$("#start-date").datepicker( "option", "altField", '#actual-start-date');
	 
	 //var altField = $(".selector").datepicker("option", "altField");
	 $("#end-date").datepicker({altFormat: 'yy-mm-dd 00:00:00', altField: '#actual-end-date' });
	 // = $("#end-date").datepicker("option", "altField");

});


function selectCat(catid) {
	var url = "main.php?action=showRecords&catSelect=" + catid;
	document.location = url;
}


function selectDesigner(designerid) {
	var url = "main.php?action=showRecords&designerSelect=" + designerid;
	document.location = url;
}

var minLength = 15;

function formSubmit(scrollId, checkTitle)
{	
	if (checkTitle) {
		if (!checkTitleMinLength('#title_2', minLength) || !checkTitleMinLength('#title_3', minLength)) {
			alert('Please fill in titles of atleast ' + minLength + ' characters.');
			return false;
		}
	}
	
	var scrollTop = getScrollPosition(scrollId);
	document.webForm.submit();
}

function formSubmitandGetRecord(scrollId, checkTitle)
{	
	if (checkTitle) {
		if (!checkTitleMinLength('#title_2', minLength) || !checkTitleMinLength('#title_3', minLength)) {
			alert('Please enter titles of at least ' + minLength + ' characters.');
			return false;
		}
	}

	document.webForm.extraAction.value = 'getRecord';
	var scrollTop = getScrollPosition(scrollId);
	document.webForm.submit();
}

function formSubmitTemplate(scrollId, checkTitle)
{	
	if (checkTitle) {
		if (!checkTitleMinLength('#title_2', minLength) || !checkTitleMinLength('#title_3', minLength)) {
			alert('Please enter titles of at least ' + minLength + ' characters.');
			return false;
		}
	}

	document.webForm.extraAction.value = 'getRecord';
	document.webForm.makeTemplates.value = 1;
	var scrollTop = getScrollPosition(scrollId);
	document.webForm.submit();
}

function checkTitleMinLength(id, minLength)
{
	return $(id).val().length >= minLength ? true: false;
}


function formCancel(scrollId)
{
	var url = "main.php?action=showRecords";
	document.location = url;
}


function getVacature(id, cat, scrollId)
{
	var scrollTop = getScrollPosition(scrollId);
	var url = "main.php?action=getRecord&id=" + id + "&catSelect=" + cat + "&scrollTop=" + scrollTop + "&scrollId=" + scrollId;
	document.location = url;
}


function getProduct(cat, designer, code, id, scrollId)
{
	var scrollTop = getScrollPosition(scrollId);
	var url = "main.php?action=getRecord&catSelect=" + cat + "&designerSelect=" + designer + "&id=" + id + "&scrollTop=" + scrollTop + "&scrollId=" + scrollId;
	document.location = url;
}

function getRecord(cat, id, scrollId)
{
	var scrollTop = getScrollPosition(scrollId);
	var url = "main.php?action=getRecord&catSelect=" + cat + "&id=" + id + "&scrollTop=" + scrollTop + "&scrollId=" + scrollId;
	document.location = url;
}

function getDesigner(cat, designer, id, scrollId)
{
	var scrollTop = getScrollPosition(scrollId);
	var url = "main.php?action=getRecord&catSelect=" + cat + "&designerSelect=" + designer + "&id=" + id + "&scrollTop=" + scrollTop + "&scrollId=" + scrollId;
	document.location = url;
}


function getScrollPosition(scrollId){
	var scrollTop = '';
	if (document.getElementById(scrollId)) {
    	var scrollTop = document.getElementById(scrollId).scrollTop;
    }
    //alert(intY);
    if(document.webForm) {
    	document.webForm.scrollTop.value = scrollTop;
    	document.webForm.scrollId.value = scrollId;
    }
    return scrollTop;
}


function setScrollPosition() {
	if (document.getElementById(scrollId)) {
		document.getElementById(scrollId).scrollTop = scrollTop;
	}
}


function printPage()
{
	window.print();
}


function closeWindow()
{
	window.close();
}


function doPopUp()
{
	window.open("popup.htm","fieldpage","width=800,height=600,toolbar=0,status=0,location=0,menubar=0,scrollbars=0,resizable=0");
}


function setFocus() {
	/* use names, not id's! */
	document.mainform.name.focus();
}


function openWin(name, nameWin ,h ,w) 
{
	var scw = screen.availWidth
	var sch = screen.availHeight
	aawindow = window.open(name, nameWin,"left="+((scw/2)-(w/2))+",top="+((sch/2)-(h/2))+",width="+w+",height="+h+",scrollbars=no");
}


function noSpam(user, domain) {
	locationstring = "mailto:" + user + "@" + domain;
	window.location = locationstring;
}


var currentField = '';
function open_edit_field(fieldName) {
	//alert('test');
	currentField = fieldName;
	var scw = document.body.clientWidth;
	var sch = document.body.clientHeight;
	var left =  ((scw/2) - 370) + 'px';
	document.getElementById("editfield").style.display = "block";
	document.getElementById("editfield").style.top = "50px";
	document.getElementById("editfield").style.left = left;
	
	var fielddata = document.webForm[fieldName].value;
	//alert(fielddata);
	document.webForm.editarea.value = fielddata;
	document.getElementById("editarea").focus();
}

function save_field(fieldName)
{
	document.webForm[fieldName].value = document.webForm.editarea.value;
	document.webForm.editarea.value = '';
	currentField = '';
	document.getElementById("editfield").style.display = "none";
}

function close_edit_field(fieldName) {
	document.webForm.editarea.value = '';
	currentField = '';
	document.getElementById("editfield").style.display = "none";
}


function clear_field(fieldName)
{
	if (confirm('Wilt u dit veld wissen?')) 
	{ 
		document.webForm[fieldName].value = "";
	}
}

function clear_field_instant(fieldName)
{
	document.webForm[fieldName].value = "";
}


function delete_page(pageid, parent, navorder)
{
	if (confirm('Wilt u deze pagina verwijderen?')) 
	{ 
		var url = "main.php?action=deleteRecord&id=" + pageid + "&parent=" + parent + "&navorder=" + navorder;
		document.location = url;
	}
}


function delete_vacature(id)
{
	if (confirm('Wilt u deze vacature verwijderen?')) 
	{ 
		var url = "main.php?action=deleteRecord&id=" + id;
		document.location = url;
	}
}


function deleteProduct(cat, designer, code, id, txt)
{
	if (confirm(txt)) 
	{ 
		var url = "main.php?action=deleteRecord&catSelect=" + cat + "&designerSelect=" + designer + "&productcodeSearch=" + code + "&id=" + id;
		document.location = url;
	}
}

function deleteRecord(id, txt, cat)
{
	if (confirm(txt)) 
	{ 
		var url = "main.php?action=deleteRecord&id=" + id + "&catSelect=" + cat;
		document.location = url;
	}
}

function deleteDesigner(id, txt)
{
	if (confirm(txt)) 
	{ 
		var url = "main.php?action=deleteRecord&id=" + id;
		document.location = url;
	}
}

function deleteHero(id, txt)
{
	if (confirm(txt)) 
	{ 
		var url = "main.php?action=deleteRecord&id=" + id;
		document.location = url;
	}
}

function deleteItem(id, txt)
{
	if (confirm(txt)) 
	{ 
		var url = "main.php?action=deleteRecord&id=" + id;
		document.location = url;
	}
}

function show_intro() {
	document.getElementById("newintro").style.display = "block";
	document.getElementById("intro").style.display = "none";
}


function hide_intro() {
	document.getElementById("newintro").style.display = "none";
	document.getElementById("intro").style.display = "block";
	document.webForm.introname.value = 'Geef hier een naam om de inleiding op te nemen in de lijst met inleidingen';
	document.webForm.intro.value = '';
}

/* login */

function show_chpwd() {
	document.getElementById("changeLoginForm").style.display = "block";
	document.getElementById("loginForm").style.display = "none";
}


function hide_chpwd() {
	document.getElementById("loginForm").style.display = "block";
	document.getElementById("changeLoginForm").style.display = "none";
}


function changenavlang(lang) {
	//alert(lang);
	if (document.webForm) {
		document.webForm.navlang.value = lang;
	}
	if (lang == "i1") {
		document.getElementById("navnl").style.display = "block";
		document.getElementById("navfr").style.display = "none";
		document.getElementById("naven").style.display = "none";
	} else if (lang == "i2") {
		document.getElementById("navnl").style.display = "none";
		document.getElementById("navfr").style.display = "block";
		document.getElementById("naven").style.display = "none";
	} else {
		document.getElementById("navnl").style.display = "none";
		document.getElementById("navfr").style.display = "none";
		document.getElementById("naven").style.display = "block";
	}
}


function selectJobCat(catid) {
	var url = "main.php?action=showRecord&catSelect=" + catid;
	document.location = url;
}


function addEvent(obj, evType, fn) {
  if (obj.addEventListener) {
    obj.addEventListener(evType, fn, false);
    return true;
  } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
  } else {
    return false;
  }
}


/* onload initialise */
addEvent(window, 'load', setScrollPosition);

