var cols;
var sel = new Array;

var editPage="";

function go(formId) {
	form = document.getElementById(formId);
	form.submit();
}

function closePopWin() {
//	hidePopWin(false);
	var actionField = document.getElementById("action");
	var form = actionField.form;
	form.submit();
}

function showEditPage(page) {
	if(editPage!="") {
		var lastPage = document.getElementById("subPage_"+editPage);
		lastPage.style.display = 'none';
	}
	showPage = document.getElementById("subPage_"+page);
	showPage.style.display = '';
	editPage = page;

	var currentPage = document.getElementById("pageName");
	currentPage.value = page;

}

var selectedTracks;
function selectTrack(disc,id) {

	var inputFieldName = "disc_"+disc;
	var imageFieldName = "discimage_"+disc+"_"+id;
	inputField = document.getElementById(inputFieldName);

	var currentValue = inputField.value;
	var re = new RegExp(".*"+id+".*","g");

	r = currentValue.search(re);
	image = document.getElementById(imageFieldName);
	if(r==false) {
		newVal = currentValue.replace(","+id,"");
		inputField.value = newVal; 
		image.src = 'http://development/colville/onedesk/style/sttw/images/tick_box_off.gif';
	} else {
		inputField.value = inputField.value + "," + id;
		image.src = 'http://development/colville/onedesk/style/sttw/images/tick_box_down.gif';
	}

}


function dispSubClass(field,action,urn) {
	var actionField = document.getElementById("action");
	actionField.value = "dispSubClass";
	var subField = document.getElementById("subField");
	subField.value = field;
	var subAction = document.getElementById("subAction");
	subAction.value = action;
	var subUrnField = document.getElementById("subUrn");
	subUrnField.value = urn;
	var form = actionField.form;
	form.submit();
}


function dispSelect(field) {
	var action = document.getElementById("action");
	var subField = document.getElementById("subField");
	action.value = "dispSelect";
	subField.value = field;
	var form = action.form;
	form.submit();
}


function removePick(field,pickUrn) {
	var action = document.getElementById("action");
	var pickField = document.getElementById("pickField");
	var pickUrnField = document.getElementById("pickUrn");
	action.value = "removePick";
	pickField.value = field;
	pickUrnField.value = pickUrn;
	var form = action.form;
	form.submit();
}

function addPick(field,pickUrn) { 
	var docParent = window.frameElement.ownerDocument;
	var action = docParent.getElementById("action");
	var pickField = docParent.getElementById("pickField");
	var pickUrnField = docParent.getElementById("pickUrn");
	action.value = "addPick";
	pickField.value = field;
	pickUrnField.value = pickUrn;
	var form = action.form;
	form.submit();
}

function mObject(){
   if (navigator.appName.indexOf ("Microsoft") !=-1){
    	return window["SingMovie"];
   }else{
    	return document["SingMovie"];
   }
}

function stop() {
   mObject().StopPlay();
}

function play() {
   mObject().Play();
}

function rewind() {
	var play = 0;	

	if(mObject().IsPlaying()){
		play=1;
	}

	mObject().GotoFrame(4);

	if(play==1) {
		mObject().Play();
	}
}

function pause() {
   if(mObject().IsPlaying()){
	    stop();
   }else{
    	play();
   }
}

function submitListForm(formName,listfield) {
	var action = document.getElementById("action_"+formName);
	if(action=="moveTo") {
		var moveTo  = document.getElementById("moveTo_"+formName);
		moveTo.value = listfield.option[listfield.value].id;
	}
	action.value = listfield.value;
	action.form.submit();
}

function submitForm(action,formName) { 
	var actionField = document.getElementById("action_"+formName);
	actionField.value = action;

	actionField.form.submit();
}

function submit(action) { 
	var actionField = document.getElementById("action");
	actionField.value = action;
	form = actionField.form;
	form.submit();
}

function doSubmit(action) { 
	var actionField = document.getElementById("action");
	actionField.value = action;

	form = actionField.form;
	form.submit();
}


var playTrack = false;
function playMP3(trackUrl) {
	if(playTrack) { stopTrack; playTrack=false; return; }
	Player.URL=trackUrl;
	playTrack = true;
}

function stopTrack() {
	Player.controls.stop();
}

function highlightRow(row,on) {
	var cell;
	for(i=0;i<cols;i++) {
		name = "cell"+row+"_"+i;
		cell = document.getElementById(name);
		if(on) {
			if(sel[row]) {
				cell.className="tableDataSelected";
			} else {
				cell.className="tableDataOver";
			}
		} else {
			if(sel[row]) {
				cell.className="tableDataSelected";
			} else {
				if((row%2)==0) {
					end="Even";
				} else {
					end = "Odd";
				}
				cell.className="tableData"+end;
			}
		}
	}
}

function selectRow(row) {
	if(sel[row]) {
		sel[row]=false;
	} else {
		sel[row]=true;
	}
	for(i=0;i<cols;i++) {
		name = "cell"+row+"_"+i;
		cell = document.getElementById(name);
		if(sel[row]) {
			cell.className="tableDataSelected";
		} else {
			if((row%2)==0) {
				end="Even";
			} else {
				end = "Odd";
			}
			cell.className="tableData"+end;
		}
	}
	
}


function showHide(id) {
	var searchBox = document.getElementById(id);
	if(searchBox.style.display=="none") {
		searchBox.style.display="";
	} else {
		searchBox.style.display="none";
	}
}

function hide(id) {
	var searchBox = document.getElementById(id);
	if(searchBox == undefined) { return; }
	searchBox.style.display="none";
}

function minMax(id,minImg,maxImg) {

	showHide(id);
	var imgId = "img"+id;
	
	var searchBox = document.getElementById(id);
	var img = document.getElementById(imgId);
	if(searchBox.style.display=="none") {
		img.src=maxImg;
	} else {
		img.src=minImg;
	}

}

function updateField(field,value,disp) {
	var searchBox = window.frameElement.ownerDocument.getElementById(field);
	searchBox.value = value;
	var dispBox = window.frameElement.ownerDocument.getElementById("disp_"+field);
	dispBox.value = disp;
	parent.hidePopWin(false);
}

function resizeFrame(width,height) {
//alert(width+":"+height);
	if(width!="same") {
		window.frameElement.style.width = width;
	}
	if(height!="same") {
		window.frameElement.style.height = height;
	}
}


function addTrack(id) {
//	var pickDisp = window.frameElement.ownerDocument.frames["pickDisp"];
	var pickDisp = document.frames["pickDisp"];
	var pickRef = pickDisp.document.getElementById("pickRef");
	pickRef.value = id;
	pickRef.form.submit();

}


function pick_select(id, all) {
	var target = document.getElementById(id+"_selected");
	var source = document.getElementById(id+"_avail");	
	
	var tpos = target.options.length;
	
	
	for(i=0;i<source.options.length;) {
		if(source.options[i].selected || all) {
			target.options[tpos] = new Option(source.options[i].text,source.options[i].value);
			target.options[tpos].selected = true;
			
			tpos++;
		    
			source.options[i] = null;
			i=0;
		} else {
			i++;
		}
	}
	updateSelectHidden(id);
}

function updateSelectHidden(id) {

	var target = document.getElementById(id+"_selected");
	var hidden = document.getElementById(id);
	
	hidden.value = "";
	for(i=0;i<target.options.length;i++) {
		hidden.value = hidden.value + "," +target.options[i].value;
	}

}

function pick_unselect(id, all) {
	var source = document.getElementById(id+"_selected");
	var target = document.getElementById(id+"_avail");	
	
	var tpos = target.options.length;
	
	for(i=0;i<source.options.length;) {
		if(source.options[i].selected || all) {
			target.options[tpos++] = new Option(source.options[i].text,source.options[i].value);
			source.options[i] = null;
			i=0;
		} else {
			i++;
		}
	}
	updateSelectHidden(id);
}

function minimizeWin() 
{
window.resizeTo(100,100);
window.moveTo(screen.width,screen.height);
}

function maximizeWin() 
{
window.resizeTo(screen.width,screen.height);
window.moveTo(0,0);
}

function openWindow(url,name,height,width,xpos,ypos) {
	window.open(url,name,"scrollbars=1,width="+width+",height="+height+",top="+ypos+",left="+xpos);
}

var UniqueID = 314 // Make each link open in a new window 
var newWinOffset = 0 // Position of first pop-up

function PlayerOpen(soundfiledesc,soundfilepath) {
	PlayWin = window.open('',UniqueID,'width=320,height=190,top=' + newWinOffset +',left=0,resizable=0,scrollbars=0,titlebar=0,toolbar=0,menubar=0,status=0,directories=0,personalbar=0');
	PlayWin.focus(); 
	
	var winContent = "<HTML><HEAD><TITLE>" + soundfiledesc + "</TITLE></HEAD><BODY bgcolor='#8B23A0'>"; 
	winContent += "<B style='color:#fff;font-size:18px;font-family:Verdana,sans-serif;line-height:1.5'>" + soundfiledesc + "</B>";
	
	winContent += "<OBJECT width='300' height='42'>"; 
	winContent += "<param name='SRC' value='" + soundfilepath + "'>";
	winContent += "<param name='AUTOPLAY' VALUE='true'>"; 
	winContent += "<param name='CONTROLLER' VALUE='true'>";
	winContent += "<param name='BGCOLOR' VALUE='#8B23A0'>"; 
	winContent += "<EMBED SRC='" + soundfilepath + "' AUTOSTART='TRUE' LOOP='FALSE' WIDTH='300' HEIGHT='42' CONTROLLER='TRUE' BGCOLOR='#8B23A0'></EMBED>";
	winContent += "</OBJECT>"; 
	
	winContent += "<p style='color:#fff;font-size:12px;font-family:Verdana,sans-serif;text-align:center'><a style='color:#fff;' href='" + soundfilepath +"'>Download this file</a> <SPAN style='font-size:10px'>(right-click or Option-click)</SPAN></p>";
	winContent += "<FORM><DIV align='center'><INPUT type='button' value='Close this window' onclick='javascript:window.close();'></DIV></FORM>"; 
	winContent += "</BODY></HTML>"; 
	
	PlayWin.document.write(winContent); 
	PlayWin.document.close(); // "Finalizes" new window 
	UniqueID = UniqueID + 1 // newWinOffset = newWinOffset + 20 // subsequent pop-ups will be this many pixels lower 

}
function changeActionAndSubmitForm (formName,url) {
	var form = document.getElementById(formName);
	form.action=url; 
	form.submit();
}

function updateFinalAdjustment(input, output, instock, direction) {
	var inputField = document.getElementById(input);
	var outputField = document.getElementById(output);
	
	if (!isNaN(inputField.value)) { 
		if ( inputField.value > 0 ) {		
			if (direction == 'increase') {
				var total = parseInt(instock) + parseInt(inputField.value);
			} else {
				var total = parseInt(instock) - parseInt(inputField.value);
			}
			if (total > 0) {
				outputField.style.color ='black';
			} else {
				outputField.style.color ='red';
			}
		} else {
			var total = "Number must be positive";
			outputField.style.color ='red';	
		}
	} else {
		var total = "Please enter a number";
		outputField.style.color ='red';
	}
	
	outputField.value=total;
	if (outputField.value == 'NaN') { outputField.value = ''; }
}

function printPage() {
    window.print();
}

function embeddedSoundFix() {
	//put this function straight after your embedded sound object
	objects = document.getElementsByTagName('object');
	for (var i = 0; i < objects.length; i++)
	{
		objects[i].outerHTML = objects[i].outerHTML;
	}
}

function getElementsByClassName(oElm, strTagName, strClassName){
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];
		if(oRegExp.test(oElement.className)){
			arrReturnElements.push(oElement);
		}
	}
	return (arrReturnElements)
}

function showHideBlock(blockName, elType, blockHeaderImgShown, blockHeaderImgHidden) {
	var rows = getElementsByClassName(document, elType, blockName);
	var headerImg = document.getElementById(blockName+"_headerImg");
	for(var row in rows) {
		var result = showHide(rows[row].id);
	}
	if (result == 1) {
		headerImg.src = blockHeaderImgHidden;
	} else {
		headerImg.src = blockHeaderImgShown;
	}
	return false;
}

function showHide(id, forceHide) {
	var searchBox = document.getElementById(id);
	if (forceHide) {
		searchBox.style.display="none";
		return 0;
	}
	
	if(searchBox.style.display=="none") {
		searchBox.style.display="";
		return 1;
	} else {
		searchBox.style.display="none";
		return 0;
	}
}

	
