var notifShowTime = 10000;
var notifShowTimeError = 10000;

function hideRoundedBox() {
  var divs;
  var box;
  divs = document.getElementsByTagName("div");
  for (var i = 0; i < divs.length; i++) {
    d = divs[i];
    if (d.className == "boxRound") {
      v = findDiv(d.innerHTML, "<DIV");
      f = findDiv(d.innerHTML, "<FORM");
      if ((v > -1) || (f > -1)) {
        c = d.parentNode.className.replace("hiddenBox", "");
        d.parentNode.className = c;
      }
    }
  }
}
function trim(stringToTrim) {
  return stringToTrim.replace(/^\s+|\s+$/g, "");
}
function findDiv(stringToSearch, el) {
  source = stringToSearch.toUpperCase();
  return source.indexOf(el, 0);
}

function canCookie(){
	writeCookie("test", "musicbox", null, "/", null, null);
	if (readCookie("test")=="musicbox")
	{
		removeCookie("test","/",null)
		cookie_set = true;
		return true;
	}
	else
	{	
	    notificationShow(0,'Diese Funktion ist nicht m&ouml;glich!<br><br>Bitte aktivieren Sie Cookies!',notifShowTimeError,false,false);
		return false;
	}
}


function readCookie(name) {
   var theCookie = document.cookie;

   // Anfangsposition des Name=Wert-Paars suchen
   var posName = theCookie.indexOf("; " + name + "=");
   if (posName == -1) {
      // vielleicht war's der erste Name in der Liste?
      if (theCookie.indexOf(name + "=") == 0) posName = 0;
      // nein? dann abbrechen mit R?ckgabewert null
      else return null;
   }

   // Anfangs- und Endposition des Kr?melwerts suchen
   var wertAnfang = theCookie.indexOf("=", posName)+1;
   var wertEnde = theCookie.indexOf(";", posName+1);
   if (wertEnde == -1) wertEnde = theCookie.length;

   // Kr?melwert auslesen und zur?ckgeben
   var wert = theCookie.substring(wertAnfang, wertEnde);
   return unescape(wert);
}

function writeCookie(name,wert,verfall,path,dom,secure) {
   neuerKeks = name + "=" + escape(wert);
   if (verfall)
      neuerKeks += "; expires=" + fixedGMTString(verfall);
   if (path) neuerKeks += "; path=" + path;
   if (dom) neuerKeks += "; domain=" + dom;
   if (secure) neuerKeks += "; secure";
   document.cookie = neuerKeks;
}

function removeCookie( name, path, dom ) {
	document.cookie = name + "=" +
	( ( path ) ? ";path=" + path : "") +
	( ( dom ) ? ";domain=" + dom : "" ) +
	";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

// Diese Funktion korrigiert den Datums-Bug von
// Netscape/Mac und liefert den korrekten GMTString:
function fixedGMTString(datum){
   var damals=new Date(1970,0,1,12);
   if (damals.toGMTString().indexOf("02")>0) {
      datum.setTime(datum.getTime()-1000*60*60*24);
   }
   return datum.toGMTString();
}


function addToShoppingBasketInternal(buyCode) {
	var shoppingBasket = readCookie("shoppingBasket");
	if (shoppingBasket && shoppingBasket.indexOf(buyCode) != -1) {
		return;
	}
	if (shoppingBasket) {
		shoppingBasket = shoppingBasket + "," + buyCode;
	} else {
		shoppingBasket = buyCode;
	}
	
	var now = new Date();
	var minutes = now.getMinutes();
	
	//also needs to be set in ShoppingBasketUtils
	minutes += 7200; // ( 24 Hours * 60 Minutes ) * 5 Days = 7200 Minutes
	now.setMinutes(minutes);
	
	writeCookie("shoppingBasket", shoppingBasket, now, "/", null, null);
}

function addToShoppingBasket(buyCode,type) {
	if(canCookie()){
		addToShoppingBasketInternal(buyCode);
	    updateShoppingBasket();	
	    
	    notificationShow(10,'' + type + ' erfolgreich hinzugef&uuml;gt',notifShowTime,true,true);
	}
}

function addTracksToShoppingBasket() {
    if(canCookie()){
	    var i;
	    var addedNumber = 0;
	    for (i=0;i<document.tracklist.length;i++) {
	      if (document.tracklist.elements[i].checked) {
	        addToShoppingBasketInternal(document.tracklist.elements[i].value);
	        addedNumber+=1;
		  }
	    }
	    updateShoppingBasket();
		if(addedNumber>0){
	    	notificationShow(10,addedNumber + ' Track(s) erfolgreich hinzugef&uuml;gt',notifShowTime,true,true);
	    }
    }
}

function updateShoppingBasket() {
	var i=getShoppingBasket();
	if (i==0) {
		document.getElementById("nbrObContents").innerHTML = "Kein";
	} else {
		document.getElementById("nbrObContents").innerHTML = "" + i;
	}
    displayBasketMask();
}

function getShoppingBasket() {
	var shoppingBasket = readCookie("shoppingBasket");
	var i=0;
	if (shoppingBasket) {
		i++;
		var idx = shoppingBasket.indexOf(",");
		while (idx != -1) {
			shoppingBasket = shoppingBasket.substring(idx+1);
			idx = shoppingBasket.indexOf(",");
			i++;
		}
	}
	return i;
}

function writeShoppingBasket() {
	var i=getShoppingBasket();
	if (i==0) {
		document.write('<span id="nbrObContents">Kein</span>');
	} else {
		document.write('<span id="nbrObContents">' + i +'</span>');
	}	
}

function PreisCheck(preis)
{
 var CPreis = preis;
 var XPreis = preis.replace(/,/g,".");
  CPreis = XPreis.replace(/\./g,"");
 p1 = XPreis.length;
 p2 = XPreis.search(/\./g);
 p3 = parseInt(p1) - parseInt(p2);
 if (p3 == 3) { if (p2 < '0') CPreis = CPreis + '00';}
 if (p3 == '0') CPreis = CPreis + '00';
 if (p3 == '1') CPreis = CPreis + '00';
 if (p3 == '2') CPreis = CPreis + '0';
 return (CPreis);
}

function getKomma(zahl)
{
 var zahl0 = new String(zahl);
 
 if (zahl0 == 0){
 return '0,00';
 }
 teil1 = zahl0.length - 2;
 zahl1 = zahl0.substr(0,teil1);
	 if(zahl1==""){
	 	zahl1=0;
	 }
 zahl2 = zahl0.substr(teil1,2);
 zahlk = zahl1 + ',' + zahl2;
  return(zahlk);
}


// Header Box for Login / Basket

function displayLoginMask() {
	document.getElementById("box_cutout_head").className = "box_cutout_head_login";
	document.getElementById("basketMask").style.display = "none";
	document.getElementById("loginMask").style.display = "block";
}
function displayBasketMask() {
	document.getElementById("box_cutout_head").className = "box_cutout_head_basket";
	document.getElementById("loginMask").style.display = "none";
	document.getElementById("basketMask").style.display = "block";
}

function redirectToURL(url){
	if(url!=""){
		window.location = url;
	}
}

/*Tell a Friend*/

function switchInfoToTaFForm() {
	 document.getElementById("tafForm").style.display = "block";
	 document.getElementById("artistStuff").style.display = "none";
}
function switchBackToArtistStuff() {
	 document.getElementById("tafForm").style.display = "none";
	 document.getElementById("artistStuff").style.display = "block";
}

function sendIt(artist) {
	recipient = document.getElementById("recipientName").value;
	name = document.getElementById("senderName").value;
	text = document.getElementById("text").value;
		
	url = 'tellAFriend.do?recipientMail='+recipient+'&senderName='+name+'&text='+text+'&uniqueName=' + artist;
	encodeURI(url);
	sendDataTAF(url);
}

function responseUpdate(result){
	
	if(result=='OK'){
		switchBackToArtistStuff()
		notificationShow(0,'E Mail wurde erfolgreich verschickt',notifShowTime,false,false);
	}
	if(result=='-1000') {
		switchBackToArtistStuff()
		notificationShow(0,'Fehlerhafter EMailHost',notifShowTime,false,false);	
	}
	if(result=='-1001') {
		switchBackToArtistStuff()
		notificationShow(0,'Fehlerhafte EMailAdresse',notifShowTime,false,false);	
	}	
	if(result=='-1002') {
		switchBackToArtistStuff()
		notificationShow(0,'Empf&auml;nger und Name m&uuml;ssen ausgef&uuml;llt werden!',notifShowTime,false,false);	
	}
	if(result=='-1010') {
		switchBackToArtistStuff()
		notificationShow(0,'Sie m&uuml;ssen angemeldet sein, um eine Empfehlung zu versenden!',notifShowTime,false,false);	
	}		
	
}

/*Trimming*/

function trim(str, chars) {
    return ltrim(rtrim(str, chars), chars);
}

function ltrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}

function rtrim(str, chars) {
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

/*Call and Pay*/
var cap1send = false;
var cap2send = false;

function sendCAPRequest() {
	if(cap1send==false){
		cap1send=true;
		document.getElementById('buyButton').style.display = "none"; 
		document.getElementById('waitButton').style.display = "block"; 
		url = 'order.do?isCallAndPay=true';
		encodeURI(url);
		sendDataCAP(url);
	}
}

function sendCAPRequest2() {
	if(cap2send==false){
		cap2send=true;
		url = 'order.do?checkCallAndPay=true';
		encodeURI(url);
		sendDataCAP2(url);
	}
}

function CAPUpdate(response) {
	if(response=="error"){
		notificationShow(0,'Leider trat ein Fehler auf',notifShowTime,false,false);	
	}else{
		document.getElementById('waitButton').style.display = "none"; 
		document.getElementById('capBox').style.display = "block"; 
		document.getElementById('capNumber').innerHTML = response;
	}
}

function CAPUpdate2(response) {
	cap2send=false;
	if (response!="OK") {
		if (response == "18") {
			notificationShow(0,"Bitte t&auml;tigen Sie zun&auml;chst den Festnetzanruf.",notifShowTime,false,false);	
		} else if (response == "19") {
			notificationShow(0,"Bitte f&uuml;hren Sie zun&auml;chst den Bezahlvorgang vollst&auml;ngig durch.",notifShowTime,false,false);	
		} else {
			notificationShow(0,'Leider trat ein Fehler auf',notifShowTime,false,false);	
		}
	} else {
		location.href = "order.do";
	}
}
