//******************************************************************************
var optionChecked=0;
var selectedOptions = new Array(); 
var selectedOptionsDesc = new Array();
var maxCheckAllowed;
var wordcount=false;
//var freezeOptFromBottom=0;

//Reorder variables and functions
//var cntrlStart = 1;
//var cntrlEnd = 6;
var cntrlObj;
var cntrlPrefix;
var cntrlSuffix;

//declaration of array
var DefaultValue = new Array();
var changedtext = new Array();
var itemid = new Array();
var itemdesc = new Array();

///declare maxwords for any screen
var maxwords=0;

//Reorders the control value
function moveCntrl(pos)
{

	if(cntrlObj != undefined)
	{
		try{
			var tmpNewPos=cntrlSuffix+pos, tmpVal;
			if(tmpNewPos == 0) tmpNewPos=cntrlEnd;
			if(tmpNewPos > cntrlEnd) tmpNewPos=cntrlStart;
			tmpVal = document.getElementById(cntrlPrefix + cntrlSuffix).value;
			document.getElementById(cntrlPrefix + cntrlSuffix).value = document.getElementById(cntrlPrefix + tmpNewPos).value;
			document.getElementById(cntrlPrefix + tmpNewPos).value = tmpVal;
			
			///repositioning the default value
			/*var tmpCntVal;
			tmpCntVal=DefaultValue[cntrlSuffix-1];
			DefaultValue[cntrlSuffix-1]=DefaultValue[tmpNewPos-1];
			DefaultValue[tmpNewPos-1]=tmpCntVal;*/
			
						
			///fill rhe array "itemid"
			itemid=document.getElementById('HiddenID').value.split(',');
			
			///repositioning the IDs
			var tmpCntVal1;
			tmpCntVal1=itemid[cntrlSuffix-1];
			itemid[cntrlSuffix-1]=itemid[tmpNewPos-1];
			itemid[tmpNewPos-1]=tmpCntVal1;
			
			document.getElementById('HiddenID').value=itemid;
			
			if (wordcount==true)
				{
					displayCountWordswithwordlimit(cntrlPrefix + cntrlSuffix,maxwords);
					displayCountWordswithwordlimit(cntrlPrefix + tmpNewPos,maxwords);
				}
										
			cntrlObj = document.getElementById(cntrlPrefix + tmpNewPos);
			cntrlSuffix = tmpNewPos;
			cntrlObj.focus();
		}
		catch(e)
		{
			window.status = e;
		}
	}
}
//Stores which control has got the focus
function setCntrlFocus(tmpObj, tmpObjPre, tmpObjSuf)
{
	cntrlObj = tmpObj;
	cntrlPrefix = tmpObjPre;
	cntrlSuffix = tmpObjSuf;
}
//******************************************************************************


function UserConfirm()
{
	return true;
}

////function to restrict not more than required no. of checkboxes checked and maintain the order of the checkboxes checked.
function CountChecked(Obj)
{
	if(Obj.checked)
	{
		if(optionChecked < maxCheckAllowed)
		{
			
			selectedOptions[selectedOptions.length]=Obj.value;
			selectedOptionsDesc[selectedOptionsDesc.length]=Obj.text;
			optionChecked = optionChecked + 1;
		}
		else
		{
			Obj.checked = false;
			alert('You can select a maximum of ' + maxCheckAllowed + ' options.');
		}
	}
	else
	{
		optionChecked = optionChecked - 1;
		var tmpArr = new Array(), tmpVal, tmpArrDesc = new Array(), tmpValDesc;

		while(selectedOptions.length)
		{
			tmpVal = selectedOptions.pop();
			tmpValDesc = selectedOptionsDesc.pop();
			if(tmpVal == Obj.value)
			{
				break;
			}
			else
			{
				tmpArr[tmpArr.length] = tmpVal;
				tmpArrDesc[tmpArrDesc.length] = tmpValDesc;
			}
		}
		tmpVal = tmpArr.length;
		while(tmpVal > 0)
		{
			selectedOptions[selectedOptions.length] = tmpArr.pop();
			selectedOptionsDesc[selectedOptionsDesc.length] = tmpArrDesc.pop();
			tmpVal--;	
		}
	}
	if (selectedOptionsDesc.length==0)
		document.getElementById('SelOptions').innerHTML=''
		
	else	
		document.getElementById('SelOptions').innerHTML='Options selected: ' + 	selectedOptionsDesc;
	//window.status = selectedOptions;
}

////function to count the words
function CountWords(Obj)
{
	var tmpArr = Obj.value.split(' ');
	if ( (Obj.value == null) || (Obj.value == '') )
	{
		document.getElementById(Obj.id + '_WC').innerHTML = '';
		return;
	}
	if (tmpArr.length == 0)
	{
		return;
	}

	var tmpWC = 0, tmpCnt;
	for(tmpCnt = 0; tmpCnt < tmpArr.length; tmpCnt++)
	{
		if( (tmpArr[tmpCnt] != null) && (tmpArr[tmpCnt] != '') )
		{
			tmpWC++;
		}
	}
	document.getElementById(Obj.id + '_WC').innerHTML = 'Word count : ' + tmpWC;
}

///stores the default value of textboxes into the array
function BodyOnLoad(name)
		{
			var tmpi;
			for(tmpi=cntrlStart;tmpi<=cntrlEnd;tmpi++)
			{
				DefaultValue[DefaultValue.length]=document.getElementById(name + tmpi).defaultValue;
			}
			//alert(DefaultValue);
		}
		

///check whether the  textbox text is changed or not and stores 0 if notchanged and 1 if changed into the array		
function CheckValChange(name)
		{
			var tmpi, strMsg='';
			for(tmpi=cntrlStart;tmpi<=cntrlEnd;tmpi++)
			{
				var str=document.getElementById(name + tmpi).value
				if(trimAll(str) != DefaultValue[tmpi - 1])
				{
					//strMsg=strMsg+'\nChanged ' + tmpi;
					changedtext[changedtext.length]=1;
				}
				else
					changedtext[changedtext.length]=0;
			}
			//alert(strMsg);
		}

///stores the value of textboxes into the array	
function gettextvalue(name)
	{
		var tmpi;
			for(tmpi=cntrlStart;tmpi<=cntrlEnd;tmpi++)
			{
				itemdesc[itemdesc.length]=trimAll(document.getElementById(name + tmpi).value).replace(/,/g,'<<&comma&>>');
			}
	}

///Move items from one listbox to another listbox without removing the items from first listbox.	
function additem(ctrlSource, ctrlTarget, maxitems)
	{
		var Source = document.getElementById(ctrlSource); 
		var Target = document.getElementById(ctrlTarget); 
		var present=new Array();
		var ispresent=false;;
		var isduplicate=false;
		var isselected=false;					
		if ((Source != null) && (Target != null)) 
			{ 
				//while ( Source.options.selectedIndex >= 0 )
				for (var i=0;i < Source.length;i++)
					{ 
						if (Source.options[i].selected==true)
							{
								if(Target.length==maxitems)
									{
										alert('Maximum ' + maxitems + ' items can be selected.')
										break;
									}
								else
									{
										for (var k=0;k<Target.length;k++)
											{
												if (Source.options[i].text==Target.options[k].text)
													{
														ispresent=true;
														isduplicate=true;
														present[present.length]=Source.options[i].text;
														break;
													} 
												else
													ispresent=false;
											}
											
										
										if (ispresent==false)
										{
										isselected=true;
										var newOption = new Option(); // Create a new instance of ListItem 
										//newOption.text = Source.options[Source.options.selectedIndex].text; 
										//newOption.value = Source.options[Source.options.selectedIndex].value; 
										newOption.text = Source.options[i].text; 
										newOption.value = Source.options[i].value; 
										Target.options[Target.length] = newOption; //Append the item in Target 
										//Source.remove(Source.options.selectedIndex); 
										}
									}
							}

					}
					
				
						
				if (isduplicate==true)
					alert(present + ' already present in the list of selected items');
						
				if (isselected==true)
					{
						//Source.options.selectedIndex=-1;
						var id=new Array();
						var desc=new Array();
						for (var j=0;j<Target.length;j++)
							{
								id[id.length]=Target.options[j].value.replace(/,/g,'<<&comma&>>');
								desc[desc.length]=Target.options[j].text.replace(/,/g,'<<&comma&>>');
								document.getElementById('itemid').value=id
								document.getElementById('itemdesc').value=desc
							}
					}
							
																		
				}
					return false;
	}
	
	///remove items from the listbox.
	function removeitem(ctrlSource)
				{
					var Source = document.getElementById(ctrlSource); 
					var isselected=false;					
					if (Source != null) 
							{ 
								while ( Source.options.selectedIndex >= 0 ) 
									{														
										isselected=true;
										//var newOption = new Option(); // Create a new instance of ListItem 
										//newOption.text = Source.options[Source.options.selectedIndex].text; 
										//newOption.value = Source.options[Source.options.selectedIndex].value; 
										//newOption.text = Source.options[i].text; 
										//newOption.value = Source.options[i].value; 
										//Target.options[Target.length] = newOption; //Append the item in Target 
										Source.remove(Source.options.selectedIndex); 
									
									}

							}
					if (isselected==true)
							{
								//Source.options.selectedIndex=-1;
								if (Source.length==0)
									{
										document.getElementById('itemid').value='';
										document.getElementById('itemdesc').value='';
										document.getElementById('isblank').value='yes';
									}
								var id=new Array();
								var desc=new Array();
								for (var j=0;j<Source.length;j++)
									{
										id[id.length]=Source.options[j].value.replace(/,/g,'<<&comma&>>');
										desc[desc.length]=Source.options[j].text.replace(/,/g,'<<&comma&>>');
										document.getElementById('itemid').value=id
										document.getElementById('itemdesc').value=desc
									}
							}
						return false;	
																		
				}	
				
				
////function to count the words and also check the word limit.
function CountWordswithwordlimit(Obj,ObjCount)
{
	var tmpArr = Obj.value.split(' ');
	if ( (Obj.value == null) || (Obj.value == '') )
	{
	//	document.getElementById(Obj.id + '_WC').innerHTML = '';
		return;
	}
	if (tmpArr.length == 0)
	{
		return;
	}

	var tmpWC = 0, tmpCnt;
	for(tmpCnt = 0; tmpCnt < tmpArr.length; tmpCnt++)
	{
		if( (tmpArr[tmpCnt] != null) && (tmpArr[tmpCnt] != '') )
		{
			tmpWC++;
		}
	}
//	document.getElementById(Obj.id + '_WC').innerHTML = 'Word count : ' + tmpWC;

	if (tmpWC > ObjCount)
	{
		var temp =LTrim(Obj.value);
		var tmpArrNew = temp.split(' ');
		var tmpWCNew = 0, tmpCntNew, tmpCount = 0, tmpStr;
		for(tmpCntNew = 0; tmpCntNew < ObjCount; tmpCntNew++)
			{
				tmpStr=tmpArrNew[tmpCntNew];
				tmpCount= tmpCount + tmpStr.length + 1;
			}

		///		Obj.value= Obj.value.substring(0,(Obj.value.length - 2));
		Obj.value= temp.substring(0,(tmpCount-1));
		alert("Maximum Word Limit: " + ObjCount);
	}

}	


////function to count the characters and also check the character limit.
function displayCountWordswithwordlimit(Obj,ObjCount)
{
	if (typeof(Obj) != 'object')
		var Obj=document.getElementById(Obj);

	if ( (Obj.value == null) || (Obj.value == '') )
	{
		//document.getElementById(Obj.id + '_WC').innerHTML = '';
		document.getElementById(Obj.id + '_WC').innerHTML ='Characters Left: ' + ObjCount;
		return;
	}

	var tmpWC = 0;

	tmpWC = Obj.value.length;

	if (tmpWC > ObjCount)
	{
		Obj.value= Obj.value.substring(0,ObjCount);
		alert("Maximum Character Limit: " + ObjCount);
		document.getElementById(Obj.id + '_WC').innerHTML = 'Characters Left : 0';
	}
	else
		document.getElementById(Obj.id + '_WC').innerHTML = 'Characters Left : ' + (ObjCount - tmpWC);
}

////function to restrict the character limit.
function characterlimit(Obj,ObjCount)
{
	if (typeof(Obj) != 'object')
		var Obj=document.getElementById(Obj);

	if ( (Obj.value == null) || (Obj.value == '') )
	{
		//document.getElementById(Obj.id + '_WC').innerHTML = '';
		//document.getElementById(Obj.id + '_WC').innerHTML ='Characters Left: ' + ObjCount;
		return;
	}

	var tmpWC = 0;

	tmpWC = Obj.value.length;

	if (tmpWC > ObjCount)
	{
		Obj.value= Obj.value.substring(0,ObjCount);
		//alert("Maximum Character Limit: " + ObjCount);
		//document.getElementById(Obj.id + '_WC').innerHTML = 'Characters Left : 0';
	}
	else
		;//document.getElementById(Obj.id + '_WC').innerHTML = 'Characters Left : ' + (ObjCount - tmpWC);
}

////function to count the words and also check the word limit.
function displayCountWordswithwordlimit_Old(Obj,ObjCount)
{
	if (typeof(Obj) != 'object')
		var Obj=document.getElementById(Obj);
		
		
	var tmpArr = Obj.value.split(' ');

	if ( (Obj.value == null) || (Obj.value == '') )
	{
		//document.getElementById(Obj.id + '_WC').innerHTML = '';
		document.getElementById(Obj.id + '_WC').innerHTML ='Words Left: ' + ObjCount;
		return;
	}
	if (tmpArr.length == 0)
	{
		return;
	}

	var tmpWC = 0, tmpCnt;
	for(tmpCnt = 0; tmpCnt < tmpArr.length; tmpCnt++)
	{
		if( (tmpArr[tmpCnt] != null) && (tmpArr[tmpCnt] != '') )
		{
			tmpWC++;
		}
	}

	if (tmpWC > ObjCount)
	{
		var temp =LTrim(Obj.value);
		var tmpArrNew = temp.split(' ');
		var tmpWCNew = 0, tmpCntNew, tmpCount = 0, tmpStr;
		for(tmpCntNew = 0; tmpCntNew < ObjCount; tmpCntNew++)
			{
				tmpStr=tmpArrNew[tmpCntNew];
				tmpCount= tmpCount + tmpStr.length + 1;
			}

		///		Obj.value= Obj.value.substring(0,(Obj.value.length - 2));
		Obj.value= temp.substring(0,(tmpCount-1));
		alert("Maximum Word Limit: " + ObjCount);
	}
	else
		document.getElementById(Obj.id + '_WC').innerHTML = 'Words Left : ' + (ObjCount - tmpWC);


}	
			
function CheckValPresent(strPrefix, objCount)
{
	var chkFound = 0;
		
	if(document.getElementById('chkSkip') != null)
	if(document.getElementById('chkSkip').checked) return true;
	
	if(objCount > 0)
	{
		for(var tmpi=1; tmpi<=objCount; tmpi++)
		{
			if(document.getElementById(strPrefix + tmpi) != null)
			{
				if(trimAll(document.getElementById(strPrefix + tmpi).value) != '')
				{
					chkFound = 1;
					break;
				}
			}			
		}
	}
	if(chkFound)
		return true;
	else
		return false;
}

function FollowUpCheckValPresent(strPrefix1,strPrefix2,strPrefix3,strPrefix4,strPrefix5,objCount)
{
	var id=new Array();
	if(objCount > 0)
	{
		for(var tmpi=1; tmpi<=objCount; tmpi++)
		{
		    for(var j=2; j<=5;j++)
				{
					if(document.getElementById(strPrefix1 + eval('strPrefix'+j) + tmpi) != null)
						{
							if(trimAll(document.getElementById(strPrefix1 + eval('strPrefix'+j) + tmpi).value) != '')
								{
									id[id.length]=tmpi;
									break;
								}
						}	
				}
		}
		document.getElementById("HiddenID").value=id;
	}
	
}

//Repoints the Onclick event of Multispell Checker to our custom function
function InitSpellChecker()
{
	var tmp = document.getElementById('PrevNextNavigation1_RapidSpellWebMultiple1');
	if(tmp != null)
		tmp.childNodes[1].onclick = SpChk;
}

//Repoints the Onclick event of Multispell Checker to our custom function, just for Match with Position Requirements Screen
function InitSpellCheckerMatchPosReq()
{
	var tmp = document.getElementById('PrevNextNavigation1_RapidSpellWebMultiple1');
	if(tmp != null)
		tmp.childNodes[1].onclick = SpChk1;
}

function InitSpellCheckerCI()
{
	var tmp = document.getElementById('PrevNextNavigation1_RapidSpellWebMultiple1');
	if(tmp != null)
		tmp.childNodes[1].onclick = SpChk2;
}

function SpChk()
{
	var tmp = document.getElementById('PrevNextNavigation1_RapidSpellWebMultiple1');
	var obj = tmp.childNodes[1];
	var tmpWidth = "35";
	var tmpTbl = document.getElementById('tblMain_1');
	if(obj.value == 'Check Spelling')
	{
		RapidSpellWebMultiple1_RunSpellCheck(true);

		tmpWidth = "85";
		obj.value = 'Done with Spell Checking';
	}
	else if(obj.value== 'Done with Spell Checking')
	{
		RapidSpellWebMultiple1_RunSpellCheck(true);
		obj.value = 'Check Spelling';
	}

	tmpTbl.rows[0].style.height=tmpWidth + 'px';
	tmpTbl.rows[1].style.height=tmpWidth + 'px';
	tmpTbl.rows[2].style.height=tmpWidth + 'px';
	tmpTbl.rows[3].style.height=tmpWidth + 'px';
	tmpTbl.rows[4].style.height=tmpWidth + 'px';
	tmpTbl.rows[5].style.height=tmpWidth + 'px';
	tmpTbl.rows[6].style.height=tmpWidth + 'px';
}

function SpChk1()
{
	var tmp = document.getElementById('PrevNextNavigation1_RapidSpellWebMultiple1');
	var obj = tmp.childNodes[1];
	var tmpWidth = "23";
	var tmpTbl = document.getElementById('tblPosReq');

	if(obj.value == 'Check Spelling')
	{
		RapidSpellWebMultiple1_RunSpellCheck(true);

		tmpWidth = "65";
		obj.value = 'Done with Spell Checking';
	}
	else if(obj.value== 'Done with Spell Checking')
	{
		RapidSpellWebMultiple1_RunSpellCheck(true);
		obj.value = 'Check Spelling';
	}

	for(var tmpi = 0; tmpi < tmpTbl.rows.length;tmpi++)
	{
		if((tmpi%5!=0)&&(tmpi%5!=1))
		{
			tmpTbl.rows[tmpi].style.height=tmpWidth + 'px';
		}
	}
}

function SpChk2()
{
	var tmp = document.getElementById('PrevNextNavigation1_RapidSpellWebMultiple1');
	var obj = tmp.childNodes[1];
	var tmpWidth = "35";
	var tmpTbl = document.getElementById('tblMain_1');

	if(obj.value == 'Check Spelling')
	{
		RapidSpellWebMultiple1_RunSpellCheck(true);

		tmpWidth = "85";
		obj.value = 'Done with Spell Checking';
	}
	else if(obj.value== 'Done with Spell Checking')
	{
		RapidSpellWebMultiple1_RunSpellCheck(true);
		obj.value = 'Check Spelling';
	}

	tmpTbl.rows[1].style.height=tmpWidth + 'px';
	tmpTbl.rows[2].style.height=tmpWidth + 'px';
	tmpTbl.rows[3].style.height=tmpWidth + 'px';
	tmpTbl.rows[4].style.height=tmpWidth + 'px';
}

//Displayed the currently selected item in Library Selection Screen in the label
function ShowSelected(objLst)
{
	for(var tmpi=0; tmpi<objLst.options.length;tmpi++)
	if(objLst.options[tmpi].selected==true){document.getElementById(objLst.id + '_FSI').innerHTML='Item Selected:<br>' + objLst.options[tmpi].text;break;}
}

//Rectifies the value within a control which is modified by Spell Checker
function RectifyCntrlValue(obj)
{
	var tmpStr=obj.value;
	var found=false;
	var tmpStr1='';
	for(var tmpCnt1=0;tmpCnt1<tmpStr.length;tmpCnt1++)
	{
		if(tmpStr.charCodeAt(tmpCnt1)==160)
			tmpStr1=tmpStr1+' ';
		else
			tmpStr1=tmpStr1+tmpStr.charAt(tmpCnt1);
	}
	obj.value=tmpStr1;
}

function CheckEnter(obj)
{
	if(window.event)
	return (window.event.keyCode==13)?false:true;
}