// Tuesday, 17 Jan. 2006 - Here's a laundry list, or punch list, of things to fix for next release:
// 1.) Add sanity check for reel names, only letters and numbers allowed. Uppercase only if you want to be strict.
// 2.) Fix sanity check for numberic entry of shot number, it hangs right now.
// 3.) Uh, see about getting the 'save' function to work locally.


//Friday, 13 Jan. 2006 - build for request for audio, just the AA/V thing I guess, their 
// (guy named tony in S Africa) machine chokes when it has none. OK...will give it a go.

//see earlier versions for more historical information regarding progress. For
//now I'm keeping it clean
//7/4/2004 2:35AM -- well, I was, but it got messy again.
// so it's come a long way since...added more error checking, few more styles, compacted everything into one page
// plus no more divs, just tables.
//and most of all got save function to break out onto global CGI mode, so browser platform no longer
//such a big deal. Even almost works on safari.

//Still...

// **check for the arrow keys
// ** think about ways to buffer a line and undo it (yikes).
//**look into writeln vs. innerText ( I already did years ago but forgot the outcome.
//** getting style conflict on tc write errors
//* maybe an email finished list option?

// ** Opera PC bad read on "." input -- leave that alone and just use the 'f' key, that's why it's there.
//** need margin on left in listout
//gets stuck in frames mode when user clears * or f out of box
// safari tabindex problem ******

var edlLine = "";
var recordOUT = 108000;
var shot = "001";
var standard;
var goodToGo = 0;
var frameFlag = 0;
var duration;
var errSide;
var tcMode;

if( window.opera && document.childNodes ) { brow = 'op7'} //find  opera (later...but why, do I do anything with this?

function filterKeys(e, currBox, sideVal) {

	if (document.getElementById) {
		side = document.getElementById(sideVal);
	}
	else { //you're fucked.
		alert ("Very sorry(!), but you'll need a more updated browser to use this page")
		window.location="http://beakfx.com/index.html#tools";
	}
	
	var charCode = e.keyCode;
	var doWhat;
	//alert (charCode); // Opera . is 78 and opera * is 42 (?? WTF!)
	//alert(charCode); // 106 = numpad *, 70 is 'f' 
    	if (charCode==190 || charCode == 110) doWhat = "dot";
    	if (charCode >= 96 && charCode <= 105) doWhat = "numpad";
    	if (charCode >= 48 && charCode <= 57) doWhat = "normal";
    	if (charCode < 32 || charCode == 37 || charCode == 39) doWhat = "badkeys";
    	if ((charCode == 78 || charCode == 190)  && brow =='op7') doWhat = "badKeys";  //deal with weird opera codes
    	if ( (currBox.name == "numericOUT") && (charCode == 106 || charCode == 70) ) {
    		doWhat = "wantFrames";
    	}
    	if (charCode == 8) doWhat = "backspace";
    	if (charCode == 9) return; // that's the tab key coming in or out, as not to flash red
    	
    	if (currBox.name == "numericIN") errSide = document.getElementById('lPane');
    		else errSide = document.getElementById('rPane');

    	switch(doWhat) {
    	
    		case "badkeys":
    		//figure out here how to turn currbox bg red...
    		errSide.style.backgroundColor='#dd3333';
    		setTimeout("errSide.style.backgroundColor='#117711'", 500);
    		//nice.
    		return;
    		
    		case "backspace":
    		writeOut(currBox, side);
    		return;
    		
    		case "dot":
    		if ( checkDigit(currBox) == 1) break;
    		currBox.value = currBox.value.substring (0, currBox.value.length-1) + "00";
    		writeOut(currBox, side);
    		return;
    		
    		case "numpad":
    		if ( checkDigit(currBox) == 1) break;
    		writeOut(currBox, side);
    		return;
    		
    		case "wantFrames":
    		frameFlag = 1;
    		return;
    		
    		case "normal":
    		if ( checkDigit(currBox) == 1) break;
    		writeOut(currBox, side);
    		return;    
    		
    	}//switch
    	
	currBox.value = currBox.value.substring(0, (currBox.value.length-1));
	errSide.style.backgroundColor='#cc3333';
    	setTimeout("errSide.style.backgroundColor='#117711'", 500);
    		
}//fn proc keys


function checkDigit (currBox) {

	var len = currBox.value.length;
	var digit = currBox.value.substr(len-1, 1); //gets last char in box
	
	if (frameFlag == 1) return;
	
	if ( ( len == 1 || len == 3 || len == 5) && digit >5) return 1; // blocks 6-9 minute entry
	if (len == 7 && digit > 2) return 1; // blocks over 30 frame entry
	if ( standard == "pal" && len == 8 
	  && ( (currBox.value.substr(len-2, 1) == 2)) && (digit > 4) ) return 1; // PAL 25+ frame protection
	if ( standard == "film" && len == 8 
	  && ( (currBox.value.substr(len-2, 1) == 2)) && (digit > 3) ) return 1; // 24p 25+ frame protection
	
	if (len > 8) return 1;
	return 0;
	
}// check digit


function writeOut (currBox, side) {

var newthing = "";
var cblen = currBox.value.length;
side.style.color="#222222"; //set back to black text
if (frameFlag == 1) {
		side.value=currBox.value.substring(1,currBox.value.length) + " frames";
		return;
	}

if (cblen < 2)  newthing = currBox.value;

if (cblen >= 2) newthing = currBox.value.substring(0,2) 
	+ ":" + currBox.value.substring(2,4);
	
if (cblen >= 4) newthing = currBox.value.substring(0,2)
	+ ":" + currBox.value.substring(2,4) + ":" 
	+ currBox.value.substring(4,6);

if (cblen >= 6) newthing = currBox.value.substring(0,2)
	+ ":" + currBox.value.substring(2,4) + ":" 
	+ currBox.value.substring(4,6) + ":"
	+ currBox.value.substring(6,8);

side.value = newthing;
	
}//writeOut


function sendLine () {
	
	var sourceIN = toFrames (document.ioForm.numericIN.value);
	var sourceOUT;
	if (frameFlag == 0) {
		sourceOUT = toFrames (document.ioForm.numericOUT.value);
		} else {
			sourceOUT = sourceIN + parseInt(document.ioForm.numericOUT.value.substring(1, document.ioForm.numericOUT.value.length) ) ;
			frameFlag = 0;
	}
	if (sourceOUT < sourceIN) {
		alert ("Oops! Out point is BEFORE in point");
		document.ioForm.numericOUT.value="";
		document.ioForm.numericOUT.focus();
		document.getElementById("tcmonR").style.color="#884444";
		return 0;
	}
	
	duration = sourceOUT-sourceIN;
	var recordIN = recordOUT;
	recordOUT= (recordIN + duration); //that should be a public var
	
	// how about this....save from  header overwirte, sort of...
	edlLine = document.edlForm.edlText.value;
	//edlLine += "\n"
	edlLine += document.ioForm.shot.value;
	edlLine += "  ";
	
	//here is where to harness sanity check for spaces in reel name.
	// better solution is to add a check reel function...and use it onBlur after reel box
	
	edlLine +=  document.ioForm.reel.value;
	for (i=document.ioForm.reel.value.length;i<9;i++) {
		edlLine += " ";
	}
	
	
//this is where AUDIO thingy should go, I think
	if (document.ioForm.audioBox.checked) {
		edlLine += "AA/V   C        "
		+ toCode(sourceIN) + " " + toCode(sourceOUT) + " "
		+ toCode(recordIN) + " " + toCode(recordOUT) + "\n";
	} else {
		edlLine += " V     C        "
		+ toCode(sourceIN) + " " + toCode(sourceOUT) + " "
		+ toCode(recordIN) + " " + toCode(recordOUT) + "\n";
	}
// i think that's how to do an 'else'
	if (document.ioForm.clipName.value != "") {
		edlLine = edlLine + "FROM CLIP NAME:" + document.ioForm.clipName.value + "\n";
	}
	if ((document.ioForm.frameBox.checked) &&(standard == "ntsc") ) {
		edlLine += "***Frame count " + duration + " Cine Expanded. " 
		+ Math.round(duration * 0.8) + " Cine Compressed. \n"
	}
	else if  (document.ioForm.frameBox.checked) edlLine += "***Frame count " + duration + "\n";
	
	document.edlForm.edlText.value=edlLine;
	
	// clean up without resetting the whole form, which is overkill
	shot = (document.ioForm.shot.value *1) + 1; //cheat out an integer and incriment.
	document.ioForm.shot.value = shot;
	checkShot();
	
	document.ioForm.numericIN.value = "";
	document.ioForm.numericOUT.value = "";
	document.ioForm.clipName.value = "";
	document.ioForm.shot.focus();
	document.ioForm.shot.select();
	
}//send line


function checkShot() {
	
//before we do anything, check to see if non-number characters were typed in.
	if (isNaN (document.ioForm.shot.value)) {
		document.ioForm.shot.focus();
		document.ioForm.shot.select();
		document.getElementById('shotBox').style.backgroundColor='#dd3333';
		document.getElementById('shotBox').style.color='#119911';
		alert("Shot number must be numeric.");
		document.ioForm.shot.value = "";
		return;
	}
	document.getElementById('shotBox').style.backgroundColor='#c8cccc';
	document.getElementById('shotBox').style.color='#000000';
	
// OK, now on with formatting (padding) shot number
	var shotBoxVal =  (document.ioForm.shot.value * 1); //make form val an integer as parseInt is broken.
	
	if (shotBoxVal < 10) document.ioForm.shot.value = "00" + shotBoxVal;
	if (shotBoxVal >= 10) document.ioForm.shot.value = "0" + shotBoxVal;
	if (shotBoxVal >= 100) document.ioForm.shot.value = shotBoxVal;
			
}//fn check shot

function checkReel (reelObj) { 
//CMX 3600 format allows only A-Z and 0-9, though I've seen lowercase letter before
// so let's preload the error variables to make the code not be 'write only'
	var charError = "Illegal characters in reel name.\nUppercase letters and digits 0-9 ONLY\nfor CMX 3600 strict.";
	var lowerError = "UPPERCASE only, please.\nUppercase letters and digits 0-9 ONLY\nfor CMX 3600 strict.";
	var reelStr = new String(reelObj.value);
	var illegalChars = /\W/;
	var lowerChars = /[a-z]/;
	
	if (illegalChars.test(reelStr)) {
		var bogusChar = illegalChars.exec(document.ioForm.reel.value);
		alert ("Character '" + bogusChar + "' isn't good.\n" + charError);
		//self.focus();
		//document.ioForm.reel.focus();
		reelObj.focus();
		reelObj.value = "XFER01";
		reelObj.select();
	}
	if (lowerChars.test(reelStr)) {
		alert ("lowercase letters in reel name.\n" + lowerError);
		reelObj.focus();
		reelObj.value = "XFER01";
		reelObj.select();
	}
	return 0;
} // check reel func

function toFrames (tc) {
	var frames;
	frames = ( (tc.substr(0,2)) * (tcMode * 3600) );
	frames = frames + ( ( (tc.substr(2,2) ) * (tcMode * 60) ) );
	frames = frames + ( ( (tc.substr(4,2) ) * tcMode) );
	frames = frames + ( ( (tc.substr(6,2) ) * 1) );
	return frames;
} //new frames


function toCode (frs) {

	var hrs = Math.floor (frs / (tcMode * 3600) );
	if (hrs < 10) hrs = "0" + hrs;
	frs = frs - (hrs * (tcMode * 3600));
	var min = Math.floor (frs / (tcMode * 60) );
	if (min < 10) min = "0" + min;
	frs = frs - (min * (tcMode * 60));
	var sec = Math.floor (frs / tcMode);
	if (sec < 10) sec = "0" + sec;
	frs = frs - (sec * tcMode);
	if (frs < 10) frs = "0" + frs;
	var formated = hrs + ":" + min + ":" + sec + ":" + frs;
	return formated;	
}//to code


function saveFile () {
	//the new style
document.edlForm.fileName.value=document.headerForm.edlfile.value;
document.edlForm.submit();
}//fn save file


function startUp() {

	var fcm = "\n";
	if (edlLine !== "") {
		alert ("Whoah! You've already started an EDL, reload page to start over\n \(use the restart button\).");
		return;
	}

	if (document.headerForm.standard[0].checked){
		standard = "ntsc";
		document.headerForm.standard[1].disabled=true;
		document.headerForm.standard[2].disabled=true;
		fcm = "\nFCM: NON-DROP FRAME\n";
		tcMode = 30;
	}
	if (document.headerForm.standard[1].checked) {
		standard = "pal";
		document.headerForm.standard[0].disabled=true;
		document.headerForm.standard[2].disabled=true;
		tcMode = 25;
		//try this...set recout to 1 hr of frames
		recordOUT = 90000;
	}
	if (document.headerForm.standard[2].checked) {
		standard = "film";
		document.headerForm.standard[0].disabled=true;
		document.headerForm.standard[1].disabled=true;
		tcMode = 24;
		recordOUT = 86400;
	}
	document.headerForm.startButton.disabled=true;
	document.getElementById('titleBox').disabled=true;

	var header = "TITLE:   " + document.headerForm.edlfile.value
	+ fcm;
	document.edlForm.edlText.value=header;
	document.ioForm.shot.focus();
	document.ioForm.shot.select();
}//got both pal and film, but should look at a real pal and film EDL to make sure ther isn't anything printed there.


function turnGreen (item) {
	var hiliteSide;
	if (item.name == "numericIN") hiliteSide = document.getElementById('lPane');
    		else hiliteSide = document.getElementById('rPane');

	hiliteSide.style.backgroundColor='#117711';
	//setTimeout("errSide.style.backgroundColor='#bbbbbb'", 500);

} //fn


function revertColor (item) {
	var hiliteSide;
	if (item.name == "numericIN") hiliteSide = document.getElementById('lPane');
    		else hiliteSide = document.getElementById('rPane');

	hiliteSide.style.backgroundColor='#aaaaaa';
} //fn