/**
 * Help util for Simple Calendar Widget
 * foramt: scwNextAction=<<function>>.runsAfterSCW(...)
 * <<function>> : self defined function, 
 *		for example: checkStartEndDay
 */

 function datePick(ele, event){
	scwNextAction=checkStartEndDay.runsAfterSCW(ele);
	scwShow(ele, event);
	return false;
}

function submitForm(form){
		if (checkStartEndDay()) form.submit(); else return false;
}

function checkAll(id) {
	var o= document.getElementById(id);
	for (var i =0; i < o.elements.length; i++){
 		ele.checked = !ele.checked;
	}
}

/**
* Atleast Select one 
*/
function selectOne(o) {
	var checkBoxArr = getSelectedCheckbox(o);
	if (checkBoxArr.length == 0) {
			 alert("Please select at least one item!");
			 return false; 
	}else return true;
}
function selectCounts(o) {
	var checkBoxArr = getSelectedCheckbox(o);
	return checkBoxArr.length; 
}
function get_radio_value(rad){
	for (var i=0; i < rad.length; i++) {
   		if (rad[i].checked){
      		var rad_val = rad[i].value;
     	}
   	}
   	return rad_val;
}

function getSelectedRadio(buttonGroup) {
   // returns the array number of the selected radio button or -1 if no button is selected
   if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            return i
         }
      }
   } else {
      if (buttonGroup.checked) { return 0; } // if the one button is checked, return zero
   }
   // if we get to this point, no radio button is selected
   return -1;
} // Ends the "getSelectedRadio" function

function getSelectedRadioValue(buttonGroup) {
   // returns the value of the selected radio button or "" if no button is selected
   var i = getSelectedRadio(buttonGroup);
   if (i == -1) {
      return "";
   } else {
      if (buttonGroup[i]) { // Make sure the button group is an array (not just one button)
         return buttonGroup[i].value;
      } else { // The button group is just the one button, and it is checked
         return buttonGroup.value;
      }
   }
} // Ends the "getSelectedRadioValue" function

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();
   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
   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

//To use one of these functions, just make a call and pass the radio button or check box object. For example, if you want to find out if at least one check box is selected and the check box field name is MyCheckBox, then write the following statements:

//var checkBoxArr = getSelectedCheckbox(document.forms[0].MyCheckBox);
//if (checkBoxArr.length == 0) { alert("No check boxes selected"); }

	function pickFolder(target, message){
		v=BrowseFolder(message);
		document.getElementById(target).value=v;
	}
	
	function BrowseFolder(message){
   		var Shell  = new ActiveXObject( "Shell.Application" );
		//var varFolder = Shell.NameSpace("C:\\"); 
   		var Folder = Shell.BrowseForFolder(0,message,0x0040,0x11);
   		if(Folder != null){
		    Folder = Folder.items(); // 
		    Folder = Folder.item();  // 
		    Folder = Folder.Path;  // 
		    //if(Folder.charAt(varFolder.length-1) != "\\"){
		    // 	Folder = varFolder + "\\";
		    //}
    		return Folder;
   		}
  	}

	function showIt(id){
		_toggleIt(id, true);
	} 
	function hideIt(id){
		_toggleIt(id, false);
	} 

	function _toggleIt(id, isDisplay){
		dis = isDisplay ? '' : 'none';
		if (document.getElementById) { // DOM3 = IE5, NS6
			document.getElementById(id).style.display = dis;
		}
		else {
			if (document.layers) { // Netscape 4
				document.id.display = dis;
			}
			else { // IE 4
				document.all.id.style.display = dis;
			}
		}
	} 

 function doSlide(id){
	timeToSlide = 15; // in milliseconds
	obj = document.getElementById(id);
	if(obj.style.display == "none"){ // si c hidden on fait le slide
		obj.style.visibility = "hidden";
		obj.style.display = "block";
		height = obj.offsetHeight;
		obj.style.height="0px";
		obj.style.visibility = "visible";
		pxPerLoop = height/timeToSlide;
		slide(obj,0,height,pxPerLoop);
	} else {
	obj.style.display = "none";
	}
}

function slide(obj,offset,full,px){
	if(offset < full){
		obj.style.height = offset+"px";
		offset=offset+px;
		setTimeout((function(){slide(obj,offset,full,px);}),1);
	} else {
		obj.style.height = "auto"; //Can be usefull in updated divs otherwise
//just use full+"px"
	}
}

	String.prototype.startsWith = function(str)
	{return (this.match("^"+str)==str)}
	

	