	/* $Id: editor.js,v 1.57 2009/07/16 13:43:37 sabarinathan Exp $*/
var caretPosition;

var collectionArray=new Array('getBrowserType()','getElementIdentifier','getHTMLElementCount','for i in range(0,n):','while x>0:','fireMouseOver','fireEventOnElement','if x >= 0:','imageCheckInfo','import','imageGetInfo','wait()','winGetInfo()');
var ac_Sort="yes";

var ac_MaxSelections=5;
var ac_PUTitleBGColor="#CADEFD";
var ac_PUBGColor="#F3F8FF";
var ac_PUWidth=150;
var ac_PUZIndex=1;
var ac_PUOpenSpeed=10;


var wordarray="";
var textareaObj=null;
var textareaobj=null;
var ac_TO=null;
var ac_BdSz=0;
var cursorPosition=0;
var startPosition=0;

var pos =0;
var n=0;
var str="";
var s="";
var startPos;
var endpos=0;
function AutoComplete()
{
	 if (document.all)
	 {
  		mc_WinW=document.documentElement.clientWidth;
  		if (!mc_WinW)
  		{
  			ac_BdSz=2;
  		}
 	}
	 autocompletetext=document.createTextNode("AutoComplete   [x]")
	 textdiv=document.createElement("DIV");
	 textdiv.style.position="absolute";
	 textdiv.style.width=(ac_PUWidth-ac_BdSz)+"px";
	 textdiv.style.height="18px";
	 textdiv.style.top="0px";
	 textdiv.style.left="0px";
	 textdiv.style.backgroundColor=ac_PUTitleBGColor;
	 textdiv.style.borderBottom="solid black 1px";
	 textdiv.style.color="#000000";
	 textdiv.style.fontSize="14px";
	 textdiv.style.textAlign="center";
	 textdiv.appendChild(autocompletetext);
	 textdiv.onclick = function(){ac_AutoHide();}
	 	
	 selectelement=document.createElement("Select");
	 selectelement.size="1"
	 selectelement.onchange=function(){ insertAtCursor();}
	

	 selectelement.style.position="absolute";
	 selectelement.style.visibility="hidden";
	 selectelement.style.width=(ac_PUWidth-20)+"px";
	 selectelement.style.backgroundColor=ac_PUBGColor;
	 selectelement.style.fontSize=="12px";
	 selectelement.style.top="25px";
	 selectelement.style.left="10px";

	 inputfield=document.createElement("INPUT");
	 inputfield.style.position="absolute";
	 inputfield.style.visibility="hidden";

	 selectdiv=document.createElement("DIV");
	 selectdiv.style.position="absolute";
	 selectdiv.style.overflow="hidden";
	 selectdiv.style.zIndex=ac_PUZIndex;
	 selectdiv.style.visibility="hidden";
	 selectdiv.style.width=ac_PUWidth+"px";
	 selectdiv.style.height="60px";
	 selectdiv.style.backgroundColor=ac_PUBGColor;
	 selectdiv.style.border="solid black 1px";
	 selectdiv.style.color="#000000";
	 selectdiv.style.fontSize="12px";
	 selectdiv.style.textAlign="center";
	 selectdiv.appendChild(textdiv);
	 selectdiv.appendChild(selectelement);
	// selectdiv.appendChild(textdiv1);
	 selectdiv.appendChild(inputfield);
	 
	 document.getElementsByTagName("body")[0].appendChild(selectdiv);
	 ac_PUS=selectdiv.style;
	 ac_SeS=selectelement.style;
	 //ac_HS=textdiv1.style;
}


function ac_Complete(textareaobj,wordarray)
{
	
	where = textareaobj;
	Current=where;
	if (document.selection)
	{
		where.focus();
		sel=document.selection.createRange();
		if(sel.text=="")
		{
			sel.text='µµµ';
			dummy=where.createTextRange();
			dummy.findText('µµµ');
			dummy.select();
			pos=where.value.indexOf('µµµ');
			document.selection.clear();
			where.focus();

		}
	}
	getposition();
	var startletter="";
	textAreaObj=textareaobj;
	ac_Content=textAreaObj.value.substring(0,textAreaObj.value.lastIndexOf(' ')+1);
	ac_Words=textareaobj.value.split('\n');
	if(isIE)
	 {
		var startletter = textAreaObj.value.charAt(pos-1);
		ac_This=startletter;
 	}
 	if(isGecko)
	{
	   	var startletter = textAreaObj.value.charAt(caretPosition-1);
		ac_This =startletter;
	}
	ac_AutoHide();
	wordArray=new Array();
	for (x=0;x<wordarray.length;x++)
	{
	  if (ac_This.toLowerCase()==wordarray[x].substring(0,ac_This.length).toLowerCase()&&ac_This.length)
	  {
   	 	wordArray[wordArray.length]=wordarray[x];
  	   }
 	}
	if (wordArray.length>0)
	{
		ac_SeS.visibility="visible";
		ac_PUS.visibility="visible";
		//ac_HS.visibility="hidden";
		selectelement.options.length=1
			for (x=0;x<wordArray.length;x++)
			{
				selectelement.options[x]=new Option(wordArray[x],wordArray[x],true,true);
			}
			selectelement.size=wordArray.length;
			//if (wordArray.length+1<ac_MaxSelections)
			//{
			//	selectelement.size=wordArray.length+1;
			//}
  		selectelement.selectedIndex=-1;
	 }
 	ac_PUS.height=(selectelement.offsetHeight+35)+"px";
}


document.onkeyup = KeyCheck;
 
  function KeyCheck(e)
  {
   
  
     var KeyID = (window.event) ? event.keyCode : e.keyCode;
     if(KeyID==27)
     {
 	ac_AutoHide();
     }
	    
   }   

function ac_AutoHide()
{
	 ac_PUS.visibility="hidden";
	 ac_SeS.visibility="hidden";
	 //ac_HS.visibility="hidden";
}
function ac_Auto(textareaobj,ac_val,ac_x,ac_y)
{
	
	cursorPosition = textareaobj.selectionStart;
 	var pos=textareaobj;
 	wordarray=collectionArray;
 	clearTimeout(ac_TO);
 	if (ac_Sort=="yes")
 	{
 	 wordarray=wordarray.sort(ac_SortLength);
 	}
	// ac_PUS.visibility="visible";
	 //ac_PUS.height="0px";
	 //ac_HS.visibility="visible";
	 ac_SeS.visibility="hidden";
	 ac_PUS.left=(ac_Left(textareaobj)+ac_x)+"px";
	 ac_PUS.top=(ac_Top(textareaobj)+ac_y)+"px";
	 textareaobj.words=wordarray;
	 textareaobj.onkeyup=function(){ ac_Complete(this,this.words); }
 	 textareaobj.ondblclick=function(){insertAtCursor();}
 	 textareaobj.onblur=function(){ ac_TO=setTimeout("ac_AutoHide()",200); }
 	 ac_Open();
}

function ac_Open()
{
 	ac_PUS.height=(selectdiv.offsetHeight+ac_PUOpenSpeed)+"px";
 	if (selectdiv.offsetHeight<60)
 	{
 		setTimeout("ac_Open()",200); 
 	}
 	else 
 	{
 		ac_PUS.height="65px"; 
 	}
}
function ac_Left(textareaobj)
{
 	ac_ObjLeft=textareaobj.offsetLeft;
 	while(textareaobj.offsetParent!=null)
 	{
 		ac_ObjParent=textareaobj.offsetParent;
    		ac_ObjLeft+=ac_ObjParent.offsetLeft;
 		textareaobj=ac_ObjParent;
	}
	return ac_ObjLeft;
}

function ac_Top(textareaobj)
{
	 ac_ObjTop=textareaobj.offsetTop;
	 while(textareaobj.offsetParent!=null)
	 {
  		ac_ObjParent=textareaobj.offsetParent;
  		ac_ObjTop+=ac_ObjParent.offsetTop;
  		textareaobj=ac_ObjParent;
 	 }
 	 return ac_ObjTop;
}

function ac_SortLength(ac_0,ac_1)
{
	 if (ac_0.length<ac_1.length){ return -1; }
	 if (ac_0.length>ac_1.length){ return 1; }
	 return 0;
}
function insertAtCursor()
{	
	 var setvalue= selectelement.options[selectelement.selectedIndex].text;
	 myField= document.getElementById("textarea");
	 var setvallength=setvalue.length;
	 var finalval="";
	 var letter="";
	 for(i=0;i<setvallength;i++)
	 {
 		var letter = letter+setvalue.charAt(i);
	 }
	 for(i=1;i<setvallength;i++)
	 {
		 var firstchar=setvalue.charAt(i);
		 finalval = finalval+firstchar;
		 var firstletter = setvalue.charAt(0);
		 var secondletter = setvalue.charAt(1);
		 startval = firstletter+secondletter;
	 }
 	 if(startval.match("fo"))
 	{
		 setvalue = setvalue+"\n\t"+'print i';
		 finalval=finalval+"\n\t"+'print i';
 	}
	 else if(letter.match("fireEventOnElement"))
	 {
		setvalue = setvalue+'("input", "name", "reguser", 1, "click")'; 
		finalval = finalval+'("input", "name", "reguser", 1, "click")'; 
	 }
	 else if(letter.match("imageCheckInfo"))
	 {
	 		setvalue = setvalue+'("Object ID", "property", "value")'; 
	 		finalval = finalval+'("Object ID", "property", "value")'; 
	 		
	 }
	 else if(letter.match("imageGetInfo"))
	 {
		 setvalue = setvalue+'("Object ID", "property")';
		 finalval = finalval+'("Object ID", "property")';
	 }
	 else if(letter.match("getElementIdentifier"))
	 {
		 setvalue = setvalue+'("input", "type", "text", 2, "name")';
		 finalval = finalval+'("input", "type", "text", 2, "name")';
	 }
	 else if(letter.match("getHTMLElementCount"))
	 {
		 setvalue=setvalue+'("input","type ","password")';
		 finalval = finalval+'("input","type ","password")';
	 }
	 else if(letter.match("fireMouseOver"))
	 {
		 setvalue = setvalue+'("link1",2,"id","4")';
		 finalval = finalval+'("link1",2,"id","4")';
	  }
	 else if(startval.match("if"))
	 {
		 setvalue = setvalue+"\n\t"+'result = fac(x)'+"\n"+'else:'+"\n\t"+'print x,"is invalid"';
		  finalval =finalval+"\n\t"+'result = fac(x)'+"\n"+'else:'+"\n\t"+'print x,"is invalid"';
	 }
	 else if(startval.match("im"))
	 {
		setvalue = setvalue+' <classname>';
		finalval = finalval+' <classname>';
	 }
	 else if(startval.match("wh"))
	 {
		 setvalue = setvalue+"\n\t"+'process(x)'+"\n\t"+'x -= 1';
		 finalval = finalval+"\n\t"+'process(x)'+"\n\t"+'x -= 1';
	}
	if (document.selection)
	{
		myField.focus();
		sel = document.selection.createRange();
		sel.text = finalval;
	}
	else if (myField.selectionStart || myField.selectionStart == '0')
	{
		startPosition = myField.selectionStart;
		var endPos = myField.selectionEnd;
		myField.value = myField.value.substring(0, startPosition-1)
		+ setvalue
		+ myField.value.substring(endPos, myField.value.length);
	}
	else 
	{
		myField.value += setvalue;
	}

}





function getposition() 
{  
 storeCaret(window.htmlContainer.document);	 
 if (document.selection) 
 {
 	window.htmlContainer.document.focus();
	var pos = document.selection.createRange();	
	pos.collapse();
	pos.moveStart("textedit",-1);	
	str=pos.text;
	
	var orig_len = str.length;			
	cstr = pos.text.replace(/\n/g,'');
	
	var new_len = cstr.length;
	// position should now be 
	var line=orig_len - new_len;
	//alert("Line Number is "+line);
	//document.form0.line.value=line;
  }  
  else if (window.htmlContainer.document.selectionStart || window.htmlContainer.document.selectionStart == '0')
   {
      	window.htmlContainer.document.focus();
      	var startPos = window.htmlContainer.document.selectionStart;   
      	strSelection = window.htmlContainer.document.value.substring(0,startPos)
      	var orig_len = strSelection.length;
      	
      	var new_len  = strSelection.replace(/\n/g,'').length;
      	var line=orig_len - new_len;
      	//alert("Line Number is "+line);
    	//document.form0.line.value=line;
  
  }
} 



 function findtext(textEl)
 {
 	
	str=prompt("Find what:","")
	//str = str.ignoreCase();
	if(str == null || str == "null")
	return 0;
	 
	 var txt, i, found;
	 if (str == "")
	 return false;
	
	 if (isIE)
	 {

		 txt = textEl.createTextRange();
		 for (i = 0; i <= n && (found = txt.findText(str)) != false; i++)
		 {
			 txt.moveStart("character", 1);
			 txt.moveEnd("textedit");
		 }
		 if (found)
		 {
			txt.moveStart("character", -1);
			txt.findText(str);
			txt.select();
			txt.scrollIntoView();
			n++;
		}
		else
		{
			if (n > 0)
			{
				n = 0;
				findnext(textEl);
			}
			else
			{
			alert(str + " was not found in this script.");
		
			}
		}
	 }
	 else
	 {
		highlightText = str;
	 	s = new String(textEl.value + '');
	 	higlightTxtInTextarea(textEl, highlightText,s,"findtext") ;
	 }
	 	
	 return false;
}
function higlightTxtInTextarea(textareaId, highlightText,s,find)
{
	
	var ht = new String(highlightText + '');
	if(ht.length == 0)
	{
		alert('highlightText has zero length!');
		return false;
	}
	if(find=="findtext")
	{
		startPos=0;
		endpos=0;
	}
	if(s.indexOf(ht)!=-1)
	{
	startPos = endpos+s.indexOf(ht);
	endpos = startPos+ht.length;
	s  = textareaId.value.substring(startPos+ht.length,textareaId.value.length);
	textareaId.focus();
	textareaId.setSelectionRange(startPos, startPos + ht.length);
	//highlightTxtInTextarea(textareaId,highlightText,textareaId.value);
	}
	else
	{
	startPos = 0;
	endpos=0;
	alert("The given string was not found");
	}
	return true; 




}

function findnext(textEl)
{
 var txt, i, found;
 	 if (str == "")
	 return false;

	 if (isIE)
	 {

		 txt = textEl.createTextRange();
		 for (i = 0; i <= n && (found = txt.findText(str)) != false; i++)
		 {
			 txt.moveStart("character", 1);
			 txt.moveEnd("textedit");
		 }
		 if (found)
		 {
			txt.moveStart("character", -1);
			txt.findText(str);
			txt.select();
			txt.scrollIntoView();
			n++;
		}
		else
		{
			if (n > 0)
			{
				n = 0;
				findnext(textEl);
			}
			else
			{
			alert(" The searched string was not found on this page.");
		
			}
		}
	 }
	 else
	 {
	
	highlightText = str;
	if (textEl.selectionStart || textEl.selectionStart == '0')
		{
			startPosition = textEl.selectionStart;
			endPos = textEl.selectionEnd;
			//alert(startPos+"position"+endPos);
			s = textEl.value.substring(endPos,textEl.value.length);
			
		
	
	//alert("s################"+s);
higlightTxtInTextarea(textEl, highlightText,s,"findnext") ;
}

}
return false;
}
 
function replace(textEl)
{
var replacetext = prompt("ReplaceWith","");
if(isIE)
{

var foundtext = document.selection.createRange().text;
textEl.value = textEl.value.replace(foundtext,replacetext);
}
else
{

	var startPosition = textEl.selectionStart;
	var endPos = textEl.selectionEnd;
	if(textEl.value.substring(startPosition,endPos)=="")
	{
		alert("select the text to replace");
	}
	foundtext = textEl.value.substring(startPosition,endPos);
	textEl.value = textEl.value.replace(foundtext,replacetext);



}

}


 function insertAtCaret (textEl,text) 
 {
       
       if (textEl.caretPos)
       {
         var caretPos = textEl.caretPos;
         caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ?text + ' ' : text;         
       }
       else if (textEl.selectionStart || textEl.selectionStart == '0')
       	{
       		startPosition = textEl.selectionStart;
       		var endPos = textEl.selectionEnd;
       		textEl.value = textEl.value.substring(0, startPosition)
       		+ text
       		+ textEl.value.substring(endPos, textEl.value.length);
       	}
       	else 
       	{
       		textEl.value += text;
	}
	
 }
 
 function moveCursorToLine(linenumber,script)
 {
    strSelection = script.value; 	
    strTemp=script.value;
    var linecount=1;
    var index;
    while(index != -1)
    {
    	index =strTemp.indexOf("\n");
    	if(index < 0) break;
    	linecount++;    	
    	if(linecount==linenumber)
    	{
    		//alert("matched linecount :"+linecount);
    		break;
    	}    	    	
    	strTemp=strTemp.substring(index+1);   	
    }
    strTemp=strTemp.substring(index+1);   	
    var startPos =strSelection.indexOf(strTemp);
    //startPos=startPos-(linenumber-1);
    var endPos = strTemp.indexOf("\n")+1;    
    var endPos=startPos+endPos;
    setCursorPosition(startPos,endPos,script);
   // document.form0.line.value=line;
 }
 
 function setCursorPosition(oStart,oEnd,script) 
 {
     
     if( script.setSelectionRange ) 
     {
     	script.focus();
        script.setSelectionRange(oStart,oEnd);         
     }
     else if( window.htmlContainer.document.createTextRange ) 
     {
         var range = window.htmlContainer.document.createTextRange();         
         range.collapse(true);         
         range.moveStart("character",oStart);  
         //range.moveEnd("character",oEnd);
         range.select();           
     }
     
}

function insertTab(textEl,text)
{
      var lineno = get_lineno();
       if (textEl.createTextRange && textEl.caretPos)
       {
	 var caretPos = textEl.caretPos;
	 caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ?text + ' ' : text;
       }
       else
       { 
	 caretPos=textEl.selectionStart;
	 strSelection = textEl.value.substring(0,caretPos);
	 strEnd = textEl.value.substring(caretPos);               
	 var NewText=strSelection+text+strEnd;
	 textEl.value=NewText;
       }
	moveCursorToLine(lineno,textEl);
}
function removeTab(textEl)
{

	var lineno = get_lineno();
	linevalue =textEl.value.split('\n');
	var textlineval = linevalue[lineno-1];
	var indexval = textlineval.indexOf("\t");

   	where = textEl;
	Current=where;
	str = textEl.value;
	if (document.selection)
	{
		where.focus();
		sel=document.selection.createRange();
		if(sel.text=="")
		{
			sel.text='µµµ';
			dummy=where.createTextRange();
			dummy.findText('µµµ');
			dummy.select();
			pos=where.value.indexOf('µµµ');
			document.selection.clear();
			where.focus();

		}
	}
	if (textEl.createTextRange && textEl.caretPos)
	{
		
		strSelection = textEl.value.substring(0,pos-1);
		var endvalue = textEl.value.substring(pos);
		if(indexval == -1)
		{
			//alert("please indent a line before outdent");
			textEl.value = textEl.value;
		}
		else
		{
				      
		        
			var newtextvalue =strSelection+endvalue;
			textEl.value = newtextvalue;
		}
	}
	else
	{ 	   
	      var caretPos=textEl.selectionStart;
	      strSelection = textEl.value.substring(0,caretPos-1);
	      strEnd = textEl.value.substring(caretPos); 
	      if(indexval==-1)
	      {
	      		//alert("please indent a line before outdent");
	      		textEl.value = textEl.value;
	      }
	      else
	      {
	      		var NewText=strSelection+strEnd;
              		textEl.value=NewText;
              }
	}
        
      moveCursorToLine(lineno,textEl);
}

function checkOwner()
{
	isOwner = document.getElementById("ownership").value;
	if(isOwner=="false")
	{
		alert("You are not authorized to modify this script.\nOnly user with \"Full Access\" privilege to the suite can modify the script.");
		return false;
	}
	return true;
}

function showFunctionGenerator(funcType,fromscreen)
{
	if(checkOwner())
	{
		checkRecordMode(funcType,fromscreen);
	}
	return;
   //var openerWindow=window.open("/webfunctional/jsp/WFFunctionSelectorScreen.jsp", "Function_Generator", config='height=425,width=600, toolbar=no, menubar=no, scrollbars=no, resizable=no,location=no, directories=no, status=no');  
 
}

function showTestCaseConfigurator()
{
	if(checkOwner())
	{
		openWindow('/webfunctional/jsp/WFTestCaseScreen.jsp','TestCase','660','550');  
	}
}

function showApplicationMap(scriptname,suitename,scriptpath)
{
    openWindow('/webfunctional/jsp/WFAppMapScreen.jsp?scriptname='+scriptname+'&suitename='+suitename+'&scriptpath='+scriptpath,'Application_Map_Editor','700','520');
  //window.open("/webfunctional/jsp/WFAppMapScreen.jsp?scriptname="+scriptname+"&suitename="+suitename,"Application_Map_Editor","toolbar=no, location=no, directories=no, status=yes, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width=700, height=520, left=150, top=50")
}

function showPlaySetting()
{

	openWindow('/webfunctional/jsp/WFPlaySettings.jsp?suite=AdventNetPayrollSystem','Play_Settings','600','460');

}

function showNewSuiteCreator()
{
	if(recordMode)
	{
		alert("Please stop recording and then create new suite");
		return false;
	}
	openWindow('/webfunctional/jsp/WFSuiteCreation.jsp','Suite_Creation','500','300');

}


function showDatabaseConfiguration()
{

	openWindow('/webfunctional/jsp/DatabaseConfigurationScreen.jsp','Database_Configuration','630','420');

}

function showDataConfiguration()
{
	if(checkOwner())
	{
		openWindow('/webfunctional/jsp/WFDataSelector.jsp','Data_Configuration','630','395');
	}
}

function showReport(suitename)
{

	openWindow('/webfunctional/jsp/WFTestSummary.jsp?suitename='+suitename,'View_Report','900','550');

}

function showMSI()
{

	document.location.href="/webfunctional/component/QEngineToolBar.msi";

}
