// JavaScript Document

	rand = Math.floor(Math.random()*100000);
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/; // pattern for email addresses

// The Resig version of addEvent ...

function addEvent( obj, type, fn ) {
  if ( obj.attachEvent ) {
    obj['e'+type+fn] = fn;
    obj[type+fn] = function(){obj['e'+type+fn]( window.event );}
    obj.attachEvent( 'on'+type, obj[type+fn] );
  } else
    obj.addEventListener( type, fn, false );
}
function removeEvent( obj, type, fn ) {
  if ( obj.detachEvent ) {
    obj.detachEvent( 'on'+type, obj[type+fn] );
    obj[type+fn] = null;
  } else
    obj.removeEventListener( type, fn, false );
}

// -- Link warning --//

function new_link(linkloc) {
	if(confirm('Clicking this link will take you to a web site which Sibelius Music is not responsible for')) { 
		window.open(linkloc,'_blank');
		}	
	}

function simple_link(linkloc) {
	window.open(linkloc,'_blank');
}

// Effects stuff

// see effects.js for how this works
function downSlide(id) {
	if ($(id).style.display == "none") {
		new Effect.SlideDown($(id));
	}
	else
	{
		new Effect.SlideUp($(id));
	}
}

function upSlide(id) {
	new Effect.SlideUp($(id));
}

function showHide(id) {
	if ($(id).style.display == "none") {
		$(id).style.display = "block";
	}
	else
	{
		$(id).style.display = "none";
	}
}

function placefooter() {
	// make the footer stick to the bottom of the window
	var oFoot = document.getElementById('footer');
	var oContainer = document.getElementById('container');
    if (document.all){
        availH = document.body.clientHeight;
	} else {
		availH = window.innerHeight;
    }

	whigh = oFoot.offsetHeight + 20;
	wcont = oContainer.offsetHeight;

	if(wcont < availH) {
		// could tidy this maths maybe (DA)
		placefoot = availH;
//		placefoot = availH  - whigh;
		oFoot.style.marginTop = placefoot+"px";
	}
}


/* ------------ Voting, favourites -------------- */

function goVote(scoreid) {		
	rand = Math.floor(Math.random()*100000);
	var url = "index.php?sm=home.vote";
  	var lresponse = "Voting for you...";
	notice.update(lresponse);
	new Ajax.Request(url, {
		method: 'get',
		parameters: {scoreID: scoreid, reqno: rand},
  		onSuccess: function(transport){
	  	var response = transport.responseText || "Sorry there was an error";
			if(response.indexOf("voting!") > -1) {
				buttonOut('votebutton');
			} else {
				var response = "";
				alert("You've already voted for this score!");	
			}
			notice.update(response);
		}
	});
}

function goFav(scoreid) {	
	var url = "index.php?sm=home.addfavourite";
  	var lresponse = "Checking favourites...";
	notice.update(lresponse);
	new Ajax.Request(url, {
		method: 'get',
		parameters: {scoreID: scoreid, reqno: rand},
  		onSuccess: function(transport){
	  	var response = transport.responseText || "Sorry there was an error";
			if(response.indexOf("added") > -1) {
				buttonOut('favbutton');
			} else {
				var response = "";
				alert("This score is already in your favourites!");	
			}
			notice.update(response);
		}
	});
}

function buttonOut(id) {	
	document.getElementById(id).style.backgroundImage = "url(_assets/_img/page_elements/button_back_grey_grad.gif)";	
}

function fvTest(scoreid) {
	rand = Math.floor(Math.random()*100000);	
	url = "index.php?sm=home.fvcheck";
	new Ajax.Updater('already', url, { method: 'get', parameters: {scoreID: scoreid, reqno: rand} });
	onSuccess: notice.update(" ");
}

/* ------------ End of voting and favourites -------------- */


/* function addEvent(elm, evType, fn) {
    elm["on"+evType]=fn;
	return;
} */ // this was in accordion.js but is useful across the site

function imgsize(size) { // resize score graphic files to different sizes but retain proportions
	newsch = 0;
	newscw = 0;
	for(i=0;i<=4;i++) {
		var graphicid = "scoregraphic"+i;
		if(document.getElementById(graphicid) || document.getElementById("scoregraphic")) {
			if(!document.getElementById("scoregraphic")) {
				var scg = document.getElementById(graphicid);
			} else {
				var scg = document.getElementById("scoregraphic");
			}
			scwidth = scg.offsetWidth;
			scwheight = scg.offsetHeight;							
			if(scwidth > size) {
				newscw = Math.round(scwidth * (size/scwidth));
				scg.style.width=newscw+"px";
				newsch = Math.round(scwheight * (size/scwidth));
				scg.style.height=newsch+"px";
			}
		}
	}
}



/* -------- Validate Storeholder Name and URL -------- */

function tcCheck(){
	if (document.all || document.getElementById){
		if($('tandcs').checked) {
			$('btn_apply').disabled = false;
		} else {
			$('btn_apply').disabled = true;
		}
	}
}

function removeSpaces(string) {
	return string.split(' ').join('');
}

function stValidate(stWhich) {
	var naValid = $('naValid');
	var urValid = $('urValid');
	var stValid = $('stValid');
	var url = "index.php?sm=storeholder.check";

	stU = removeSpaces(document.stForm.storeURL.value);
	stURL = stU.replace(/[^a-zA-Z 0-9]+/g,'');
	stN = document.stForm.storeName.value;
	document.stForm.storeURL.value = stURL;
	
	if(stWhich == "url" && stU != "") {
		var lresponse = "Validating store URL";
		stValid.update(lresponse);
		new Ajax.Request(url, {
			method: 'get',
			parameters: {storeURL: stU, reqno: rand},
  			onSuccess: function(transport){
				var response = transport.responseText || "Sorry there was an error";
				urValid.update(response);
				lresponse = "Done validating!";
				stValid.update(lresponse);
			}
		});
	}

	if(stWhich == "name" && stN != "") {
		var lresponse = "Validating store name";
		stValid.update(lresponse);
		new Ajax.Request(url, {
			method: 'get',
			parameters: {storeName: stN, reqno: rand},
  			onSuccess: function(transport){
				var response = transport.responseText || "Sorry there was an error";
				naValid.update(response);
				lresponse = "Done validating!";
				stValid.update(lresponse);
			}
		});
	}
}

function doValidating() {
	stValidate('url'); // just check once more that details haven't been changed or they haven't validated yet
	stValidate('name');
	setTimeout("stApply()", 1000);
}

function stApply() { // if T&Cs have been ticked in storeholder.signup then we can try and apply
	if(document.stForm.stURLValid && document.stForm.stNameValid) {
		if(document.stForm.stURLValid.value == 1 && document.stForm.stNameValid.value == 1) {
			document.stForm.submit();
		} else {
			alert("You need to create a unique name and URL before opening your store!");	
		}
	} else {
		alert("You need to create a unique name and URL before opening your store!");	
	}
}

/* ------- End of Storeholder checks -------- */

/* ------- Join mailing list ---------------- */

function joinList() {
	rand = Math.floor(Math.random()*100000);
	var mlist = $('mlist');
	var mailList = $('mailList');
	var addEmail = $('addEmail').value;
	var userID = $('userID').value;
	var mailaction = $('mailAction').value;
	if(reg.test(addEmail) == false) {
		mailList.update("<p style=\"padding-left: 10px\" class=\"err\">Please enter a valid email address");
	} else {
		mailList.update("Updating the mailing list");
		var url = "index.php?sm=account.mailinglist";
		new Ajax.Request(url, {
		method: 'get',
		parameters: {addEmail: addEmail, mailAction: mailaction, userid: userID, reqno: rand},
  		onSuccess: function(transport){
	  	var response = transport.responseText || "Sorry there was an error";
			mlist.update(response);
			mailList.update('');
		}
	});
	}
}

/* ------- Tell A Friend -------------------- */

function validate() {
   	var address = $('toField');
   	var greeting = $('greetingField');
   	var score_id = $('score_id');	
	var score_details = $('score_details');
	var scoreurl = $('score_url');
	
//	alert(document.getElementById("score_details").value);
	
	if(reg.test(address.value) == false) {
		alert("Please supply a valid email address");
		return false;
   	} else {
		doTell(address.value, greeting.value, score_id.value, score_details.value, scoreurl.value);
		address.value = "";
	   	greeting.value = "";
		score_details = "";
	}
}

function doTell(email,greeting,scoreid,score_details,scoreurl) {
//	alert("Sending to: "+email+" Greeting: "+greeting+",\r ScoreID: "+scoreid+",\r Score details: "+score_details+",\r Score URL: "+scoreurl);
	var notice = $('toldim');
	var url = "index.php?sm=scores.tellafriend";
  	var lresponse = "Sending "+greeting+" to "+email;
	notice.update(lresponse);
//	alert (lresponse);
	new Ajax.Request(url, {
		method: 'get',
		parameters: {scoreID: scoreid, emailAddress: email, score_details: score_details, score_url: scoreurl, greeting: greeting, reqno: rand},
  		onSuccess: function(transport){
	  	var response = transport.responseText || "Sorry there was an error";
		notice.update(response);
		}
	});
}

function validateEmail() {
	var message = $('messageField');
   	var score_id = $('score_id');	
	var score_details = $('score_details');
	var scoreurl = $('score_url');
	if ((message.value.length==0) || (message.value==null)) {
		alert("Please enter a message");
		return false;
   	} else {
		doEmailComp(message.value, score_id.value, score_details.value, scoreurl.value);
	   	message.value = "";
		score_details = "";
	}
}

function doEmailComp(message, scoreid, score_details, score_url) {
	//alert("Sending Message: "+message+",\r ScoreID: "+scoreid+",\r Score details: "+score_details+",\r Score URL: "+score_url);
	var notice = $('emailedim');

	var url = "index.php?sm=scores.emailcomposer";
  	var lresponse = "Sending "+message;
	notice.update(lresponse);
//	alert (lresponse);
	new Ajax.Request(url, {
		method: 'get',
		parameters: {scoreID: scoreid, score_details: score_details, score_url: score_url, message: message, reqno: rand},
  		onSuccess: function(transport){
	  	var response = transport.responseText || "Sorry there was an error";
		notice.update(response);
		}
	});
}


/* ------- End of friend --------- */

/* ------- Post A Review --------- */

function revValidate() {
   	var revText = $('revText');
   	var revHeadline = $('revHeadline');	
   	var score_id = $('score_id');
	if(revText.value == "" || revHeadline.value == "") {
		alert("Please complete all review fields before sending. Thanks!");
	} else {
		doReview(score_id.value, revText.value, revHeadline.value);
		revText.value = "";
	   	revHeadline.value = "";
	}
}

function doReview(scoreid,revText,revHeadline) {
	var notice = $('reviewpage');
	var url = "index.php?sm=scores.postreview";
  	var lresponse = "Posting your review...";
	notice.update(lresponse);
	new Ajax.Request(url, {
		method: 'get',
		parameters: {scoreid: scoreid, revText: revText, revHeadline: revHeadline, reqno: rand},
  		onSuccess: function(transport){
	  	var response = transport.responseText || "Sorry there was an error";
		notice.update(response);
		}
	});
}


/* ------- End of review posting --------- */

/* ------- Latest scores for front page ------ */

function showLatest() {
	rand = Math.floor(Math.random()*100000);
	var aj_latest = $('aj_latest');
	aj_latest.update("<img src='_assets/_img/progress-dots.gif' />");
	var url = "index.php?sm=home.latestlive";
	new Ajax.Request(url, {
	method: 'get',
	parameters: {reqno: rand},
  		onSuccess: function(transport){
	  	    var response = transport.responseText || "Sorry there was an error";
			aj_latest.update(response);
	    }
	});
}

/* ------- Colour picker for stores ------ */

function swap_img(button_id,graphic_name,use_text) {
	document.getElementById(button_id).src = graphic_name;	
	var text="&nbsp;";
	if (use_text) {
		text = document.getElementById(button_id).alt;
	}
	document.getElementById("ButtonText").innerHTML=text;
}

function change_fill_demo_colour(colourid) {
	document.getElementById('fillcolourid').value=colourid;
	set_fill_demo_colour();
	hide_div('fillcolourpicker')
}				

function change_box_demo_colour(colourid) {
	document.getElementById('boxcolourid').value=colourid;
	set_box_demo_colour();
	hide_div('boxcolourpicker')
}

function set_box_demo_colour() {
	var colours=new Array("FFFFFF", "FFFFFF", "DEDEDE", "979797", "666666", "000000", "C8EBFF", "00FFFC", "E1C6FF", "FF00A8", "B51FD8", "8045C0", "00AEEF", "7AC2EC", "0000FF", "001DA1", "5A027A", "D0FFD0", "C1DE8A", "00FF00", "006100", "FFFFCA", "D9D18B", "B69630", "FFE200", "FFD792", "E9A86E", "C27638", "FF7800", "5D2C00", "FFC0BE", "EC2A22", "720B07");
	color=colours[document.getElementById('boxcolourid').value];
	document.getElementById('boxcolourdemo').style.backgroundColor="#" + color;
}

function set_fill_demo_colour() {
	var fillcolours= new Array("FFFFFF", "FFFFFF", "DEDEDE", "979797", "666666", "000000", "C8EBFF", "00FFFC", "E1C6FF", "FF00A8", "B51FD8", "8045C0", "00AEEF", "7AC2EC", "0000FF", "001DA1", "5A027A", "D0FFD0", "C1DE8A", "00FF00", "006100", "FFFFCA", "D9D18B", "B69630", "FFE200", "FFD792", "E9A86E", "C27638", "FF7800", "5D2C00", "FFC0BE", "EC2A22", "720B07");
	color=fillcolours[document.getElementById('fillcolourid').value];
	document.getElementById('fillcolourdemo').style.backgroundColor="#" + color;
}

function set_colour_picker_colour(color) {
	document.getElementById('democolourcell').style.backgroundColor="#" + color;
	document.getElementById('democolourhex').value=color;
}

function apply_colour() {
	var col=document.getElementById('democolourhex').value;
 	for (loop=0;loop<document.form.colradio.length;loop++) {
		if (document.form.colradio[loop].checked) {
			radio=document.form.colradio[loop].value;

			switch(radio) {
				case "1":
				var element="text";
				break;
				case "2": 
				var element="headings";
				break;
				case "3":
				var element="links";
				break;
				case "4":
				var element="headingsonfg";
				break;
				case "5": 
				var element="linksonfg";
				break;
				case "6": 
				var element="bg";
				break;
			}
  
			if (radio > -1) {
				document.getElementById(element).value=col;
			}
			set_colours();
		}
	}
}

function select_radio(select_this) {
	 for (loop=0 ; loop<document.form.colradio.length; loop++) {
	 	document.form.colradio[loop].checked=false;
		if (document.form.colradio[loop].value == select_this) {
			radio=document.form.colradio[loop].checked=true;
		}
	}
}

function set_colours() {
	
	if (document.getElementById('text').value.length == 6) {
		document.getElementById('textcell').style.backgroundColor = "#" + document.getElementById('text').value;
	}
	if (document.getElementById('headings').value.length == 6) {
		document.getElementById('headingcell').style.backgroundColor = "#" + document.getElementById('headings').value;
	}
	if (document.getElementById('links').value.length == 6) {
		document.getElementById('linkscell').style.backgroundColor = "#" + document.getElementById('links').value;
	}
	if (document.getElementById('headingsonfg').value.length == 6) {
		document.getElementById('headingsonfgcell').style.backgroundColor = "#" + document.getElementById('headingsonfg').value;
	}
	if (document.getElementById('linksonfg').value.length == 6) {
		document.getElementById('linksonfgcell').style.backgroundColor = "#" + document.getElementById('linksonfg').value;
	}
	if (document.getElementById('bg').value.length == 6) {
		document.getElementById('bgcell').style.backgroundColor = "#" + document.getElementById('bg').value;
	}

}

function hide_div(id) {
	document.getElementById(id).style.visibility="hidden";
}

function show_div(id) {
	document.getElementById(id).style.visibility="visible";
}

				
function default_colors() {
	if (confirm("Do you really want to reset your color scheme to the SibeliusMusic.com default?")) {
		change_box_demo_colour(15);
		change_fill_demo_colour(1);
		document.getElementById("text").value="000000";
		document.getElementById("headings").value="002667";
		document.getElementById("links").value="FF0000";
		document.getElementById("headingsonfg").value="FFFFFF";
		document.getElementById("linksonfg").value="DDDDDD";
		document.getElementById("bg").value="FFFFFF";
		set_colours();
	}
}
			
function preview_store() {
	if (parseInt(navigator.appVersion)>3) {
		if (navigator.appName=="Netscape") {
			winW = window.innerWidth;
			winH = window.innerHeight;
		}
		if (navigator.appName.indexOf("Microsoft")!=-1) {
			winW = document.body.offsetWidth;
			winH = document.body.offsetHeight;
		}
	}
	winY = window.screenTop;
	winX = window.screenLeft + 50;
	winW -=100;
	winH -=100;
	window.open('/html/sibeliusmusic/cached_stores/133202_0.html',"preview_window","toolbar=yes,menubar=yes,location=yes,scrollbars=yes,resizable=yes,left=" + winX + ",top=" + winY + ",width=" + winW + ",height="+ winH);
}

/* ------------- End of the colour picker -------------- */

/***********************************************
* Textarea Maxlength script- © Dynamic Drive (www.dynamicdrive.com)
* This notice must stay intact for legal use.
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

function ismaxlength(obj){
	var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : "";
	if (obj.getAttribute && obj.value.length>mlength) {
		obj.value=obj.value.substring(0,mlength);
		alert("The maximum number of characters has been reached.");
	}
}




/* -------------- Feedback form checker ------------------- */

function feedback() {
   	var name = $('fName');
   	var email = $('fEmail');
   	var details = $('fDetails');	
	var fForm = $('feedbackform');
	
	if(reg.test(email.value) == false) {
		alert("Please supply a valid email address");
		return false;
   	}
}

/* --------------- End of feedback form checker --------- */


/* ---------------Associated Scores --------------------- */

function associate(assocID,scoreID) {
	var	url = "index.php?sm=scores.associated";
	if ($(assocID).checked == true) {
		AssocAction = "add";
	} else {
		AssocAction = "delete";	
	}
	new Ajax.Request(url, {
		method: 'get',
		parameters: {ScoreID: scoreID, AssocID: assocID, AssocAction: AssocAction}
/*		,onSuccess: function(transport){
			if(AssocAction == "add"){ 
				alert("This has been associated");
			} else {
				alert("This HAS NOT been associated");
			}
		} */
	});
}

/* --------------- End of Associated Scores --------------------- */


/* --------------- delete parts ------------------------- */

function deletepart(partID) {
	var url = "index.php?sm=scores.partdelete";
	new Ajax.Request(url, {
		method: 'get',
		parameters: {PartID: partID}
		,onSuccess: function(transport){
			if(AssocAction == "add"){ 
				alert("This has been deleted");
			} else {
				alert("This HAS NOT been deleted");
			}
		}	
	});
}

/* end of delete parts ---- */

function setBannerCookie() {
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+7);
	document.cookie="email_banner"+ "=" +escape("hide")+
	((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
	$('p_banner').hide();
}

/* --- update paypal email --- */

function add_pp_address(ppEmail,loc) {
	rand = Math.floor(Math.random()*100000);
	var mailOut = $('mailOut');
	var mailConf = $('pp_conf');
	if(reg.test(ppEmail) == false) {
		mailOut.update("<p>&nbsp;</p><p style=\"color: #f00; font-weight: bold\">Please enter a valid email address</p>");
	} else {
		mailOut.update("<p>Updating your Paypal email address</p>");
		var url = "index.php?sm=account.pp_add";
		new Ajax.Request(url, {
		method: 'get',
			parameters: {ppEmail: ppEmail, reqno: rand, refPage: loc},
			onSuccess: function(transport){
			var response = transport.responseText || "Sorry there was an error";
				if(response == 1) {
					mailOut.update("<p>&nbsp;</p><p style=\"color: #f00; font-weight: bold\">That Paypal email address may already be in use through Sibelius Music</p>");
				} else {
					mailConf.update(response);
					$('sub_button').show();
				}
			}
		});
	}
}