projectNawSid	= 0;
projectSid		= 0;
projectMaster	= 0;
projectUurloon	= 0;
projectBeroep	= "";
$projectInfo	= "";
projectStart	= "";
projectEnd		= "";
function ctrlHours($obj, e) {
	if ( $obj.type == "button" && $obj.value == "Logout" ) {
		logoutHours();
		return;
	}
	if ( $obj.type != "button" && e.keyCode != 13 )					return;
	document.getElementById("hoursPassword").value = "pnawta";
	document.getElementById("hoursCode").value = "pnaw";
	if ( !mailCtrl(document.getElementById("hoursEmail").value) )	return;
	if ( document.getElementById("hoursPassword").value == "" )		{
		alert("Vul a.u.b. uw password in");
		return;
	}
	if ( document.getElementById("hoursCode").value.length != 4 )	{
		//alert("Vul a.u.b. een code van vier posities in");
		//return;
	}
	getHrs("pnaw", "login", document.getElementById("hoursEmail").value + "{-|-}" + document.getElementById("hoursPassword").value + "{-|-}" + document.getElementById("hoursCode").value);
}
function logoutHours() {
	setcookie("projectNawSid=0");
	setcookie("projectSid=0");
	setcookie("projectUurloon=0");
	setcookie("projectBeroep=");
	setcookie("projectMaster=0");
	getPage("");
}
function backRepports() {
	getPage("");
}
function createNew($tb) {
	if ( $tb != "pnaw" && projectNawSid < 1 ) {
		document.getElementById("slCh").selectedIndex = 0;
		alert("Permission denied");
		return;
	}
	getHrs($tb, "newAccount", "");
}
function getHrs($action, $info, $login) {
	if ( !xmlhttp ) $is = -1; else $is = 1;
	$url = "z-php/hours.php?url=" + document.getElementById("url").value + "&site=" + document.getElementById("site").value + "&page=" + document.getElementById("page").value + "&action=" + $action + "&info=" + $info + "&login=" + $login + "&xmlhttp=" + $is;
	if ( !xmlhttp ) {
		frames['useFrame'].location = $url;
	} else {
		xmlhttp.open("GET", $url, true);
		xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState==4) {
				hoursReturn($action, xmlhttp.responseText);
			}
		}
		xmlhttp.send(null);
	}
}
$fields = "";
$passes = ";code;password;";
function hoursReturn($action, $code) {
	getcookies();
	if ( projectNawSid > 0 || projectMaster > 0 ) {
		if ( document.getElementById("mkN") != null ) {
			document.getElementById("mkN").innerHTML = "<a href='javascript:getRapportages(\"declarations\", \"\")'>Rapportages</a> | <a href='javascript:getRapportages(\"faktureren\", \"\")'>Faktureren</a>";
		}
	}
	$fields = "";
	if ( !xmlhttp ) $code = frames['useFrame'].document.body.innerHTML;
	//alert($code);
	$htm = "";
	$html = "<table border='1' cellpadding='0' style='width: 100%'><tr><td>";
	$html += "<table border='1' cellpadding='0'>";
	$vc = $code.split(":");
	//alert($vc[2]);
	if ( $vc[3].substr(0,4) == "say;" ) {
		alert($vc[3].substr(4));
	} else if ( $vc[3].substr(0,9) == "projects;" ) {
		document.getElementById("loginButton").value = "Logout";
		$html = "<select id='slCh' onchange='projectOptions(this)'><option>Kies</option><option value='account'>Account</option><option value='newProject'>Nieuw project</option>";
		$v = $vc[3].substr(9).split(",");
		for ( $ic in $v ) {
			$m = $v[$ic].split(";");
			$html += "<option value='" + $m[0] + "'>" + $m[1] + "</option>";
		}
		$html += "</select>";
		document.getElementById("projectSelect").innerHTML = $html;
	} else if ( $code.substr(0,3) == "db:" ) {
		if ( document.getElementById("loginButton") != null ) document.getElementById("loginButton").value = "Logout";
		$cc = $vc[3].split("{-|-}");
		$code = $cc[0].split("+");
		for ( $ic in $code ) {
			$v = $code[$ic].split(";");
			$fields += ";" + $v[0];
			if ( $vc[1].substr(0, 8) == "pproject" && $vc[2] > 0 && projectMaster < 1 ) {
				if ( $passes.indexOf(";" + $v[0] + ";") > -1 ) continue;
				$html += "<tr><td style='width: 1%' noWrap>" + $v[0] + ":</td><td>" + $v[2] + "</td></tr>";
				continue;
			}
			/*
			if ( $v[2] == "" ) {
				if ( $v[0].indexOf("mail") > -1 ) $v[2] = "eddysite@gmail.com"; else $v[2] = $vc[1].substr(0, 6);
			}
			*/
			if ( $v[1].indexOf("enum") > -1 ) {
				$kz = $v[1].replace("enum(", "");
				$kz = $kz.replace(")", "");
				$kz = $kz.split(",");
				$html += "<tr><td style='width: 1%' noWrap>" + $v[0] + ":</td><td><select name='hrs_" + $v[0] + "' id='hrs_" + $v[0] + "'>";
				for ( $ic in $kz ) {
					if ( $kz[$ic] == $v[2] ) $cl = " selected"; else $cl = "";
					$html += "<option value='" + $kz[$ic] + "'" + $cl + ">" + $kz[$ic] + "</option>";
				}
				$html += "</select></td></tr>";
			} else if ( $v[0] == "datum" ) {
				$html += "<tr><td>" + $v[0] + ":</td><td><img id='datum_" + $v[0] + "' onload='meta_calender(this)' src='z-img/dummy.gif' width='1' height='1' alt=''></td></tr>";
				$html += "<input type='hidden' name='hrs_" + $v[0] + "' id='hrs_" + $v[0] + "' value='" + $v[2] + "'>";
			} else if ( $v[1].indexOf("date") > -1 ) {
				//$d = new Date();
				//$html += "<tr><td>" + $v[0] + ":</td><td><input type='text' name='hrs_" + $v[0] + "' id='hrs_" + $v[0] + "' value='" + $d.getYear() + "-" + twee($d.getMonth()) + "-" + twee($d.getDate()) + "'> (jjjj-mm-dd)</td></tr>";
				$html += "<tr><td>" + $v[0] + ":</td><td><img id='datum_" + $v[0] + "' onload='meta_calender(this)' src='z-img/dummy.gif' width='1' height='1' alt=''></td></tr>";
				$html += "<input type='hidden' name='hrs_" + $v[0] + "' id='hrs_" + $v[0] + "' value='" + $v[2] + "'>";
			} else if ( $v[1].indexOf("blob") > -1 ) {
				$htm += "<tr><td colspan='2'>" + $v[0] + "<br><textarea cols='40' rows='5' name='hrs_" + $v[0] + "' id='hrs_" + $v[0] + "'>" + $v[2] + "</textarea></td></tr>";
			} else {
				if ( $v[0] == "achternaam" ) document.getElementById("loginNaam").innerHTML = $v[2];
				if ( $passes.indexOf(";" + $v[0] + ";") > -1 ) $type = "password"; else $type = "text";
				if ( $v[0] == "code" ) $cl = " maxlength='4'"; else $cl = "";
				if ( $vc[1].substr(0, 13) == "pdeclarations" && ($vc[2] == "" || $vc[2] == 0) && $v[0] == "aantal" ) $v[2] = 8;
				if ( $vc[1].substr(0, 13) == "pdeclarations" && ($vc[2] == "" || $vc[2] == 0) && $v[0] == "prijs" ) $v[2] = projectUurloon;
				if ( $vc[1].substr(0, 13) == "pdeclarations" && ($vc[2] == "" || $vc[2] == 0) && $v[0] == "omschrijving" ) $v[2] = projectBeroep;
				$html += "<tr><td>" + $v[0] + ":</td><td><input" + $cl + " type='" + $type + "' name='hrs_" + $v[0] + "' id='hrs_" + $v[0] + "' value='" + $v[2] + "'></td></tr>";
				if ( $type == "password" ) {
					$html += "<tr><td>nogmaals:</td><td><input type='" + $type + "' id='nog_" + $v[0] + "' value='" + $v[2] + "'></td></tr>";
				}
			}
		}
		$html += $htm;
		if ( (!($vc[1].substr(0, 8) == "pproject" && projectMaster < 1)) || $vc[2] == 0 ) {
			$html += "<tr><td colspan='2' style='text-align: right'><input type='button' value='Opslaan' onclick='saveTb(\"saveHoursRecord\")'></td></tr>";
		}
		$html += "</table></td><td style='width: 100%; text-align: right'>";
		$src = document.getElementById("url").value + "/" + $vc[1] + "/" + $vc[2] + ".jpg";
		$html += "<img width='100' height='125' style='border: 1px #000000 solid' src='" + $src + "' alt='' border='0' onclick='upload(this, -100, 0, \"" + $vc[1] + "\", \"" + $vc[2] + "\")'>";
		$html += "</td></tr></table>";
		$html += "<input type='hidden' name='hoursUseTable' value='" + $vc[1] + "'>";
		$html += "<input type='hidden' name='hoursSid' value='" + $vc[2] + "'>";
		if ( $vc[1].substr(0, 13) == "pdeclarations" ) {
			document.getElementById("projectInfo").innerHTML = $html;
		} else {
			document.getElementById("accountInfo").innerHTML = $html;
		}
		$fields = $fields.substr(1);
		if ( typeof $cc[1] == "string" ) {
			$html = "<select id='slCh' onchange='projectOptions(this)'><option>Kies</option><option value='account'>Account</option><option value='newProject'>Nieuw project</option>";
			$v = $cc[1].split(",");
			for ( $ic in $v ) {
				$m = $v[$ic].split(";");
				$html += "<option value='" + $m[0] + "'>" + $m[1] + ": " + $m[2] + "</option>";
			}
			$html += "</select>";
			document.getElementById("projectSelect").innerHTML = $html;
		}
		if ( $vc[1].substr(0, 8) == "pproject" && $vc[2] > 0 && projectMaster < 1 ) {
			getHrs("pdeclarations", 0, "");
		}
	}
	document.getElementById("hoursPassword").value = "";
	document.getElementById("hoursCode").value = "";
}
$nverplicht = ";bedrijf;gsm;opmerking;";
function saveTb($act) {
	if ( $fields == "" ) return;
	$not = "'~`#$%^&*=+|\\}{[]:;?<>/" + String.fromCharCode(34);
	$flds = $fields.split(";");
	for ( $ic in $flds ) {
		$c = document.getElementById("hrs_" + $flds[$ic]).value;
		for ( $i = 0; $i < $not.length; $i ++ ) {
			if ( $c.indexOf($not.substr($i, 1)) > -1 ) {
				alert("Het teken " + $not.substr($i, 1) + " mag niet gebruikt worden!");
				return;
			}
		}
		if ( $nverplicht.indexOf(";" + $flds[$ic] + ";") > -1 ) continue;
		if ( $passes.indexOf(";" + $flds[$ic] + ";") > -1 ) {
			if ( document.getElementById("hrs_" + $flds[$ic]).value != document.getElementById("nog_" + $flds[$ic]).value ) {
				alert($flds[$ic] + " komt niet overeen met nogmaals");
				return;
			}
		} else if ( $flds[$ic].indexOf("mail") > -1 ) {
			if ( !mailCtrl(document.getElementById("hrs_" + $flds[$ic]).value) ) return;
		} else if ( document.getElementById("hrs_" + $flds[$ic]).value == "" || document.getElementById("hrs_" + $flds[$ic]).value == "0000-00-00") {
			alert("Veld " + $flds[$ic] + " is een verplicht invoerveld");
			return;
		}
	}
	submitUseFrame($act);
}
function saveInfTb($act) {
	submitUseFrame($act);
}
function projectOptions($obj) {
	if ( $obj.value == "" ) {
		document.getElementById("accountInfo").innerHTML = "Mutatie";
		document.getElementById("projectInfo").innerHTML = $projectInfo;
		return;
	}
	if ( $obj.value == "newProject" ) createNew("pproject");
	else if ( $obj.value == "account" ) {
		getHrs("pnaw", projectNawSid, "");
		document.getElementById("projectInfo").innerHTML = $projectInfo;
	} else {
		setcookie("projectSid=" + $obj.value);
		getHrs("pproject", $obj.value, "");
	}
}
function setStatus($obj, $sid) {
	if ( $obj.value == "gefaktureerd" ) {
		alert("Dit is een systeemstatus en mag niet handmatig worden ingesteld");
		$obj.selectedIndex = 0;
		return;
	}
	if ( $obj.value == "verwijderen" ) {
		if ( !confirm("Deze declaratie verwijderen?") ) {
			$obj.selectedIndex = 0;
			return;
		}
	}
	getRapportages("setStatus", $sid + "-" + $obj.value);
	if ( $obj.value == "verwijderen" ) {
		while ( $obj.tagName != "TR" ) {
			$obj = $obj.parentNode;
			if ( $obj.tagName == "BODY" ) break;
		}
		if ( $obj.tagName == "TR" ) $obj.style.visibility = "hidden";
	}
}
// RAPPORTAGES
function getRapportages($action, $info) {
	if ( $action == "fakuteren" ) {
		if ( !confirm("Digitale fakturering verwerken?") ) return;
	}
	if ( !xmlhttp ) $is = -1; else $is = 1;
	$url = "z-php/rapportage.php?url=" + document.getElementById("url").value + "&site=" + document.getElementById("site").value + "&page=" + document.getElementById("page").value + "&action=" + $action + "&info=" + $info + "&xmlhttp=" + $is;
	if ( !xmlhttp ) {
		frames['useFrame'].location = $url;
	} else {
		xmlhttp.open("GET", $url, true);
		xmlhttp.onreadystatechange=function() {
			if (xmlhttp.readyState==4) {
				rapportReturn($action, xmlhttp.responseText);
			}
		}
		xmlhttp.send(null);
	}
}
function rapportReturn($action, $code) {
	if ( !xmlhttp ) $code = frames['useFrame'].document.body.innerHTML;
	if ( $action == "getNaw" || $action == "getProject" || $action == "getDeclarations" || $action == "getOpmerking" ) {
		getcookies();
		document.getElementById("dbMutateTd").innerHTML = xmlhttp.responseText;
		document.getElementById("dbMutateDiv").style.top = 20 + document.body.scrollTop;
	} else if ( $action.substr(0,4) == "say:" ) {
		alert($action.substr(4));
	} else if ( $action == "setStatus" ) {
		$a = 1;
	} else {
		document.getElementById("contentDiv").innerHTML = $code;
	}
}
function getHoursSelect($obj, $value) {
	if ( $value == "go" ) getPage("");
	else if ( $obj.type == "checkbox" ) {
		if ( $obj.checked ) {
			setcookie($value + "=1");
		} else {
			setcookie($value + "=0");
		}
	} else setcookie($value + "=" + $obj.value);
}
