// Show Ajax Errors

function showError(r){
  //console.log(r);
  if((r.responseText != "")&&($('ajax_result'))){
  var re = new RegExp(/<body>([\s\S]*)<\/body>/m);
  var nobody = re.exec(r.responseText);
  $('ajax_result').setHTML(nobody[1]);
  $('ajax_result').setStyle('display', 'block');
  }else{
  alert("Error: " + r.status + " - " + r.statusText);
  }
}


// Extra Check for Ajax Error in XML

function CheckIfError(r){

	var result = r.documentElement;
	var error = result.getElementsByTagName('error')[0].firstChild.data;
	var error_string = result.getElementsByTagName('error_string')[0].firstChild.data;

	if(error == 1){
		if(error_string != ""){
			alert("Error: " + error_string);
		}

	return true;
	}

return false;
}

// rating functions

function setRating(rating, media_key){

	var myAjax = new Ajax(
		"/ajax_com.php?action=mediarating",
			{
				method:'post',
        onFailure: function(e){showError(this.transport)},
        onComplete: function(e){showRatingResult(this.transport.responseXML)},
        data:'rating=' + rating + '&mediakey=' + media_key
			}
	).request();
}

function showRatingResult(r){
  //r.responseXML.documentElement.getElementsByTagName('stars')[0].firstChild.data;

	if(CheckIfError(r)) return ;
	var result = r.documentElement;
	var stars = result.getElementsByTagName('stars')[0].firstChild.data;
  $('rating_result').setHTML(stars);
}


function checkMediaForm(){


	if(!$('comment_input_id').value){
		$('comment_input_id').focus();

		new Fx.Style('comment_input_id', 'background-color').start('#FF0000','#ffffff');

		return false;
	}

	var form_value = $('comment_input_id').value;
	form_value = form_value.replace(/<\/?[^>]+>/gi, '');

  var myAjax = new Ajax(
		"/ajax_com.php?action=comments",
			{
				method:'post',
				onFailure:function(e){showError(this.transport)},
				onComplete:function(e){showCommentResult(this.transport.responseText)},
				data: 'comment=' + form_value + '&panne=' + $('panne_id').value
			}
	).request()
return false;
}


function showCommentResult(r){
	$('comment_input_id').value = '';
  $('comment_show').setHTML(r);
  $('comment_display').setStyle('display','none');
  $('comment_hidden').setStyle('display','block');
}


function toggleSaveButton(){
  
  var save_src = '/templates/default/images/german/save.gif'
  
  if(window.frames['iframe_upload_name'].document.getElementById('upload_success_message') == null){
    save_src = '/templates/default/images/german/save_disable.gif'
  };  
   
  if($('upload_media_title_id').value==""){
    save_src = '/templates/default/images/german/save_disable.gif'
  }
  if($('upload_media_description_id').value==""){
    save_src = '/templates/default/images/german/save_disable.gif'
  }
  if($('upload_media_tags_id').value==""){
    save_src = '/templates/default/images/german/save_disable.gif'
  }
  if($('upload_media_city_id').value==""){
    save_src = '/templates/default/images/german/save_disable.gif'
  }
  if($('upload_title_id').value==""){
    save_src = '/templates/default/images/german/save_disable.gif'
  };
 
 $('upload_submit_id').src = save_src;
};

function checkUploadForm(){
 var all_ok = true;
 var msg = "";
  if(window.frames['iframe_upload_name'].document.getElementById('upload_success_message') == null){
    all_ok = false;
    msg += 'Bitte lade eine Datei hoch.\n';
  };   
  if($('upload_media_title_id').value==""){
    all_ok = false;
    msg += 'Bitte gib einen Titel für die Panne ein.\n';
  }
  if($('upload_media_description_id').value==""){
    all_ok = false;
    msg += 'Bitte die Beschreibung des Mediums vergeben.\n';
  }
  if($('upload_media_tags_id').value==""){
    all_ok = false;
    msg += 'Bitte gib Tags an.\n';
  }
  if($('upload_media_city_id').value==""){
    all_ok = false;
    msg += 'Bitte gib einen Ort ein.\n';
  }
  if($('upload_title_id').value==""){
    all_ok = false;
    msg += 'Bitte gib einen Titel für das Medium ein.\n';
  }
  
  if(all_ok == false){
   alert(msg);
   return false;
  }else{
   $('upload').submit();
  };
  return false;	
}

function showMediaDescription(show){
 if(show==1){
  $('description_text').setStyle('display','block');
  $('bookmarks_text').setStyle('display','none');
  $('description_title').setStyle('color','#FF8C05');
  $('description_div').setStyle('background-image','url(/templates/default/images/icons/point_menu.gif)');
  $('bookmarks_title').setStyle('color','#979797');
  $('bookmarks_div').setStyle('background-image','url(/templates/default/images/icons/point_menu_gray.gif)');
 }else{
   $('description_text').setStyle('display','none');
  $('bookmarks_text').setStyle('display','block');
  $('description_title').setStyle('color','#979797');
  $('description_div').setStyle('background-image','url(/templates/default/images/icons/point_menu_gray.gif)');
  $('bookmarks_title').setStyle('color','#FF8C05');
  $('bookmarks_div').setStyle('background-image','url(/templates/default/images/icons/point_menu.gif)');
 };
}
