/*
	Naked Music
	Copyright 2006 Lovell Fuller
*/

function isws(s)
{
   var i;
   var whitespace = " \t\n\r";
   if ((s == null) || (s.length < 1)) return true;
   for (i = 0; i < s.length; i++)
   {
      var c = s.charAt(i);
      if (whitespace.indexOf(c) == -1) return false;
   }
   return true;
}

function isEmail(s)
{
	if ((s == null) || (s.length < 1)) return false;
	if (isws(s) == true) return false;
	var i = 1;
	var sLength = s.length;
	while ((i < sLength) && (s.charAt(i) != "@")) i++;
	if ((i >= sLength) || (s.charAt(i) != "@")) return false; else i += 2;
	while ((i < sLength) && (s.charAt(i) != ".")) i++;
	if ((i >= sLength - 1) || (s.charAt(i) != ".")) return false; else return true;
}

function isDate(s)
{
	// TODO: validate date in DD/MM/YYYY format
	return true;
}

function isCurrency(s)
{
	// TODO: validate float to 2dp
	return true;
}

function trim(str)
{
	return str.replace(/^\s*|\s*$/g,"");
}

function confirmBack(message)
{
	var cancel = false;
	if (message)
	{
		cancel = confirm(message);
	}
	else
	{
		cancel = confirm("Are you sure you want to go back without saving?");
	}
	if (cancel)
	{
		history.go(-1);
	}
}

function helpOpen(code)
{
	var helpWindow = window.open("/help/" + code, "help", "width=460,height=360,scrollbars=1");
	helpWindow.focus();
}

function onChangeMedia(formName, fieldName, artistCode, elementId)
{
	var field = eval("document." + formName + "." + fieldName);
	var element = document.getElementById(elementId);
	if (field && element)
	{
		var mediaId = field.options[field.selectedIndex].value;
		var url = "/media/" + artistCode + "/" + mediaId + ".jpg";
		if (mediaId == '')
		{
			url = "/images/clear.gif";
		}
		element.src = url;
	}
	return true;
}

/* toggle the appearance of a layer */
function getDivElementFromName(whichDiv)
{
	var divElement;
	if (document.getElementById)
		divElement = document.getElementById(whichDiv);
	else if (document.all)
		divElement = document.all[whichDiv];
	else if (document.layers)
		divElement = document.layers[whichDiv];
	return divElement;
}
function toggleDiv(whichDiv)
{
	var divElement = getDivElementFromName(whichDiv);
	var isVisible = divElement.style;
	if (isVisible.display == '' && divElement.offsetWidth != undefined && divElement.offsetHeight != undefined)
		isVisible.display = (divElement.offsetWidth != 0 && divElement.offsetHeight != 0) ? 'block' : 'none';
	isVisible.display = (isVisible.display == '' || isVisible.display == 'block') ? 'none' : 'block';
}
function showDiv(whichDiv)
{
	var divElement = getDivElementFromName(whichDiv);
	divElement.style.display = 'block';
}
function hideDiv(whichDiv)
{
	var divElement = getDivElementFromName(whichDiv);
	divElement.style.display = 'none';
}

/* remove Google toolbar styling */

if (window.attachEvent) window.attachEvent("onload", googleSetListeners);
function googleSetListeners()
{
	inputList = document.getElementsByTagName("INPUT");
	for (i = 0; i < inputList.length; i++)
	{
		inputList[i].attachEvent("onpropertychange", googleRemoveYellow);
		inputList[i].style.backgroundColor = "";
	}
	selectList = document.getElementsByTagName("SELECT");
	for (i = 0; i < selectList.length; i++)
	{
		selectList[i].attachEvent("onpropertychange", googleRemoveYellow);
		selectList[i].style.backgroundColor = "";
	}
}
function googleRemoveYellow()
{
	if(event.srcElement.style.backgroundColor != "") event.srcElement.style.backgroundColor = "";
}

/* embed flash video player */
function writeFlash(p)
{
	var mediaId = p.src;
	var html = '';
	if (mediaId.indexOf('.flv') > 0)
	{
		var splash = mediaId.substring(0, mediaId.length - 4) + '.jpg';
		html += '<object type="application/x-shockwave-flash" data="/flash/FlowPlayer.swf" wmode="transparent" width="320" height="264">';
		html += '<param name="movie" value="/flash/FlowPlayer.swf" />';
		html += '<param name="allowScriptAccess" value="sameDomain" />';
		html += '<param name="quality" value="high" />';
		html += '<param name="wmode" value="transparent" />';
		html += '<param name="flashvars" value="config={videoFile: \'' + mediaId + '\', autoPlay: false, loop: false, showMenu: false, showFullScreenButton: false, initialScale: \'fit\', splashImageFile:\'' + splash + '\', scaleSplash: true}" />';
		html += '</object>';
	}
	if (mediaId.indexOf('.mp3') > 0)
	{
		var mediaName = document.title;
		html += '<object type="application/x-shockwave-flash" data="/flash/xspf_player_slim.swf?song_url=' + encodeURI(mediaId) + '&amp;song_title=' + encodeURI(mediaName) + '&amp;player_title=click%20to%20play" width="400" height="15">';
		html += '<param name="allowScriptAccess" value="sameDomain" />';
	    html += '<param name="movie" value="/flash/xspf_player_slim.swf?song_url=' + encodeURI(mediaId) + '&amp;song_title=' + encodeURI(mediaName) + '&amp;player_title=click%20to%20play" />';
		html += '<param name="quality" value="high" />';
		html += '<param name="wmode" value="transparent" />';
		html += '</object>';
	}
	if (html.length > 0)
	{
		document.write(html);
	}
}

/* TinyMCE custom file browser */
function dispatchesFileBrowser(fieldName, url, type, win) {
	var uri = "/tinymce/" + type;
	if (url && (type == "image" || type == "media")) {
		uri = uri + "/media/" + url.replace(/[^0-9]/g, "");
	}
	tinyMCE.activeEditor.windowManager.open({
        file : uri,
        title : "Browse",
        width : 620,
        height : 400,
        resizable : "yes",
        inline : "yes",
        close_previous : "no"
    }, {
        window : win,
        input : fieldName
    });
    return false;
}
