String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

function show(id) {
  document.getElementById(id).style.display="block";
}

function cannot_be_empty(id,text) {
  var value=document.getElementById(id).value;
  if (value=="") {
    alert(text); 
    return false;
  }
  else {
    return true;
  }
}


function getElementsByClassName(oElm, strTagName, strClassName){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements);
}

function submit_catalog(url, id) {
	var location=document.getElementById(id).value;
	window.location=url+"?catalog="+location;
}

/*Check whether the required input fields are empty or not before making an effort to locate a dealership on the map*/

function check_address_input() {
  if(document.getElementById("DealerCity").value=="" || document.getElementById("DealerAddress").value=="") {

    document.getElementById('try_to_locate_error').innerHTML="Please fill in both the city and address fields to make the automatic locator work!";
    return false;
  }
  else {
    document.getElementById('try_to_locate_error').innerHTML="";
    return true;
  }
}


/*function for completing the city's name to Budapest, when the county is set to Budapest*/

function autocomplete_budapest() {
  var county=document.getElementById('DealerCounty').value;
  if(county=="Budapest") {
    document.getElementById("DealerCity").value="Budapest";
  }
}


/*this will enable the user to step back to the search results from the dealer profiles*/

function show_dealer_search_results() {
  document.getElementById("dealer").style.display="none";
  document.getElementById("dealerSearch").style.display="block";
  document.getElementById('map').style.display='block';
  display_map();
  show_markers_on_map();
}

function show_dealer_profile() {
  document.getElementById('map').style.display='none';
  document.getElementById('dealerSearch').style.display='none';
  document.getElementById('dealer').style.display='block';
  show_profile_map();
}



function picture_popup (img, height, width, url)
{
	window.open( url + "/show_picture_popup/" + img , "PictureWindow", "status = no, height = " + height + ", width = " + width + ", resizable = 0" )
}

function ad_details_popup (id, height, width, url)
{
	window.open( url + '/show_ad_details_popup/' + id , 'AdWindow', 'height = 300, width = 300, scrollbars = yes, resizable = no, status = no' )	
}

function write_html_element(text,id)
{
	x = document.getElementById(id);
	x.innerHTML = '';
	x.innerHTML = text;
}


function tinyMCEInsertHTML(html) {
  tinyMCE.activeEditor.execCommand("string.replace", "[IMAGEGALLERY]", "");
  tinyMCE.activeEditor.execCommand("mceInsertContent",false, html);
  var images = $('images').value;
  $('ArticlegalleryImageIds').value = images;
}


function tinyMCEcopyDiv( source )
{
	x = document.getElementById(source);
	tinyMCE.activeEditor.execCommand('mceInsertContent', false, x.innerHTML);
	//tinyMCE.activeEditor.execCommand('mceInsertRawHTML', false, x.innerHTML);
	//tinyMCE.activeEditor.setContent(x.innerHTML);
	
}


function tinyMCEInsertGallery(  ) {
  tinyMCE.activeEditor.dom.remove("gallery_container", false);
  
  var el = tinyMCE.activeEditor.dom.create('p', {id: 'gallery_container'});
  tinyMCE.activeEditor.selection.setNode(el);
  
  tinyMCE.activeEditor.dom.setHTML('gallery_container', 'IMAGEGALLERY START<div class="temporary_gallery">Images will be loaded there</div>IMAGEGALLERY END');
  
//  var el = tinyMCE.activeEditor.dom.create('div', {id : 'gallery_container', class : 'temporary_gallery'}, 'Image gallery will be rendered here');
//  tinyMCE.activeEditor.selection.setNode(el);


  var images = $('images').value;
  $('ArticlegalleryImageIds').value = images;
}

/*Content editor: this function will load predefined image sizes to the appropriate box*/

function load_picture_size(from, input_w_id, input_h_id, output_w_id, output_h_id) {
  var original_width=document.getElementById(input_w_id).value;   //the original width's ID is same as "to"
  var original_height=document.getElementById(input_h_id).value;
  var selected_width=document.getElementById(from).value;
  if (selected_width!="") {  
    document.getElementById(output_w_id).value=selected_width;  
    calculateImageHeight(original_width, original_height, selected_width, output_h_id);
  }
}


function calculateImageHeight ( origWidth, origHeight, newWidth, targetId )
{
	var target = document.getElementById(targetId);
	var newHeight = newWidth * origHeight / origWidth;
	target.value = (Math.round(newHeight)).toString();
}

function calculateImageWidth ( origWidth, origHeight, newHeight, targetId )
{
	target = document.getElementById(targetId);
	newWidth = newHeight * origWidth / origHeight;
	target.value = (Math.round(newWidth)).toString();
}

function flash_apply_msg() {
  Element.show("apply_msg");new Effect.Highlight("apply_msg");void(0);
}

function clearFileInput ( id )
{
	x = document.getElementById(id);
	x.value = '';	
}


function checkAllFields(ref)
{
/*var chkAll = document.getElementById('checkAll');*/
var chkAll = false;
/*var checks = document.getElementsByName('data[Picture][File_checked]'); */
var checks = document.getElementById('scrolldiv').getElementsByTagName('input'); 
var copyButton = document.getElementById('copyChecked');
var boxLength = checks.length;
var allChecked = false;
var totalChecked = 0;
	if ( ref == 1 )
	{
		if ( chkAll.checked == true )
		{
			for ( i=0; i < boxLength; i++ )
			checks[i].checked = true;
		}
		else
		{
			for ( i=0; i < boxLength; i++ )
			checks[i].checked = false;
		}
	}
	else
	{
		for ( i=0; i < boxLength; i++ )
		{
			if ( checks[i].checked == true )
			{
			allChecked = true;
			continue;
			}
			else
			{
			allChecked = false;
			break;
			}
		}
		if ( allChecked == true )
		chkAll.checked = true;
		else
		chkAll.checked = false;
	}
	for ( j=0; j < boxLength; j++ )
	{
		if ( checks[j].checked == true )
		totalChecked++;
	}
	if (totalChecked > 5 )
	{
		alert("You may only upload 20 pictures at a time!\n\nPlease uncheck some if you want to select others!");
		for ( i=0; i < boxLength; i++ )
		{
			if ( checks[i].checked == true )
			{
			}
			else
			{
					checks[i].visibility = 'hidden';
			}
	
		}
	}
	else
	for ( i=0; i < boxLength; i++ )
	{
		checks[i].disabled = false;
	}	
	copyButton.value = "Copy ["+totalChecked+"] Selected";
}

function unCheckAllFields()
{
	var checks = document.getElementById('scrolldiv').getElementsByTagName('input'); 
	var copyButton = document.getElementById('copyChecked');
	var boxLength = checks.length;
	var totalChecked = 0;
	for ( i=0; i < boxLength; i++ )
		checks[i].checked = false;		
}

function mainimage_menu() {
  var menuitems=$('mainMenu').childElements();
  var menuitem_counter=0;
}

function playflv(id) {
  flowplayer(id, "/player/flowplayer-3.1.5.swf",  { 
    clip: { 
      autoPlay: false,  
      autoBuffering: true
    } 
  });
}

function launch_videos() {
  $$("a.embedded_video").each(function(e) {
    playflv(e.id);
  })
}


function show_flv(id, url, file) {
	alert("Showflv-be eljut");
	var s1 = new SWFObject( url+'player/mediaplayer.swf', 'player'+id, '240', '180', '7');
	s1.addParam('allowfullscreen', 'true');
	s1.addParam('allowscriptaccess', 'always');
	s1.addParam('flashvars','file='+url+file);
	alert("S1 write: "+"preview_"+id);
	if(s1.write('preview_'+id)) {
	}
	else {
	}
	
}


function imagetransition() {
  var divs = $$('div.mainimage');               //the wrapper divs of the images
  var top_div = divs[0];                        //wrapper of the first image
  var new_div = top_div.cloneNode(true);      //ez másolja le a div-et, és nem az innerHTML
  new_div.style.display = 'none';
  new_div.setAttribute('id', 'showthis');
  var container = $('mainimage_container');
  container.appendChild(new_div);
  
  var top_div_id = top_div.getAttribute('id');
  top_div.setAttribute('id','removethisdiv');      //we'll set 

  new Effect.Appear('showthis',  {duration: 1.6,

    afterFinish: function() {
      container = document.getElementById('mainimage_container');
      container.removeChild(document.getElementById('removethisdiv'));
      new_div.setAttribute('id', top_div_id);
    }     
  });

  
}

var first_or_not = 0;

function slideshow(delay) {
  var slideshowdelay = delay * 1000;
  t = setTimeout("slideshow("+delay+")", slideshowdelay);
  if(first_or_not == 0) {
    first_or_not = 1;
  }
  else {
    imagetransition();
  }
}

function test_image() {
  new Effect.Appear('test_image', {duration: 6, from: 0.0, to: 1.0});
}


function panelslider() {
  var divs = $$('div.akciostermek');
  var top_div = divs[0];
  var top_div_id = top_div.getAttribute('id');
  var top_div_new_id = top_div.setAttribute('id','removethisdiv');
  
  new Effect.Move('sliding_area', {x:0, y:-250, duration:2, mode: 'relative', 
      beforeStart: function() {
        t = setTimeout("",1000);
        new Effect.Fade('removethisdiv', {duration:2.5});
        var top_div_content = top_div.innerHTML;
        var container = $('sliding_area');
        var new_div = document.createElement('div');
        new_div.setAttribute('id', top_div_id);
        new_div.setAttribute('class', 'akciostermek');
        new_div.style.display = 'none';
        new_div.innerHTML = top_div_content;
        container.appendChild(new_div);    
        new Effect.Appear(top_div_id);
 
      },
      afterFinish: function() {
        sliding_area = document.getElementById('sliding_area');
        sliding_area.style.top = '0px';        
        sliding_area.removeChild(document.getElementById('removethisdiv'));
      }
  });
  return true;
}

function slide_panel()
{
  t = setTimeout("slide_panel()",5000);
  panelslider();
}


function contentmanager_tab_change() {
  
  //console.log("Meghívódott");
  new Effect.Opacity('tinycontainer', {from: 1.0, to: 0, duration: 0.4});   //dimm
  new Effect.BlindUp('tinycontainer', {duration:0.8, afterFinish: function() {
    new Effect.Opacity('tinycontainer', {from: 0, to: 1.0, duration: 0.6});
    new Effect.BlindDown('tinycontainer',{duration:0.8});
    }
  });
}

var pos = 0;
var stop = 0;

function slide(direction, imagestrip_id, window_id) {
  window_width = document.getElementById(window_id).offsetWidth;
  imagestrip_width = document.getElementById(imagestrip_id).offsetWidth;
  width = imagestrip_width - window_width;  
  if(direction == "forward") {
    if(pos > 0-width) {
      pos = pos - 2;
    }
  }  
  if(direction == "backward") {
    if(pos < 0) {
      pos = pos + 2;
    } 
  } 
  if(stop != 1) {
    t = setTimeout("slide('"+direction+"', '"+imagestrip_id+"', '"+window_id+"')", 2);
    do_slide(direction, imagestrip_id);
  }
  stop = 0;
}

function do_slide(direction, imagestrip_id) {
  if(stop!=1) {
    if(direction == "forward") {
      $(imagestrip_id).style.left= pos+1+"px";
      $(imagestrip_id).style.left= pos+"px";
    }
    else {
      $(imagestrip_id).style.left= pos-1+"px";
      $(imagestrip_id).style.left= pos+"px";
    }
  }
}

function stop_slide() {
  stop = 1;  
} 

function checkSelectedImages() {
  if($('images').value == '') {
    alert('Please select some images for the gallery by dragging them from the upper box into the lower box!');
    return(false);
  }
  else {
    return(true);
  }
}


/*Used when generating articlegallery previews in the contentitem editor*/

