var lw = null;
var updateLineId, activeListId;
var modifyPopup = null;
var modifyWeb = null;

function ModifyOpen(lineID, listID){
	updateLineId = lineID;
	activeListId = listID;
	
	if (modifyWeb == null) {
		modifyWeb = new LiveWeb(ModifyReturned);
	}
	
	modifyWeb.Get("adrListMethods.php?mode=loadLine&id=" + lineID);
}


function ModifyReturned(success, result) {
	if (!success) {
		alert(result);
		return;
	}
	
	result = eval('(' + result + ')');
	
	//Load the editor fields.
	Get('modifyCharacter').value = result.character;
	Get('modifyReelNumber').value = result.reelNumber;
	Get('modifySceneNumber').value = result.sceneNumber;
	Get('modifyTcIn').value = result.tcIn;
	Get('modifyTcOut').value = result.tcOut;
	Get('modifyLines').value = jsnRepair(result.lines);
	Get('modifyNotes').value = jsnRepair(result.notes);
	//Get('message'+updateLineId).innerHTML = result.date;
	
	//Open the popup
	if (modifyPopup == null){
		modifyPopup = new Popup();
	}
	
	//alert('hello');
	modifyPopup.Background('modifyPopup', document.body, 'white', 75, true);
	//modifyPopup.PositionCenter('modifyPopup', true);
	modifyPopup.Open('modifyPopup','lineContainer'+updateLineId);
}


function updateLine(){
	if (lw == null) {
		lw = new LiveWeb(updateLineReturned);
	}
	
	lw.SetPost("mode", "updateLine");
	lw.SetPost("id", updateLineId);
	lw.SetPost("activeListId", activeListId);
	lw.SetPost("modifyCharacter", document.getElementById('modifyCharacter').value);
	lw.SetPost("modifyReelNumber", document.getElementById('modifyReelNumber').value);
	lw.SetPost("modifySceneNumber", document.getElementById('modifySceneNumber').value);
	lw.SetPost("modifyTcIn", document.getElementById('modifyTcIn').value);
	lw.SetPost("modifyTcOut", document.getElementById('modifyTcOut').value);
	lw.SetPost("modifyLines", document.getElementById('modifyLines').value);
	lw.SetPost("modifyNotes", document.getElementById('modifyNotes').value);
	lw.Post('adrListMethods.php');
}


function updateLineReturned(success, result){
	if (!success) {
		alert('Error: ' + result);
		return;
	}
	
	result = eval("(" + result + ")");
	var messageText = result.message;
	
	if (result.status == "success"){
		Get('character' + updateLineId).innerHTML = result.character;
		Get('reelNumber' + updateLineId).innerHTML = result.reelNumber;
		Get('sceneNumber' + updateLineId).innerHTML = result.sceneNumber;
		Get('tcIn' + updateLineId).innerHTML = result.tcIn;
		Get('tcOut' + updateLineId).innerHTML = result.tcOut;
		Get('lines' + updateLineId).innerHTML = jsnRepair(result.lines,1);
		Get('notes' + updateLineId).innerHTML = jsnRepair(result.notes,1);
		Get('firstName' + updateLineId).innerHTML = result.firstName;
		Get('lastName' + updateLineId).innerHTML = result.lastName;
		Get('lastName' + updateLineId).innerHTML = result.lastName;
		Get('adrLinesHistoryDate' + updateLineId).innerHTML = result.adrLinesHistoryDate;		
		Get('adrListsHistoryDate').innerHTML = result.adrLinesHistoryDate;		
		
		setTimeout("Get('message" + updateLineId + "').innerHTML = ''", 5000);
		ModifyClose();
	}
	
	if (result.debug != ''){
		Get('debugMessage').innerHTML = result.debug;
		//messageText = '<a href="javascript:DebugOpen()">' + messageText + '</a>';
	}
	
	Get('message'+updateLineId).innerHTML = messageText;
}


function DeleteLine(id){
	if (lw == null) {
		lw = new LiveWeb(DeleteLineReturned);
	}
	
	lw.Get('adrList.php?mode=delete&id=' + id);
}


function DeleteLineReturned(success, result){
	if (!success){
		alert('Error: ' + result);
		return;
	}
	
	result = eval("(" + result + ")");
	
	if (result.status == "success") {
		//alert('Saved Successfully by ' + result.data);
		document.getElementById('message').value = result.date;
	}
}


function ModifyClose(){
	modifyPopup.CloseAll();
}


var debugPopup = null;
function DebugOpen() {
	if (debugPopup == null){
		debugPopup = new Popup();
	}
	
	debugPopup.PositionCenter('debugPopup', true);
	debugPopup.Open('debugPopup', null);
}


function DebugClose(){
	debugPopup.CloseAll();
}


function validateNewAdr(timecodeIn,timecodeOut,whichone){
	var timecode1 = document.getElementById(timecodeIn).value;
	var timecode2 = document.getElementById(timecodeOut).value;
	var timecodeCheck = /^\d{2}:\d{2}:\d{2}:[0-3]\d$/;
	var errors = 0;
	var answer = 'Errors: ';
	document.getElementById("validationResponse"+whichone).innerHTML='&nbsp;';

	if(timecode1 != ''){
		if(timecodeCheck.test(timecode1) == 0){
			answer += '<br />Check Timecode In. ';
			errors = 1;
		}
	}
	if(timecode2 != ''){
		if(timecodeCheck.test(timecode2) == 0){
			answer += '<br />Check Timecode Out. ';
			errors = 1;
		}
	}
	if (errors){
		document.getElementById("validationResponse"+whichone).innerHTML=answer;
	}else{
		document.getElementById("validationResponse"+whichone).innerHTML='Timecode is OK.';
	}

}


function clearField(field){
	var thingy = document.getElementById(field);
	if(thingy.value == '00:00:00:00'){
		thingy.value = '';
	}
}


function showHideModify(whichOne,showorhide){
	var line = document.getElementById(whichOne);
	var buttonHider = document.getElementById('buttonHider');
	var funkymonkey = document.getElementsByTagName("div");
	//e = document.getElementsByTagName("span")
	
	if(showorhide == 'both'){
		if(line.style.display=='none'){
			line.style.display='';
			for (i=0;i<funkymonkey.length;i++){
				if (funkymonkey[i].id == "buttonHider"){
					funkymonkey[i].style.display = 'none';
				}
			}
		}else{
			line.style.display = 'none';
			for (i=0;i<funkymonkey.length;i++){
				if (funkymonkey[i].id == "buttonHider"){
					funkymonkey[i].style.display = 'block';
				}
			}	
		}
	}
	if(showorhide == 'show'){
		line.style.display = '';
		for (i=0;i<funkymonkey.length;i++){
			if (funkymonkey[i].id == "buttonHider"){
				funkymonkey[i].style.display = 'none';
			}
		}
	}
	if(showorhide == 'hide'){
		line.style.display = 'none';
		for (i=0;i<funkymonkey.length;i++){
			if (funkymonkey[i].id == "buttonHider"){
				funkymonkey[i].style.display = 'block';
			}
		}	
	}
}


function deleteLineConfirmation(id){
	if(confirm("Are you sure you want to delete this line?")){
		location.href = 'adrList.php?delete=' + id;
		return true;
	}else{
		return false;
	}
}


function undeleteLine(id){
	location.href = 'adrList.php?undelete=' + id;
	return true;
}


function approveConfirmation(){
		if(confirm("Are you sure you want to approve this line?")){
		return true;
	}else{
		return false;
	}
}


function jsnRepair(thingy,forhtml){
	if(forhtml){
		return thingy.replace(/\[br\]/g,'<br />\n');
	}else{
		return thingy.replace(/\[br\]/g,'\n');
	}
}

function arrow(putThis,intoThis){
	var thingToPut = document.getElementById(putThis).innerHTML;
	var whereToPut = document.getElementById(intoThis);
	whereToPut.value = thingToPut;
	//alert ('thingToPut: '+thingToPut+'\nintoThis: '+intoThis+'\nwhereToPut: '+whereToPut);
}

function tcswap(){
	var tcIn = document.getElementById('tcIn').value;
	var tcOut = document.getElementById('tcOut').value;
	document.getElementById('tcIn').value = tcOut;
	document.getElementById('tcOut').value = tcIn;
}
