var tinyMCE;

function submitListen(options) { 
    $('#'+options.formId).submit(function() {
    	if (tinyMCE!=null) tinyMCE.triggerSave(); 
        $(this).ajaxSubmit(options).block({message: $("#wmsg"), css: {border:'none', backgroundColor: 'transparent'}, overlayCSS: {backgroundColor: '#fff'}}).ajaxStop(function(){$(this).unblock()}); 
        return false; 
    });
}
function checkErrors(xmldata)  { 
			    
			    iserror = $("message[name='iserror']", xmldata).text();
			    message =  $("message[name='message']", xmldata).text();
			    string =  $("message[name='string']", xmldata).text();
			    if (iserror) {
			    	showError();
			    	return true;
			    }
			    else return false;
			    
}
function showError() {
	
	if (!document.getElementById('float_error')) {
		$('<div id="popup_bg"></div><div id="float_error"><div id="err_text"></div><div id="err_string"></div><div id="a_cont" align="center"><a href="" onclick="hideError(); return false;">OK</a></div></div>').appendTo('body');
	}
	$('#popup_bg').css("width", document.body.clientWidth+"px").css("height", document.body.clientHeight+"px").css("top", 0+"px").css("left", "0px").fadeTo("100", "0.7");
	$("#err_text").empty().append(message);
	$("#err_string").empty().append(string);
	$('#float_error').css("top", 300+"px");
	$('#float_error').fadeIn(100);

}

function hideError() {
	$('#float_error').fadeOut(200);
	$('#popup_bg').fadeOut(200);
}

function popup(url, width, height, opacity) {

	if (!width) width = 700;
	if (!height) height = 600;
	if (!opacity) opacity = 1;
	
	left_offset = Math.round((document.body.clientWidth - width) / 2);
	top_offset = Math.round((document.body.clientHeight - height) / 2);
	
	if (left_offset<0) left_offset = 0;
	if (top_offset<0) top_offset = 0;
	
	
	if (!document.getElementById('popup')) {
		$('<div id="popup_bg"></div><div id="popup"><div id="popup_close"><a href="javascript://" onclick="popupClose();"><img src="/images/main/delete_button.png" /></a></div><div id="popup_content"></div></div>').appendTo('body');
	}
	$('#popup_content').empty().attr("class", "indicator");
	$('#popup_content').css("height", height+"px")
	
	$.ajax({
		type:"POST",
		url:url,
		data: "no_xml=1",
		dataType:"html",
		async:true,
		success: function(source){
						
			$('#popup_content').empty().attr("class", "").append(source);
			
		}
	});
	
	switch(navigator.appName)
	{
	  case 'Microsoft Internet Explorer':
	    scrollY = document.documentElement.scrollTop;
	  break;
	  default:
	    scrollY = window.pageYOffset;
	}
	
	$('#popup_bg').css("width", document.body.clientWidth+"px").css("height", document.body.clientHeight+"px").css("top", 0+"px").css("left", "0px").fadeTo("200", "0.7");
	$('#popup').css("width", width+"px").css("top", top_offset+"px").css("left", left_offset+"px").css("opacity", opacity).fadeIn(200);

}

function popupClose() {
	$('#popup').fadeOut(300);
	$('#popup_bg').fadeOut(300);
}

function submitForm(formId) {

	$('#'+formId).block({message: null, overlayCSS: {backgroundColor: '#fff'}});
	document.getElementById(formId).submit();

}
function confirmDelete(deleteUrl, deleteObj){
	
	if (!document.getElementById('popup')) {
		$('<div id="popup_bg"></div>').appendTo('body');
	}
	$('#popup_bg').css("width", document.body.clientWidth+"px").css("height", document.body.clientHeight+"px").css("top", 0+"px").css("left", "0px").fadeTo("100", "0.7");
	$("#drop_warning_link").attr("href", deleteUrl);
	$("#del_obj").empty().append(''+deleteObj+'');
	$('#drop_warning').css("top", 300+"px");
	$('#drop_warning').fadeIn(100);
	
}
function hideWarning() {
	$('#drop_warning').fadeOut(200);
	$('#popup_bg').fadeOut(200);
}
function process_file_name(input) {
	
		var filemask;
		var slidedown = 0;
		
		if(navigator.appName.indexOf("Explorer")!=-1) filemask = /.+\\(.+)\.([0-9a-zA-Z]{1,6})$/;
		else filemask = /(.+)\.([0-9a-zA-Z]{1,6})$/;
		
		var fileattr = filemask.exec(input.value);
		var filename = translit(fileattr[1]);
		var fileext = fileattr[2].toLowerCase();
		
		filename = filename + "." + fileext;
		
		var fileexts = new Array("jpg", "jpeg", "png", "gif");
		
		
		
		for (i=0; i<fileexts.length; i++) {
			
			if (fileext == fileexts[i]) slidedown = 1;
			
				
		}
		
		if (slidedown) {
			if ($("#img_panel").css("display")=="none") $("#img_panel").slideDown(100);
		}
		else $("#img_panel").css("display", "none");
		
		$("#file_name").attr("value", filename);
		
}

function dump(arr,level) {
	var dumped_text = "";
	if(!level) level = 0;
	
	//The padding given at the beginning of the line.
	var level_padding = "";
	for(var j=0;j<level+1;j++) level_padding += "    ";
	
	if(typeof(arr) == 'object') { //Array/Hashes/Objects
	 for(var item in arr) {
	  var value = arr[item];
	 
	  if(typeof(value) == 'object') { //If it is an array,
	   dumped_text += level_padding + "'" + item + "' ...\n";
	   dumped_text += dump(value,level+1);
	  } else {
	   dumped_text += level_padding + "'" + item + "' => \"" + value + "\"\n";
	  }
	 }
	} else { //Stings/Chars/Numbers etc.
	 dumped_text = "===>"+arr+"<===("+typeof(arr)+")";
	}
	return dumped_text;
} 

function date2timestamp(year, month, day, hour, min, sec) {
  return (Date.UTC(year, month-1, day, hour, min, sec) / 1000);
}
