// JavaScript Document

//Function to change the visibility of div's used for i.e. the menu.
// Determine Browser type
var browserType;

if (document.layers) {browserType = "nn4"}
if (document.all) {browserType = "ie"}
if (window.navigator.userAgent.toLowerCase().match("gecko")) {browserType= "gecko"}


function getObjectById(obt){
	if (browserType == "gecko" )
		 document.poppedLayer = eval('document.getElementById(obt)');
	else if (browserType == "ie")
	     document.poppedLayer = eval('document.all[obt]');
	else
	     document.poppedLayer = eval('document.layers[obt]');
	     
	return document.poppedLayer;
}

function changeVisible(obt){
	if (browserType == "gecko" )
		 document.poppedLayer = eval('document.getElementById(obt)');
	else if (browserType == "ie")
	     document.poppedLayer = eval('document.all[obt]');
	else
	     document.poppedLayer = eval('document.layers[obt]');
	     
	if(document.poppedLayer.style.display == "none"){
		document.poppedLayer.style.display = "block";
	}else{
		document.poppedLayer.style.display = "none";
	}
}


/*
* Function to check forms that have mandatory fields.
* Usage: <FORM name="formName" action="actionUrl.php" method="POST" onSubmit="checkForm(new Array("fieldName1","fieldName2"),new Array("type1","type2"))">
* Types:
* notEmpty
* email
* onlyNumbers
* onlyDoubles
* password
* selectNotEmpty (empty or initial select item should have value -1)
* checkbox
* * = not implemented yet
*/
function checkForm(myForm, fields, types){

  var result = true;
  var isEmpty = false;
  var invalidEmail = false;
  var notANumber = false;
  var notADouble = false;
  var invalidPassword = false;
  var invalidPasswordLength = false;
  var selectIsEmpty = false;
  var checkboxNotChecked = false;
  var invalidDoubleCheck = false;
   
  for (i=0; i<fields.length; i++){
    switch(types[i]){
      case '':
        if(!checkNotEmpty(myForm[fields[i]])){result=false;isEmpty=true;}
      break;
      case null:
        if(!checkNotEmpty(myForm[fields[i]])){result=false;isEmpty=true;}
      break;
      case 'checkbox':
        if(!checkboxChecked(myForm[fields[i]])){result=false; checkboxNotChecked=true;}
      break;
      case 'notEmpty':
        if(!checkNotEmpty(myForm[fields[i]])){result=false;isEmpty=true;}
      break;
      case 'selectNotEmpty':
        if(!checkSelectNotEmpty(myForm[fields[i]])){result=false;selectIsEmpty=true;}
      break;
      case 'email':
        if(!validateEmail(myForm[fields[i]])){result=false;invalidEmail=true;}
      break;
      case 'onlyNumbers':
        if(!validateOnlyNumber(myForm[fields[i]])){result=false;notANumber=true;}
      break;
      case 'onlyDoubles':
        if(!validateOnlyDouble(myForm[fields[i]])){result=false;notADouble=true;}
      break;
      case 'password':
        if(!validatePassword(myForm[fields[i][0]],myForm[fields[i][1]])){
          result=false;
          invalidPassword=true;
        }else if(!checkPasswordLength(myForm[fields[i][0]],5)){
          result=false;
          invalidPasswordLength=5;
        }
      break;
      case 'doubleCheck':
        if(!validatePassword(myForm[fields[i][0]],myForm[fields[i][1]])){
          result=false;
          invalidDoubleCheck=true;
        }
      break;
    }
  }
  if(isEmpty){
    alert("Please fill in all mandatory fields.");
  }
  if(selectIsEmpty){
    alert("Please select an item in the select box.");
  }
  if(invalidEmail){
    alert("Please fill in a valid e-mail address.");
  }
  if(notANumber){
    alert("The red fields can only contain numbers.");
  }
  if(notADouble){
    alert("The red fields can only contain decimal numbers.");
  }
  if(invalidPassword){
    alert("The two passwords do not match.");
  }
  if(invalidDoubleCheck){
    alert("The two fields do not match.");
  }
  if(invalidPasswordLength!=false){
    alert("The password length should be a minimum of "+invalidPasswordLength+" characters.");
  }
  if(checkboxNotChecked){
    alert("Please check the checkbox to continue.");
  }
  return result;
}

function checkPasswordLength(lengthPass, passLength){
  if(lengthPass.value.length<passLength){
    lengthPass.focus();
    lengthPass.style.backgroundColor="#99CCFF";
    return false;
  }
  lengthPass.style.backgroundColor="";
  return true;
}

function validatePassword(firstPass, secondPass){
    if(!(firstPass.value==secondPass.value)){
      firstPass.focus();
      firstPass.style.backgroundColor="#99CCFF";
      secondPass.style.backgroundColor="#99CCFF";
      return false;
    }
    firstPass.style.backgroundColor="";
    secondPass.style.backgroundColor="";
    return true;
}

function checkboxChecked(myObject){
  if(!myObject.checked){
      myObject.focus();
      myObject.style.backgroundColor="#99CCFF";
      return false;
  }
  myObject.style.backgroundColor="";
  return true;
}

function checkNotEmpty(myObject){
  if(myObject.value==""){
      myObject.focus();
      myObject.style.backgroundColor="#99CCFF";
      return false;
  }
  myObject.style.backgroundColor="";
  return true;
}

function checkSelectNotEmpty(myObject){
  if(myObject.options[myObject.selectedIndex].value==-1){
    myObject.focus();
    myObject.style.backgroundColor="#99CCFF";
    return false;
  }
  myObject.style.backgroundColor="";
  return true;
}

function validateEmail(myObject){
  
if(!myObject.value.match(/^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/)){
      myObject.style.backgroundColor="#99CCFF";
      myObject.focus();
      return false;
  }
  myObject.style.backgroundColor="";
  return true;
}

function validateOnlyNumber(myObject){
  if(!myObject.value.match(/^[0-9]+$/)){
      myObject.style.backgroundColor="#FF6633";
      myObject.focus();
      return false;
  }
  myObject.style.backgroundColor="";
  return true;
}

function validateOnlyDouble(myObject){
  if(!myObject.value.match(/^[0-9]+\.?[0-9]*$/)){
      myObject.style.backgroundColor="#FF6633";
      myObject.focus();
      return false;
  }
  myObject.style.backgroundColor="";
  return true;
}

function getHTTPObject()
{
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	return req;
}

http = getHTTPObject();
/**
* Function the retrieve a HTML-part from an URL. The innerHTML from the object will 
* be replaced with this HTML-part.
*
*/
function request(url, func) {
	http.open("GET", url, true);
	http.onreadystatechange = func;
	http.send(null);
}

function enable(id){
  document.getElementById(id).disabled = false;
}
                
function disable(id){
  document.getElementById(id).disabled = true;
}
 function visible(elmnt) {
    if (elmnt == "CPC") {
      document.getElementById('CPC').style.visibility="visible"
      document.getElementById('CPCWEB').style.visibility="visible"
      document.getElementById('CPL').style.visibility="hidden"
      document.getElementById('CPLWEB').style.visibility="hidden"
    }
    if (elmnt == "CPL") {
      document.getElementById('CPL').style.visibility="visible"
      document.getElementById('CPLWEB').style.visibility="visible"
      document.getElementById('CPC').style.visibility="hidden"
      document.getElementById('CPCWEB').style.visibility="hidden"
    }
    if (elmnt == "Beide") {
      document.getElementById('CPC').style.visibility="visible"
      document.getElementById('CPCWEB').style.visibility="visible"
      document.getElementById('CPL').style.visibility="visible"
      document.getElementById('CPLWEB').style.visibility="visible"
    }
  }
  
function getHTTPObject()
{
	if (window.XMLHttpRequest) {
		req = new XMLHttpRequest();
	} else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
	}
	return req;
}

http = getHTTPObject();
/**
* Function the retrieve a HTML-part from an URL. The innerHTML from the object will 
* be replaced with this HTML-part.
*
*/
function request(url, func) {
	http.open("GET", url, true);
	http.onreadystatechange = func;
	http.send(null);
}

//Info divs
enabled = 0;

function showInfoDiv(text) {
	document.getElementById('infoDiv').innerHTML = text;
	document.getElementById('infoDiv').style.visibility="visible";
	enabled = 1;
}

function hideInfoDiv() {
	document.getElementById('infoDiv').style.visibility="hidden";
	enabled = 0;
}

function mousePos(e) {
	if(enabled == 1) {
		if(document.all) {
			xm = event.clientX + document.body.scrollLeft + 100;
			ym = event.clientY + document.body.scrollTop - 140;
		}
		else {
			xm = e.pageX + 12;
			ym = e.pageY;
		}
		document.getElementById('infoDiv').style.top = ym + "px";
		document.getElementById('infoDiv').style.left = xm + "px";
	}
}

document.onmousemove = mousePos;
