
// SHORTWRITING
var w = window;
var d = document;
var db = d.body;
var da = d.all;

// QUICK BROWSER CHECK [IE, FIREFOX, OPERA]
var ie = (da && !opera) ? true : false;
var firefox = (!da && !opera) ? true : false;
var opera = (da && opera) ? true : false;

// DISABLE BROWSER BACK BUTTON [IE & FIREFOX]
function removeBackButton() {
 var o = parent;
 if (o) o = o.frames["backbutton"];
 if (o) o = o.document;
 if (o) { ow = function(o) { o.open(); o.write("<html><head><scr" + "ipt>window.history.forward(99);</scr" + "ipt></head><body></body></html>"); o.close(); }; ow(o); ow(o); }
}


function init() {
if (parent && parent.frames && parent.frames["mmbcontent"]) {;} else { top.location.href = "http://www.maakmijnboek.nl/"; } // frame check
removeBackButton();
init2();
}

function getLeft(i) {	if (i) { if (i.offsetParent) { return (i.offsetLeft + getLeft(i.offsetParent)); } else { return (i.offsetLeft); } } }
function getTop(i) { if (i) { if (i.offsetParent) { return (i.offsetTop + getTop(i.offsetParent)); } else { return (i.offsetTop); } } }



function updatePageHeight() {
	var mmbframe = parent.document.getElementById("mmbcontent");
	mmbframe.style.height = (document.body.offsetHeight) + "px";
	mmbframe.style.height = (document.body.offsetHeight) + "px"; // just to be sure
}


function showOrHideFlashBoek(tog) {
	var spot = getElementById("flashboekspot"); // if spot, boek is visible on screen
	var flashboek = parent.document.getElementById("flashboek");
	if (!flashboek || !execFlash._obj) return;

	if (tog == "hide") spot = null;

	if (spot) {
		flashboek.style.left = getLeft(spot) + "px"; // set left
		flashboek.style.top = getTop(spot) + "px"; // set top
	} else {
		flashboek.style.left = "770px"; // set left
		flashboek.style.top = "0px"; // set top
		execFlash._obj.style.width = "1px"; // flash movie width
		execFlash._obj.style.height = "1px"; // flash movie height
	}
}


function update_BoekDimensions(args) {
	var spot = getElementById("flashboekspot"); // if spot, boek is visible on screen
	if (!spot || !execFlash._obj) return;

	spot.style.width = args.w + "px"; // set width
	spot.style.height = args.h + "px"; // set height
	execFlash._obj.style.width = args.w + "px"; // flash movie width
	execFlash._obj.style.height = args.h + "px"; // flash movie height
	updatePageHeight();
}


function flipBoekVoorkant() { execFlash.send( { action: "boekFlipVoorkant", boek: boek } ); }
function flipBoekAchterkant() { execFlash.send( { action: "boekFlipAchterkant", boek: boek } ); }
function flipBoekVoorEnAchterkant() { execFlash.send( { action: "boekFlipVoorEnAchterkant", boek: boek } ); }


function set_TabKeuze(id) {
	id = (id.getAttribute ? id.getAttribute("id") : id); // get id or is already an id
	var temp = id.split("_");

	var stap = temp[0];
	var type = temp[1];
	var group = temp[2];

	var saved = boek[stap][group]["tab"];
	var index = (temp[3]) ? temp[3] : (saved ? saved : 1);


	var elnew = (getElementById(id)) ? getElementById(id) : getElementById(id + "_" + saved);
	if (!elnew) return null;

	var elold = getElementById(stap + "_tabs_" + group + "_" + saved);

	if (elold) elold.src = fileToFullPath( { filename: "border_tabs_off.png" } ); // change img source
	if (elnew) elnew.src = fileToFullPath( { filename: "border_tabs_on.png" } ); // change img source

//alert(el.onclick);
elnew.onclick2 = elnew.onclick;
elnew.onclick = null;
elnew.style.cursor = "default";

if (elnew != elold) {
 elold.onclick = elnew.onclick2;
 elold.style.cursor = "pointer";
}

	//content

var ctnew = getElementById(stap + "_content_" + group + "_" + index); // get element reference
	if (!ctnew) return null;

	var ctold = getElementById(stap + "_content_" + group + "_" + saved); // get element reference

	if (ctold) ctold.style.display = "none";
	if (ctnew) ctnew.style.display = "block";

	var lbnew = getElementById(stap + "_labels_" + group + "_" + index); // get element reference
	if (!lbnew) return null;

	var lbold = getElementById(stap + "_labels_" + group + "_" + saved); // get element reference

	if (lbold) lbold.style.color = "#c8c8c8";
	if (lbnew) lbnew.style.color = "#000000";

	boek[stap][group]["tab"] = index; // update change

 updatePageHeight();
}



function set_Keuze(id, ret) {
	var temp = id.split("_");

	var stap = temp[0];
	var tab = temp[1];
	var group = temp[2];

	var saved = boek[stap][tab][group];
	var index = (temp[3]) ? temp[3] : (saved ? saved : 1);

	if (ret != "always" && temp[3] && boek[stap][tab][group] * 1 == index * 1) return null; // no changes

	var elnew = (getElementById(id)) ? getElementById(id) : getElementById(id + "_" + saved);
	if (!elnew) return null;

	var elold = getElementById(stap + "_" + tab + "_" + group + "_" + saved);

	if (elold) elold.src = fileToFullPath( { path: "group/" + group, filename: "_border_uit.png" } ); // change img source
	if (elnew) elnew.src = fileToFullPath( { path: "group/" + group, filename: "_border_aan.png" } ); // change img source

	boek[stap][tab][group] = index * 1; // update change

	return index; // return the new index
}





function set_BestellingBonnetje(args) {
	if (!args) return;
	var bk = boek.stap6.kostprijs;
	var aantal = (args.aantal) ? args.aantal : "??";
	var karakters = (args.karakters) ? args.karakters : "??????";
	var pagina = (args.pagina) ? args.pagina : "???";
	var stukprijs = (args.stukprijs) ? zeroFillAndFloat(args.stukprijs, 2) : "??.??";
	var totaalprijs = (args.totaalprijs) ? zeroFillAndFloat(args.totaalprijs, 2) : "???.??";
	var prijsmetbtw = (args.prijsmetbtw) ? zeroFillAndFloat(args.prijsmetbtw, 2) : "???.??";

	var html = '';
	html+= '<table style="border:2px solid #000;width:98%;background-color:#fff;" cellspacing="8" cellpadding="0">';
	html+= '<tr>';
	html+= '<td align="left" valign="top" style="width:80%;">';
	html+= '<div class="text_big_black">BOEKEN x ' + aantal + ' stuks</div>';
	html+= '<div class="text_smaller_black">PRIJS PER BOEK € ' + stukprijs + '<br />' + karakters + ' KARAKTERS<br />ONGEVEER ' + pagina + ' PAGINA\'S</div>';
	html+= '</td>';
	html+= '<td align="right" valign="top" style="width:20%;padding-left:40px;"><div class="text_big_black">' + totaalprijs + '</div></td>';
	html+= '</tr><tr>';
	html+= '<td align="left" valign="top" style="width:80%;">';
	html+= '<div class="text_big_blue">BEZORGING</div>';
	html+= '</td>';
	html+= '<td align="right" valign="top" style="width:20%;padding-left:40px;"><div class="text_big_blue">GRATIS</div></td>';
	html+= '</tr><tr>';
	html+= '<td align="left" valign="top" style="width:80%;">';
	html+= '<div class="text_big_black" style="padding-top:5px;">TOTAAL INCL. BTW</div>';
	html+= '</td>';
	html+= '<td align="right" valign="top" style="width:20%;padding-left:40px;border-top:2px solid #000;"><div class="text_big_black" style="padding-top:5px;">' + prijsmetbtw + '</div></td>';
	html+= '</tr>';
	html+= '</table>';

	bk.stukprijs = stukprijs;
	bk.prijsmetbtw = prijsmetbtw; // for step 7
	var bonnetje = getElementById("stap6_kostprijs_bonnetje");
	bonnetje.innerHTML = html;
}



function focusNext(elemName, evt, func) {
	var evt = (evt) ? evt : event;
	var charCode = (evt.charCode) ? evt.charCode : ((evt.which) ? evt.which : evt.keyCode);
	if (charCode == 13 || charCode == 3) {
		var elm = getElementById(elemName);
		//elm.setAttribute('autocomplete', 'off'); // firefox focus() bug fix, not working (so now hardcoded in html)
		elm.focus();
		if (func) func(); // if function, execute it
		return false;
	}
	return true;
}



function uploadImage(args) {
	var htmlsrc = args.htmlsrc;	if (!htmlsrc) return;
	var er = getElementById(htmlsrc + "_error");
	if (er) er.style.display = "none"; // hide error message, if any

	execFlash.send( { action: "uploadImage", boek: boek, htmlsrc: htmlsrc, type: args.type } );
}


function cancel_Upload(args) {
	var htmlsrc = args.htmlsrc;	if (!htmlsrc) return;

	var bt = getElementById(htmlsrc + "_button");
	var pb = getElementById(htmlsrc + "_progressbar");
	var er = getElementById(htmlsrc + "_error");

	if (bt) bt.style.display = "block"; // make button visible again
	if (pb) { pb.style.backgroundColor = "#FF2768"; } // make progressbar red again
	if (er) { er.innerHTML = args.error; er.style.display = "block"; } // show error message

	var bk = getBoekUploadObject(args.type); // boek object ref
	if (bk) { bk.filename = null; bk.filesize = null; } // set attributes to null
}



function update_Upload(args) {
	var bk = getBoekUploadObject(args.type);
	if (args.filename) bk.filename = args.filename;
	if (args.filex) bk.filex = args.filex;
	if (args.filey) bk.filey = args.filey;
	if (args.type != "tekstupload") execFlash.send( { action: "updateAchtergrond", boek: boek } );
}


function update_progressBar(args) {
	if(!getElementById(args.htmlsrc)) return; // no progress bar

	var bk = getBoekUploadObject(args.type);

	var filename = exif_unescape(args.filename);
	var htmlsrc = args.htmlsrc;
	var bytesLoaded = args.bytesLoaded;
	var bytesTotal = args.bytesTotal;
	var sendorget = args.sendorget; // upload or downloading upload
	var procent = 0;

	if (sendorget == "send") procent = Math.floor(bytesLoaded / (bytesTotal / 70));
	if (sendorget == "get") procent = 70 + Math.floor(bytesLoaded / (bytesTotal / 30));
	if (sendorget == "both") procent = Math.floor(bytesLoaded / (bytesTotal / 100));

	if (bk) bk.filesize = bytesTotal;
	update_progressBar2( { filename: filename, htmlsrc: htmlsrc, bytesLoaded: bytesLoaded, bytesTotal: bytesTotal, procent: procent } );
}




function update_progressBar2(args) {
	var bk = getBoekUploadObject(args.type);

	var filename = args.filename;
	var htmlsrc = args.htmlsrc;
	var bytesLoaded = args.bytesLoaded;
	var bytesTotal = args.bytesTotal;
	var procent = args.procent;

	var fn = getElementById(htmlsrc + "_filename");
	var fs = getElementById(htmlsrc + "_filesize");
	var pb = getElementById(htmlsrc + "_progressbar");
	var bt = getElementById(htmlsrc + "_button");

	if (!bt) return;
	bt.style.display = (bytesLoaded * 1 != bytesTotal * 1) ? "none" : "block"; // if busy, hide button

	fn.innerHTML = (filename.length > 39) ? "..." + filename.substring(filename.length - 35) : filename;
	fs.innerHTML = "(" + bytesLoaded + " van " + bytesTotal + " KB)";

	pb.style.backgroundColor = "#A0D101";
	pb.style.width = ((procent > 100 ? 100 : procent) + "%").toString();
}





function set_UploadVenster(args) {
	if (!args) return;
	if (!args.target) return;

	var bk = getBoekUploadObject(args.type);
	if (!bk) return;

	var target = args.target;
	var onclick = 'uploadImage( { htmlsrc: ' + ("'" + target + "'") + ', type: ' + ("'" + args.type + "'") + ' } )';

	var html = '';
	html+= '<table style="border:2px solid #000;width:98%;margin-top:5px;" cellspacing="8" cellpadding="0">';
	html+= '<tr>';
	html+= '<td valign="top">';
	html+= '<div align="center" class="text_smaller_black" style="margin-bottom:2px;"><span id="' + target + '_filename">geen bestand</span></div>';
	html+= '<div align="center" class="text_smaller_black" style="margin-bottom:5px;"><span id="' + target + '_filesize">(0 KB)</span></div>';
	html+= '<div id="' + target + '_progressbar" style="width:100%;background-color:#FF2768;margin-bottom:8px;"><img style="width:100%;height:6px;" src="img/empty.gif" /></div>';
	html+= '<div align="center"><img src="./img/button_browse.png" id="' + target + '_button" onclick="' + onclick + '" style="cursor:pointer;" alt="Klik hier om een eigen bestand te uploaden" /></div>';
	html+= '<div id="' + target + '_error" align="center" style="display:none;margin-top:5px;" class="text_big_red">BESTAND UPLOAD MISLUKT.</div>';
	html+= '</td>';
	html+= '</tr>';
	html+= '</table>';

	var venster = getElementById(target);
	if (!venster) return;
	venster.innerHTML = html;

	if (bk.filename) update_progressBar2( { filename: bk.filename, htmlsrc: target, type: args.type, bytesLoaded: bk.filesize, bytesTotal: bk.filesize, procent: 100 } );
}







function maakMijnBoek() { var x = 1; return false; } // dummy function (for href='....')


function mRepl(txt, str, wit) { return txt.split(str).join(wit); } // multi replace


function removeNewLines(_s) {
  var _t = "";
  for (_i=0;_i<_s.length;_i++) { if (_s.charCodeAt(_i) != 13 && _s.charCodeAt(_i) != 10) _t += _s.charAt(_i); }
  return _t;
}



function exif_unescape(x) { return unescape(decodeURI(x)); }


function exif_receiveExternalData(args) {
	var array = args.exif_data;
	args.exif_data = array.join("");
	if (args.exif_data) args.exif_data = exif_unescape(args.exif_data);
	if (args.exif_onload) window[args.exif_onload](args);
}





function embedFlashOrAlternate(a) {
 // version 1.1 (incl. menu and base properties)
 if (a && a.src && a.target) {
  a.name = (a.name ? a.name : 'flashmovie'); // name property or nothing
  a.id = a.name; // id property (copy of name)
  a.width = (a.width ? ' width="' + a.width + '" ' : ' '); // width tag or nothing
  a.height = (a.height ? ' height="' + a.height + '" ' : ' '); // height tag or nothing
  a.quality = (a.quality ? a.quality : 'HIGH'); // quality, default is HIGH
  a.bgcolor = (a.bgcolor ? a.bgcolor : '#ffffff'); // bgcolor, default is #ffffff
  a.flashvars = (a.flashvars ? a.flashvars : ''); // flashvars, default is ''
  a.wmode = (a.wmode ? a.wmode : 'window'); // wmode, default is window
  a.base = (a.base ? a.base : '.'); // base, default is '.' path

  var str = '<object tabindex="-1" id="' + a.id + '" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"' + a.width + a.height
          + 'codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab">'
					+ '<param name="menu" value="false" /><param name="base" value="' + a.base + '" />'
					+ '<param name="movie" value="' + a.src + '" /><param name="quality" value="' + a.quality + '" />'
          + '<param name="bgcolor" value="' + a.bgcolor + '" /><param name="allowScriptAccess" value="sameDomain" />'
          + '<param name="flashvars" value="' + a.flashvars + '" /><param name="wmode" value="' + a.wmode + '" />'
          + '<embed name="' + a.name + '" src="' + a.src + '" quality="' + a.quality + '" menu="false"'
          + ' bgcolor="' + a.bgcolor + '"' + a.width + a.height + ' base="' + a.base + '"'
          + ' flashvars="' + a.flashvars + '" wmode="' + a.wmode + '" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" '
          + 'pluginspage="http://www.macromedia.com/go/getflashplayer"><\/embed><\/object>';

	a.target.innerHTML = str;
}
}



function getElementById(id) { return document.getElementById(id); }


function fileToFullPath(args) {
	if (!args) return null;
	if (!args.filename) return null;
	if (!args.path) args.path = "";
	
	var url = mRepl("./img/" + args.path + "/" + args.filename, "//", "/");
	return url;
}



function gaNaarBackStep(bk) {
 var backstep = bk.backstep;
 var backstep2 = bk.backstep2;
 var backstep3 = bk.backstep3;

 bk.backstep = null;
 bk.backstep2 = null;
 bk.backstep3 = null;

 gaNaarStap(backstep, backstep2, backstep3);
}



function gaNaarStap(stap, stap2, stap3, stap4) {
	showOrHideFlashBoek("hide");
	loadPage({ page: (admin ? "admin" : "maakmijnboek"), step: stap, step2: stap2, step3: stap3, step4: stap4 });
}



function gaNaarLaatsteStap() {
	if (boek.stap6.kostprijs.klaar == 1) { alert("Wij zijn bezig uw bestelling te verwerken!"); return null; }
	if (boek.stap6.kostprijs.klaar == 2) { alert("U heeft al een bestelling gedaan!"); return null; }

	var current_stap = boek.current.stap;
	for (var i = 1; i <= 6; i++) {
		boek.current.stap = i;
		if (check_Stappen() != true) { gaNaarStap(i); return; }
	}

	set_BestellingUitvoeren();
}


function gaNaarBestellingResultaat(args) {
	var id = exif_unescape(args.klantid) * 1;
	boek.stap6.kostprijs.klaar = (id > 0) ? 2 : 0; // was 1 (in uitvoering), nu 2 (klaar) of 0 (niet besteld)
	loadPage({ page: (id > 0 ? "bestellingklaar" : "bestellingnietklaar"), step: null, step2: id });
}

//
function gaNaarStapLogin() {
	execFlash.send( { action: "checkSecureLogin", securecode: (getElementById('loginNaam').value + getElementById('loginPaswoord').value) } );
}

function gaNaarStapLogin2(args) {
	top.secure = args.secure; gaNaarStap('overzicht', 'nieuw', 1);
}


function check_Waarschuwing() {
	if (!boek.current.stap) return;

	var warning = getElementById("stap_warning");
	var warningtext = getElementById("stap_warning_text");

	var bk = boek["stap" + boek.current.stap];
	if (!bk || !bk.waarschuwing) return;
	var tekst = bk.waarschuwing.join("<br /><br />");

	if (tekst && tekst.length > 0) {
		warningtext.innerHTML = tekst;
		warning.style.display = "block";
	} else {
		warningtext.innerHTML = "";
		warning.style.display = "none";
	}
}



function setBackgroundColor(args) {
 if (!args || !args.color) return;
 var obj = (args.id) ? document.getElementById(args.id) : args.object;
 if (!obj) return;
 obj.style.backgroundColor2 = obj.style.backgroundColor;
 obj.style.backgroundColor = args.color;
}

function resetBackgroundColor(args) {
 if (!args) return;
 var obj = (args.id) ? document.getElementById(args.id) : args.object;
 if (!obj || !obj.style.backgroundColor) return;
 obj.style.backgroundColor = obj.style.backgroundColor2;
}



function getObjectFromArrayById(args) {
	if (!args) return null;
	if (!args.id) return null;
	if (!args.array) return null;

	var id = args.id;
	var array = args.array;
	var i, el, x;

	for (i = 0; i < array.length; i++) {
		el = array[i];
		for (x in el) { if (el[x] == id) return el; }
	}
	return null;
}




