// File name : dhtmleditorfuncitons.js
// Usage : used for blink, type writer font and countdown functionality

  var x=1; //Global variable used to display each character index in type writer font.
  var txtvar="" // Global variable used to store the text as a whole for all words selected for type writer font.
  var j=0; //Global variable used to track the number of words for type writer font.
  var text_type; //GLobal variable to store the word selected for Type writer font.
  var arrlen=new Array(); //Global variable to store the total number of characters for type writer font.
  var IsSound=false; // Global variable checks whether users has selected sound on or off.
  var delaytimer; //GLobal variable to on/off the delay time.
  var delayInSeconds=0; // Global variable used to set the delay time to start the type writer font.
  var timerval=0; // GLoabal variable used to set the time interval for type writer font between every two characters.
  var iDiv = 0; // Global variable used to give the unique name to every division tag.
  var CountDownVal=10; // Global variable used to store the start countdown value.
  var winstatus; // Global variable used to store the value for window status like hidden/visible
  var checkInstallMsg=false  

//  function used to write every character of the passed word as argument selected for type writer font  in the form of division tag
//  Parameter : txt -> word for ttf
//  Parameter : isImage -> whether selected is Image in ttf

function writetext(txt,IsImage)
{
	try
	{
		j++;
		// if user has not selected any image in the ttf tag 
		if (IsImage==undefined)
		{
			txt=replaceSubstring(txt,">",">");
			txt=replaceSubstring(txt,"<","<");
			txt=replaceSubstring(txt,"&","&");
			div_text=txt;
			txt=replaceSubstring(txt," ","z");
			txt=replaceSubstring(txt,";","a");
			txt=replaceSubstring(txt,".","p");
			txt=replaceSubstring(txt,"?","q");
			txt=replaceSubstring(txt,"'","o");
			txt=replaceSubstring(txt,'"',"d");
			txt='x'+txt;
			// i -> counter to count the number of chars in txt variable
			for(var i=1; i<=txt.length; i++)
			{
				document.write('<span id="' + j + txt + i + '" style="visibility:hidden;">');
				document.write(div_text.charAt(i-1) + '</span>');
			}
			txtvar=txtvar+txt;
			arrlen[arrlen.length]=txt.length;
		}
		else
		{
			// for image in ttf tag
			document.write('<span id="'+ j + "g1" + '" style="visibility:hidden;">');
			document.write(txt + '</span>');
			txtvar=txtvar+"g";
			arrlen[arrlen.length]=1;
		}
	}	
	catch(e){}
}

//  function is called from HTML file for type writer font style.
//  Parameter : txt -> word for ttf
//  Parameter : isSound -> whether user has selected to run sound

function type(txt,isSound)
{
	try
	{
		if(isSound!=undefined)
			IsSound=isSound;
		if (txt.indexOf("<~~>")>-1)	
		{
			txt=replaceScriptTags(txt);
		}
		// str -> to store the complete string
		// leftstr -> to store the complete left string
		// rightstr -> to store the complete right string
		// len -> to store the complete string length
		var str="",leftstr="";rightstr="",len=0;
		text_type=replaceSubstring(txt," "," ");
		if(text_type.indexOf("<")>-1)
		{
			// loop to extract tags on left and right side of the words selected for ttf tags

			for(len=0;len<text_type.length;)
			{
				// loop to track starting of the html tag
				while(text_type.charAt(len)!='<' && len<text_type.length)
				{
					str=str+text_type.charAt(len);
					len++;
				}
				if (str!="") {writetext(str);str="";}
				if(text_type.charAt(len+1)=='/' && len<text_type.length)
				{
					// loop to track end  of the html tag
					while(text_type.charAt(len)!='>' && len<text_type.length)
					{
						rightstr=rightstr+text_type.charAt(len);
						len++;
					}
					rightstr=rightstr+text_type.charAt(len);
					document.write(rightstr);
					rightstr="";
				}
				else
				{
					while(text_type.charAt(len)!='>' && len<text_type.length)
					{
						leftstr=leftstr+text_type.charAt(len);
						len++;
					}
					leftstr=leftstr+text_type.charAt(len);
					if (leftstr.substring(0,4).toLowerCase()=="<img" || leftstr.substring(0,6).toLowerCase()=="<input" || leftstr.substring(0,5).toLowerCase()=="<text")
						writetext(leftstr,true)
					else	
						document.write(leftstr);
					leftstr="";
				}
				len++;
			}
		}
		else
		{
			// if word is selected by user without any formatting tag
			writetext(text_type);
		}
	}	
	catch(e){}
}

// function sets the delay time and is called from HTML file with seconds for delay
// Parameter : delaySeconds -> seconds for delay

function setDelayTime(delaySeconds)
{
	try
	{
		if(delaySeconds!=undefined)
			delayInSeconds=delaySeconds;
	}catch(e){}
}

// function calls at the end of HTML file to start displaying of type writer font chars.

function runtimer()
{
	try
	{
		// if user has selected sound play 
		if(IsSound)
		{
			// write tags as per browser types

			if(navigator.appName == "Microsoft Internet Explorer" && (navigator.userAgent.toLowerCase().indexOf('opera')==-1))
			{
				document.write('<BGSOUND id="TTFBGSOUND_ID">');
			}
			else
			{
				// check if any plugin is installed
				if (navigator.plugins) 
				{
		        		for (i=0; i < navigator.plugins.length; i++ )
					{
						// check if quicktime plugin is installed
					        if (navigator.plugins[i].name.indexOf("QuickTime") >= 0)
						{	
							// This is used for the firefox sound
							document.write('<embed src="../hoveradFirefoxsound.swf" width="0" height="0" autostart=false hidden="true" id="Hsound" enablejavascript="true">');	            
							break;
						}
					}
					if(i==navigator.plugins.length)
					{
						if(checkInstallMsg==false)
						{
							checkInstallMsg=true;
							var ans; // to store the user ok or cancel actions
							ans=confirm("Please Install QuickTime Plug-in to run sound");	
							if(ans==true)
							{
								window.parent.location.href="http://www.apple.com/quicktime/download/";
								return;
							}
						}
					}
        			}
				else
				{
						if(checkInstallMsg==false)
						{
							checkInstallMsg=true;								
							var ans;// to store the user ok or cancel actions
							ans=confirm("Please Install QuickTime Plug-in to run sound");	
							if(ans==true)
							{
								window.parent.location.href="http://www.apple.com/quicktime/download/";
								return;
							}
						}
				}
			}
		}
		if(delayInSeconds>0)
		{	
			var ds=(delayInSeconds*1000);
			delaytimer=setTimeout("runTTF()",ds);
		}
		else
			runTTF();
	}
	catch(e){}
}

// function is called after delayseconds are over for type writer font to start displaying the type writer font chars

function runTTF()
{
	try
	{		
		if (delaytimer!=undefined) clearTimeout(delaytimer);
		if(j!=0)
		{	
			startpos=0;
			// loop run for the complete words arrays and display each word char by char in ttf funcitonality
			for(var i=0;i<arrlen.length;i++)
			{
				text_type=txtvar.substring(startpos,arrlen[i]+startpos);
				j=i+1;
				for(m=1;m<=arrlen[i];m++)
				{
					x=m;
					// actually shows the division contains the character
					dotype();
				}
				timerval=400*x+(timerval);
				startpos=startpos+arrlen[i];
			}
		}
		j=0;
	}
	catch(e){}
}

// function is called from runTTF function for showing each character in type writer font 

function dotype()
{
	try
	{
		setTimeout("Show('"+j+text_type+x+"')",400*x+(timerval));
	}
	catch(e){}
}

// function to set the window status hidden when sound status is stop.
// parameter : soundobj2 -> id of embed tag

function stopsound_ttf(soundobj2)
{
	winstatus="hidden"
}

// function to play the sound.
// parameter : soundobj2 -> id of embed tag

function playsound_ttf(soundobj2)
{
	try
	{
		// play sound if window is visible 
		if(winstatus != "hidden")
		{ 
			if(navigator.appName == "Microsoft Internet Explorer" && (navigator.userAgent.toLowerCase().indexOf('opera')==-1))
			{
				if(!window.closed) 
				{
					document.all['TTFBGSOUND_ID'].src="hoveradFirefoxsound.swf" ;
				}
			}
			else
			{
				var thissound=document.getElementById('Hsound');
				if(!window.closed) 
				{
					thissound.Play();
				}
				else
				{
					thissound.stop();		
				}
				thissound=null;
			}
		}
	}
	catch(e){}			
}

// function to show the type writer font characters along with sound .
// parameter : layername -> embed src tag id 

function Show(layerName)
{
	try
	{
		eval('this.document.getElementById'+'("'+layerName+'")'+'.style.visibility="visible"');
		if(eval('this.document.getElementById'+'("'+layerName+'")'+'.innerHTML')!="" && eval('this.document.getElementById'+'("'+layerName+'")'+'.innerHTML')!=" " && IsSound) playsound_ttf('sound2')
		if(x<text_type.length)
		{
		  x++;
		  dotype();
		}
 	}
	catch(e){}
}

// function to replace the a substring with another substring from the string
// parameter : inputstring -> source string 
// parameter : fromString -> substring to be replaced
// parameter : inputstring -> substring to be replaced with

function replaceSubstring(inputString, fromString, toString) 
{
	var temp = inputString;
	if (toString.indexOf(fromString) == -1) 
	{
		while (temp.indexOf(fromString) != -1) 
		{
			var toTheLeft = temp.substring(0, temp.indexOf(fromString));
			var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
			temp = toTheLeft + toString + toTheRight;
		}
   }
   else 
   {
		var midStrings = new Array("~", "`", "_", "^", "#");
		var midStringLen = 1;
		var midString = "";
		while (midString == "") {
        for (var i=0; i < midStrings.length; i++) 
        {
            var tempMidString = "";
            for (var j=0; j < midStringLen; j++) 
            {
				 tempMidString += midStrings[i]; 
			}
            if (fromString.indexOf(tempMidString) == -1) 
            {
               midString = tempMidString;
               i = midStrings.length + 1;
            }
         }
	 }
	 while (temp.indexOf(fromString) != -1) 
	 {
         var toTheLeft = temp.substring(0, temp.indexOf(fromString));
         var toTheRight = temp.substring(temp.indexOf(fromString)+fromString.length, temp.length);
         temp = toTheLeft + midString + toTheRight;
     }
	 while (temp.indexOf(midString) != -1) 
	 {
         var toTheLeft = temp.substring(0, temp.indexOf(midString));
         var toTheRight = temp.substring(temp.indexOf(midString)+midString.length, temp.length);
         temp = toTheLeft + toString + toTheRight;
     }
   } 
 return temp; 
}

// function to toggle the display status in Blink functionality on/off
// parameter : sID -> division id 

function Toggle(sID)
{
	try
	{
		var obItem;
		obItem = document.getElementById(sID);
		// if the object is hidden then it will be visible and vice versa
		if(obItem.style.visibility=='hidden')
		{
			obItem.style.visibility='visible';
		}
		else
		{
			obItem.style.visibility='hidden';
		}
		setTimeout("Toggle('" + sID + "')", 1000);
	}
	catch(e){}
}

// function to replace the special type of tags present in script 
// parameter : scriptText -> argument passed in the function from HTML file. 

function replaceScriptTags(scriptText)
{
		try
		{
			// checks for existing of the tags and replace them with the appropriate functions
			if (scriptText.indexOf("<~>displaydate()<~~>")>=0)
			{
				scriptText=replaceSubstring(scriptText,"<~>displaydate()<~~>",retdisplaydate());
			}
			if (scriptText.indexOf("<~>countBlink()<~~>")>=0)
			{
				document.write(scriptText.substring(0,scriptText.indexOf("<~>countBlink()<~~>")));
				countBlink();
				document.write(scriptText.substring(scriptText.indexOf("<~>countBlink()<~~>")+19));
				scriptText="";
			}
			if (scriptText.indexOf("<~>displayaddday()<~~>")>=0)
			{
				scriptText=replaceSubstring(scriptText,"<~>displayaddday()<~~>",retdisplayaddday());
			}
			if (scriptText.indexOf("<~>displayaddday(")>-1)
			{
				startp=0
				startp=scriptText.indexOf("<~>displayaddday(")+17
				no="";
				while(scriptText.charAt(startp)!=")")
				{
					no=no+scriptText.charAt(startp)
					startp=startp+1
				}
				scriptText=replaceSubstring(scriptText,"<~>displayaddday("+no+")<~~>",retdisplayaddday(parseInt(no)))
			}
			return scriptText;
		}
		catch(e)
		{}
}

// function to remove the span tags if exists in the argument of Blink fucntion 
// parameter : txt -> argument checked for existance of span tags

function removeSpanTag(txt)
{
	if (txt.indexOf('<SPAN')>txt.indexOf('</SPAN'))
		txt=txt.substring(0,txt.indexOf('</SPAN>')-1)+txt.substring(txt.indexOf('</SPAN>')+7)
	return txt;
}

// fucntion to Blink the text called from HTML file
// parameter : sText -> text to be blinked

function Blink(sText)
{
	try
	{
		sText=replaceScriptTags(sText);
		l=0;g=0;
		// loop to check for opening and closing tags around words to be blinked
		for(i=0;i<sText.length;i++)
		{
			if(sText.charAt(i)=="<") 
				l++;
			else 
				if(sText.charAt(i)==">") g++;
		}
		if ((l-g)==1) 
			sText=sText + ">"
		else
			if ((l-g)==-1) 
				sText="<" + sText  
		sText=removeSpanTag(sText);
		if(navigator.userAgent.toLowerCase().indexOf('opera')!=-1)
		{
//			for other than motion
			if (sText.indexOf("</o:p>")!=-1 || sText.indexOf("</P>")!=-1 || sText.indexOf("</H")!=-1)
			{
				document.write("<div id='blk" + iDiv + "'>" + sText + "</div>");
			}
			else
			{
				document.write("<span id='blk" + iDiv + "'>" + sText + "</span>");				
			}
			iDiv++;
		}
		else if(navigator.appName == "Netscape")
		{
			document.write("<blink>" + sText + "</blink>");
		}
		else if(navigator.appName == "Microsoft Internet Explorer")
		{
			document.write("<span id='blk" + iDiv + "'>" + sText + "</span>");
			iDiv++;
		}
		else
		{
			document.write(sText);
		}
	}
	catch(e){}
}

// Function called at the end of Html file to start blinking of text

function runBlink()
{
	if(delayInSeconds>0)
	{	
		var ds=(delayInSeconds*1000);
		delaytimer=setTimeout("setBlink()",ds);
	}
	else
		setBlink();
}

// Function called from runBlink function to show blinking.

function setBlink()
{
	if (delaytimer!=undefined) clearTimeout(delaytimer);
	if(navigator.appName != "Netscape")
	{
		for(i=0;i<iDiv;i++)		
			Toggle("blk" + i);
	}	

} 

// Function to return current date

function retdisplaydate()
{
	try
	{
		var currdt=new Date();
		return ((currdt.getMonth()+1)+"/"+currdt.getDate()+"/"+currdt.getFullYear())
	}	
	catch(e){}
}

// Function to display current date

function displaydate()
{
	try
	{
		var currdt=new Date();
		document.write ((currdt.getMonth()+1)+"/"+currdt.getDate()+"/"+currdt.getFullYear())
	}	
	catch(e){}
}

// Function to display current date with number of days added to current date
// parameter : daysToAdd -> number of days added to current date

function displayaddday(daysToAdd)
{
	try
	{
		if(daysToAdd == undefined) 
			daysToAdd=0;
		var currDt=new Date();
		currDt.setDate(currDt.getDate()+parseInt(daysToAdd));
		document.write((currDt.getMonth()+1)+"/"+currDt.getDate()+"/"+currDt.getFullYear());
	}
	catch(e){}
}

// Function to return current date with number of days added to current date
// parameter : daysToAdd -> number of days added to current date

function retdisplayaddday(daysToAdd)
{
	try
	{
		if(daysToAdd == undefined) 
			daysToAdd=0;
		var currDt=new Date();
		currDt.setDate(currDt.getDate()+parseInt(daysToAdd));
		return((currDt.getMonth()+1)+"/"+currDt.getDate()+"/"+currDt.getFullYear());
	}
	catch(e){}
}

// function to start the countdown timer  called from countBlink function
// parameter : displaytime -> time taken before dispalying countdown timer

function CountToggle(displaytime)
{
	try
	{
		// close the window if count down comes to zero 
		if (CountDownVal==0)
		{
			closeAd();
			return;
		}
		else
			document.getElementById("countDown").innerHTML  =CountDownVal--;
		setTimeout("CountToggle()", 1000);
	}
	catch(e){}
}


// function to display the countdown timer 
// parameter : displaytime -> time taken before dispalying countdown timer


function countBlink(displaytime)
{
	CountDownVal=(displaytime+1)
	try
	{
		document.getElementById("countDown").innerHTML  =CountDownVal--;
//		document.write("<span name='countDown' id='countDown'>" + CountDownVal + "</span>");
		CountToggle(displaytime);
	}
	catch(e){}
}

function countBlink1(time,sec)
{
	setTimeout("countBlink(" + time + ")",(sec * 1000));
}

