/**
 * The javascript utility functions
 *
 * @version 	1.0 Dec 19 2003
 * @author 	Shahid M.
 */


function differenceInDays(date1, date2) {
	try {
		var days = 0;
		if ( (date1 == null) || (date2 == null) ) {
			return days;
		}
	
		var ms = date2 - date1;
		days = ms / (1000 * 60 * 60 * 24); 

		return Math.round( days );
	} catch (error) {
		alert('Javascript error in utils.js.differenceInDays(): ' + error.description );
	}
}


// converts the arguments into javascript date - hour and minut arguments are optional
//the param dateStr has the format 12/31/2003  (month/day/year)
function toDate(dateStr, hourStr, minutStr) {
  var mm = dateStr.substring(0,2) - 1;
  var dd = dateStr.substring(3,5);
  var yy = dateStr.substring(6);

  var theDate
  if ( (hourStr == null) || (minutStr == null) ) {
    theDate = new Date(yy, mm, dd)
  } else {
    theDate = new Date(yy, mm, dd, hourStr, minutStr)
  }
  return theDate
}

function getDateDifference(dateStr1, dateStr2) {
	try {
		return toDate(dateStr1) - toDate(dateStr2);
	} catch (error) {
		alert('Error in util.js.getDateDifference: ' + error.description );
	}
}


// returns the submit string
function getSubmitString( theForm ) {
  var result = ""
  for (itemIndex=0; itemIndex < theForm.elements.length; itemIndex++ ) {
    var elem = theForm.elements[itemIndex]
    var include = true
    if ( ( elem.type == "radio" ) || ( elem.type == "checkbox") ){
      include = (elem.checked);
    }
    if (include) {
      result += elem.name + "=" + elem.value + "&"
    }
  }
  return result;
}

// returns the array number of the selected radio button or -1 if no button is selected
function getSelectedRadio(buttonGroup) {
  if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
    for (var index=0; index < buttonGroup.length; index++) {
      if (buttonGroup[ index ].checked) {
        return index
      }
    }
  } else {
    if (buttonGroup.checked) { // if the one button is checked, return zero
      return 0;
    }
  }
  // if we get to this point, no radio button is selected
  return -1;
} // Ends the "getSelectedRadio" function

// returns the value of the selected radio button or "" if no button is selected
function getSelectedRadioValue(buttonGroup) {
  if (buttonGroup == null) {
    return "";
  }
  var selectedIndex = getSelectedRadio( buttonGroup );
  if (selectedIndex == -1) {
    return "";
  } else {
    if ( buttonGroup[ selectedIndex ] ) { // Make sure the button group is an array (not just one button)
      return buttonGroup[ selectedIndex ].value;
    } else { // The button group is just the one button, and it is checked
      return buttonGroup.value;
    }
  }
}


function getSelectedCheckbox(buttonGroup) {
  // Go through all the check boxes. return an array of all the ones
  // that are selected (their position numbers). if no boxes were checked,
  // returned array will be empty (length will be zero)
  var retArr = new Array();
  if (buttonGroup != null) {
    var lastElement = 0;
    if (buttonGroup[0]) { // if the button group is an array (one check box is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
        if (buttonGroup[i].checked) {
          retArr.length = lastElement;
          retArr[lastElement] = i;
          lastElement++;
          }
      }
    } else { // There is only one check box (it's not an array)
      if (buttonGroup.checked) { // if the one check box is checked
        retArr.length = lastElement;
        retArr[lastElement] = 0; // return zero as the only array value
      }
    }
  }
  return retArr;
} // Ends the "getSelectedCheckbox" function

function getSelectedCheckboxValue(buttonGroup) {
  // return an array of values selected in the check box group. if no boxes
  // were checked, returned array will be empty (length will be zero)
  var retArr = new Array(); // set up empty array for the return values
  if (buttonGroup != null) {
    var selectedItems = getSelectedCheckbox(buttonGroup);
    if (selectedItems.length != 0) { // if there was something selected
      retArr.length = selectedItems.length;
      for (var i=0; i<selectedItems.length; i++) {
        if (buttonGroup[selectedItems[i]]) { // Make sure it's an array

          retArr[i] = buttonGroup[selectedItems[i]].value;
        } else { // It's not an array (there's just one check box and it's selected)
          retArr[i] = buttonGroup.value;// return that value
        }
      }
    }
  }
  return retArr;
} // Ends the "getSelectedCheckBoxValue" function

function NewWindow(url, width, height, scroll) {
  var winLeft = (screen.width - width) / 2;
  var winTop = (screen.height - height) / 2;
  winprops = 'height=' + height + 
  			 ',width=' + width + 
  			 ',top=' + winTop +
  			 ',left=' + winLeft + 
  			 ',scrollbars=' + scroll + 
  			 ',resizable';
  if (url.indexOf('?') > 0 ) {
  	url += '&IsBrowserWindow=Y'
  } else {
  	url += '?IsBrowserWindow=Y'
  } 	
  window_instance = window.open( url , "bandhanPopup", winprops ) //the popup window name is used in login.jsp
  window_instance.focus()
}


function validateNumber( value ) {
	return ( parseInt(value) == value);
}

function validateFloat(value) {
	return ( parseFloat(value) == value);
}

//Jsp calls this function before document.form.submit ();
function onBeforeSubmit (theDocument)
{	
	try
	{
		//navigates to all forms
		for (formIndex = 0; theDocument.forms.length > formIndex ; formIndex ++)
		{
			//trims all the fields of the current form
			trimAll (theDocument.forms[formIndex]);
		}
	} catch(error) {
		alert('Error in util.js.onBeforeSubmit() ' + error.description )
	}
}

function trimAll (theForm)
{
	try
	{	
		for (elementIndex = 0; theForm.elements.length > elementIndex; elementIndex ++)
		{
			if (theForm.elements[elementIndex].type == 'text')
			{
				theForm.elements[elementIndex].value = trim (theForm.elements[elementIndex].value);
			}
		}
	} catch(error) {
		alert('Error in util.js.trimAll() ' + error.description )
	}
}


// Remove leading + trailing spaces and carriage returns
function trim(nonTrimed)
{
	try
	{
		while ((nonTrimed.substring(0,1) == ' ') || (nonTrimed.substring(0,1) == '\n') || (nonTrimed.substring(0,1) == '\r'))
		{
			nonTrimed = nonTrimed.substring(1,nonTrimed.length);
		}

		while ((nonTrimed.substring(nonTrimed.length-1,nonTrimed.length) == ' ') || (nonTrimed.substring(nonTrimed.length-1,nonTrimed.length) == '\n') || (nonTrimed.substring(nonTrimed.length-1,nonTrimed.length) == '\r'))
		{
			nonTrimed = nonTrimed.substring(0,nonTrimed.length-1);
		}
	} catch(error) {
		alert('Error in util.js.trim() ' + error.description )
	}

	return nonTrimed;
}	  	

// selects the given value of the given list object
function selectListValue(listObject, value) {
	var desiredIndex = -1;
	for (index=0; (index < listObject.options.length) && desiredIndex==-1; index++ )  {
		if (listObject.options[index].value == value) {
			desiredIndex = index;
		}
	}
	listObject.selectedIndex = desiredIndex;
}


// used to restrict the paste for the input field 
// call from the onkeyup() and onkeydown() events of the input field 
// <input type="text" name="faxNoR" onkeyup="return stopPaste(this)" onkeydown="return stopPaste(this)" > 
function stopPaste(theField) { 
    // Check if the control key is pressed. 
    // If the Netscape way won't work (event.modifiers is undefined), 
    // try the IE way (event.ctrlKey) 
    var ctrl = typeof event.modifiers == 'undefined' ? 
                event.ctrlKey : event.modifiers & Event.CONTROL_MASK; 



    // Check if the 'V' key is pressed. 
    // If the Netscape way won't work (event.which is undefined), 
    // try the IE way (event.keyCode) 
    var v = typeof event.which == 'undefined' ? 
                event.keyCode == 86 : event.which == 86; 



    // If the control and 'V' keys are pressed at the same time 
    if ( ctrl && v ) { 
        // ... discard the keystroke and clear the text box 
        theField.value = ''; 
        return false; 
    } 
    return true; 
} 



// used to restrict the right mouse click 
// use for the input filed in the onmousedown event 
// <input type="text" name="faxNoR" onmousedown="return rightClickCheck()" > 
function rightClickCheck() { 
    var rightClick = ( typeof event.which == 'undefined' ) ? event.button == 2 : event.which == 3; 
    if (rightClick) { 
        // event.button = 0 doesn't work. 
        // the code is terminated after this line and the alert after this is not displayed. 
        // so, just display the alert to stop the right click 
        //(typeof event.which == 'undefined') ? event.button = 0 : event.which = 0; 
        
        alert('Right Click not allowed'); 
        return false; 
    }    
    return true; 
} 

