// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
// SAMSON IT Custom Javascript functions for CodeCharge Studio
// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = =
var OMO_COLOR = '#EFEEF2';

var quickSelectKeystrokes;
var quickSelectObjectWithFocus;
var quickSelectLastKeyMillis;

function medewerkerSelect(e, obj)
{
	var locatieSelectBox   = document.getElementById("sel_locatie");
	var selectedLocatie = locatieSelectBox.options[locatieSelectBox.selectedIndex].value;
	var selectedLocatieStr = locatieSelectBox.options[locatieSelectBox.selectedIndex].text;
	var selectedMedewerker = obj.options[obj.selectedIndex].value;
	var selectedMedewerkerStr = obj.options[obj.selectedIndex].text;

	// is er een medewerker geselecteerd?	
	if( selectedMedewerker != 0 ) 
	{
		var vraag = "U wilt medewerker " + selectedMedewerkerStr + " toekennen aan een opdracht voor locatie:\n"+
					selectedLocatieStr + "\n\nEen medewerker mag geen eindcontrole uitvoeren als hij/zij"+
					" deze locatie eerder ook heeft geinventariseerd.\nKlik op 'OK' als "+selectedMedewerkerStr+
					" niet zowel de inventarisatie als de eindcontrole verzorgd.";
					
		if( selectedLocatie == "" )
		{
			vraag = "Er is nog geen locatie geselecteerd\nWeet u zeker dat u deze medewerker wil toekennen aan deze opdracht?";
		}
		if( !confirm(vraag) )
		{
			obj.selectedIndex = 0;
		}
	}
	return false;
}

function opdrachttypeSelect(e, obj)
{
	if (obj==null){
		obj = document.getElementById("selectOpdrachttype");
	}
	var divInspectieOpdracht = document.getElementById("div_inspectie_opdracht");
	var divInspectieLocatie  = document.getElementById("div_inspectie_locatie");
	var divInspectieRegels   = document.getElementById("div_inspectie_regels");

	// hardcoded: inspectie=1, analyse=2 (values from database)
	if( obj.options[obj.selectedIndex].value == 2 ) 
	{
		// analyse selected
		divInspectieOpdracht.style.display = 'none';
		
		divInspectieRegels.style.display = 'none';
	}
	else
	{
		// inspectie selected
		divInspectieOpdracht.style.display = 'block';
		
		divInspectieRegels.style.display = 'block';
	}
	return false;
}

function quickSelect(e, obj) {
	currentTimeMillis = new Date();
	currentTimeMillis = currentTimeMillis.valueOf();
	interval = 2000;
	if (currentTimeMillis > quickSelectLastKeyMillis + interval || obj.id != quickSelectObjectWithFocus) {
		quickSelectKeystrokes = null;
		quickSelectObjectWithFocus = obj.id;
	} 
	quickSelectLastKeyMillis = currentTimeMillis;
	if(window.event) { // IE
		keynum = e.keyCode;
	} else if(e.which) {// Netscape/Firefox/Opera
		keynum = e.which;
	}
	keystroke = String.fromCharCode(keynum).toLowerCase();
	if (quickSelectKeystrokes == null) {
		quickSelectKeystrokes = keystroke;
	} else {
		quickSelectKeystrokes += keystroke;
	}
	imax = obj.options.length;
	keystrokesLength = quickSelectKeystrokes.length;
	for (i = 0; i < imax ; i++) {
		currentText = obj.options[i].text;
		currentTextFragment = currentText.substr(0, keystrokesLength).toLowerCase();
		if (currentTextFragment == quickSelectKeystrokes) {
			obj.options[i].selected = true;
			break;
		}
	}
	return false;
}


/**
 * Opens plain window with url
 */
	function openUrlInWindow(url,windowName,width,height,parameters)	{	//
		var standardParameters = "status=0,menubar=0,resizable=1,location=0,directories=0,scrollbars=1,toolbar=0";
		if (parameters == "") { parameters = standardParameters; }
		regularWindow = window.open(url,windowName,"height=" + height + ",width=" + width + "," + parameters);
		regularWindow.focus();
		return false;
	}

function setRowstyle (oRow, bSetting) {
	// highlites the row (and its cells) using style
	if (isIE) {
		var i, imax, cChilds, oChild;
	
		cChilds = oRow.children;
		imax = cChilds.length;

		oRow.style.backgroundColor = (bSetting ? OMO_COLOR : "")
		
		for (i=0;i<imax;i++) {
			oChild = cChilds[i];
			oChild.style.backgroundColor = (bSetting ? OMO_COLOR : "")
		}
	}
}

function setCellstyle (oCell, bSetting) {
	// highlites the row (and its cells) using style
	if (isIE) {
		oCell.style.backgroundColor = (bSetting ? OMO_COLOR : "");
	}
}

function setSingleRowStyle( oRow, bSetting )
{
  var i;

  oRow.style.backgroundColor = (bSetting ? OMO_COLOR : "")
  

  for( i=0; i<oRow.cells.length; i++ )
  {
    oRow.cells[i].style.backgroundColor = (bSetting ? OMO_COLOR : "");
  }
}

function setCompoundRowstyle (oCompoundRow, bSetting) {
  var rowIndex, tableSection;

  // first do the row itself
  setSingleRowStyle( oCompoundRow, bSetting );
  
  // see if it is a compound row, i.e. if the first cell spans more than one row
  rowIndex = oCompoundRow.rowIndex;
  tableSection = oCompoundRow.parentNode;

  if( (oCompoundRow.cells[0].rowSpan > 1) &&
      (tableSection.rows.length > rowIndex) )
  {
    setSingleRowStyle( tableSection.rows[rowIndex+1], bSetting );
  }
}


function clickRow(oRow) {
  // clicks the first link in the row
  var oLink;
  
  oLink = oRow.getElementsByTagName("a");
  document.location = oLink[0].href;
}

function clickCheck(oRow) {
	// (un-)check the first checkbox in the row
	var oCheck;
	var bCont;
	bContinue = true;
	
	if (isIE) {
		if (window.event.srcElement.tagName.toLowerCase() != "input") {
			oCheck = oRow.getElementsByTagName("input");
			imax = oCheck.length;
			for (i=0;i<imax && bContinue;i++) {
				if (oCheck[i].type = "checkbox") {
					oCheck[i].checked = !oCheck[i].checked;
					bContinue = false;
				}
			}
		}
	}
}

function Mod(a, b) { return a-Math.floor(a/b)*b }
function Div(a, b) { return Math.floor(a/b) }

function normalizeTime(oTextbox) {
	sVal = oTextbox.value;
	aVal = sVal.split(/\W/);

	iHour = null;
	iMinute = 0;
	
	if (sVal.length == 4) { // eg. 2130 -> 21:30
		iHour = Div(sVal, 100);
		iMinute = Mod(sVal, 100);
	} else {
		switch (aVal.length) {
			case 1:
				iHour = aVal[0];
				break;
			case 2:
				iHour = aVal[0];
				iMinute = aVal[1];
				break;
		}
	}

	if (sVal.length > 0 && iHour != null)	{
		if (iHour > 23) iHour = 0;
		if (iMinute > 59) iMinute = 0;
	
		sHour = zeros(iHour);
		sMinute = zeros(iMinute);
		
		oTextbox.value = sHour+":"+sMinute;
	}

}

function normalizeAmount(oTextbox) {
	sVal = oTextbox.value;
	fVal = parseFloat(sVal);
	if (isNaN(fVal)) {
		oTextbox.value = "";
	} else {
		oTextbox.value = fVal.toFixed(2);
	}
}

function normalizeDate(oTextbox) {
	sVal = oTextbox.value;
	aVal = sVal.split(/\W/);
	
	iYear = 0;
	
	switch (aVal.length) {
		case 1:
			iDate = aVal[0];
			
			d = new Date();
			iMonth = d.getMonth();
			iYear = d.getFullYear();
			break;
		case 2:
			iDate = aVal[0];
			iMonth = aVal[1]-1;

			d = new Date();
			iYear = d.getFullYear();
			break;
		case 3:
			iDate = aVal[0];
			iMonth = aVal[1]-1;
			iYear = aVal[2];
			
			if (iYear.length < 3) {
				if (iYear < 70) {
					iYear = 2000 + Number (iYear);
				} else {
					iYear = 1900 + Number (iYear);
				}
			}
			
			break;
	}
	
	if (iYear != 0 && sVal.length > 0)	{
		oDate = new Date(iYear, iMonth, iDate);
		sDay = zeros(oDate.getDate());
		sMonth = zeros(oDate.getMonth()+1);
		
		if (!isNaN(oDate.getFullYear())) {
			oTextbox.value = sDay+"-"+sMonth+"-"+oDate.getFullYear();
		}
	}
}

function normalizeDateMMYYYY(oTextbox) {
	sVal = oTextbox.value;
	aVal = sVal.split(/\W/);
	
	iYear = 0;
	iDate = 1;
	
	switch (aVal.length) {
		case 1:
			iMonth = aVal[0]-1;
			
			d = new Date();
			iYear = d.getFullYear();
			break;
		case 2:
			iMonth = aVal[0]-1;
			iYear = aVal[1];
			
			if (iYear.length < 3) {
				if (iYear < 70) {
					iYear = 2000 + Number (iYear);
				} else {
					iYear = 1900 + Number (iYear);
				}
			}
			
			break;
	}
	
	if (iYear != 0 && sVal.length > 0)	{
		oDate = new Date(iYear, iMonth, iDate);
		sDay = zeros(oDate.getDate());
		sMonth = zeros(oDate.getMonth()+1);
		
		if (!isNaN(oDate.getFullYear())) {
			oTextbox.value = sMonth+"-"+oDate.getFullYear();
		}
	}
}

function normalizeDateYYYY(oTextbox) {
	sVal = oTextbox.value;
	aVal = sVal.split(/\W/);
	
	iYear = 0;
	
	switch (aVal.length) {
		case 1:
			iYear = aVal[0];
			iMonth = 1;
			iDate = 1;
			
			if (iYear.length < 3) {
				if (iYear < 70) {
					iYear = 2000 + Number (iYear);
				} else {
					iYear = 1900 + Number (iYear);
				}
			}
			break;
	}
	
	if (iYear != 0 && sVal.length > 0)	{
		oDate = new Date(iYear, iMonth, iDate);
		sDay = zeros(oDate.getDate());
		sMonth = zeros(oDate.getMonth()+1);
		
		if (!isNaN(oDate.getFullYear())) {
			oTextbox.value = oDate.getFullYear();
		}
	}
}

function zeros(sDay) {
	sDay = sDay.toString();
	if (sDay.length == 1) {
		sDay = "0"+sDay;
	}
	return sDay;
}

// Met dit script kan je een 'variabele fotolijst' window openen, welke met een click op de foto weer wordt gesloten.
//  Laad Scriptje: 
//  <SCRIPT LANGUAGE="JavaScript" SRC="fotolijst.js"></SCRIPT>
// 
// In Linkje (de x & y zijn de afmetingen van de foto in pixels)
//  <A HREF="JavaScript:openFoto('gpics/ski952.jpg', x, y)
// 
// Fotolijst.html  moet er zo uitzien:
//    <HTML>
//    <HEAD><TITLE>B'vo Fotolijst</TITLE></HEAD>
//    <BODY bgcolor="#000000">
//    
//    <script language="JavaScript">
//    
//    document.write( "<a href='JavaScript:self.close();'><img src="+location.search.substring(1, location.search.length )+" border=0></a>");
//    
//    </script>
//    
//    </body>
//    </HTML>
function openFoto( location, width, height )
{
	var win = window.open( 'fotolijst.html?'+location, '_blank', 'resizable=no,scrollbars=no,status=0,width='+(width+23)+',height='+(height+23) );
}


function selectAll(formObj, fieldName, isInverse)
// Selecteer alle opties in een multi-select (of inverteer de selectie).
// invoer van form en afzonderlijk de naam van de (multi)checkbox omdat (ccs) '[]' in de naam opneemt en 
// javascript hier niet mee uit de voeten kan...
{
   for (var i=0;i < formObj.length;i++) 
   {
      fldObj = formObj.elements[i];
      if (fldObj.type == 'checkbox' && fldObj.name == fieldName)
      { 
         if(isInverse)
            fldObj.checked = (fldObj.checked) ? false : true;
         else fldObj.checked = true; 
       }
   }
}

function selectMonsterId() {
	var s;
	s=MM_findObj("s_MONSTER_ID");
	s.focus();
}

function openWDFPopup( categorie, id )
{
	window.open( ServerURL + "webdocfactory/wdfDialog.php?wdf_categorie_id=" + categorie + "&wdf_id=" + id, null, "height=500,width=600,status=yes,toolbar=no,menubar=no,location=no" );
}

function startWDF()
{
	alert("webdocfac");
}

function getCookie(c_name)
{
	if (document.cookie.length>0)
  	{
  		c_start=document.cookie.indexOf(c_name + "=");
  		if (c_start!=-1)
    	{ 
    		c_start=c_start + c_name.length+1;
    		c_end=document.cookie.indexOf(";",c_start);
    		if (c_end==-1) c_end=document.cookie.length;
    		return unescape(document.cookie.substring(c_start,c_end));
    	} 
  	}
	return null
}

function setCookie(c_name, value, expiredays)
{
	var exdate=new Date();
	exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate);
}

/**
*	Returns format uppercase
*/
function formatUpperCase(oTextBox)
{
	oTextBox.value = oTextBox.value.toUpperCase();
}
