function LTrim( value ) {
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
}

function RTrim( value ) {
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
}

function trim( value ) {
	return LTrim(RTrim(value));
}

function remix_info_popup(remix_id) {
	if(window.screen) {
		var px = (screen.availWidth - 460) / 2;
		var py = (screen.availHeight - 500) / 2;
	} else {
		var px = (1024 - 460) / 2;
		var py = ( 768 - 500) / 2;
	}

	window.open('http://www.remix64.com/box.php?id='+remix_id+'01', 'remixinfo', 'dependent, toolbar=no, location=no, status=no, menubar=no, scrollbars=no, resizable=no, width=460, height=500, left='+px+', top='+py+', screenX='+px+', screenY='+py );
}

function toggleDiv(div_id) {
	if(document.getElementById(div_id).style.display == 'block' || document.getElementById(div_id).style.display == 'inline') {
		hideDiv(div_id);
	} else {
		showDiv(div_id);
	}
}

function toggleVisibility(div_list) {
	if(!(div_list == "" || div_list == null)) {
		if(div_list.indexOf(",") == -1) {
			toggleDiv(div_list);
		} else {
			var trimmed = trim(div_list);
			var elArr = trimmed.split(",");
			var i = 0;
			while (i < elArr.length) {
				toggleDiv(elArr[i]);
				i++;
			}
		}
	}	
}

function hideDiv(div_id) {
	document.getElementById(div_id).style.display = 'none';
}

function showDiv(div_id) {
	document.getElementById(div_id).style.display = 'block';
}

function toggleDis(src, dest) {
	if(document.getElementById(src).checked == true) {
		document.getElementById(dest).disabled = false;
	} else {
		document.getElementById(dest).disabled = true;
	}
}

function validateForm(form_id) {
	var rVal = false;
	var fMsg = "";
	var r = 0;
	switch(form_id) {
		case 0:
			if(document.getElementById('l_arranger').value == 0) {
				fMsg += "You must select an arranger name/alias.\n";
			}
			if(document.getElementById('l_password').value.length < 3) {
				fMsg += "Password too short. At least 3 characters required.\n";
			}
			break;
		case 1:
			if(document.getElementById('r_arranger').value == 0) {
				fMsg += "You must select an arranger name/alias.\n";
			}
			if(document.getElementById('security_code').value.length != 6) {
				fMsg += "Anti-spam code too short. 6 characters required.\n";
			}
			break;
		case 2:
			for(arr in arrangers) {
				if(arrangers[arr] == document.getElementById('in_name').value.toLowerCase()) {
					fMsg += "The name/alias of the arranger already exists.\n";
				}
				r++;
			}
			for(eml in emails) {
				if(emails[eml] == document.getElementById('in_email').value.toLowerCase()) {
					fMsg += "The email address is already in use.\n";
				}
				r++;
			}
			if(document.getElementById('in_name').value.length < 2) {
				fMsg += "Name/alias too short. At least 2 characters required.\n";
			}
			if(document.getElementById('in_password').value.length < 3) {
				fMsg += "Password too short. At least 3 characters required.\n";
			}
			if(document.getElementById('security_code2').value.length != 6) {
				fMsg += "Anti-spam code too short. 6 characters required.\n";
			}
			if(!checkEmail(document.getElementById('in_email').value)) {
				fMsg += "There seems to be an error with the email address.\n";
			}
			if(document.getElementById('in_email').value != document.getElementById('in_email2').value) {
				fMsg += "Error with email address confirmation.\n";
			}
			if(document.getElementById('in_password').value != document.getElementById('in_password2').value) {
				fMsg += "Error with password confirmation.\n";
			}
			break;
		case 3:
			if(document.getElementById('addnew').value == 0 && document.getElementById('production').value == 0) {
				fMsg += "You must select a production.\n";
			}
			if(document.getElementById('addnew').value == 1) {
				if(document.getElementById('prod_name').value == '') {
					fMsg += "You must at least enter a production name.\n";
				}
				if(document.getElementById('addcomp').value == 1 && document.getElementById('new_composer').value == '') {
					fMsg += "Enter the name of the composer.\n";
				}
			}
			break;
		case 4:
			Stamp = new Date();
			if(document.getElementById('remix_name').value == '') {
				fMsg += "You must enter a remix name.\n";
			}
			if(document.getElementById('remix_year').value < 1985 || document.getElementById('remix_year').value > Stamp.getFullYear()) {
				fMsg += "You must enter a valid year between 1985 and current year.\n";
			}
			break;
		case 5:
			var str = document.getElementById('remix_file').value;
			if(str != '' && str.substr((str.length-4),4).toLowerCase() != ".mp3") {
				fMsg += "You can only submit .mp3-files.\n";
			} else if(str == '') {
				fMsg += "You must select a file or enter a file name.\n";
			} else {
				document.getElementById('waiter').style.visibility = 'visible';
				document.getElementById('waitImage').src=document.getElementById('waitImage').src;
			}
			break;
		case 6:
			if(confirm("Are you sure you want to cancel the submission?")) {
				document.getElementById('cancelform').submit();
			}
			break;
		case 7:
			if(confirm("Are you sure you want to confirm the submission?")) {
				document.getElementById('confirmform').submit();
			}
			break;
		case 8:
			if(document.getElementById('j_name').value.length < 2) {
				fMsg += "Name too short. At least 2 characters required.\n";
			}
			if(!checkEmail(document.getElementById('j_email').value)) {
				fMsg += "There seems to be an error with the email address.\n";
			}
			if(document.getElementById('j_password').value != "" || document.getElementById('j_password2').value != "") {
				if(document.getElementById('j_password').value.length < 3) {
					fMsg += "Password too short. At least 3 characters required.\n";
				}
				if(document.getElementById('j_password').value != document.getElementById('j_password2').value) {
					fMsg += "Error with password confirmation.\n";
				}
			}
			break;
		case 9:
			if(!checkEmail(document.getElementById('a_email').value)) {
				fMsg += "There seems to be an error with the email address.\n";
			}
			if(document.getElementById('a_password').value != "" || document.getElementById('a_password2').value != "") {
				if(document.getElementById('a_password').value.length < 3) {
					fMsg += "Password too short. At least 3 characters required.\n";
				}
				if(document.getElementById('a_password').value != document.getElementById('a_password2').value) {
					fMsg += "Error with password confirmation.\n";
				}
			}
			break;
		case 10:
			if(document.getElementById('submission_running').value == 1) {
				var question = "You have a submission in progress. Pressing OK will mean you'll lose the current process.\n\nAre you sure you want to edit the submission?\n\nThis will revoke the submission from the queue and you will lose your current slot in the queue.\nYou may also choose this to be able to cancel the submission completely.";
			} else {
				var question = "Are you sure you want to edit the submission?\n\nThis will revoke the submission from the queue and you will lose your current slot in the queue.\nYou can also choose this to be able to cancel the submission completely.";
			}
			if(!confirm(question)) {
				fMsg = "Ok, skip the cancellation!";
			}
			break;
		case 11:
			if(document.getElementById('j_name').value.length < 3 || document.getElementById('j_password').value.length < 3) {
				fMsg = "You need to fill out both fields correctly.";
			}
			break;
		default:
			break;
	}
	if(fMsg == "") {
		rVal = true;
		if(form_id == 5) {
			document.getElementById('stepform').submit();
			document.getElementById('waitImage').src=document.getElementById('waitImage').src;
		}
		if(form_id == 10) {
			document.getElementById('edittuneform').submit();
		}
	} else {
		rVal = false;
		if(form_id != 10) {
			alert(fMsg);
		}
	}
	return rVal;
}

function checkEmail(e) {
	ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";
	for(i=0; i < e.length ;i++) {
		if(ok.indexOf(e.charAt(i)) < 0) { 
			return (false);
		}
	}

	if (document.images) {
		re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
		re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
		if (!e.match(re) && e.match(re_two)) {
			return (-1);
		}
	}
}

function prevote(id, title, arranger_id, arranger_name) {
	window.open("http://www.remix64.com/box_prevoting.php?id="+id+"01"+"&title="+title+"&arranger_id="+arranger_id+"01&arranger_name="+arranger_name, "prevotethingy",   "toolbar=no,location=no,status=no,menubar=no,scrollbars=no,resizable=no,width=460,height=500,left=450,top=200,screenX=450,screenY=300");
}

function doApprove(id, title) {
	if(id > 0) {
		if(confirm("Do you really want to approve the submission of " + title + "?")) {
			document.getElementById('cmd').value = "doapprove";
			document.getElementById('rmx_id').value = id;
			document.getElementById('juryform').submit();
		}
	}
}

function doDeny(id, title) {
	if(id > 0) {
		if(confirm("Do you really want to decline the submission of " + title + "?")) {
			document.getElementById('cmd').value = "dodeny";
			document.getElementById('rmx_id').value = id;
			document.getElementById('juryform').submit();
		}
	}
}

function switchCheck(frm, chkStr, chk) {
	for (var i = 0; i < frm.elements.length; i++) {
		var e = frm.elements[i];
		if ((e.name.substr(0, chkStr.length) == chkStr) && (e.type == 'checkbox')) {
			e.checked = chk.checked;
		}
	}
}

function bundleWaiter() {
	document.getElementById('waiter').style.visibility = 'visible';
	document.getElementById('waitImage').src=document.getElementById('waitImage').src;
	document.getElementById('bundleform').submit();
}