/* saebo js */

function getSortableOrderIds(elementId,hiddenId){
	var sortableListData = Sortable.serialize(elementId);		
	if($(hiddenId))	{
		$(hiddenId).value = sortableListData;					
	}
}

// check all form elements
function checkAll(form,tmatch) {
	for(i=0;i<form.length;i++){
		if(form.elements[i].name.match(tmatch)){
			form.elements[i].checked=true;
		}
	}
}
// uncheck all formelements
function uncheckAll(form,tmatch) {
	for(i=0;i<form.length;i++){
		if(form.elements[i].name.match(tmatch)){
			form.elements[i].checked=false;
		}
	}
}
// AJAX: send data to php script in background
// vars -> query string
// action -> action for switch in ajax.php
// message_container -> id of html element
function sendRequest(vars,action,message_container){
	URL = '/ajax.php?ajax-action='+action+'&'+vars;
	new Ajax.Request(
		URL,
		{
			method: 'post',
			//encoding: 'ISO-8859-1',			
			onLoading: function()
			{
				document.getElementById(message_container).innerHTML = '<img src="/images/icons/ajax-loader.gif" alt="Wird geladen" align="left" />&nbsp;&nbsp;Bitte warten Sie einen Moment!<br clear="all" />';				
			},									
			onSuccess: function(transport) 
			{
				if(document.getElementById(message_container))
				{
					document.getElementById(message_container).innerHTML = transport.responseText;
				}
				else
				{
					alert(transport.responseText);
				}				
			},			
			onFailure: function(){ alert('Something went wrong...'); },
			onException: function(){ alert('Something went wrong...'); }
		}
	);
}

function getAjaxPreview() {
	var intError = true;
	
	var strVariables = '';

	// get edition values
	var requestArrEdition = new Array();
	var editionElem = document.forms[1].elements["advertisement_edition[]"];
	var editionLength = editionElem.length;
	for(var i = 0; i < editionLength; i++)
	{				
		if(editionElem[i].checked == true)
		{
			requestArrEdition.push(editionElem[i].value); 
		}		
	}	
	// build request string
	// edition
	strVariables += 'edition=' + requestArrEdition.join('||');
	// headline 

	strVariables += '&headline=' + escape($('advertisement_headline').value);	
	// text
	//strVariables += '&text=' + escape($('advertisement_body').value);
	strVariables += '&text=' + encodeURIComponent($('advertisement_body').getValue());
	
	//chiffre
	var chiffreElem =  document.forms[1].elements["advertisement_chiffre"];
	var chiffreLength = chiffreElem.length;
	for(var i = 0; i < chiffreLength; i++)
	{				
		if(chiffreElem[i].checked == true)
		{
			strVariables += '&chiffre=' + chiffreElem[i].value; 
		}		
	}
	// category
	strVariables += '&fk_category_id=' + escape($('fk_category_id').value);
	
	// count publication dates
	var dateCount = 0;
	var datesElem = document.forms[1].elements["publicationdates[]"];	
	var datesLength = datesElem.length; 	 
	if(!datesLength){
    dateCount = 1;
  }
	for(var i = 0; i <= chiffreLength; i++)
	{
		if(datesElem[i] && datesElem[i].checked == true)
		{
			dateCount++;
		}
	}	
	strVariables += '&dates='+dateCount;
	
	// fk_user_id
	if($('fk_user_id'))
	{
		strVariables += '&fk_user_id=' + $('fk_user_id').value;
	}
	sendRequest(strVariables,'adv-preview','adv-preview');	
}

function getAjaxPassword() {		
	var strVariables = '';
	sendRequest(strVariables,'gen-pw','gen-pw');	
}

function checkEmail(s)
{
 var a = false;
 var res = false;
 if(typeof(RegExp) == 'function')
 {
  var b = new RegExp('abc');
  if(b.test('abc') == true){a = true;}
  }

 if(a == true)
 {
  reg = new RegExp('^([a-zA-Z0-9\\-\\.\\_]+)'+
                   '(\\@)([a-zA-Z0-9\\-\\.]+)'+
                   '(\\.)([a-zA-Z]{2,4})$');
  res = (reg.test(s));
 }
 else
 {
  res = (s.search('@') >= 1 &&
         s.lastIndexOf('.') > s.search('@') &&
         s.lastIndexOf('.') >= s.length-5)
 }
 return(res);
} 
