function encodeurl(s) {
	var ret;
	if(typeof(encodeURIComponent) == 'undefined') {
		ret = encode(s);
		ret = ret.replace(/\+/g,'%2B');
	} else {
		ret = encodeURIComponent(s);
	}
	return ret;
}

String.prototype.trim = function(){     var r = /^\s+|\s+$/,         a = this.split(/\n/g),         i = a.length;     while(i-- > 0)         a[i] = a[i].replace(r, '');     return a.join('\n');}

function dropParamFromUrl(url, param) {
	param = "&" + param + "=";
	if(url.indexOf(param) != -1) {
		var s1 = url.substr(0, url.indexOf(param));
		var s2 = url.substr(url.indexOf(param) + param.length, url.length);
		if(s2.indexOf("&"))
			s2 = s2.substr(0, s2.indexOf(param));
		else
			s2 = "";
		url = s1 + s2;
	}
	return url;
}

function getRadioValue(el) {
	value = el.value;
		for(i=0; i< el.length; i++) {
			if(el[i].checked) {
				value = el[i].value;
				break;
			}
		}
	return value;
}

function search(sessionid, searchformid) {
	var sForm = "searchform" + searchformid;
	var sTerm = document.forms[sForm].term.value;
	sTerm = sTerm.trim();
	sTerm = encodeurl(sTerm);
	url = dropParamFromUrl(window.location.href, "term");
	if(sTerm.length < 3) {
		window.location.href = url + "&searchError=true"+"&term="+sTerm;
	} else {
		window.location.href = "ActionServlet?cmd=freesearch&sessionid="+sessionid+"&term="+sTerm+"&fields=all";
	}
}

function mosearch(sessionid, searchformid) {
	var sForm = "searchform" + searchformid;
	var sTerm = document.forms[sForm].term.value;

	window.location.href = "ActionServlet?cmd=freesearch&sessionid="+sessionid+"&term="+sTerm+"&fields=all";
	return false;
}

function addWithFormat(addto, type, typeid, albumid) {

	var formatcode = encodeurl(document.forms["formats"]["format[" + type + typeid + "]"].value);

	if (formatcode != "") {
		type = (type == "release") ? "album" : type;
		if (addto == "list") {
		//add to shopping list
			url = "ActionServlet?cmd=addmerkliste&sessionid=" + sessionid + "&" + type + "id=" + typeid + "&formatcode=" + formatcode;
		} else {
		//add to cart
			url = "ActionServlet?cmd=addbasket&sessionid=" + sessionid + "&" + type + "id=" + typeid + "&formatcode="+formatcode;
		}
		if (albumid != "") {
			url += "&albumid="+albumid;
		}
		location.href = url;
		return false;
	} else {
		//fallback don't send format but put things via html in cart
		return true;
	}
}

function goLabel(sessionid) {
      var labelid = encodeurl(document.forms["labelmenu"].labels.value);
      if(!labelid == "")
        location.href = "ActionServlet?cmd=labeldetails&sessionid="+sessionid+"&labelid="+labelid;
}

function goCurrency(sessionid) {
	  var currencycode = encodeurl(document.forms["currencymenu"].currencies.value);
      if(!currencycode == "")
        location.href = "ActionServlet?cmd=setcurrency&sessionid="+sessionid+"&currencycode="+currencycode;
}

function goSorting(sessionid) {
	  var sortingcode = encodeurl(document.forms["sortingmenu"].sortings.value);
      if(!sortingcode == "")
        location.href = "ActionServlet?cmd=setsorting&sessionid="+sessionid+"&sortingcode="+sortingcode;
}

function goBoostercode(sessionid, special, id) {    var boostercode = encodeurl(document.forms["boostercodeform"].elements["boostercode"].value);
    if (special == "nokia") {
  		location.href="ActionServlet?sessionid="+sessionid+"&cmd=boostercode&boostercode="+boostercode+"&subcmd=advice-special&adviceid=1122284490016";
  	} 
  	else if (special == "hansen") {
  		location.href="ActionServlet?sessionid="+sessionid+"&cmd=boostercode&boostercode="+boostercode+"&subcmd=advice-special&adviceid=1129287125952";
  	}
	else if (special == "brazil") {
                location.href="ActionServlet?sessionid="+sessionid+"&cmd=boostercode&boostercode="+boostercode+"&subcmd=advice-special&adviceid=1158676004435";
        }
	else if (special == "sonar") {
                location.href="ActionServlet?sessionid="+sessionid+"&cmd=boostercode&boostercode="+boostercode+"&subcmd=advice-special&adviceid="+id;
        }
	else if (special == "album-special") {
                location.href="ActionServlet?sessionid="+sessionid+"&cmd=boostercode&boostercode="+boostercode+"&subcmd=album-special&albumid="+id;
        }
  	else if (special == "promo-special") {
  		location.href="ActionServlet?sessionid="+sessionid+"&cmd=boostercode&boostercode="+boostercode+"&subcmd=promo-special&promoid="+id;  	
  	} 
	else if (special == "advice-special") {
                location.href="ActionServlet?sessionid="+sessionid+"&cmd=boostercode&boostercode="+boostercode+"&subcmd=advice-special&promoid="+id;
        } 
	else {
  		location.href="ActionServlet?sessionid="+sessionid+"&cmd=boostercode&boostercode="+boostercode;
  	}}

function popup(URL,width,height,scrollbars) {        window.open(URL,"ft_PopNotRock","width="+width+",height="+height+",left=100,top=100,scrollbars="+scrollbars);}

function confirmCart(sessionid, type) {
	var email = encodeurl(document.forms["warenkorbform"].elements["email"].value);
	var newsletter = false;
	var nle = document.forms["warenkorbform"].elements["newsletter"];
	if(nle!=null) {
		newsletter = nle.checked;
	}

	if (type != "free") {
		var payment = getRadioValue(document.forms["warenkorbform"].elements["payment"]);
	} else {
		var payment = "free";
	}
	var format = getRadioValue(document.forms["warenkorbform"].elements["format"]);
	var loc = org_rdp;
	loc += "?cmd=checkoutaction&sessionid=" + sessionid + "&email=" + email + "&payment=" + payment + "&format=" + format + "&type=" + type+"&newsletter="+newsletter;
	location.href = loc;
}

function moConfirmCart(sessionid, type) {
	var email = encodeurl(document.forms["warenkorbform"].elements["email"].value);
	 var newsletter = false;
        var nle = document.forms["warenkorbform"].elements["newsletter"];
        if(nle!=null) {
                newsletter = nle.checked;
        }


	if (type != "free") {
		var payment = "firstgate";
	} else {
		var payment = "free";
	}
	var format = "mp3";
	var loc = org_rdp;
	loc += "?cmd=checkoutaction&sessionid=" + sessionid + "&email=" + email + "&payment=" + payment + "&format=" + format + "&type=" + type+"&newsletter="+newsletter;
	location.href = loc;
}


/**
 * DHTML date validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
 */
// Declaring valid date character, minimum year and maximum year
var dtCh= ".";
var minYear=1950;
var maxYear=2008;

function isInteger(s){
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag){
	var i;
    var returnString = "";
    // Search through string's characters one by one.
    // If character is not in bag, append to returnString.
    for (i = 0; i < s.length; i++){   
        var c = s.charAt(i);
        if (bag.indexOf(c) == -1) returnString += c;
    }
    return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
    // EXCEPT for centurial years which are not also divisible by 400.
    return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
   } 
   return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)

	var strDay = dtStr.substring(0,pos1)
	var strMonth = dtStr.substring(pos1+1, pos2)
	var strYear = dtStr.substring(pos2+1)

	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : mm/dd/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}

