/*************************************************************
 *                                                           *
 *                                                           *  
 *                                                           *  
 *************************************************************/ 

/*************************************************************
 *                                                           * 
 *         Removes all white space characters at             *
 *         the beginin and end of string.                    *
 *                                                           *  
 *************************************************************/ 
	String.prototype.trim = function ()
	  {
 	     return this.replace(/^\s+/, '').replace(/\s+$/, '');
		}
/*************************************************************
 *                                                           * 
 *           Reset valeus of global variables                * 
 *                                                           *  
 *************************************************************/  
function clearValues(){
	filePath=null;
	subtitleText=null;
	editableSubtitleTemplate=null;
	document.getElementById('subtitleWindow').innerText='';	
	document.getElementById('timestampWindow').innerText='';
	}	
/*************************************************************
 *                                                           *
 *       Returns full path of srt file being opened.         *  
 *                                                           *  
 *************************************************************/ 	
function getFilePath(){
		filePath=document.getElementById('fileInput').value;
		openTextFile(filePath, 'forReading', false, 'default');	
		return filePath;		
	}
/************************************************************
 *                                                          *
 *          Reads text file and returns as string.          * 
 *                                                          *
 ************************************************************/
function openTextFile(path, ioMode, create, myFormat){ 
	var mode = ""
	var TriStateFalse = 0;	
	if ( filePath.substr((filePath.toLowerCase().lastIndexOf("."))+1,3) != "srt")
		{
			alert(filePath.substr((filePath.lastIndexOf("\\"))+1) +"\nis Not a srt File.");		  
		}	
	else
		{		
			if (ioMode == 'forReading') {
				 mode = 1
			}
			if (ioMode == 'forWriting'){
				 mode = 2
			}
			if (ioMode == 'forAppending'){
				 mode = 8
			} 
			
			if (myFormat == 'default') {
				 setFormat = -2
			}
			if (myFormat == 'unicode'){
				 setFormat = -1
			}
			if (myFormat == 'ascii'){
				 setFormat = 0
			}	 	
			var fso = new ActiveXObject("Scripting.FileSystemObject");
			var textStream = fso.openTextFile(path, mode, create, setFormat);
			if (ioMode == 'forReading') 
				{			
					subtitleText = textStream.readAll();
					textStream.Close();
					fso = null;					
					displaySubtitle('subtitleWindow',subtitleText, 'inText' );
					stripSubPlacementAndTimeCodes3(subtitleText);
					return subtitleText;
				}
			if (ioMode == 'forWriting') 
				{		
					if (arguments[4]=="TEMPLATE")
						{
							textStream.writeline(postionAndTime);
						}
					else if("NEW")
						{
							textStream.writeline(subtitleText);
						}		
					   		
				}
			textStream.Close();
			fso = null;
		}	
	}
/*************************************************************
 *                                                           *
 *         Displays sting in designated element.             *  
 *                                                           *  
 *************************************************************/ 
function displaySubtitle(elmId, text, textMethod) {		 
		if(textMethod=="inText")
			{
				document.getElementById(elmId).innerText=text;				
			}
		else if(textMethod=="outText")
		   {
				document.getElementById(elmId).outerText=text;	
			}
		else if(textMethod=="inHTML")
		   {
				document.getElementById(elmId).innerHTML=text;	
			}
		else if(textMethod=="outHTML")
		   {
				document.getElementById(elmId).outerHTML=text;	
			}			
	}	
/*************************************************************
 *                                                           *
 *    Removes spoken words from srt file leaving only        *  
 *    position numeber and time stamps. Then also            * 
 *    creates html for editable srt file then  updates       * 
 *    div 'timestampWindow'                                  *
 *                                                           *  
 *************************************************************/ 
function stripSubPlacementAndTimeCodes3(text){
			editableSubtitleTemplate=text.replace(/(\d{1,5}\r\n)(\d{2}.*)(.|\n)*?(\n)\s/gi, '<div>$1\n<div>$2\n<div style=\"padding:0px; margin:0px\"><textarea style=\"background-color:#E0E0E0; \" cols=\"59\" onfocus=\'this.style.cssText="background-color=#C2C2D6"\' onblur=\"updateSrtText(this); this.style.cssText=\'background-color=#E0E0E0\'\"></textarea></div></div></div>');
			postionAndTime=text.replace(/(\d{1,5}\r\n\d{2}.*)(.|\n)*?(\n)\s/gi, '$1$3');
			displaySubtitle('timestampWindow', editableSubtitleTemplate, 'inHTML')
	}	
/*************************************************************
 *                                                           *
 *     As changes are made to srt updates are reflected      *  
 *     in variable 'subtitleText'.                            *
 *                                                           * 
 *************************************************************/ 
function updateSrtText(divID){
		nodeToChange=(divID.parentElement.parentElement.parentElement.innerText.replace(/(\d{1,5})\s*(\d{2}\:.*)(.|\n)*/, '$2')).trim();		
		//nodeToChange=nodeToChange
		var sRegExInput = new RegExp(nodeToChange+'\r\n(.|\n)*?\r\n\r\n');
		subtitleText=subtitleText.replace(sRegExInput, nodeToChange+'\r\n'+ divID.value+'\r\n\r\n')				
	}
/*************************************************************
 *                                                           *
 *                                                           *  
 *                                                           *  
 *************************************************************/ 
function saveSrt(fileToSave){
	if (document.getElementById('fileInput').value=='')
		{
			alert("Please press the 'Browse' button to load a srt file.")
		}
	else
		{ 		
			var newFilePath = filePath.replace(/(.*)\.(.{3})/, '$1_'+fileToSave+'.$2');	
			openTextFile(newFilePath, 'forWriting', true, 'default', fileToSave);
		}		
	}	
