var choosenTags = new Array();
var choosenTagsEdit = new Array();

$.fn.isVisible = function () {
	return this.css('visibility') !== 'hidden' && this.css('display') !== 'none';
};

function switchVisibility(id,speed){
	if($('#'+id).isVisible()) $('#'+id).hide(speed);
	else $('#'+id).show(speed);	
}

/*
 * dont use "class" as identidier
 * ie wont work anymore if you do
*/
function switchClassVisibility(classId,speed){
	if($('.'+classId).isVisible()) $('.'+classId).hide(speed);
	else $('.'+classId).show(speed);	
}

function showLoadingScreen(){
	//$('#sendButton').attr('disabled','disabled');
	$('#loadingBox').show();
	return true;
}


function loadKategorieData(kategorieID,tiefe,isEdit){	
	$.ajax({
		type: "GET",
		url: '/ajax/reply_kategorie_request.php',
		data: "katID="+kategorieID+"&isEdit="+isEdit,
		dataType: "html",
		success: function(data){
			insertKategorieData(data,tiefe);
		}
	});
}

function insertKategorieData(data,tiefe){
	$('.artikel_select_element:gt('+(tiefe-1)+')').replaceWith('');
	$('#kategorie_select').append(data);
}

function loadSubKategories(kategorieID){	
	$.ajax({
		type: "GET",
		url: '/ajax/reply_subkategorie_request.php',
		data: "katID="+kategorieID,
		dataType: "html",
		success: function(data){
			$('#search_special').html(data);
		}
	});
}

function bidCheck(minimalgebot){
	var gebot = kovertKomma($('#gebot').attr('value'));
	if(isNaN(gebot)){
		jAlert('Das abgegebene Gebot ist ung&uuml;ltig','Gebot nicht akzeptiert');
		return false;
	}
	
	if(gebot < minimalgebot){
		jAlert('Das Mindest-Gebot betr&auml;gt ' + number_format(minimalgebot,2,',') + ' Swapytaler','Mindestgebot unterschritten');
		return false;
	}
	else{
		bidAlert(gebot);
	}
}

function bidAlert(gebot){
	jConfirm(number_format(gebot,2,',') + ' Swapytaler bieten?', 'Gebot abgeben', function(r) {
		$('#gebot').attr('value',gebot); // set #gebot.value to gebot (with '.'-seperator)
		if(r) $('#bid').submit();
	});
}


function switchEAN(){
	if($('#input').attr('disabled')){
		$('#input,#EANButton').removeAttr("disabled");
		$('#response').show();
		$('#submit_without_ean').hide();
	}
	else{
		$('#input,#EANButton').attr('disabled', 'disabled');
		$('#response').hide();
		$('#submit_without_ean').show();
	}
}

function requestEAN(EAN,kategorieID,isEdit){
	if(EAN != ''){
		$.ajax({
			type: "GET",
			url: '/ajax/reply_ean_request.php',
			data: "ean="+EAN+"&kategorieID="+kategorieID+"&isEdit="+isEdit,
			dataType: "html",
			success: function(data){
				$('#response').html(data);
			}
		});
	}
}

function requestASIN(ASIN,kategorieID,isEdit){
	if(ASIN != ''){
		$.ajax({
			type: "GET",
			url: '/ajax/reply_ean_request.php',
			data: "asin="+ASIN+"&kategorieID="+kategorieID+"&isEdit="+isEdit,
			dataType: "html",
			success: function(data){
				$('#response').html(data);
			}
		});
	}
}

function requestEANInhaltsangabe(EAN){
	tinyMCE.execCommand("mceRemoveControl", true, 'beschreibung');
	if(EAN != ''){
		$.ajax({
			type: "GET",
			url: '/ajax/reply_ean_inhaltsangabe_request.php',
			data: "ean="+EAN,
			dataType: "html",
			success: function(data){	
				$('#eanReply').html(data);
				
			}
		});
		
	}
}

function selectWunsch(id){
	$('#wunsch_form').show();
	$('#taler').hide();
	$('#artikel').hide();
	if(id != 'both'){
		$('#'+id).show();
		$('#choosen').attr('value',id);
	}
	else{
		$('#taler').show();
		$('#artikel').show();
		$('#choosen').attr('value','both');
	}
}

function chooseAllgemeineBewertung(posNeg){
	if(posNeg > 0){
		$('#allgemeine_bewertung_negativ').removeClass('choosenAllgemeineBewertung');
		$('#allgemeine_bewertung_negativ').addClass('notChoosenAllgemeineBewertung');
		$('#allgemeine_bewertung_positiv').removeClass('notChoosenAllgemeineBewertung');
		$('#allgemeine_bewertung_positiv').addClass('choosenAllgemeineBewertung');
		$('#tauschpartnerBewertung').attr('value',1);
		$('#allgemeine_bewertung_negativ_radio').removeAttr('checked');
		$('#allgemeine_bewertung_positiv_radio').attr('checked','checked');
	}
	if(posNeg < 0){
		$('#allgemeine_bewertung_positiv').removeClass('choosenAllgemeineBewertung');
		$('#allgemeine_bewertung_positiv').addClass('notChoosenAllgemeineBewertung');
		$('#allgemeine_bewertung_negativ').removeClass('notChoosenAllgemeineBewertung');
		$('#allgemeine_bewertung_negativ').addClass('choosenAllgemeineBewertung');
		$('#tauschpartnerBewertung').attr('value',-1);
		$('#allgemeine_bewertung_positiv_radio').removeAttr('checked');
		$('#allgemeine_bewertung_negativ_radio').attr('checked','checked');
	}
}


/* Bewertungen */

var maxBewertung = 5;

function bewertung_rating_over(ebene, element){
	for(var i = element; i > 0; i--) 
		if(i != $('#bewertung_' + ebene).attr('value'))
			$('#bewertung_' + ebene + '_' + i).attr('src','/img/symbol/rating_over.gif');
	
}

function bewertung_rating_off(ebene, element){
	
	for(var i = element; i > 0; i--) 
		if(i > $('#bewertung_' + ebene).attr('value')) // wenn i nicht der bereits selektierte Wert ist
			$('#bewertung_' + ebene + '_' + i).attr('src','/img/symbol/rating_off.gif');
}

function bewertung_select(ebene, element){

	for(var i = 1; i <= maxBewertung; i++) $('#bewertung_' + ebene + '_' + i).attr('src','/img/symbol/rating_off.gif');

	for(var i = element; i > 0; i--) $('#bewertung_' + ebene + '_' + i).attr('src','/img/symbol/rating_on.gif');
	
	
	$('#bewertung_' + ebene).attr('value',element);
	
}


function limitChars(textid, limit, infodiv){
	var text = $('#'+textid).val(); 
	var textlength = text.length;
	if(textlength > limit){
		$('#' + infodiv).html('Es sind maximal '+limit+' Zeichen erlaubt!');
		$('#'+textid).val(text.substr(0,limit));
		return false;
	}
	else{
		$('#' + infodiv).html('Du kannst noch '+ (limit - textlength) +' Zeichen eingeben.');
		return true;
	}
}
 
 


function getSuggestedTags(startingLetters){
		
	$.ajax({
		type: "GET",
		url: '/ajax/suggest_tags.php',
		data: "startingLetters="+startingLetters,
		dataType: "html",
		success: function(data){
			//insertKategorieData(data,tiefe);
			$('#suggestedTags').html(data);
		}
	});

}

function completeSuggestionTag(tagName){
	$('#tagInputField').attr('value',tagName);
	$('#suggestedTags').html('');
}

function addTag(tagName){

	tagName = tagName.toLowerCase();

	if(tagName != '' && tagName.match(/^[a-z0-9äöüß\s]+$/)){
	
		var contains = false;
	
		for(var i = 0; i < choosenTags.length; i++){
			if(choosenTags[i] == tagName) contains = true;
		}
		
		
		
		if(!contains){
	
			$.ajax({
				type: "GET",
				url: '/ajax/add_tag.php',
				data: "tagName="+tagName,
				dataType: "html",
				success: function(data){				
					$('#tagInputField').attr('value','');
					$('#suggestedTags').html('');
					
					$('#choosenTags').html(data);
					choosenTags.push(tagName);
					
				}
			});
		}
		
	}
}

function removeTag(tagName){

	var elementFound = false;
	for(var i = 0; i < choosenTags.length; i++){
		if(choosenTags[i] == tagName) elementFound = true;
		if(elementFound) choosenTags[i] = choosenTags[i+1];
	}
	choosenTags.pop();	

	if(elementFound){
	
		$.ajax({
			type: "GET",
			url: '/ajax/remove_tag.php',
			data: "tagName="+tagName,
			dataType: "html",
			success: function(data){					
				
				$('#choosenTags').html(data);
					
			}
		});
	}

}


function number_format (number, decimals, dec_point, thousands_sep){ // from http://www.fobit.com
  var exponent = "";
  var numberstr = number.toString ();
  var eindex = numberstr.indexOf ("e");
  if (eindex > -1){
    exponent = numberstr.substring (eindex);
    number = parseFloat (numberstr.substring (0, eindex));
  }
  
  if (decimals != null){
    var temp = Math.pow (10, decimals);
    number = Math.round (number * temp) / temp;
  }
  var sign = number < 0 ? "-" : "";
  var integer = (number > 0 ? 
      Math.floor (number) : Math.abs (Math.ceil (number))).toString ();
  
  var fractional = number.toString ().substring (integer.length + sign.length);
  dec_point = dec_point != null ? dec_point : ".";
  fractional = decimals != null && decimals > 0 || fractional.length > 1 ? 
               (dec_point + fractional.substring (1)) : "";
  if (decimals != null && decimals > 0){
    for (i = fractional.length - 1, z = decimals; i < z; ++i)
      fractional += "0";
  }
  
  thousands_sep = (thousands_sep != dec_point || fractional.length == 0) ? 
                  thousands_sep : null;
  if (thousands_sep != null && thousands_sep != ""){
	for (i = integer.length - 3; i > 0; i -= 3)
      integer = integer.substring (0 , i) + thousands_sep + integer.substring (i);
  }
  
  return sign + integer + fractional + exponent;
}

function kovertKomma(Wert){
  var PosPunkt = Wert.indexOf(".",0);
  var PosKomma = Wert.indexOf(",",0);
  if (PosKomma < 0) return Wert;

  while ((0 <= PosPunkt) && (PosPunkt < PosKomma))
    {
    Wert = Wert.substring(0, PosPunkt) + Wert.substring(PosPunkt + 1, Wert.length);
    PosPunkt = Wert.indexOf(".",0);
    PosKomma--;
    }

  PosKomma = Wert.indexOf(",",0);
  if (PosKomma >= 0)
    {
    Wert = Wert.substring(0, PosKomma) + "." + Wert.substring(PosKomma + 1, Wert.length);
    }

  return parseFloat(Wert);
}

function kommentarAntwort(kommentarID, memberID){
	if($('#kommentar_antwort_' + kommentarID).is(':visible')){
		$('#antworten_link_' + kommentarID).html('Antworten');
		$('#kommentar_antwort_' + kommentarID).hide();
	}
	else{
		$('#kommentar_antwort_' + kommentarID).show();
		$('#antwortAufHidden_' + kommentarID).attr('value',kommentarID);
		$('#kommentarAufID_' + kommentarID).html(kommentarID);
		$('#antworten_link_' + kommentarID).html('Abbrechen');
	}
	
}

function checkHochgeladeneBilder(){
	if($('#hochgeladene_bilder').length == 0){
		return confirm('Sicher dass keine Bilder verwendet werden sollen? Angebote ohne Bilder werden auf der Startseite nicht angezeigt.');
	}
	else{
		return true;
	}
}

function checkBewertung(){
	var tauschabwicklung = $('#bewertung_6').val();
	var zustand = $('#bewertung_4').val();
	var schnelligkeit = $('#bewertung_3').val();
	var tauschpartnerBewertung = $("input[name='tauschpartnerBewertung']:checked").val();
	var kommentar = $('#kommentarArea').val();

	if(tauschabwicklung == 0 || zustand == 0 || schnelligkeit == 0 || (typeof(tauschpartnerBewertung) == 'undefined' && typeof($('#tauschpartnerBewertung').val()) != 'undefined') || kommentar == ''){
		alert('Es müssen alle Bewertungen abgegeben werden.');
		return false;
	}
	
	var confirm_string = "Folgende Bewertung wirklich abgeben?\r\n\r\n";

	if(typeof(tauschpartnerBewertung) != 'undefined'){
		confirm_string += "Tauschpartner-Bewertung:\t" +((tauschpartnerBewertung == 1) ? 'positiv' : 'negativ') + '\r\n\r\n';
	}
	if(tauschabwicklung != 0 && typeof(tauschabwicklung) != 'undefined') confirm_string += 'Tauschabwicklung:\t\t\t' + tauschabwicklung + ' von 5 Sterne\r\n';
	if(zustand != 0 && typeof(zustand) != 'undefined') confirm_string += 'Artikelzustand:\t\t\t\t' + zustand + ' von 5 Sterne\r\n';
	if(schnelligkeit != 0 && typeof(schnelligkeit) != 'undefined') confirm_string += 'Versandschnelligkeit:\t\t' + zustand + ' von 5 Sterne\r\n\r\n';

	if(kommentar != '' && typeof(kommentar) != 'undefined'){
		confirm_string += 'Kommentar:\t\t\t\t' + kommentar;
	}
	return confirm(confirm_string);

}

function loadFreundWerben(schritt, typ, themenfeld, groesse){	
	$.ajax({
		type: "GET",
		url: '/ajax/reply_freunde_werben_request.php',
		data: "schritt="+schritt+"&typ="+typ+"&themenfeld="+themenfeld+"&groesse="+groesse,
		dataType: "html",
		success: function(data){
			if(schritt=="themenfeld"){
				$('#themenfeld').html(data);
				$('#groesse').html('');
				$('#werbemittel').html('');
				$('#ergebnis').html('');
			}
			if(schritt=="groesse"){
				$('#groesse').html(data);
				$('#werbemittel').html('');
				$('#ergebnis').html('');
			}
			if(schritt=="werbemittel"){
				$('#werbemittel').html(data);
				$('#ergebnis').html('');
			}
		}
	});
}

function loadFreundWerbenErgebnis(id){	
	$.ajax({
		type: "GET",
		url: '/ajax/reply_freunde_werben_request.php',
		data: "schritt=ergebnis&id="+id,
		dataType: "html",
		success: function(data){
			$('#ergebnis').html(data);
		}
	});
}

