// ---------------------------------------------------------------------
// JAVASCRIPT	: Kamper GUI, enquete
// REVISION		: 
//		v1.00 - 03jun2011
// ---------------------------------------------------------------------

//this function includes all necessary js files for the application
function include(file) {
	var script  = document.createElement('script');
	script.src  = file;
	script.type = 'text/javascript';
	script.defer = true;
	document.getElementsByTagName('head').item(0).appendChild(script);
} // include()

include("/SpryAssets/xpath.js");
include("/SpryAssets/SpryData.js");

var enqID = 20110605; 	// Start date of the enqute, YYYYMMDD
var daysActive = 30;		// Aantal dagen enquete is actief

function validateEnquete() {
	// return; // Kill enquete
	var start		= new Date();
	start.setFullYear( String(enqID).substr(0,4), Number(String(enqID).substr(4,2))-1, String(enqID).substr(6,2) );
	var now			= new Date();
	var future	= new Date( start.getTime() + (daysActive * 24 * 60 * 60 * 1000) );
	//alert(start+" \n "+now+" \n "+future);
	if ( Number(now.format("Ymd")) <= Number(future.format("Ymd")) ) {
		// Enquete is nog actief, pak cookie info
		var enquete = getCookie ( "_edp" );
		if ( enquete!=null && enquete!="" ) {
			// Cookie bestaat, check enquete ID
			// alert(Number(enquete)+"-"+enqID);
			if ( Number(enquete) != enqID ) {
				// Het betreft een nieuwe enquete. Open de enquete.
				window.document.getElementById("enqueteWrapper").className = "";
			} // EnIf, nieuwe enquete
		} else {
			// Cookie bestaat niet of is gewist. Open de enquete.
			window.document.getElementById("enqueteWrapper").className = "";
		} // EndIf
	} // EndIf 
} // validateEnquete() 

function cancelEnquete() {
	setCookie ( "_edp", enqID, (daysActive+2) );
	window.document.getElementById("enqueteWrapper").className = "nodisplay";
} // cancelEnquete()

function formSendData(form) {
	var theForm = typeof form != 'object' ? document.getElementById(form):form;
//	var ret = Spry.Widget.Form.validate(theForm);
	var ret = true;
	
	if (ret) {
		var radio1 = window.document.getElementsByName('vraag1');
		var radio2 = window.document.getElementsByName('vraag2');
		var radio3 = window.document.getElementsByName('vraag3');
		var radioVal1 = radio1[0].checked?radio1[0].value: radio1[1].checked?radio1[1].value:"-"; 
		var radioVal2 = radio2[0].checked?radio2[0].value: radio2[1].checked?radio2[1].value:"-"; 
		var radioVal3 = radio3[0].checked?radio3[0].value: radio3[1].checked?radio3[1].value:"-"; 
		var formData =  "vraag1="+encodeURIComponent(radioVal1);
		formData 		+= "&vraag2="+encodeURIComponent(radioVal2);
		formData 		+= "&vraag3="+encodeURIComponent(radioVal3);
		// alert(formData);
   	Spry.Utils.loadURL('POST', "/cms_db_enquete.php", true, afterSentData, {postData: formData, headers: {"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8"}});
	} // EndIf
	
	return false;
} // formSendData()

function afterSentData(request) {
	var result = eval('('+request.xhRequest.responseText+')');			// This result will be of the type JSON
	switch ( result['status'] ) {
		case "OK" :
			setCookie ( "_edp", enqID, 30 );
			break;
		default:
			// een fout, probeer het later nog eens.
			break;
	} // EndSwitch()
	window.document.getElementById("enqueteWrapper").className = "nodisplay";
} // afterSentData()

function getCookie(c_name) {
	var i,x,y,ARRcookies=document.cookie.split(";");
	for (i=0;i<ARRcookies.length;i++) {
	  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
	  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  	x=x.replace(/^\s+|\s+$/g,"");
  	if (x==c_name) {
	    return unescape(y);
    } // EndIf
  } // EndFor
} // getCookie()

function setCookie(c_name,value,exdays) {
	var exdate=new Date();
	exdate.setDate(exdate.getDate() + exdays);
	var c_value=escape( String(value) ) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
	document.cookie=c_name + "=" + c_value;
	// alert(c_name + "=" + c_value);
} // setCookie()

Date.prototype.format = function(format) {
	var returnStr = '';
	var replace = Date.replaceChars;
	for (var i = 0; i < format.length; i++) {
		var curChar = format.charAt(i);
		if (replace[curChar]) {
			returnStr += replace[curChar].call(this);
		} else {
			returnStr += curChar;
		}
	}
	return returnStr;
};

Date.replaceChars = {
	shortMonths: ['jan.','feb.','mrt.','apr.','mei.','jun.','jul.','aug.','sep.','okt.','nov.','dec.'],
	longMonths: ['januari','februari','maart','april','mei','juni','juli','augustus','september','oktober','november','december'],
	shortDays: ['zo.', 'ma.', 'di.', 'wo.', 'do.', 'vr.', 'za.'],
	longDays: ['zondag', 'maandag', 'dinsdag', 'woensdag', 'donderdag', 'vrijdag', 'zaterdag'],
	
	// Day
	d: function() { return (this.getDate() < 10 ? '0' : '') + this.getDate(); },
	D: function() { return Date.replaceChars.shortDays[this.getDay()]; },
	j: function() { return this.getDate(); },
	l: function() { return Date.replaceChars.longDays[this.getDay()]; },
	N: function() { return this.getDay() + 1; },
	S: function() { return (this.getDate() % 10 == 1 && this.getDate() != 11 ? 'st' : (this.getDate() % 10 == 2 && this.getDate() != 12 ? 'nd' : (this.getDate() % 10 == 3 && this.getDate() != 13 ? 'rd' : 'th'))); },
	w: function() { return this.getDay(); },
	z: function() { return "Not Yet Supported"; },
	// Week
	W: function() { return "Not Yet Supported"; },
	// Month
	F: function() { return Date.replaceChars.longMonths[this.getMonth()]; },
	m: function() { return (this.getMonth() < 9 ? '0' : '') + (this.getMonth() + 1); },
	M: function() { return Date.replaceChars.shortMonths[this.getMonth()]; },
	n: function() { return this.getMonth() + 1; },
	t: function() { return "Not Yet Supported"; },
	// Year
	L: function() { return (((this.getFullYear()%4==0)&&(this.getFullYear()%100 != 0)) || (this.getFullYear()%400==0)) ? '1' : '0'; },
	o: function() { return "Not Supported"; },
	Y: function() { return this.getFullYear(); },
	y: function() { return ('' + this.getFullYear()).substr(2); },
	// Time
	a: function() { return this.getHours() < 12 ? 'am' : 'pm'; },
	A: function() { return this.getHours() < 12 ? 'AM' : 'PM'; },
	B: function() { return "Not Yet Supported"; },
	g: function() { return this.getHours() % 12 || 12; },
	G: function() { return this.getHours(); },
	h: function() { return ((this.getHours() % 12 || 12) < 10 ? '0' : '') + (this.getHours() % 12 || 12); },
	H: function() { return (this.getHours() < 10 ? '0' : '') + this.getHours(); },
	i: function() { return (this.getMinutes() < 10 ? '0' : '') + this.getMinutes(); },
	s: function() { return (this.getSeconds() < 10 ? '0' : '') + this.getSeconds(); },
	// Timezone
	e: function() { return "Not Yet Supported"; },
	I: function() { return "Not Supported"; },
	O: function() { return (-this.getTimezoneOffset() < 0 ? '-' : '+') + (Math.abs(this.getTimezoneOffset() / 60) < 10 ? '0' : '') + (Math.abs(this.getTimezoneOffset() / 60)) + '00'; },
	P: function() { return (-this.getTimezoneOffset() < 0 ? '-' : '+') + (Math.abs(this.getTimezoneOffset() / 60) < 10 ? '0' : '') + (Math.abs(this.getTimezoneOffset() / 60)) + ':' + (Math.abs(this.getTimezoneOffset() % 60) < 10 ? '0' : '') + (Math.abs(this.getTimezoneOffset() % 60)); },
	T: function() { var m = this.getMonth(); this.setMonth(0); var result = this.toTimeString().replace(/^.+ \(?([^\)]+)\)?$/, '$1'); this.setMonth(m); return result;},
	Z: function() { return -this.getTimezoneOffset() * 60; },
	// Full Date/Time
	c: function() { return this.format("Y-m-d") + "T" + this.format("H:i:sP"); },
	r: function() { return this.toString(); },
	U: function() { return this.getTime() / 1000; }
};
