/* ---------------------------- */
/* XMLHTTPRequest Enable */
/* ---------------------------- */
function createObject() {
var request_type;
var browser = navigator.appName;
if(browser == "Microsoft Internet Explorer"){
request_type = new ActiveXObject("Microsoft.XMLHTTP");
}else{
request_type = new XMLHttpRequest();
}
return request_type;
}

var http = createObject();

/*** jQuery ***/
/* fade mask */
	var J = jQuery.noConflict();
	J(document).ready(function(){
 		J('.container').after('<div class="mask"></div>');
		J('.mask').animate({opacity: 1.0}, 100).fadeOut('slow');

		J('.nav li').click(function(){
			var url = J(this).attr('href');

			J('.mask').fadeIn('slow', function(){
				document.location.href = url;
			})
			return false;
		});

	});

/*** mouseover ***/
function mOver(id,css)
{
var cssName = document.getElementById(id);
cssName.setAttribute("class", "mo-" + css);
cssName.setAttribute("className", "mo-"+ css);
}
function mOut(id,css)
{
var cssName = document.getElementById(id);
cssName.setAttribute("class", css);
cssName.setAttribute("className", css);
}
/*** ------------------------------------ ***
PANORAMA

****---------------------------------------***/
// var pan = document.getElementById();









/*** -----------------------------------------***/



function nxtStep(value)
{
var mynext = document.getElementById("step-" + value);
mynext.style.display = 'block';
var currentValue = --value;
// alert(currentValue);
var currentPage = document.getElementById("step-" + currentValue);
currentPage.style.display = "none";
}
function prevStep(value)
{

	if(value == 1) 
	{
	var currentValue = 3;
	// alert(currentValue);
	var currentPage = document.getElementById("step-" + currentValue);
	currentPage.style.display = "none"; 
	var homePage = document.getElementById("step-1");
	homePage.style.display = "none";
	}
	if(value == 4)
	{
	var previous = document.getElementById("step-" + value);
	previous.style.display = "block";

	var currentValue = 5;
	var currentPage = document.getElementById("step-" + currentValue);
	currentPage.style.display = "none";
	}
	if( (value != 1) || (value != 4))
	{
	var previous = document.getElementById("step-" + value);
	previous.style.display = "block";

	var currentValue = ++value;
	var currentPage = document.getElementById("step-" + currentValue);
	currentPage.style.display = "none";
	}

}
function startOver()
{
window.location='http://localhost/bakflean/kunstwerk.php';
}

function payment()
{
window.location='prepare.php';
}
/* -------------------------- */
/* PRODUCT SELECTION */
/* -------------------------- */
function catchId(artistproductid,artistid)
{
nxtStep('2');
/*
var artistProductId = artistproductid;
var artistId = artistid;
http.open('get', 'session_handler.php?artistId='+artistId+'&artistProductId='+artistProductId+'&nocache = '+nocache);
http.onreadystatechange = insertStatusProduct;
http.send(null);

	function insertStatusProduct() 
	{
		if(http.readyState == 4){ 
		var response = http.responseText;
		// else if fields are ok show a message: "Site added+ site URL".
		document.getElementById('insert_response').innerHTML = response;
		}
	} */
}
/* -------------------------- */
/* MODEL SELECTION */
/* -------------------------- */
function catchM(posname,posmodelname)
{
var posName = posname;
var posModelName = posmodelname;
window.location='http://localhost/bakflean/kunstwerk.php?step-3&posname='+posName+'&posmodelname='+posModelName;
}
function catchModel(posname,posmodel)
{
var posName = posname;
var posModel = posmodel;
http.open('get', 'session_handler.php?posId='+posName+'&posModel='+posModel+'&nocache = '+nocache);
http.onreadystatechange = insertStatusModel;
http.send(null);
nxtStep('3');
	function insertStatusModel() 
	{
		if(http.readyState == 4){ 
		var response = http.responseText;
		// else if fields are ok show a message: "Site added+ site URL".
		document.getElementById('insert_response').innerHTML = response;
		}
	}
}

/* -------------------------- */
/* INSERT  CUSTOMER */
/* -------------------------- */
/* Required: var nocache is a random number to add to request. This value solve an Internet Explorer cache issue */
var nocache = 0;
function insert() {
// Optional: Show a waiting message in the layer with ID login_response
document.getElementById('insert_response').innerHTML = "Moment alstublieft..."
// Required: verify that all fileds is not empty. Use encodeURI() to solve some issues about character encoding.
var url = "insert.inc.php";
var params = "firstname=firstname&lastname=lastname&street=street&address=address&zip=zip&location=location&phone=phone&email=email";
http.open("POST", url, true);

//Send the proper header information along with the request
http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
http.setRequestHeader("Content-length", params.length);
http.setRequestHeader("Connection", "close");

http.onreadystatechange = function() {//Call a function when the state changes.
	if(http.readyState == 4 && http.status == 200) {
		alert(http.responseText);
	}
}
window.location='kunstwerk.php?step-5';
}
/*
// Set te random number to add to URL request
nocache = Math.random();
// Pass the login variables like URL variable
http.open('post', 'insert.inc.php?firstname='+firstName+'&lastname='+lastName+'&nocache = '+nocache);
http.onreadystatechange = insertReply;
http.send(null);
nxtStep('5');
}
*/
/*** Form validation ***/
/* registration */
function validateFormOnSubmit2(theForm) {
var reason = "";

  reason += validateEmpty(theForm.firstname);
  reason += validateEmpty(theForm.lastname);
  reason += validateEmpty(theForm.street);
  reason += validateEmpty(theForm.address);
  reason += validateEmpty(theForm.zip);
  reason += validateEmpty(theForm.location);
  reason += validatePhone(theForm.phone);
  reason += validateEmail(theForm.email);
//reason += validatePassword(theForm.pwd);
  //reason += validatePhone(theForm.phone);
  //reason += validateEmpty(theForm.from);
      
  if (reason != "") {
    alert("Enkele velden zijn onjuist ingevuld:\n" + reason);
    return false;
  }

  return true;
}


/* contact page */
function validateFormOnSubmit(theForm) {
var reason = "";

  //reason += validateUsername(theForm.username);
  //reason += validatePassword(theForm.pwd);
  reason += validateEmail(theForm.email);
  //reason += validatePhone(theForm.phone);
  //reason += validateEmpty(theForm.from);
      
  if (reason != "") {
    alert("Enkele velden zijn onjuist ingevuld:\n" + reason);
    return false;
  }

  return true;
}

/* empty fields */
function validateEmpty(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = "The required field has not been filled in.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}
/* username */
function validateUsername(fld) {
    var error = "";
    var illegalChars = /\W/; // allow letters, numbers, and underscores
 
    if (fld.value == "") {
        fld.style.background = 'Yellow'; 
        error = "You didn't enter a username.\n";
    } else if ((fld.value.length < 5) || (fld.value.length > 15)) {
        fld.style.background = 'Yellow'; 
        error = "The username is the wrong length.\n";
    } else if (illegalChars.test(fld.value)) {
        fld.style.background = 'Yellow'; 
        error = "The username contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
/* password */
function validatePassword(fld) {
    var error = "";
    var illegalChars = /[\W_]/; // allow only letters and numbers 
 
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter a password.\n";
    } else if ((fld.value.length < 7) || (fld.value.length > 15)) {
        error = "The password is the wrong length. \n";
        fld.style.background = 'Yellow';
    } else if (illegalChars.test(fld.value)) {
        error = "The password contains illegal characters.\n";
        fld.style.background = 'Yellow';
    } else if (!((fld.value.search(/(a-z)+/)) && (fld.value.search(/(0-9)+/)))) {
        error = "The password must contain at least one numeral.\n";
        fld.style.background = 'Yellow';
    } else {
        fld.style.background = 'White';
    }
   return error;
}   
/* email */
function trim(s)
{
  return s.replace(/^\s+|\s+$/, '');
}

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "Het email adres is niet ingevuld.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "Vul aub een geldig email adres in.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "Het email adres bevat ongeldige karakters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}
/* phone */
function validatePhone(fld) {
    var error = "";
    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');    

   if (fld.value == "") {
        error = "Er is geen telefoonnummer ingevuld.\n";
        fld.style.background = 'Yellow';
    } else if (isNaN(parseInt(stripped))) {
        error = "Het telefoonnummer bevat ongeldige tekens.\n";
        fld.style.background = 'Yellow';
    } else if (!(stripped.length == 10)) {
        error = "Het telefoonnummer heeft een ongeldig aantal tekens. Voer ook het netnummer in.\n";
        fld.style.background = 'Yellow';
    }
    return error;
}
