var index = 0;
var loadinterval = 0;
var page_size = 0;
var current_portal_id = 0;
var current_gallery_id = 0;
var current_timefilter = 0;
var current_photo_index = 0;
var idealwindow = 0;

function SetOrderQTY(index, qty){		LoadElement("http://www.masterphoto.nl/bestellijst_set_qty.asp?index="+ index +"&qty="+ qty, "productlijst", "ShowTeBetalen();"); }
function SetOrderProduct(index, product_id){	LoadElement("http://www.masterphoto.nl/bestellijst_set_product_id.asp?index="+ index +"&product_id="+ product_id, "productlijst", "ShowTeBetalen();"); }
function CloneOrder(index){			LoadElement("http://www.masterphoto.nl/bestellijst_clone.asp?index="+ index, "productlijst", "ShowTeBetalen();"); }
function DelOrder(index){			LoadElement("http://www.masterphoto.nl/bestellijst_del.asp?index="+ index, "productlijst", "ShowTeBetalen();"); }
function ShowTeBetalen(){			LoadElement("http://www.masterphoto.nl/bestellijst_te_betalen.asp", "te_betalen"); }
function ShowOrderlist(){ 			LoadElement("http://www.masterphoto.nl/bestellijst_productlijst.asp", "productlijst", "ShowTeBetalen();"); }
function AddOrder(){				ShowOverlay(true); LoadElement("/bestellijst_add.asp?gallery_id="+ current_gallery_id +"&timefilter="+ current_timefilter +"&photo_index="+ current_photo_index, "overlay", "ShowOrderlist();"); }
function HideOrders(){				ShowOverlay(false); LockOrderview(false); }
function ShowOrders(){				ShowOverlay(true); LoadElement("http://www.masterphoto.nl/bestellijst.asp", "overlay", "ShowOrderlist();"); }
function CancelOrder(){				LoadElement("http://www.masterphoto.nl/bestellijst_wissen.asp", "overlay", "HideOrders();"); }

function FinishOrder(result){
	LoadElement("http://www.masterphoto.nl/bestelling_verwerkt.asp?result="+ Url.encode(result), "bestelbon");
	idealwindow.close();	
}

function StartiDeal(){
	idealwindow = window.open("about:blank", "iDealWindow");

	form = document.getElementById('form1');
	form.action = "https://ideal.rabobank.nl/ideal/mpiPayInitRabo.do";
	form.target = "iDealWindow";

	idealwindow.focus();
	form.submit();
}

function Bestelbon(){

	var result = "";

	var voornaam = document.getElementById("voornaam").value;
	var achternaam = document.getElementById("achternaam").value;
	var adres = document.getElementById("adres").value;
	var postcode = document.getElementById("postcode").value;
	var woonplaats = document.getElementById("woonplaats").value;
	var email = document.getElementById("email").value;

	if(voornaam.length<2) result += "Geef een geldige voornaam op.<br>";
	if(achternaam.length<2) result += "Geef een geldige achternaam op.<br>";
	if(adres.length<2) result += "Geef een geldig adres op.<br>";
	if(postcode.length<6) result += "Geef een geldige postcode op.<br>";
	if(woonplaats.length<2) result += "Geef een geldige woonplaats op.<br>";
	if(email.length<4) result += "Geef een geldig email adres op.<br>";

	if(voornaam.length<2) document.getElementById("voornaam").style.backgroundColor = "#FF9999"; else document.getElementById("voornaam").style.backgroundColor = "#FFFFFF";
	if(achternaam.length<2) document.getElementById("achternaam").style.backgroundColor = "#FF9999"; else document.getElementById("achternaam").style.backgroundColor = "#FFFFFF";
	if(adres.length<2) document.getElementById("adres").style.backgroundColor = "#FF9999"; else document.getElementById("adres").style.backgroundColor = "#FFFFFF";
	if(postcode.length<6) document.getElementById("postcode").style.backgroundColor = "#FF9999"; else document.getElementById("postcode").style.backgroundColor = "#FFFFFF";
	if(woonplaats.length<2) document.getElementById("woonplaats").style.backgroundColor = "#FF9999"; else document.getElementById("woonplaats").style.backgroundColor = "#FFFFFF";
	if(email.length<4) document.getElementById("email").style.backgroundColor = "#FF9999"; else document.getElementById("email").style.backgroundColor = "#FFFFFF";

	if(result.length == 0){

		LockOrderview(true);

		LoadElement("http://www.masterphoto.nl/bestellijst_bestelbon.asp?voornaam="+ Url.encode(voornaam) +"&achternaam="+ Url.encode(achternaam) +"&adres="+ Url.encode(adres) +"&postcode="+ Url.encode(postcode) +"&woonplaats="+ Url.encode(woonplaats) +"&email="+ Url.encode(email), "bestelbon");

	} else {
	}
}



function AanvraagVerwijderFoto(){
	window.open('mailto:info@masterphoto.nl?subject=Aanvraag verwijderen foto');
}

function ShowOverlay(bool){
	var value = "hidden";
	if(bool)value = "visible";
	document.getElementById("shadow").style.visibility = value;
	document.getElementById("overlay").style.visibility = value;
}

function LockOrderview(bool){
	var value = "hidden";
	if(bool)value = "visible";
	document.getElementById("stap1_lock").style.visibility = value;
	document.getElementById("stap2_lock").style.visibility = value;
}

function Login(){
	var username = document.getElementById("gebruiker").value;
	var password = document.getElementById("wachtwoord").value;
	LoadElement("http://www.masterphoto.nl/galleryview_login.asp?gallery_id="+ current_gallery_id +"&username="+ username +"&password="+ password, "portalview");
}

function GetCurrentPage(){
	return document.getElementById("pageselect").selectedIndex;
}

function GratisDownloaden(){
	window.location='http://www.masterphoto.nl/PhotoIndex.asp?gallery_id='+ current_gallery_id +'&timefilter='+ current_timefilter +'&photo_index='+ current_photo_index +'&photo_format=fullsize';
}

function EmailDownloaden(){
	showEmailForward();
}


function sendForward(forward_email){

	// check email adres validity

	if(!emailCheck(forward_email))return;

	div = document.getElementById('email_forward');

	html = '<br><div style="font: bold 14px Arial; color: #FFC904;">GRATIS FOTO DOWNLOAD</div>';
	html += '<br><div style="font: 12px Arial;">';

	html += 'Bezig met versturen...<br>';

	div.innerHTML = html;
//	alert("http://www.masterphoto.nl/send_photo.asp?forward_email="+ Url.encode(forward_email) +"&gallery_id="+ current_gallery_id +"&timefilter="+ current_timefilter +"&photo_index="+ current_photo_index);

	var url = "http://www.masterphoto.nl/send_photo.asp?forward_email="+ Url.encode(forward_email) +"&gallery_id="+ current_gallery_id +"&timefilter="+ current_timefilter +"&photo_index="+ current_photo_index;

	SendHTTP(url, "finishEmailForward()");
}

function finishEmailForward(){
	var lastresponse = getLastResponse();

	div = document.getElementById('email_forward');

	html = '<br><div style="font: bold 14px Arial; color: #FFC904;">GRATIS FOTO DOWNLOAD</div>';
	html += '<br><div style="font: 12px Arial;">';

//	html += lastresult;

	html += '<br><br>Klik op OK om dit venster te sluiten.<br><br>';
	html += '<input type="button" style="cursor: pointer; border: 2px #FFC904 solid; background-color: #FFC904; color: black;"';
	html += 'OnClick="document.getElementById(\'shadow\').style.visibility=\'hidden\'; ';
	html += 'document.getElementById(\'email_forward\').style.visibility=\'hidden\';" value="OK">';

	div.innerHTML = html;
}

function showEmailForward(){

	div = document.getElementById("email_forward");

	html = '<br>'
	html += '<div style="font: bold 14px Arial; color: #FFC904;">GRATIS FOTO DOWNLOAD</div>'
	html += '<br>'
	html += '<div style="font: 12px Arial;">'
	html += 'Vul je e-mail in en we sturen je de foto<br>'
	html += 'gratis toe!<br>'
	html += 'Je e-mail wordt niet gebruikt voor SPAM*.<br>'
	html += '<br>'
	html += '<input type="input" id="email_forward_value" style="width: 270px; border: 2px white solid; text-align: center;"><br><br>'
	html += '<input type="button" style="cursor: pointer; border: 2px #FFC904 solid; background-color: #FFC904; color: black;" Value="Annuleren" OnClick="document.getElementById(\'shadow\').style.visibility=\'hidden\'; document.getElementById(\'email_forward\').style.visibility=\'hidden\';">&nbsp;'
	html += '<input type="button" style="cursor: pointer; border: 2px #FFC904 solid; background-color: #FFC904; color: black;" value="Verstuur" OnClick="sendForward(document.getElementById(\'email_forward_value\').value);"><br>'
	html += '<br>'
	html += '</div>'

	html += '<div style="font: 10px Arial;">'
	html += '*Je mail adres zal niet aan derden worden doorverkocht. Je machtigt alleen bovenstaande<br>'
	html += 'adverteerder eenmalig een mailtje te sturen.'
	html += '</div>'

	div.innerHTML = html;
	div.style.visibility = "visible";

	document.getElementById("shadow").style.visibility = "visible";

}


function HideGallery(){
	document.getElementById("portalview").innerHTML = "";
	document.getElementById("portalview").style.visibility = "hidden";
}

function Resize(element_name){

	element = document.getElementById(element_name);

	var screen_x = window.innerWidth;
	var screen_y = window.innerHeight;

	if(screen_x == undefined){
		var screen_x = document.body.offsetWidth;
		var screen_y = document.body.offsetHeight;
	}

	var portal_width = 795;
	var portal_height = 632;

	element.style.left = 0.5 * (screen_x - portal_width);
	element.style.top  = 0.5 * (screen_y - portal_height);
}

function LoadPortal(portal_id, element_name){

	current_portal_id = portal_id;
	current_gallery_id = 0;
	current_timefilter = 0;
	current_photo_index = 0;

	Resize(element_name);

	element = document.getElementById(element_name);
	element.style.visibility = "visible";

	LoadElement("http://www.masterphoto.nl/portalview.asp?portal_id="+ portal_id, element_name);
}

function LoadGallery(gallery_id, timefilter, element_name){
	current_gallery_id = gallery_id;
	current_timefilter = timefilter;
	current_photo_index = 0;

	Resize(element_name);

	element = document.getElementById(element_name);
	element.style.visibility = "visible";

	LoadElement("http://www.masterphoto.nl/galleryview.asp?gallery_id="+ gallery_id +"&timefilter="+ timefilter, element_name);
}

function PreviewPhoto(index){
	current_photo_index = index;
	document.getElementById("photo_preview").style.backgroundImage = "url('http://www.masterphoto.nl/PhotoIndex.asp?gallery_id="+ current_gallery_id +"&timefilter="+ current_timefilter +"&photo_index="+ index +"&photo_format=medium')";
}

function UpdateMatrix(pagesize){

	var element = document.getElementById("thumbnail_0");
	var image = new Array();
	image.length = 0;
	if(loadinterval != 0) window.clearInterval(loadinterval);

	loadinterval = window.setInterval(function(){
		var finished = true;
		for(var j=0; j<image.length; j++)
			if(!image[j].complete){
				finished = false;
			} else {
				if(image[j].width > 0) document.getElementById("thumbnail_"+ j).style.visibility = "visible";
			}
		if(finished) window.clearInterval(loadinterval);
	}, 100);

	var i = 0;
	while(element){
		// document.getElementById("thumbnail_"+ i).style.visibility = "visible";

		var newimage = "http://www.masterphoto.nl/PhotoIndex.asp?gallery_id="+ current_gallery_id +"&photo_index="+ (i + (pagesize*page)) +"&photo_format=thumb";

		element.style.visibility = "hidden";

		image[i] = new Image();
		image[i].src = newimage;

		document.getElementById("thumbnail_"+ i).style.backgroundImage = "url('"+ newimage +"')";
		i ++;
		element = document.getElementById("thumbnail_"+ i);		
	}

}

function showImage(index, v){
	alert(index +" goes to "+ v);
	document.getElementById("thumbnail_"+ index).style.visibility = v;
}

function SetPage(new_page, pagesize){
	page = new_page;

	if(page < 0) page = 0;
	if(page > document.getElementById("pageselect").childNodes.length-1) page = document.getElementById("pageselect").childNodes.length-1;

	// Enable disable buttons en zo
	if(page == 0) document.getElementById("navigate_previous").style.visibility = "hidden";
	else document.getElementById("navigate_previous").style.visibility = "visible";
	if(page == document.getElementById("pageselect").childNodes.length-1)  document.getElementById("navigate_next").style.visibility = "hidden";
	else document.getElementById("navigate_next").style.visibility = "visible";

	document.getElementById("pageselect").selectedIndex = page;

	UpdateMatrix(pagesize);
}

function dhtmlLoadScript(url){
	var e = document.createElement("script");
	e.src = url;
	e.type="text/javascript";
	document.getElementsByTagName("head")[0].appendChild(e);
}

// onload = function(){
//   dhtmlLoadScript("dhtml_way.js");
//}


/**
*
*  URL encode / decode
*  http://www.webtoolkit.info/
*
**/

var Url = {

    // public method for url encoding
    encode : function (string) {
        return escape(this._utf8_encode(string));
    },

    // public method for url decoding
    decode : function (string) {
        return this._utf8_decode(unescape(string));
    },

    // private method for UTF-8 encoding
    _utf8_encode : function (string) {
        string = string.replace(/\r\n/g,"\n");
        var utftext = "";

        for (var n = 0; n < string.length; n++) {

            var c = string.charCodeAt(n);

            if (c < 128) {
                utftext += String.fromCharCode(c);
            }
            else if((c > 127) && (c < 2048)) {
                utftext += String.fromCharCode((c >> 6) | 192);
                utftext += String.fromCharCode((c & 63) | 128);
            }
            else {
                utftext += String.fromCharCode((c >> 12) | 224);
                utftext += String.fromCharCode(((c >> 6) & 63) | 128);
                utftext += String.fromCharCode((c & 63) | 128);
            }

        }

        return utftext;
    },

    // private method for UTF-8 decoding
    _utf8_decode : function (utftext) {
        var string = "";
        var i = 0;
        var c = c1 = c2 = 0;

        while ( i < utftext.length ) {

            c = utftext.charCodeAt(i);

            if (c < 128) {
                string += String.fromCharCode(c);
                i++;
            }
            else if((c > 191) && (c < 224)) {
                c2 = utftext.charCodeAt(i+1);
                string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
                i += 2;
            }
            else {
                c2 = utftext.charCodeAt(i+1);
                c3 = utftext.charCodeAt(i+2);
                string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
                i += 3;
            }

        }

        return string;
    }

}

function emailCheck(emailStr){

	var checkTLD=1;
	var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
	var emailPat=/^(.+)@(.+)$/;
	var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
	var validChars="\[^\\s" + specialChars + "\]";
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
	var atom=validChars + '+';
	var word="(" + atom + ")";
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");
	var matchArray=emailStr.match(emailPat);

	if (matchArray==null) {
		alert("Email is incorrect (controleer de @ en de .'s)");
		return false;
	}

	var user =matchArray[1];
	var domain=matchArray[2];

	for (i=0; i < user.length ; i++) {
		if (user.charCodeAt(i) > 127) {
			alert("De gebruikersnaam bevat ongeldige tekens.");
			return false;
	   }
	}

	for (i=0; i < domain.length; i++) {
		if (domain.charCodeAt(i) > 127) {
			alert("De domeinnaam bevat ongeldige tekens.");
			return false;
	   }
	}

	var IPArray=domain.match(ipDomainPat);
	if (IPArray!=null) {

		for (var i=1;i<=4;i++) {
		    var te = IPArray[i];
			if ( te > 255) {
				alert("Bestemmings IP adres is incorrect.");
				return false;
			}
		}
	}

	var atomPat=new RegExp("^" + atom + "$");
	var domArr=domain.split(".");
	var len=domArr.length;

	if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) {
		alert("Het adres moet eindigen met een bekend domein of een twee letter landcode.");
		return false;
	}

	if (len<2) {
		alert(" Het adres mist een hostnaam.");
		return false;
	}

	return true;
}


