// The selected Items Array
var selectedItemsIndexArr = new Array();

// Number of selected row
var rowCount = 0;
	
// Last row selected
var lastRowObj = null;
var KEEP			= "KEEP";
var CLEAR			= "CLEAR";
var formInitialValues = new Array();
var updatedForm		= false;

var gView = 0;

/*
This file contains methods that are written specific for
this application.
*/


/*
Name : setTitle
Input: new title information
Desc : The method sets a new title for the frameset
*/

function setTitle(titleStr){
	if (parent.title!=null){
		parent.title.topTitle.innerText = titleStr;		
	}
}


/*
Name : submitForm
Input : action
Desc : The method submits the form.'action' is the 
       requested operation (button).         
*/
function submitForm(action){
	if (document.forms[0].LAction!=null){
		document.forms[0].LAction.value = action;		
		document.forms[0].submit();
	}
}

/*
Name : submitSimpleForm
Input : action
Desc : The method submits the form.'action' is the 
       requested operation (button).         
*/
function submitSimpleForm(action,GroupID,Updatecounter,Description){
	if (document.forms[0].LAction!=null)
		document.forms[0].LAction.value = action;
	if (document.forms[0].hdnGroupID!=null)
		document.forms[0].hdnGroupID.value = action;
	if (document.forms[0].hdnUpdatecounter!=null)
		document.forms[0].hdnUpdatecounter.value = action;
	if (document.forms[0].hdnDescription!=null)
		document.forms[0].hdnDescription.value = action;
	if ((document.forms[0].LAction!=null) && (document.forms[0].hdnGroupID!=null) && (document.forms[0].hdnUpdatecounter!=null) && (document.forms[0].hdnDescription!=null) )
		document.forms[0].submit();	
}


/*
Name : action_MouseOver_MenuButton
Input: Object element
Desc : This method replaces the style of an object that 
		contains a button. This method is called when the
		mouseOver event accurs
*/

function action_MouseOver_MenuButton(obj){
	switch(obj.className){
		case "ButtonClass"		: 
			obj.className = "ButtonClassOver"		;break
		case "SmallButtonClass" : 
			obj.className = "SmallButtonClassOver"	;break
		case "BigButtonClass"	: 
			obj.className = "BigButtonClassOver"	;break 
		case "searchButton"		: 
			obj.className = "searchButtonOver"	;break 
		case "DBlButton" : 
			obj.className = "DBlButtonOver"	;break 
	}
}

/*
Name : action_MouseClick_MenuButton
Input: Object element
Desc : This method activates the relevant validation methods
*/

function action_MouseClick_MenuButton(obj){
	switch(obj.className){
		case "ButtonClassDisabled"		: 
		case "SmallClassDisabled" : 
		case "BigClassDisabled"	:
			showMessage("אין לך הרשאות לביצוע פעולה זו");
			break; 
	}
}

/*
Name : action_MouseOut_MenuButton
Input: Object element
Desc : This method replaces the style of an object that 
		contains a button. This method is called when the
		mouseOut event accurs
*/

function action_MouseOut_MenuButton(obj){	
	switch(obj.className){
		case "ButtonClassOver"		: 
			obj.className = "ButtonClass"			;break
		case "SmallButtonClassOver" : 
			obj.className = "SmallButtonClass"		;break
		case "BigButtonClassOver"	: 
			obj.className = "BigButtonClass"		;break
		case "searchButtonOver"		: 
			obj.className = "searchButton"	;break 
		case "DBlButtonOver" : 
			obj.className = "DBlButton"	;break
	}
}

/*

Name : showMessage
Input: The message string to be presented
Desc : This method sets the message string and makes the message
		object visible
*/

function showMessage(str){
	showErr(str);
}

/*
Name : showErr
Input: The error string to be presented
Desc : This method sets the error strings and makes the error
		object visible
*/

function showErr(str){
		// Set the new error message
		document.all.msgTextObj.innerHTML		= str;
		
		// make sure that the user didn't minimize the last time
		document.all.msgMainDataObj.className	="objShow";
		
		// Show the tool bar
		document.all.errorToolBar.className		="objShow";
		
		// Make sure the minimize icon the shown
		document.all.currentErrAction.src = "../../images/errorMessages/MinimizeButton.gif";
		
		// Show the object
		document.all.msgObj.className			= "objShow";
	}

/*
Name : setErrAction
Input: mode - an indicator on which of the 3 possible buttons
		related to the error object was pressed
Desc : When opening the error object 3 buttons are available:
		minimize : shows only the error title
		maximize : shows all the error object
		close	 : closes the error object , makes it invisible
*/
	
function setErrAction(mode){
		if (mode=="close")
                {                         
                        document.all.msgObj.className = "objHide";
                }
		else if (mode=="check"){
			if (document.all.msgMainDataObj.className=="objShow"){
				document.all.msgMainDataObj.className="objHide";
				document.all.currentErrAction.src = "../../images/errorMessages/MaxmizeButton.gif";
			}else{
				document.all.msgMainDataObj.className="objShow";
				document.all.currentErrAction.src = "../../images/errorMessages/MinimizeButton.gif";
			}
		}
	}

/*
Name : setRowAction
Input: a row object
Desc : selects or unselects the row
*/	
function setRowAction(obj){
	switch(obj.className){
		case "rowUnselected" : 
			// case status is multiselection
			if (multiSelection){
				obj.className = "rowSelected";
				rowCount++;
			}else{
			// case status is single selection
				if (lastRowObj == null)
					lastRowObj = obj;
				else
					lastRowObj.className = "rowUnselected";
				obj.className = "rowSelected";
				lastRowObj = obj;
				rowCount++;
			}
			break;
		case "rowSelected" :
			if (rowCount>0){
				obj.className = "rowUnselected";
				rowCount--;
				lastRowObj = null;
			}
			break;
		
		}
}


function showHelp(pageCode){
	var pageName;
	var attr;
	var helpWindow;
	attr = "directories=no,location=no,menubar=no,status=no,titlebar=no,toolbar=no";
	attr += ",scrollbars=no,width=620,height=150,resizable=no";
	switch(pageCode){
		case "searchItemLanding" : 
		case "MRLItemsMain" :
			pageName = "../../help/" + pageCode + "Help.asp";
			helpWindow = open(pageName,"",attr);
			helpWindow.focus();
			break;
		}
}

function changeObjDisplay(searchCriteriaObj,imgObj,searchResultsObj){
	if (searchCriteriaObj.className=="objHide"){
		searchResultsObj.style.height = lastSearchResultsHeight; 
		searchCriteriaObj.className = "objShow";
		imgObj.src="../../images/errorMessages/MinimizeButton.gif"
		}
	else{
		lastSearchResultsHeight = searchResultsObj.style.height;
		searchResultsObj.style.height = parseInt(searchResultsObj.style.height) * 2;
		searchCriteriaObj.className = "objHide";
		imgObj.src="../../images/errorMessages/MaxmizeButton.gif"
		}
}

/*****************************************************************
	Name : CombineOptions
	Input: a Select object
	Desc : The method combines all the Option values into a string, 
	       separated by '-'.
*******************************************************************/			
function CombineOptions(objSelect){
	var i
	var length
	var strOptions = ""
	length = objSelect.length;
	for (i=0;i<length;i++){
		strOptions = strOptions+(objSelect.options[i].value);
		if (i<(length-1))
			strOptions = strOptions+"-"; }
	return strOptions;
}

/*
Name : showICalendar 
Input: The object that the final date value should be placed into
Desc : The method displays the iframe containing the calendar object 
		in the coordinates where the mouse was pressed.
Remark : The Iframe object ID must be - "ICalendarObj"
		 If you put the IFRAME under a FORM element it won't be
		 recognized.
*/

function showICalendar(destObj){
	calWidth = 274;
	calHeight = 200;
	
	destX = event.clientX +  document.body.scrollLeft;
	destY = event.clientY +  document.body.scrollTop;
	
	maxWidth = document.body.offsetWidth;
	maxHeight = document.body.offsetHeight;
	
	if (destX + calWidth > maxWidth && destX - calWidth > 0)
		destX-=calWidth;
		 
	if (destY + calWidth > maxHeight && destY - calHeight > 0)
		destY-=calHeight;

	with(document.all){
		ICalendarObj.style.top = destY;
		ICalendarObj.style.left = destX;
		ICalendarObj.style.display = "";
	}
	with(document.frames("ICalendarObj")){
		destObject = destObj;	
		initCalendar();
	}
	document.frames("ICalendarObj").parentRef = document.all.ICalendarObj;
}	

function hideCalendar(){
	document.all.ICalendarObj.style.display="";
}

/* 3 variables must be declared before this page can be use
rem -- PAGE_TITLE - the page title.
rem -- DETAIL_MESSAGE - the message to be shown
rem -- TARGIL_NAME - the name of the targil
rem -- If the value is CLEAR then the content is cleared
rem -- If the value is KEEP then the value is not changed
*/

function updateTitleData(page_title,targil_name,details_message){
	
	if (page_title.toUpperCase()==CLEAR)
		page_title = "&nbsp;";
		
	if (details_message.toUpperCase()==CLEAR)
		details_message = "&nbsp;";
		
	if (targil_name.toUpperCase()==CLEAR)
		targil_name = "&nbsp;";
		
	if (parent!=null && parent.title!=null){
		if (details_message!=KEEP)
			parent.title.document.all.details.innerHTML = details_message;
		
		if (targil_name!=KEEP)
			parent.title.document.all.targilName.innerHTML = targil_name;
		
		if (page_title!=KEEP)
			parent.title.document.all.screenName.innerHTML = page_title;
	}
}

function updateUserData(gorem,jobTitle){
	if (parent!=null && parent.menu!=null){
		parent.menu.document.all.goremName.innerHTML = gorem;
		parent.menu.document.all.jobTitle.innerHTML = jobTitle;
	}
}

function fillInitialValues(){
	//formInitialValues.slice(0,formInitialValues.length);
	formInitialValues=null;
	formInitialValues = new Array();
	numOfElements = document.forms[0].length;
	for (i=0;i<numOfElements;i++)
		formInitialValues[formInitialValues.length] = document.forms[0].elements[i].value;	
}

function checkUpdateDAN(){
	updatedForm		= false;	
	numOfElements	= document.forms[0].length;
	check			= false;
	i				= 0;	
	while(i<numOfElements && !check){
		if (formInitialValues[i]!=document.forms[0].elements[i].value)
			check=true;
		else
			i++;
	}
	//alert(check);
	return (check);
}
	
function checkUpdate(){
	updatedForm		= false;
	numOfEleminArray   = formInitialValues.length;
	numOfElements	= document.forms[0].length;
	check			= false;
	i				= 0;
	if (numOfEleminArray > 0){
		while(i<numOfElements && !check){
			if (formInitialValues[i] != document.forms[0].elements[i].value){		
				if (document.forms[0].elements[i].type == "text"){
					if (document.forms[0].elements[i].readOnly != true)
						check=true;
					else
						i++;
				}
				else
					check=true;	
			}
			else
				i++;
		}
	}
	return (check);
}

/***********************************************************
Name  : checkUpdateWindow           
Desc  : The method checks if there was any change in the window.
		If there was - it will ask the user if he wants to proceed
		 without saving the changes.
***********************************************************/
function checkUpdateWindow(action){ 
	if (document.forms[0]!=null) {
        if (document.forms[0].Pi_Win_State!=null) { //Page with Pi_Win_State     		
     		if(document.forms[0].Pi_Win_State.value != gView){     			
     			if (checkUpdate()){ // An update has occured.
					msg = "?בוצעו שינויים על חלון זה. האם ברצונך לצאת מהחלון ללא שמירת השינויים";
					if (confirm(msg))
						submitForm(action);
				}
				else  // No update.
					submitForm(action);      			
     		}
     		else
     			submitForm(action); 
        }
        else{ //Page without Pi_Win_State    			
     		if (checkUpdate()){ // An update has occured.
				msg = "?בוצעו שינויים על חלון זה. האם ברצונך לצאת מהחלון ללא שמירת השינויים";
				if (confirm(msg))
					submitForm(action);
			}
			else  // No update.
				submitForm(action);     			
        }
    }	
}

/* checkUpdateWindowOld */
function checkUpdateWindowOld(action){   
	if (checkUpdate()){ // An update has occured.
		msg = "?בוצעו שינויים על חלון זה. האם ברצונך לצאת מהחלון ללא שמירת השינויים";
		if (confirm(msg))
			submitForm(action);
	}
	else  // No update.
		submitForm(action);
}

	
/***********************************************************
Name  : searchItemInSelected
Input : itemID          
Desc  : 
***********************************************************/
function searchItemInSelected(itemID){
	check = false;
	i=0;
	while(!check && i<selectedItemsIndexArr.length)
		if (selectedItemsIndexArr[i]==itemID)
			check=true;
		else
			i++;
	return i;
}
/***********************************************************
Name  : removeSelItem
Input : The chosen index           
Desc  : 
***********************************************************/

function removeSelItem(itemIndex){
	if (selectedItemsIndexArr.length==1)
		selectedItemsIndexArr = new Array();
	else if (itemIndex==0)
		selectedItemsIndexArr=selectedItemsIndexArr.slice(1,selectedItemsIndexArr.length);
	else if(itemIndex == selectedItemsArr.length -1)
		selectedItemsIndexArr=selectedItemsIndexArr.slice(0,selectedItemsIndexArr.length-1);
	else
		selectedItemsIndexArr = selectedItemsIndexArr.slice(0,itemIndex).concat(selectedItemsIndexArr.slice(itemIndex+1,selectedItemsIndexArr.length));		
}

/***********************************************************
Name  : setChosenArray
Input : The chosen Parit ID.           
Desc  : 
***********************************************************/
function setChosenArray(ParitID,hdnParitObj){       
 //	if (document.forms[0]!=null) {
   	   if (hdnParitObj!=null) {	
   			 if (multiSelection){
   				tempIndex = searchItemInSelected(ParitID);
   				if (tempIndex<selectedItemsIndexArr.length)
   					removeSelItem(tempIndex);
   				else
   					selectedItemsIndexArr[selectedItemsIndexArr.length]=ParitID;
   				hdnParitObj.value = selectedItemsIndexArr.sort().toString();
   			 }else    
				hdnParitObj.value  = ParitID;
          }          
    //  }   
}	
	
	
	