// JavaScript Document

var timerID = 0;

/**
  * update the url form field based on the content of another thing (usualy the title etc)
**/
function updateURLField(field, string, table, parent, id) {
	
	var temp = new String(string);
	temp = stripVowelAccent(string);
	temp = temp.replace(/[^a-zA-Z0-9]+/g,'-');
	
	var val = temp.toLowerCase();
	
	// ajax request to check for current item
	if(!parent) {
		parent = '0';
	}
	
	document.getElementById(field).value = temp.toLowerCase();

	clearTimeout(timerID);
	timerID = setTimeout('checkUrl(\''+table+'\', \''+val+'\', \''+parent+'\', \''+id+'\')', 1500);
		
}


/**
  * gets rid of unwanted accented characters in a string
**/
function stripVowelAccent(str) {
	var s = str;
	
	var rExps=[ /[\xC0-\xC2]/g, /[\xE0-\xE2]/g,
	/[\xC8-\xCA]/g, /[\xE8-\xEB]/g,
	/[\xCC-\xCE]/g, /[\xEC-\xEE]/g,
	/[\xD2-\xD4]/g, /[\xF2-\xF4]/g,
	/[\xD9-\xDB]/g, /[\xF9-\xFB]/g ];
	
	var repChar=['A','a','E','e','I','i','O','o','U','u'];
	
	for(var i=0; i<rExps.length; i++) {
		s=s.replace(rExps[i],repChar[i]);
	}
	
	return s;
}


/**
  * check the url name via ajax call
  * return html / string to output to form
**/
function checkUrl(table, val, parent, id) {
	
	var req = new Request({method: 'get', 
						  	autoCancel: true,
						  	url: '/ajax_gateway/checkUrlname.php',
							onSuccess: function(response){
					
								$('urlOk').innerHTML = response;

							}
							}
						  );
	
	req.send('t='+table+'&u='+val+'&p='+parent+'&id='+id);
}


/**
  * holds all info and functions for the timed ajax edit_lock table update
  * php defined global (JAVA_EDIT_TIMER - /defaults/javadefaults.php) holds the reference to the initial call
  * edit manager class sets up data and starts the regular ajax calls 
**/
function setUpdateTimer() {
	
	var userID = null;
	var module_name = null;
	var itemID = null;
	
	var intID = null;
	
	this.setUpdateDetails = function(u, m, i) {
		userID = u;
		module_name = m;
		itemID = i;
	}
	
	this.set = function(time) {
		t = (!time) ? 60000 : time;
		intID = setInterval(updateEditInfo, t, this.userID, this.module_name, this.itemID);
	}
	
	function updateEditInfo() {
		var req = new Request({method: 'get', url: '/ajax_gateway/updateEditInfo.php'});
		req.send('u='+userID+'&m='+module_name+'&i='+itemID);
		
	}
	
}


/**
  * bulk add view restrictions to pages
  * this function is recursive and checks / unchecks all children from a certain level until no more children are found
  * @el: object; the initial checkbox item that is checked
**/
function permissionsTickChildren(el) {
	var val = el.checked;
	var children = $('el_'+el.value).getChildren();
	
	for(var i = 0; i<children.length; i++) {
		children[i].getFirst().checked = val;
		
		if(document.getElementById('el_'+children[i].getFirst().value)) {
			permissionsTickChildren(children[i].getFirst());
		}
	}
}


/**
  * shows or hides the date field for setting an items display dates
  * also affects the notifications date field - simply for the user interface
**/
function showHideDate(val) {
	var disp  = (val) ? "block" : "none";
	var nDisp = (val) ? "none" : "block";
	
	var elements = $$('p.showHide');
	for(var i = 0; i<elements.length; i++) {
		//elements[i].style.display = disp;
		elements[i].setStyle('display', disp);
	}
	
	$$('span#notificationDateField').setStyle('display', nDisp);
}








