/* Ein neues XMLHttpRequest Objekt erstellen, um mit dem Webserver zu sprechen */
var xmlHttp = false;
/*@cc_on @*/
/*@if (@_jscript_version >= 5)
try {
	xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
	try {
		xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
	} catch (e2) {
		xmlHttp = false;
	}
}
@end @*/

var isNav4, isIE4, isMac, isNav6;
if (parseInt(navigator.appVersion.charAt(0)) >= 4) {
	isStd = (navigator.appName == "Netscape") ? true : false
	isIE  = (navigator.appName.indexOf("Microsoft") != -1) ? true : false
	isMac = (navigator.platform.indexOf("Mac") != -1) ? true : false
	isPNG = (isIE && ! isMac) ? false : true
}

if (!xmlHttp && typeof XMLHttpRequest != 'undefined') {
	xmlHttp = new XMLHttpRequest();
}

// Add eventhandler
function addEvent(obj, evType, fn, useCapture){
	if(obj) {
		if (obj.addEventListener){
			obj.addEventListener(evType, fn, useCapture);
			return true;
		} else if (obj.attachEvent){
			var r = obj.attachEvent("on"+evType, fn);
			return r;
		} else {
			alert("Handler could not be attached");
		}
	}
}

// Remove eventhandler
function removeEvent(obj, evType, fn, useCapture){
	if (obj.removeEventListener){
		obj.removeEventListener(evType, fn, useCapture);
		return true;
	} else if (obj.detachEvent){
		var r = obj.detachEvent("on"+evType, fn);
		return r;
	} else {
		alert("Handler could not be removed");
	}
}


var fJobs = 'jobs';
var fArchive = 'archive';
var fStep2 = 'second';
var fBrandSelect = 'brand-select';
var fBrandSelector = 'brand-selector';
var fBrandSwitch = 'brand-switcher';
var fSubbrand = 'subbrand';
var fAttribute = 'attribute';
var fColors = 'colors';
var fNext = 'next-step';
var fColorBox = 'colorbox';
var fFilter = 'filter';
var fBasket = 'jp-basket';
var fManagers = 'managers';

var eFilterReset = 'filterreset';

function displayMediaForCampaign(id) {
	var url = "/ajax/mediaForCampaign.php?id=" + escape(id)

	if(Element.visible(fJobs)) {
		Element.hide(fJobs);
		Element.hide(fArchive);
	} else {
		Element.show(fJobs);
		Element.show(fArchive);
	}
	
	xmlHttp.open("GET", url, true);

	xmlHttp.onreadystatechange = returnMediasForCampaign;
	
	xmlHttp.send(null);
}

function returnMediasForCampaign() {
	if (xmlHttp.readyState == 4) {
		if (xmlHttp.status == 200) {
			var response = xmlHttp.responseText;
			document.getElementById(fStep2).innerHTML = response;
			Effect.toggle(fStep2);
		}
	}	
}

function switchForeign(val, sel) {
	var url = "/ajax/switchForeign.php?sf_jp=" + escape(val) +"&sel=" +escape(sel);
	
	xmlHttp.open("GET", url, true);

	xmlHttp.onreadystatechange = returnSwitchedForeign;
	
	xmlHttp.send(null);
}

function returnSwitchedForeign() {
	if (xmlHttp.readyState == 4) {
		if (xmlHttp.status == 200) {
			var response = xmlHttp.responseText;
			var parts = response.split(":|_|:");
			$(fBrandSelect).innerHTML = parts[1];
			new Effect.Highlight(fBrandSelect);
			if($(fBrandSelector).selectedIndex==0) {
				Element.hide(fSubbrand);
				Element.hide(fAttribute);
				Element.hide(fBasket);
				Element.hide(fManagers);
			}
			$(fBrandSwitch).innerHTML = parts[0];
		}
	}	
}

function updateColorsAndSubbrand(obj) {
	if(obj.options[obj.selectedIndex].value=='') {
		Element.hide(fSubbrand);
		Element.hide(fColors);
		Element.hide(fBasket);
		return;	
	}
	var url = "/ajax/updateSubbrandAndColors.php?id=" + escape(obj.options[obj.selectedIndex].value);
	
	xmlHttp.open("GET", url, true);

	xmlHttp.onreadystatechange = returnUpdatedSubbrandAndColors;
	
	xmlHttp.send(null);	
}

function returnUpdatedSubbrandAndColors() {
	if (xmlHttp.readyState == 4) {
		if (xmlHttp.status == 200) {
			Element.show(fSubbrand);
			Element.show(fColors);
			Element.show(fBasket);
			var response = xmlHttp.responseText;
			var parts = response.split(":|_|:");
			document.getElementById(fSubbrand).innerHTML = parts[0];
			document.getElementById(fColors).innerHTML = parts[1];
			if(parts[2]!="") {
				updateColorBox(parts[2]);	
			}
		}
	}	
}

function updateSubbrand(obj) {
	if(obj.options[obj.selectedIndex].value=='') {
		Element.hide(fSubbrand);
		Element.hide(fAttribute);
		//Element.hide(fBasket);
		Element.hide(fColors);
		return;	
	}
	var url = "/ajax/updateSubbrand.php?id=" + escape(obj.options[obj.selectedIndex].value);
	
	xmlHttp.open("GET", url, true);

	xmlHttp.onreadystatechange = returnUpdatedSubbrand;
	
	xmlHttp.send(null);	
}

function returnUpdatedSubbrand() {
	if (xmlHttp.readyState == 4) {
		if (xmlHttp.status == 200) {
			Element.show(fSubbrand);
			Element.show(fAttribute);
			//Element.show(fBasket);
			var response = xmlHttp.responseText;
			var parts = response.split(":|_|:");
			$(fSubbrand).innerHTML = parts[0];
			if(parts[1]!="") {
				document.getElementById(fAttribute).innerHTML = parts[1];
			}
			if(parts[2]!="") {
				var myBasket = document.getElementById(fBasket);
				myBasket.innerHTML = parts[2];
				Element.show(fBasket);
			}
			//if(parts[3]!="") {
			//	document.getElementById(fNext).innerHTML = parts[3];
			//}
			if(parts[4]!="" && parts[5]!="") {
				$(fManagers).innerHTML = parts[4]+parts[5];
				for (var i=6; i<parts.length; i++) {
					$(fManagers).innerHTML += parts[i];
				}
				$(fManagers).style.marginTop = "5px";
				Element.show(fManagers);
			}
			if(parts[2]!="") {
				Element.show(fNext);
			} else {
				Element.hide(fNext);
			}
/*			document.getElementById('subbrand-selector').disabled = document.getElementById('brand-selector').disabled;*/
		}
	}	
}

function updateColorBox(id, box, inc) {
	var url = "/ajax/updateColorBox.php?id=" + escape(id) + "&box="+ escape(box) + "&inc=" + escape(inc);
	
	xmlHttp.open("GET", url, true);

	xmlHttp.onreadystatechange = returnColorBox;
	
	xmlHttp.send(null);
}

function returnColorBox() {
	if (xmlHttp.readyState == 4) {
		if (xmlHttp.status == 200) {
			//Element.show(fColors)
			var response = xmlHttp.responseText;
			var parts = response.split(":|_|:");
			document.getElementById(parts[1]).innerHTML = parts[0];
		}
	}	
}

function toggle_brand(id, active, defval) {
	jobwrapper= $("jobs_"+id);
	jobtriangle = $("triangle_brand_"+id);
	if (navigator.userAgent.indexOf("Safari") > 0) {
		isIE = false;
	} else if (navigator.product == "Gecko") {
		isIE = false;
	} else {
		isIE = true;
	}
	if(Element.visible(jobwrapper)) {
		if (checkChangesBrand(id)) {
			unsetChangesBrand(id);
			Element.hide(jobwrapper);
			jobtriangle.src = '/img/jobpouch/triangle.c.grey.jpg';
			if(isIE==true) {
				th = document.getElementsByTagName("th");
				for(i=0;i<th.length;i++) {
					cn = "th_"+id;
					cv = th[i].className;
					m = cv.indexOf(cn);
					if(m!=-1) {
						th[i].style.visibility = "hidden";
					}
				}
			} else {
				th = document.getElementsByName('th_'+id);
				for(i=0;i<th.length;i++) {
					th[i].style.visibility = "hidden";
				}
			}
		}
		return;	
	} else {
		jobtriangle.src = '/img/jobpouch/triangle.o.grey.jpg';
		var url = "/ajax/getJobsForBrand.php?id=" + escape(id)+"&active="+escape(active)+"&default="+escape(defval);
		xmlHttp.open("GET", url, true);

		xmlHttp.onreadystatechange = returnJobsForBrand;
	
		xmlHttp.send(null);
	}
}

function returnJobsForBrand() {
	if (navigator.userAgent.indexOf("Safari") > 0) {
		isIE = false;
	} else if (navigator.product == "Gecko") {
		isIE = false;
	} else {
		isIE = true;
	}
	if (xmlHttp.readyState == 4) {
		if (xmlHttp.status == 200) {
			var response = xmlHttp.responseText;
			var parts = response.split(":|_|:");
			if($("jobs_"+parts[0])==null) {
				alert(response);
				return;	
			}
			$("jobs_"+parts[0]).innerHTML = parts[1];
			Element.show($("jobs_"+parts[0]));
			if(isIE==true) {
				th = document.getElementsByTagName("th");
				for(i=0;i<th.length;i++) {
					cn = "th_"+parts[0];
					cv = th[i].className;
					m = cv.indexOf(cn);
					if(m!=-1) {
						th[i].style.visibility = "visible";
					}
				}
			} else {
				th = document.getElementsByName('th_'+parts[0]);
				for(i=0;i<th.length;i++) {
					th[i].style.visibility = "visible";
				}
			}
		}
	}	
}

function toggle_childs(id) {
	if (navigator.userAgent.indexOf("Safari") > 0) {
		isIE = false;
	} else if (navigator.product == "Gecko") {
		isIE = false;
	} else {
		isIE = true;
	}
	if(isIE==true) {
		child = document.getElementsByTagName("tr");
		row = $("row_job_"+id);
		for(i=0;i<child.length;i++) {
			if(child[i].className=="child_"+id) {
				if(Element.visible(child[i])==false) {
					Element.show(child[i]);
					child[i].style.background = "#eeeeee";
					$("img_"+id).src = '/img/jobpouch/triangle.o.red.jpg';
					row.style.background = "#dddddd";
				} else if (checkChangesRow(row)) {
					unsetChangesRow(row);
					Element.hide(child[i]);
					$("img_"+id).src = '/img/jobpouch/triangle.c.grey.jpg';
					child[i].style.background = "#cccccc";
					row.style.background = "#cccccc";
				}
			}
		}
	} else {
		child = document.getElementsByName('child_'+id);
		row = $("row_job_"+id);
		for(i=0;i<child.length;i++) {
			if(Element.visible(child[i])==false) {
				Element.show(child[i]);
				child[i].style.background = "#eeeeee";
				$("img_"+id).src = '/img/jobpouch/triangle.o.red.jpg';
				row.style.background = "#dddddd";
			} else if (checkChangesRow(row)) {
				unsetChangesRow(row);
				Element.hide(child[i]);
				child[i].style.background = "#cccccc";
				row.style.background = "#cccccc";
				$("img_"+id).src = '/img/jobpouch/triangle.c.grey.jpg';
			}
		}
	}
}

function toggle_history(id) {
	h = $("history_"+id);
	r = $("history_toprow_"+id);
	if(Element.visible(h)==false) {
		Element.show(h);
		$("history_img_"+id).src = '/img/jobpouch/triangle.o.red.jpg';
		r.style.background = "#dddddd";
		h.style.background = "#dddddd";
	} else {
		Element.hide(h);
		$("history_img_"+id).src = '/img/jobpouch/triangle.c.grey.jpg';
		r.style.background = "#cccccc";
		h.style.background = "#cccccc";
	}
}

function filterAssettype(obj) {
	sel = document.getElementsByName('aa[]');
	for(i=0;i<sel.length;i++) {
		selMatch = false;	
		opt = sel[i].options;
		var firstHit = false;
		for(x=0;x<opt.length;x++) {
			if(opt[x].value!='') {
				if (x==0) {
					opt[x].style.backgroundColor = '#fff';
					opt[x].style.color = '#000';
					opt[x].className = "optMatch";
					val = obj.value.toLowerCase();
					txt = opt[x].text.toLowerCase();
					hit = txt.indexOf(val);
					firstHit = (hit!=-1);
				} else {
					val = obj.value.toLowerCase();
					txt = opt[x].text.toLowerCase();
					hit = txt.indexOf(val);
					if(firstHit || hit!=-1) {
						selMatch = true;	
					opt[x].style.backgroundColor = '#fff';
					opt[x].style.color = '#000';
							opt[x].className = "optMatch";
/*						if(val!="") {
							opt[x].className = "optMatch";
						} else {
							opt[x].className = "optMatch"; //sonst aktualisiert der ie nicht die hintergrundfarbe
//							opt[x].className = "";
						}*/
					} else {
					opt[x].style.backgroundColor = '#ccc';
					opt[x].style.color = '#808080';
						opt[x].className = "optNomatch";
					}
				}
			}
		}
		if(selMatch==true) {
			sel[i].disabled = false;
			sel[i].className = "selVisible";
		} else {
			sel[i].disabled = true;
			sel[i].className = "selHidden";
		}
	}
/*	if(obj.value!="") {
		Element.show(eFilterReset);	
	} else {
		Element.hide(eFilterReset);	
	}*/
}

function resetFilter() {
	obj = $(fFilter);
	obj.value = "";
	Element.hide(eFilterReset);	
	filterAssettype(obj);
}

function addNewAdmedia() {
		var url = "/ajax/addNewAdmedia.php?cat=" + escape(document.getElementById('id_miscellaneous').value)+"&name="+escape(document.getElementById('newAttribute').value);
		document.getElementById('id_miscellaneous').selectedIndex=0;
		document.getElementById('newAttribute').value='';
	
		xmlHttp.open("GET", url, true);

		xmlHttp.onreadystatechange = returnAddedAdmedias;
	
		xmlHttp.send(null);
}

function addAdmedia(obj) {
	if(obj.selectedIndex!=0) {
		var url = "/ajax/addAdmedia.php?id=" + escape(obj.options[obj.selectedIndex].value);
		obj.selectedIndex=0;
	
		xmlHttp.open("GET", url, true);

		xmlHttp.onreadystatechange = returnAddedAdmedias;
	
		xmlHttp.send(null);
	}
}

function returnAddedAdmedias() {
	if (xmlHttp.readyState == 4) {
		if (xmlHttp.status == 200) {
			var response = xmlHttp.responseText;
			var parts = response.split(":|_|:");
			if(parts[0]!="") {
				document.getElementById(fBasket).innerHTML = parts[0];
				Element.show(fBasket);
				Element.show(fNext);
				eval(parts[1]);
			} else {
				document.getElementById(fBasket).innerHTML = "";
				Element.hide(fNext);
			}
		} else {
			document.getElementById(fBasket).innerHTML = "";
			Element.hide(fNext);
		}
	}
}

function trashAdmedia(id, trashAll) {
	var url = "/ajax/addAdmedia.php?id=" + id + "&trash=" + trashAll;
	
	xmlHttp.open("GET", url, true);

	xmlHttp.onreadystatechange = returnAddedAdmedias;
	
	xmlHttp.send(null);
}

function add_color(inc) {
        var total = document.getElementById("add-color-wrapper_"+inc).getElementsByTagName("div").length;

        // Clone the first div in the series
        var div = document.getElementById("add-color-wrapper_"+inc).getElementsByTagName("div")[0].cloneNode(true);

        // Reset the cloned labels "for" attributes
        /*
        var labels = div.getElementsByTagName('label');
        
        for(var i = 0, lbl; lbl = labels[i]; i++) {
                // Set the new labels "for" attribute
                if(lbl["htmlFor"]) {
                        lbl["htmlFor"] = lbl["htmlFor"].replace(/[0-9]+/g, total);
                } else if(lbl.getAttribute("for")) {
                        lbl.setAttribute("for", lbl.getAttribute("for").replace(/[0-9]+/, total));
                }
        }
        */
        
        total++;
        
        document.getElementById("colorcount_"+inc).value = total;
        
        // Reset the inputs name and id attributes
        var inputs = div.getElementsByTagName('input');
        for(var i = 0, inp; inp = inputs[i]; i++) {
                // Set the new inputs id and name attribute
                inp.id = inp.name = inp.id.replace(/[0-9]+/g, total);
                if(inp.type == "text") inp.value = "";
        }

        // Reset the selects name and id attributes
        var selects = div.getElementsByTagName('select');
        for(var i = 0, sel; sel = selects[i]; i++) {
                // Set the new selects id and name attribute
                sel.id = sel.name = sel.id.replace(/[0-9]+/g, total);
                sel.selectedIndex = 0;
        }

        // Reset the button name and id attributes
        var inputs = div.getElementsByTagName('button');
        for(var i = 0, inp; inp = inputs[i]; i++) {
                inp.id = inp.name = inp.id.replace(/[0-9]+/g, total);
        }

        // Create a delete button
        var deleteBut = document.createElement('img');
        deleteBut.className = "delete-button";
        deleteBut.name = "delete_"+inc;
        deleteBut.src = "/img/jobpouch/trash.gif";
        //deleteBut.appendChild(document.createTextNode('X'));
        deleteBut.onclick = destroyLine;
        
        // Add the delete button to the wrapper div
        div.appendChild(deleteBut);

        // DOM inject the wrapper div
        document.getElementById("add-color-wrapper_"+inc).appendChild(div);

        // Stop the event
        return false;
}

function resetLineNumbers(inc) {
        var divs = document.getElementById("add-color-wrapper_"+inc).getElementsByTagName("div");
        for(var i = 0, div; div = divs[i]; i++) {
                var inps = div.getElementsByTagName('input');
                for(var j = 0, inp; inp = inps[j]; j++) {
                        inp.id = inp.name = inp.id.replace(/[0-9]+/g, i+1);
                }
                var buts = div.getElementsByTagName('button');
                for(var j = 0, but; but = buts[j]; j++) {
                        but.id = but.name = but.id.replace(/[0-9]+/g, i+1);
                }
        }
}

function destroyLine() {
		inc = this.name.split("_");
		inc = inc[inc.length-1];

        this.parentNode.parentNode.removeChild(this.parentNode);
        resetLineNumbers(inc);
}

function toggle_assettype(id) {
	if (navigator.userAgent.indexOf("Safari") > 0) {
		isIE = false;
	} else if (navigator.product == "Gecko") {
		isIE = false;
	} else {
		isIE = true;
	}
	if(isIE==true) {
		child = document.getElementsByTagName("tr");
		for(i=0;i<child.length;i++) {
			if(child[i].className=="assettype_"+id) {
				if(Element.visible(child[i])==false) {
					Element.show(child[i]);
					$("triangle_assettype_"+id).src = '/img/jobpouch/triangle.o.yellow.jpg';
				} else {
					Element.hide(child[i]);
					$("triangle_assettype_"+id).src = '/img/jobpouch/triangle.c.yellow.jpg';
				}
			}
		}
	} else {
		child = document.getElementsByName('assettype_'+id);
		for(i=0;i<child.length;i++) {
			if(Element.visible(child[i])==false) {
				Element.show(child[i]);
				$("triangle_assettype_"+id).src = '/img/jobpouch/triangle.o.yellow.jpg';
			} else {
				Element.hide(child[i]);
				$("triangle_assettype_"+id).src = '/img/jobpouch/triangle.c.yellow.jpg';
			}
		}
	}
}

function update_all(source) {
	pp = document.getElementsByName("date_prepress[]");	
	for(jp_i=0;jp_i<pp.length;jp_i++) {
		if(jp_i!=source) {
			$("date_prepress_"+jp_i).value = $("date_prepress_"+source).value
			$("amount_"+jp_i).value = $("amount_"+source).value
			$("date_document_"+jp_i).value = $("date_document_"+source).value
			$("time_prepress_"+jp_i).value = $("time_prepress_"+source).value
			$("time_document_"+jp_i).value = $("time_document_"+source).value
			$("proof_"+jp_i).selectedIndex = $("proof_"+source).selectedIndex
			$("data_"+jp_i).selectedIndex = $("data_"+source).selectedIndex
			$("shipping_method_"+jp_i).selectedIndex = $("shipping_method_"+source).selectedIndex
			if($('shipping_method_'+jp_i).options[$('shipping_method_'+jp_i).selectedIndex].value=="00000003" || $('shipping_method_'+jp_i).options[$('shipping_method_'+jp_i).selectedIndex].value=="00000004" || $('shipping_method_'+jp_i).options[$('shipping_method_'+jp_i).selectedIndex].value=="00000005") {
				Element.show("address_field_"+jp_i);
				$("address_field_em_"+jp_i).innerHTML = $("address_field_em_"+source).innerHTML;
			}

			if($("address_"+source) && $("address_"+jp_i)) {
				$("address_"+jp_i).value = $("address_"+source).value
			} else {
				if($("address_"+jp_i)) {
					$("address_"+jp_i).value = "";	
				}
			}
			$("ship_to_"+jp_i).selectedIndex = $("ship_to_"+source).selectedIndex
			$("hint_"+jp_i).value = $("hint_"+source).value
			//checkAddressField($('shipping_method_'+jp_i), jp_i);
			
			//$("select-color-basic_"+jp_i).selectedIndex = $("select-color-basic_"+source).selectedIndex
			//updateColorBox($("select-color-basic_"+jp_i).options[$("select-color-basic_"+jp_i).selectedIndex].value, 'box-color-basic_'+jp_i, jp_i)
			//if($("select-color-special_"+source)) {
			//	$("select-color-special_"+jp_i).selectedIndex = $("select-color-special_"+source).selectedIndex
			//	updateColorBox($("select-color-special_"+jp_i).options[$("select-color-special_"+jp_i).selectedIndex].value, 'box-color-special_'+jp_i, jp_i)
			//}
		}
	}
}

var liveT = null;
var searchObject = null;
function ls(obj) {
	searchObject = obj;
	if (liveT) {
		window.clearTimeout(liveT);
	}
	liveT = window.setTimeout("lsStart()",100);
}

function lsStart() {
	obj = searchObject;
	inc = obj.id.split("_");
	inc = inc[inc.length-1];
	
	if(obj.value=="") {
		updateImage('', inc, '');
		Element.hide("reset_"+inc);
		return;	
	} else {
		Element.show("reset_"+inc);
	}
	
	$('searchresult_'+inc).innerHTML = "<div class='loading'>Lade Daten...</div>";
	var url = "/ajax/search.php";
	var para = 'query='+escape(obj.value)+"&inc="+inc;
	
	new Ajax.Updater(
		'searchresult_'+inc, 
		url, 
		{
			method: 'get',
			parameters: para
		}
	);
}

function updateImage(img, inc, action) {
	if(!$("query_"+inc)) {
		parent.$("query_"+inc).value = "";
		parent.$("reset_"+inc).style.display = "none";
		parent.$('searchresult_'+inc).innerHTML = "<div class='loading'>Aktualisieren...</div>";

		var url = "/ajax/updateimage.php";
		var para = 'img='+escape(img)+"&inc="+inc+"&action="+action;
		
		var opt = {
				method: 'get',
				parameters: para,
				onSuccess: function(t) {
					parent.$('searchresult_'+inc).innerHTML = t.responseText;	
				}
		}		
		new Ajax.Request(
			url,
			opt
		);
	} else {
		$("query_"+inc).value = "";
		Element.hide("reset_"+inc);
		$('searchresult_'+inc).innerHTML = "<div class='loading'>Aktualisieren...</div>";
		var url = "/ajax/updateimage.php";
		var para = 'img='+escape(img)+"&inc="+inc+"&action="+action;

		new Ajax.Updater(
			'searchresult_'+inc,
			url,
			{
				method: 'get',
				parameters: para			
			}
		);
	}
}

function changeLayoutForm(f, inc) {
	if(f=="d") {
		$("database_"+inc).style.fontWeight = 'bold';	
		$("upload_"+inc).style.fontWeight = 'normal';	
		Element.hide("iframe_"+inc);
		Element.show("searchfield_"+inc);
	} else {
		$("database_"+inc).style.fontWeight = 'normal';	
		$("upload_"+inc).style.fontWeight = 'bold';	
		Element.hide("searchfield_"+inc);
		Element.show("iframe_"+inc);
	}
}

function submitUploadForm(inc) {
	Element.hide("field_for_upload_"+inc);
	Element.show("indicator_for_upload_"+inc);
	$("upload_form_"+inc).submit();
}

function addCampaign(id) {
	var url = "/ajax/addCampaign.php?id="+escape(id);
	resetFilter();
	var opt = {
		method: 'get',
		onSuccess: function(t) {
			Element.show(fBasket);
			Element.show(fNext);
		}
	}
	new Ajax.Updater(
		fBasket,
		url,
		opt
	);
}

function checkAddressField(obj, inc) {
	if(obj.name.match(/\[/)) {
		form_inc = "_"+inc;
	} else {
		form_inc = '';	
	}
	if(!$('ship_to'+form_inc)) {
		form_inc = "_"+inc;	
	}
	var sm = obj.options[obj.selectedIndex].value;
	var st = $('ship_to'+form_inc).options[$('ship_to'+form_inc).selectedIndex].value;
	var url = "/ajax/addAddressField.php?sm="+escape(sm)+"&st="+escape(st)+"&inc="+escape(inc);
	var opt = {
		method: 'get',
		onSuccess: function(t) {
			if(t.responseText==null || t.responseText=='') {
				Element.hide('address_field'+form_inc);	
			} else {
				Element.show('address_field'+form_inc);	
			}
		}
	}
	new Ajax.Updater(
		'address_field'+form_inc,
		url,
		opt
	);
}

if (isPNG) {
	poof1 = new Image(64,64); poof1.src = "/img/jobpouch/poof-1.png";
	poof2 = new Image(64,64); poof2.src = "/img/jobpouch/poof-2.png";
	poof3 = new Image(64,64); poof3.src = "/img/jobpouch/poof-3.png";
	poof4 = new Image(64,64); poof4.src = "/img/jobpouch/poof-4.png";
	poof5 = new Image(64,64); poof5.src = "/img/jobpouch/poof-5.png";
} else {
	poof1 = new Image(64,64); poof1.src = "/img/jobpouch/poof-1.gif";
	poof2 = new Image(64,64); poof2.src = "/img/jobpouch/poof-2.gif";
	poof3 = new Image(64,64); poof3.src = "/img/jobpouch/poof-3.gif";
	poof4 = new Image(64,64); poof4.src = "/img/jobpouch/poof-4.gif";
	poof5 = new Image(64,64); poof5.src = "/img/jobpouch/poof-5.gif";
}

function resetJobpouch(msg) {
	if(confirm(msg)==true) {
		poofCount = 0;
        poofTimer = setInterval("animatePoof()", 75);	
	} else {
		return false;	
	}
}

function animatePoof() {
   poofCount++;

   if (poofCount == 6) {
     $("pouch").src = "/img/blank.png";
     clearInterval(poofTimer);
     document.location.href = "/jobpouch/reset/";
   }
   else {
     $("pouch").width = 64;
     $("pouch").height = 64;
     $("pouch").src = eval("poof"+poofCount+".src");
   }
}


var jobToUpdate;
function showJob(parent, id, action) {
	if (action=="o" || (action=="c" && checkChanges(id))) {
		unsetChange(id);
		var url = "/ajax/showJob.php?parent="+parent+"&id="+id+"&action="+action;
		jobToUpdate = id;
		xmlHttp.open("GET", url, true);
		xmlHttp.onreadystatechange = returnJob;
		xmlHttp.send(null);
	}
}

function returnJob () {
	if (navigator.userAgent.indexOf("Safari") > 0) {
		isIE = false;
	} else if (navigator.product == "Gecko") {
		isIE = false;
	} else {
		isIE = true;
	}
	if (xmlHttp.readyState == 4) {
		if (xmlHttp.status == 200) {
			var response = xmlHttp.responseText;
			var parts = response.split(":|_|:");
			$("child_"+jobToUpdate).innerHTML = parts[0];
			eval(parts[1]);
			datePickerController.create();
		}
	}	
}

var arrayChanges = new Array();
function throwChange(id) {
	arrayChanges[id] = true;
}

function unsetChange(id) {
	arrayChanges[id] = false;
}

function submitAndResetChanges(id) {
	unsetChange(id);
	$('jobform-'+id).submit();
}

function checkChanges(id, noOut) {
	if (hasChanges(id)) {
		if (noOut) {
			return false;
		} else {
			return confirmChanges();
		}
	}
	return true;
}

function hasChanges(id) {
	return arrayChanges[id];
}

function confirmChanges() {
	return confirm('Sie haben noch ungespeicherte Änderungen.'+"\n"+'Wählen Sie "Ok", um fortzufahren oder "Abbrechen", um auf der Maske zu verbleiben und Ihre Änderungen zu speichern.')	
}

function getColByClassName(row, classname) {
	var cols = row.getElementsByTagName('td');
	for (var x in cols) {
		if (!isNaN(Number(x))) {
			if (cols[x].className==classname) {
				return cols[x];
			}
		}
	}
}

function getRowsByClassName(parent, classname) {
	var rows = parent.getElementsByTagName('tr');
	var returnArray = new Array();
	for (var x in rows) {
		if (!isNaN(Number(x))) {
			if (rows[x].className==classname) {
				returnArray[returnArray.length] = rows[x];
			}
		}
	}
	return returnArray;
}

function checkChangesRow(row, noOut) {
	var rows = getRowsForRow(row);
	var changes = false;
/*	alert(forms.length);*/
	for (var x in rows) {
		if (!isNaN(Number(x))) {
			var id = rows[x].id.substring(6);
			if (!checkChanges(id, true)) {
				changes = true;
			}
		}
	}
	if (changes) {
		if (noOut) {
			return false;
		} else {
			return confirmChanges();
		}
	}
	return true;
}

function getRowsForRow(row) {
	return getRowsByClassName(row.parentNode, 'child_'+row.id.substring(8));
}

function unsetChangesRow(row) {
	var rows = getRowsForRow(row);
	for (var x in rows) {
		if (!isNaN(Number(x))) {
			var id = rows[x].id.substring(6);
			unsetChange(id);
		}
	}
}

function checkChangesBrand(id) {
	var rows = getRowsForBrand(id);
	var changes = false;
	for (var x in rows) {
		if (!isNaN(Number(x))) {
			if (rows[x].id.substr(0, 8)=='row_job_');
				if (!checkChangesRow(rows[x], true)) {
					changes = true;
				}
		}
	}
	if (changes) {
		return confirmChanges();
	}
	return true;
}

function getRowsForBrand(id) {
	return document.getElementById('jobs_'+id).getElementsByTagName('tr');
}

function unsetChangesBrand(id) {
	var rows = getRowsForBrand(id);
	for (var x in rows) {
		if (!isNaN(Number(x))) {
			if (rows[x].id.substr(0, 8)=='row_job_') {
				var rows2 = getRowsForRow(rows[x]);
				for (var y in rows2) {
					if (!isNaN(Number(y))) {
						var id = rows2[y].id.substring(6);
						unsetChange(id);
					}
				}
			}
		}
	}
}

function showJobDate(parent, id, action) {
	if (action=="o" || (action=="c" && checkChanges(id))) {
		unsetChange(id);
		var url = "/ajax/showJob.php?parent="+parent+"&id="+id+"&action="+action+"&date=1";
		jobToUpdate = id;
		xmlHttp.open("GET", url, true);
		xmlHttp.onreadystatechange = returnJob;
		xmlHttp.send(null);
	}
}

function updateCheckbox(obj, id, field) {
	if(obj.checked==true) {
		var val = 'Y';	
	} else {
		var val = 'D';	
	}
	var url = "/ajax/updateCheckbox.php?id="+escape(id)+"&field="+escape(field)+"&value="+escape(val);
	new Ajax.Request(url);
}

var autocomplete = false;

function doAutoComplete(id) {
	if(autocomplete==false) {
		sm_id = $("shipping_method_"+id).options[$("shipping_method_"+id).selectedIndex].value;
		address = id;
		new Ajax.Autocompleter('address_'+id, 'address_auto_complete_'+id, '/ajax/auto_complete_address.php?id='+sm_id+'&addr='+address, {});
	}
	autocomplete = true;
}

function resetAutoComplete() {
	autocomplete = false;
}

function updateArchive(id) {
	document.getElementById('archive_'+id).innerHTML='<img src="/img/date_go.png" alt="ins Archiv verschoben" title="ins Archiv verschoben" />';
	var url = "/ajax/updateArchive.php?id="+escape(id);
	new Ajax.Request(url);
}

function ucfirst(text) {
	return text.slice(0,1).toUpperCase() + text.slice(1);
}
/*
function keyUp(Ereignis) {
	if (this.id=='customer_text') {
		var id = 'Customer';
	} else {
		var id = ucfirst(this.id);
	}
	var proposeId = 'propose'+id;
	var mouseoverVar = id.toLowerCase()+'Mouseover';
	if (window.event && window.event.keyCode) {
		var taste = window.event.keyCode;
	} else {
		var taste = Ereignis.which; 
	}
	switch (taste) {
		default:
			if (taste==8 || taste==46 || (taste>=48 && taste<=90) || (taste>=96 && taste<=111) || (taste>=186 && taste<=192) || (taste>=219 && taste<=222)) {
				if (id=='Customer') {
					if (this.value=='') {
						document.getElementById(proposeId).style.display='none';
						return;
					}
				}
				loadXMLDoc('ajax/propose'+id+'.php?value='+escape(this.value)+(id=='Jobnr'?'&customer='+document.getElementById('customer').value:''));
			}
		    break;
	}
}

function keyDown(Ereignis) {
	if (this.id=='customer_text') {
		var id = 'Customer';
	} else {
		var id = ucfirst(this.id);
	}
	var proposeId = 'propose'+id;
	var mouseoverVar = id.toLowerCase()+'Mouseover';
	if (window.event && window.event.keyCode) {
		var taste = window.event.keyCode;
	} else {
		var taste = Ereignis.which; 
	}
	eval ('var actCustomer = document.getElementById(proposeId).childNodes['+mouseoverVar+'];');
	if (actCustomer) {
		switch (taste) {
			case 38:
				actCustomer.style.backgroundColor="white"; actCustomer.style.color="black";
				eval (''+mouseoverVar+'--;');
				eval ('if ('+mouseoverVar+'==-1) { '+mouseoverVar+' = document.getElementById(proposeId).childNodes.length-1; }');
				eval ('actCustomer = document.getElementById(proposeId).childNodes['+mouseoverVar+'];');
				actCustomer.style.backgroundColor="blue"; actCustomer.style.color="white";
		      	break;
			case 40:
				actCustomer.style.backgroundColor="white"; actCustomer.style.color="black";
				eval (''+mouseoverVar+'++;');
				eval ('if ('+mouseoverVar+'==document.getElementById(proposeId).childNodes.length) { '+mouseoverVar+' = 0; }');
				eval ('actCustomer = document.getElementById(proposeId).childNodes['+mouseoverVar+'];');
				actCustomer.style.backgroundColor="blue"; actCustomer.style.color="white";
		      	break;
			case 9:
			case 13:
		    	actCustomer.onclick();
		      	break;
		}
	}
	if (taste==27) {
   		document.getElementById(proposeId).style.display='none';
	}
}

function updateContactImg() {
	var imgs = document.getElementById('contact').parentNode.getElementsByTagName('img');
	if (imgs.length==0) {
		var img = document.createElement('img');
	    img.style.position = 'absolute';
		document.getElementById('contact').parentNode.appendChild(img);
		img.onclick = function(){document.getElementById('hiddenContact').value='1'; document.getElementById('formDelivery').submit();}
	} else {
		var img = imgs[0];
	}
	if (document.getElementById('contact').value=='') {
		img.src='/img/add.png';
	} else {
		img.src='/img/edit.png';
	}
}
*/
function getIndexOfValue(select, value) {
	for (var k=0;k<select.options.length;k++) {
		if (select.options[k].value==value) {
			return k;
		}
	}
}

function checkField(id, label) {
	if (document.getElementById(id).value=='') {
		alert('Bitte geben Sie '+label+' an.');
		document.getElementById(id).focus();
		return false;
	}
	return true;
}

function checkShipToFields(id) {
	return (checkField('shipTo'+id+'Name', 'den Namen')
		&& checkField('shipTo'+id+'Street', 'die Straße')
		&& checkField('shipTo'+id+'Zip', 'die PLZ')
		&& checkField('shipTo'+id+'Town', 'die Stadt'));
}

Ajax.Eval = Class.create();
Object.extend(Object.extend(Ajax.Eval.prototype, Ajax.Request.prototype), {
  initialize: function(url, pars) {
    this.transport = Ajax.getTransport();
    this.setOptions({method:'post', parameters:pars});
    this.options.onComplete = (function(transport) {
      eval(transport.responseText);
    });
    this.request(url);
  }
});

function submitShipTo(id) {
	var queryString = 'id='+encodeURI(document.getElementById('shipTo'+id+'Id').value)+
		'&name='+encodeURI(document.getElementById('shipTo'+id+'Name').value)+
		'&abbr='+encodeURI(document.getElementById('shipTo'+id+'Abbr').value)+
		'&street='+encodeURI(document.getElementById('shipTo'+id+'Street').value)+
		'&zip='+encodeURI(document.getElementById('shipTo'+id+'Zip').value)+
		'&forceTown='+encodeURI(document.getElementById('shipTo'+id+'ForceTown').value)+
		'&town='+encodeURI(document.getElementById('shipTo'+id+'Town').value)+
		'&phone='+encodeURI(document.getElementById('shipTo'+id+'Phone').value)+
		'&fax='+encodeURI(document.getElementById('shipTo'+id+'Fax').value)+
		'&email='+encodeURI(document.getElementById('shipTo'+id+'Email').value)+
		'&url='+encodeURI(document.getElementById('shipTo'+id+'Url').value);
	new Ajax.Eval('/ajax/submitShipTo.php?id='+encodeURI(id), queryString);
}

function submitDate(id, type) {
	var queryString = 'date='+encodeURI(document.getElementById('input_'+type+'_date_'+id).value)+
		'&time='+encodeURI(document.getElementById('input_'+type+'_time_'+id).value);
	new Ajax.Eval('/ajax/submitDate.php?id='+encodeURI(id)+'&type='+encodeURI(type), queryString);
}

function openShipToForm(id) {
	if (document.getElementById('ship_to_'+id).value=="") {
		document.getElementById('shipTo'+id+'Id').value=
			document.getElementById('shipTo'+id+'Name').value=
			document.getElementById('shipTo'+id+'Abbr').value=
			document.getElementById('shipTo'+id+'Street').value=
			document.getElementById('shipTo'+id+'Zip').value=
			document.getElementById('shipTo'+id+'ForceTown').value=
			document.getElementById('shipTo'+id+'Town').value=
			document.getElementById('shipTo'+id+'Phone').value=
			document.getElementById('shipTo'+id+'Fax').value=
			document.getElementById('shipTo'+id+'Email').value=
			document.getElementById('shipTo'+id+'Url').value="";
		document.getElementById('shipToForm'+id).style.display='block';
	} else {
		new Ajax.Eval('/ajax/getShipToData.php?id='+escape(id)+'&id_company='+escape(document.getElementById('ship_to_'+id).value));
	}
}

function clapJobinfo(element) {
	element.parentNode.parentNode.getElementsByTagName('div')[1].style.display='block';
	element.parentNode.parentNode.removeChild(element.parentNode);
}