// JavaScript Document


// ------------
// ------------
// Register user data
// ------------
// ------------

function registeruser(theForm){

	// contract alert for new members
	if(theForm.action.indexOf("new") > 0){
	//alert("new user. Checking contract");
		if(!theForm.contract_.checked){alert("Du måste godkänna avtalet"); theForm.contract_.focus(); return false;}
		if(!theForm.password.value){alert("Ange lösenord"); theForm.password.focus(); return false;}
		if(theForm.password_.value != theForm.password.value){alert("Det bekräftade lösenordet stämmer inte"); theForm.password_.focus(); return false;}
	}

	// alerts for standard users
	if(theForm.email.value.indexOf("@") < 1){alert("Ange epostadress"); theForm.email.focus(); return false;}
	if(!theForm.firstname.value){alert("Ange förnamn"); theForm.firstname.focus(); return false;}
	if(!theForm.lastname.value){alert("Ange efternamn"); theForm.lastname.focus(); return false;}
	
	// alerts for publishers
	if(theForm.wanna_be_a_publisher.value == "yes"){
		if(!theForm.organisationnumber.value){alert("Ange organisationsnummer"); theForm.organisationnumber.focus(); return false;}
		if(theForm.accounttype.value == "0"){alert("Ange kontotyp"); theForm.accounttype.focus(); return false;}
		if(!theForm.accountnumber.value){alert("Ange kontonummer"); theForm.accountnumber.focus(); return false;}
		if(!theForm.adress.value){alert("Ange adress"); theForm.adress.focus(); return false;}
		if(!theForm.zipcode.value){alert("Ange postnummer"); theForm.zipcode.focus(); return false;}
		if(!theForm.city.value){alert("Ange postort"); theForm.city.focus(); return false;}
	}
	
	// resets
	if(theForm.url.value == "http://"){theForm.url.value = "";}
	
	
	// set hidden values
	
	for(i=0; i<theForm.elements.length; i++){
		curElement = theForm.elements[i];
		isTempImput = curElement.name.indexOf("_");
		if(isTempImput == 0){
			i++;
			val = curElement.checked ? 1 : 0;
			theForm.elements[i].value = val;
		}
	}
	
	
	theForm.submit();
}
function login(){
	theForm = document.getElementById("loginform");
	if(!validate_email(theForm.email.value,"Ange din epostadress")){theForm.email.focus(); return;}
	theForm.submit();
}
function remind_password(){
	theForm = document.getElementById("loginform");
	if(!validate_email(theForm.email.value,"Ange din epostadress")){return;}
	theForm.action = "?command=remind_password";
	theForm.submit();
}
function validate_email(str,alerttxt){
	apos=str.indexOf("@")
	dotpos=str.lastIndexOf(".")
	if (apos<1||dotpos-apos<2){
		alert(alerttxt);
		return false;
	}else{
		return true;
	}
}
	


// ------------
// ------------
// Register song data
// ------------
// ------------

function saveSong(theForm){


	// alerts
	if(!theForm.rights_ok.checked){alert("Du får bara lägga upp sånger med tillstånd från upphovsmännen"); theForm.rights_ok.focus(); return false;}
	if(theForm.elements[1].name == "userfile"){
		fileName = theForm.userfile.value.toLowerCase();
		if((fileName.indexOf(".pdf") == -1) && (fileName.indexOf(".sib") == -1) && (fileName.indexOf(".mus") == -1)){alert("Bifoga en not i PDF eller SibeliusScorch-format"); return false;}
		//if(fileName.indexOf("å") != -1){alert("Filnamnet får inte innehålla bokstaven Å");return false;}
		//if(fileName.indexOf("ä") != -1){alert("Filnamnet får inte innehålla bokstaven Ä");return false;}
		//if(fileName.indexOf("ö") != -1){alert("Filnamnet får inte innehålla bokstaven Ö");return false;}
		//if(fileName.indexOf("ü") != -1){alert("Filnamnet får inte innehålla bokstaven Ü");return false;}
	}
	if(theForm.title.value == ""){alert("Ange titel"); theForm.title.focus(); return false;}
	if(theForm.composers.value == ""){alert("Ange kompositör"); theForm.composers.focus(); return false;}
	if((theForm.lyric.value != "") && (theForm.authors.value == "")){alert("Ange textförfattare"); theForm.authors.focus(); return;}

	// resets
	if(theForm.url.value == "http://www.minsite.se/minljudfil.mp3"){theForm.url.value = "";}
	
	// arrange specifications
	arrspec = "_";
	for(i = 0; i < theForm.elements.length; i++){
		obj = theForm.elements[i];
		if(obj.name.indexOf("as_") > -1){
			if(obj.checked){arrspec += " " + obj.name;}
		}
	}
	theForm.arrangespec.value = arrspec;
	
	// target group specifications
	targetgroups = "_";
	for(i = 0; i < theForm.elements.length; i++){
		obj = theForm.elements[i];
		if(obj.name.indexOf("tg_") > -1){
			if(obj.checked){targetgroups += " " + obj.name;}
		}
	}
	
	theForm.targetgroups.value = targetgroups;
	
	// submit
	theForm.submit();

}


function addFileUploadBox()
{
	var targetElement = document.getElementById("fileuploadbox");
	var htmlStr = "";
	htmlStr += '<input type="hidden" name="MAX_FILE_SIZE" value="500000">';
    htmlStr += '<input name="userfile" type="file" size="30">';
    htmlStr += '<font color="#FF0000">*</font> (max 500kB)';
	targetElement.innerHTML = htmlStr;
	
}

function savePackage(theForm){


	// alerts
	if(theForm.title.value == ""){alert("Ange paketets namn"); theForm.title.focus(); return false;}
	
	// check that at least one song is selected
	var checkedSongs = getTickedBoxes(theForm, "songid_");
	if(!checkedSongs.length){alert("Markera minst en sång"); return false;}
	
	// submit
	theForm.submit();

}


function HL_deletesong(id){
	if(confirm("Vill du verkligen ta bort den här sången?")){
		window.location = "?command=deletesong&songid="+id;
	}
}

function HL_deletepackage(id){
	if(confirm("Vill du verkligen ta bort det här paketet?")){
		window.location = "?command=deletepackage&packageid="+id;
	}
}



function HL_deleteMenuLabel(theForm){
	if(theForm.count.value != ""){alert("Detta tema används av " + theForm.count.value + " sånger. Det får inte raderas."); return;}
	theForm.action = "?command=deletemenulabel";
	theForm.submit();
}


// ------------
// ------------
// MouseOver interaction
// ------------
// ------------


function setClass(obj, className){
	obj.className = className;
}



// ------------
// ------------
// Open window
// ------------
// ------------

function HL_openWindow(url, windowName, parameters){
	/*var x = 0, y = 0; // default values
	alert(url);
	
	if (document.all) {
	  x = window.screenTop + 200;
	  y = window.screenLeft + 200;
	}
	else if (document.layers) {
	  x = window.screenX + 200;
	  y = window.screenY + 200;
	}
	*/
	
	var popup = window.open(url, windowName, parameters);
}




// ------------
// ------------
// Open invoice window
// ------------
// ------------


// note:
function HL_showInvoice(invoiceID){
	window.open("invoice.php?invoiceid="+invoiceID, "invoice", "width=700, height=600");
}
function HL_buyCopyright(songID){
	window.open("invoice.php?songid="+songID+"&volume=0", "invoice", "width=700, height=600");
}

function HL_createPackageInvoice(packageid){
	window.open("invoice.php?packageid="+packageid+"&volume=1", "invoice", "width=700, height=600");
}



function findForm(element, resetOthers){
	var theForm = element.form;
	for(i = 0; i < theForm.elements.length; i++){
		obj = theForm.elements[i];
		//alert(obj.value);
		if(obj.value == "Visa alla"){obj.value = "";}
		if(obj.value == "-"){obj.value = "";}
		if(obj != element && i < 4 && resetOthers){obj.selectedIndex = 0;}
		//alert(obj.value);
	}
	theForm.submit()
}

function clearMe(textField){
	if(textField.value == "Visa alla"){textField.value = "";}
}

// ------------
// ------------
// Generic function to check/uncheck text boxes in a form
// ------------
// ------------

function HL_checkBoxes(theForm, targetBoxName, state){
	//alert(state);
	for(i=0;i<theForm.elements.length;i++){
		obj = theForm.elements[i];
		if(obj.name.indexOf(targetBoxName) > -1){
			obj.checked = state;
		}
	}
}
function getTickedBoxes(theForm, targetBoxName){
	
	var returnArray = new Array();
	for(i=0;i<theForm.elements.length;i++){
		obj = theForm.elements[i];
		if(obj.name.indexOf(targetBoxName) > -1){
			if(obj.checked){returnArray.push(obj);}
		}
	}
	return returnArray;
}

function setVisible(id, state) {
    obj = document.getElementsByTagName("div");
	var val = state ? "visible" : "hidden";
	obj[id].style.visibility = val;
}

function hideShowMessageHeader(messageType){
	var state = messageType.indexOf("email_") == 0;
	setVisible("header_div", state);
}
/*
function entsub(event,ourform) { 
  if (event && event.which == 13) 
    login(); 
  else 
    return true;
} */
