var left1;
var left2;

function confirmLink(locationURL,msg){
	Check = confirm(msg);
	if(Check == true){
		location.href=locationURL;
	}
}

function putColor(listField,color){
	window.opener.document.all[listField].value=color; 
	var listFieldCode=listField+'_code';
	window.opener.document.all[listFieldCode].style.backgroundColor=color;
	window.close('colors');
}



function saveProduct(){
	var position=parent.scale.document.forms[0].position.value;
	var product=parent.scale.document.forms[0].productId.value;
	var side=parent.scale.document.forms[0].side.value;
	var width=parent.scale.document.images["epresso"].width;
	var height=parent.scale.document.images["epresso"].height;
	var fileName=parent.scale.document.forms[0].fileName.value;
	if(parent.scale.document.forms[0].color!=""){
		var color = parent.scale.document.forms[0].color.value;
	}
	else{
		var color="";
	}
	if(parent.auswahl.document.forms[0].pSize.value){
		var size=parent.auswahl.document.forms[0].pSize.value;
	}
	
	//alert('position: '+position+'\nproductId: '+product+'\nside: '+side+'\nwidth: '+width+'\nheight: '+height+'\nfileName: '+fileName+'\ncolor: '+color+'\nsize: '+size);
	parent.saveProduct.location.href = '/php/user/saveProduct.php?position='+position+'&product='+product+'&side='+side+'&width='+width+'&height='+height+'&fileName='+fileName+'&color='+color+'&size='+size;
	
}

function postProductId(productId){
	parent.scale.location.href = "/user/modul1.php?productId="+productId+"&fileName="+document.forms[0].datei.value;
	parent.bildUpload.document.forms[0].productId.value=productId;
	parent.auswahl.location.href = "/user/auswahl.php?productId="+productId;
}

function posTextLayer(layerId){
	var coords=getPosition(parent.auswahl.document.images["clear"]);
	if(coords){
		position(layerId,coords.x+5,coords.y+10);
	}
	showHideLayers(layerId,'','show')
}


function reloadUpload(){
	parent.bildUpload.location.href = "/user/bildUpload.php?productId="+parent.bildUpload.document.forms[0].productId.value+"&side="+parent.bildUpload.document.forms[0].side.value+"&position="+parent.bildUpload.document.forms[0].position.value;
}

function choosePicture(picName){
	parent.auswahl.document.forms[0].datei.value=picName;
	parent.auswahl.document.forms[1].fileName.value=picName;
	var productId=parent.scale.document.forms[0].productId.value;
	var side=parent.bildUpload.document.forms[0].side.value;
	var position=parent.bildUpload.document.forms[0].position.value;
	if(parent.scale.document.forms[0].color!=""){
		var color = parent.scale.document.forms[0].color.value;
	}
	else{
		var color="";
	}
	parent.scale.location.href = "/user/modul1.php?productId="+productId+"&side="+parent.bildUpload.document.forms[0].side.value+"&position="+parent.bildUpload.document.forms[0].position.value+"&fileName="+picName+"&color="+color;
	parent.auswahl.location.href = "/user/auswahl.php?productId="+parent.bildUpload.document.forms[0].productId.value+"&side="+parent.bildUpload.document.forms[0].side.value+"&position="+parent.bildUpload.document.forms[0].position.value+"&fileName="+picName+"&color="+color;
}

function postSide(side){
	parent.bildUpload.document.forms[0].side.value=side;
}


function position(lay,x,y){
	document.getElementById(lay).style.left = x;
	document.getElementById(lay).style.top = y;
}

function getPosition (element) {
    if (document.layers) {
      if (typeof element.x != 'undefined')
        return { x: element.x, y: element.y };
      else if (typeof element.pageX != 'undefined')
        return { x: element.pageX, y: element.pageY }
      else
        return null;
    }
    else if (element.offsetParent) {
      var coords = {x : 0, y: 0};
      while (element) {
        coords.x += element.offsetLeft;
        coords.y += element.offsetTop;
        element = element.offsetParent;
      }
      return coords;
    }
    else
      return null;
}

function init() {
	NN= document.layers ? true : false;
	IE= document.all ? true : false;
	if (NN) document.captureEvents(Event.MOUSEDOWN | Event.MOUSEUP);
	document.onmousedown = startDrag;
	document.onmouseup = endDrag;
	var coords=getPosition(document.images["clear"]);
	if(coords){
		//position('regler1',coords.x+240,coords.y);
		//position('regler2',coords.x+243,coords.y+110);
		left1=coords.y+20;
		left2=coords.y+130;
	}
}

function startDrag(e) {
	var found = false;

	if (NN) {
		// Netscape
		var gefunden = 0;
		var obj = document.layers[1];
		// Ueberpruefen, wob Schieberegler angeklickt wurde
		if ((e.pageX > obj.left) && (e.pageX < obj.left + obj.clip.width) && (e.pageY > obj.top) && (e.pageY < obj.top + obj.clip.height))  gefunden = true;
		if (gefunden) {
			current = document.layers[1];
			dy = e.pageY - current.top;
		
			// Event-Capturing
			document.captureEvents(Event.MOUSEMOVE);
			document.onmousemove = drag;
			return false;
		}
	} 

	if (IE) {
		// MSIE
		var obj = window.event.srcElement;
		if (obj.parentElement.id.indexOf("2") != -1) {
			current = obj.parentElement.style;
			dy = window.event.clientY - current.pixelTop;
			document.onmousemove = drag;
			return false;
		}
	}
	// Benutzer hat nicht auf den Schieberegler geklickt
	current = null;
	return false;
}

function drag(e) {
	// Der Schieberegler wird positioniert und der Regelbereich dabei auf die H&ouml;he
	// der Reglerskala begrenzt

	if (current != null) {
		if (NN) {
			current.top = e.pageY - dy;
			if (current.top<left1) current.top=left1;
			if (current.top>left2) current.top=left2;
			var regelwert=current.top-left1;
		}
		if (IE) {
			current.pixelTop = window.event.clientY - dy;
			if (current.pixelTop<left1) current.pixelTop=left1;
			if (current.pixelTop>left2) current.pixelTop=left2;
			var regelwert=current.pixelTop-left1;
		}
		regelwert1=Math.round(regelwert);
		regelwert2=(Math.round(regelwert/1))*-1;

	}
	scale(regelwert1);
	return false;
}

function endDrag(e) {
	if (NN) document.releaseEvents(Event.MOUSEMOVE);
	document.onmousemove = null;
	current = null;
	saveProduct();
	return false;
}

function getSize(){
	var height=document.images["epresso"].height;
	var width=document.images["epresso"].width;
	
	var widthReal=eval(width*12);
	var heightReal=eval(height*12);
	
	alert('breite: '+width+' höhe: '+height+' breite real: '+widthReal+' höhe real: '+heightReal);
}

function postFileName(fileName){
	parent.auswahl.document.forms[0].datei.value=fileName;
}

function findObj(n, d) {
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function showHideLayers() {
  var i,p,v,obj,args=showHideLayers.arguments;
  for (i=0; i<(args.length-2); i+=3) if ((obj=findObj(args[i]))!=null) { v=args[i+2];
    if (obj.style) { obj=obj.style; v=(v=='show')?'visible':(v=='hide')?'hidden':v; }
    obj.visibility=v; }
}

function reloadPage(init) {  //reloads the window if Nav4 resized
  if (init==true) with (navigator) {if ((appName=="Netscape")&&(parseInt(appVersion)==4)) {
    document.pgW=innerWidth; document.pgH=innerHeight; onresize=reloadPage; }}
  else if (innerWidth!=document.pgW || innerHeight!=document.pgH) location.reload();
}
reloadPage(true);

function saveMenue(main,subCat){
	document.images[menue].src="/menue/saveMenueSession.php?main="+main+"&sub="+subCat;
}

function openWindow(theURL,winName,features) {
  window.open(theURL,winName,features);
}

function validateForm() { //v4.0
  var i,p,q,nm,test,num,min,max,errors='',args=validateForm.arguments;
  for (i=0; i<(args.length-2); i+=3) { test=args[i+2]; val=findObj(args[i]);
    if (val) { nm=val.name; if ((val=val.value)!="") {
      if (test.indexOf('isEmail')!=-1) { p=val.indexOf('@');
        if (p<1 || p==(val.length-1)) errors+='- '+nm+' muß eine gültige E-Mail Adresse enthalten.\n';
      } else if (test!='R') { num = parseFloat(val);
        if (isNaN(val)) errors+='- '+nm+' muß Nummern enthalten.\n';
        if (test.indexOf('inRange') != -1) { p=test.indexOf(':');
          min=test.substring(8,p); max=test.substring(p+1);
          if (num<min || max<num) errors+='- '+nm+' muß eine Nummer zwischen '+min+' und '+max+'entahlten.\n';
    } } } else if (test.charAt(0) == 'R') errors += '- '+nm+' ist ein Pflichtfeld.\n'; }
  } if (errors) alert('Folgende/r Fehler ist/sind aufgetreten:\n'+errors);
  document.returnValue = (errors == '');
}