String.prototype.trim = function() {
	return (this.replace(/^[\s\xA0]+/, "").replace(/[\s\xA0]+$/, ""));
};

String.prototype.equalsIgnoreCase = function(str) {
	return (str.toLowerCase() == this.toLowerCase());
};

String.prototype.startsWith = function(str) {
	return (str.toLowerCase() == this || this.toLowerCase().match("^"+str.toLowerCase())==str.toLowerCase());
};

String.prototype.endsWith = function(str) {
	return (str.toLowerCase() == this || this.toLowerCase().match(str.toLowerCase()+"$") == str.toLowerCase());
};

function stopRKey(evt) { 
  var evt = (evt) ? evt : ((event) ? event : null); 
  var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null); 
  if ((evt.keyCode == 13) && (node.type=="text"))  {return false;} 
} 

function show_loading_screen() {
	if ($("#cmswiz_popup").length > 0)
		$("#cmswiz_popup").css("display","none");
	if ($("#cmswiz_back").length > 0)
		$("#cmswiz_back").css("display","none");
	
	var z = cw_current_z();
	
	if ($("#loading_back").length == 0)
		$("#body").append( '<div id="loading_back" style="display:block;position:absolute;left:0px;top:0px;background-image:url(\'http://www.faircredits.nl/cmswiz/back.png\');"></div>' );
	if ($("#loading").length == 0)
		$("#body").append( '<div id="loading" style="display:block;position:absolute;"><img src="/img/loading.gif"/></div>' );
	
	var screenwidth = Math.max( 0, document.body.scrollWidth, document.body.offsetWidth, document.documentElement.clientWidth, document.documentElement.scrollWidth, document.documentElement.offsetWidth ); 
	var screenheight = Math.max( 0, document.body.scrollHeight, document.body.offsetHeight, document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight );
	var l = Math.round ( screenwidth / 2 - 60, 0 );
	var t = Math.round ( screenheight / 2 - 125, 0 );
	
	$("#loading_back")
		.css( "width", screenwidth + "px" )
		.css( "height", screenheight + "px" )
		.css( "zIndex", z + 1 )
		.css( "display", "block" );
	$("#loading")
		.css( "left", l + "px" )
		.css( "top", t + "px" )
		.css( "zIndex", z + 2 )
		.css( "display", "block" );
}

function hide_loading_screen() {  
	$("#loading_back").css("display","none");
	$("#loading").css("display","none");
}


function showLoginForm() {
	var html = '' +
		'<form method="post" name="login" action="/" onkeypress="var e = e || event; if (e.keyCode == 27) cw_hidePopup(); if (e.keyCode == 13) document.login.submit();">' +
		'<input type="hidden" name="action" value="login">' +
		'<table cellspacing="0" cellpadding="5" style="width:100%">' +
		'	<tr>' +
		'		<td align="left" width="16"><img src="/img/icons/im-msn.png" width="16" height="16"></td>' +
		'		<td align="left"><a href="subscribe.php">nieuwe account aanmaken</a></td>' +
		'	</tr>' +
		'	<tr>' +
		'		<td align="left" width="16"><img src="/img/icons/stock_mail-receive.png" width="16" height="16"></td>' +
		'		<td align="left"><a href="#" onClick="cw_hidePopup();showSendPassword();">gegevens kwijt?</a></td>' +
		'	</tr>' +
		'</table><br>' +
		'<table cellspacing="0" cellpadding="5" style="width:100%">' +
		'	<tr>' +
		'		<td align="left">Gebruikersnaam:</td>' +
		'		<td align="left"><input type="text" name="username" size="30" style="padding:0px"></td>' +
		'	</tr>' +
		'	<tr>' +
		'		<td align="left">Wachtwoord:</td>' +
		'		<td align="left"><input type="password" name="password" size="30" style="padding:0px"></td>' +
		'	</tr>' +
		'	<tr>' +
		'		<td align="left" colspan="3">' +
		'			<a href=\"#\" class=\"cw_button\" onClick=\"document.login.submit()\"><span>inloggen</span></a>' +
		' 		</td>' +
		'	</tr>' +
		'</table>' +
		'</form>';
	cw_showPopup( html );
}

function showLoginFormGroupware() {
	var html = '' +
		'<form method="post" name="login" action="/groupware/index.php" onkeypress="var e = e || event; if (e.keyCode == 27) cw_hidePopup(); if (e.keyCode == 13) document.login.submit();">' +
		'<input type="hidden" name="action" value="login">' +
		'<table cellspacing="0" cellpadding="5" style="width:100%">' +
		'	<tr>' +
		'		<td align="left">Gebruikersnaam:</td>' +
		'		<td align="left"><input type="text" name="username" size="30" style="padding:0px"></td>' +
		'	</tr>' +
		'	<tr>' +
		'		<td align="left">Wachtwoord:</td>' +
		'		<td align="left"><input type="password" name="password" size="30" style="padding:0px"></td>' +
		'	</tr>' +
		'	<tr>' +
		'		<td align="left" colspan="3">' +
		'			<a href=\"#\" class=\"cw_button\" onClick=\"document.login.submit()\"><span>inloggen</span></a>' +
		' 		</td>' +
		'	</tr>' +
		'</table>' +
		'</form>';
	cw_showPopup( html );
}

function showContactForm( emailaddress ) {
	var html = '' +
		'<form method="post" action="/" name="contact" onkeypress="var e = e || event; if (e.keyCode == 27) cw_hidePopup(); if (e.keyCode == 13 && checkcontactform()) document.contact.submit();">' +
		'<input type="hidden" name="action" value="contact">' +
		'<table cellspacing="0" cellpadding="5">' +
		'	<tr>' +
		'		<td align="left">Naam:</td>' +
		'		<td align="left"><input type="text" id="name" name="name" size="50" style="padding:0px"></td>' +
		'	</tr>' +
		'	<tr>' +
		'		<td align="left">E-mailadres:</td>' +
		'		<td align="left"><input type="text" id="emailaddress" name="emailaddress" value="' + emailaddress + '" size="50" style="padding:0px"></td>' +
		'	</tr>' +
		'	<tr>' +
		'		<td valign="top" align="left">Vraag:</td>' +
		'		<td align="left"><textarea id="question" name="question" rows="6" style="padding:0px;width:100%"></textarea></td>' +
		'	</tr>' +
		'	<tr>' +
		'		<td colspan="2" align="left">' +
		'			<a href=\"#\" class=\"cw_button\" onClick=\"if (checkcontactform()) document.contact.submit()\"><span>verstuur</span></a>' +
		' 		</td>' +
		'	</tr>' +
		'</table>' +
		'</form>';
	cw_showPopup( html );
	document.contact.onkeypress = stopRKey;
}

function showSendPassword() {
	var html = '' +
		'<form method="post" action="/" name="sendpassword" onkeypress="var e = e || event; if (e.keyCode == 27) cw_hidePopup(); if (e.keyCode == 13) document.sendpassword.submit();">' +
		'<input type="hidden" name="action" value="sendpassword">' +
		'<table cellspacing="0" cellpadding="5">' +
		'	<tr>' +
		'		<td align="left" colspan="2">' +
		'			<b class="catheader">Gebruikersnaam of wachtwoord vergeten?</b>' +
		'		</td>' +
		'	</tr>' +
		'	<tr>' +
		'		<td align="left" colspan="2">' +
		'			Vul hieronder je e-mailadres in, en je ontvangt je gebruikersnaam en een nieuw wachtwoord' +
		'		</td>' +
		'	</tr>' +
		'	<tr>' +
		'		<td align="left" style="width:125px;">E-mailadres:</td>' +
		'		<td align="left"><input type="text" name="emailaddress" style="width:100%;padding:0px"></td>' +
		'	</tr>' +
		'	<tr>' +
		'		<td align="left" colspan="2">' +
		'			<a href=\"#\" class=\"cw_button\" onClick=\"document.sendpassword.submit()\"><span>stuur wachtwoord</span></a>' +
		' 		</td>' +
		'	</tr>' +
		'</table>' +
		'</form>';
	cw_showPopup( html );
}

function reloadwindow() {
	window.location.href = window.location.href;
}

function ShowHide(elem) {
	var x = document.getElementById(elem).style;
	x.display = (x.display == 'block') ? 'none' : 'block';
}

function val(i) {
	return document.getElementById(i).value;
}
function delval(i) {
	document.getElementById(i).value = '';
}

function checkcontactform() {
	var errors = '';
	if (val('name').length == 0) errors += '- Vul je naam in\n';
	if (!emailCheck(val('emailaddress'))) errors += '- Vul een correct e-mailadres in\n';
	
	if (errors == '') {
		return true;
	} 
	else {
		alert('Je hebt niet alles (goed) ingevuld!\n\n'+errors);
		return false;
	}
}

function checkaccountform( checkpassword ) {
	if (val('s_password').length > 0) checkpassword = true;
	if (val('s_password_again').length > 0) checkpassword = true;
	var errors = '';
	if (val('s_firstname').length == 0) errors += '- Vul je voornaam in\n';
	if (val('s_lastname').length == 0) errors += '- Vul je achternaam in\n';
	if (val('s_username').length == 0) errors += '- Vul je gebruikersnaam in\n';
	if (val('s_password').length < 6 && checkpassword) errors += '- Vul een wachtwoord in van minstens 6 karakters\n';
	if (val('s_password_again').length == 0 && checkpassword) errors += '- Herhaal wachtwoord\n';
	if (val('s_password') != val('s_password_again') && checkpassword) {
		errors += '- Je hebt niet 2x hetzelfde wachtwoord ingevuld\n';
		delval('s_password');
		delval('s_password_again');
	}
	if (!emailCheck(val('s_emailaddress'))) errors += '- Vul een correct e-mailadres in\n';
	//if (val('s_country').length == 0) errors += '- Vul je land in\n';
	if (val('s_terms') != '1') errors += '- Je dient akkoord te gaan met de algemene voorwaarden\n';
	
	if (errors == '') {
		return true;
	} 
	else {
		alert('Je hebt niet alles (goed) ingevuld!\n\n'+errors);
		return false;
	}
}


var XMLHttpFactories = [
	function () {return new XMLHttpRequest()},
	function () {return new ActiveXObject('Msxml2.XMLHTTP')},
	function () {return new ActiveXObject('Msxml3.XMLHTTP')},
	function () {return new ActiveXObject('Microsoft.XMLHTTP')}
];

function createXMLHTTPObject() {
	var xmlhttp = false;
	for (var i=0;i<XMLHttpFactories.length;i++) {
		try {
			xmlhttp = XMLHttpFactories[i]();
		}
		catch (e) {
			continue;
		}
		break;
	}
	return xmlhttp;
}	

function sendRequest( url, callback ) {
	var req = createXMLHTTPObject();
	if (!req) return;
	req.open('GET',url,true);
	req.send();
	req.onreadystatechange = function () {
		if (req.readyState != 4) 
			return;
		if (req.status != 200 && req.status != 304)
			return;
		callback( req );
	}
}



/* EMAIL VALIDATION SCRIPT */

function emailCheck (emailStr) {
	/* The following pattern is used to check if the entered e-mail address
	   fits the user@domain format.  It also is used to separate the username
	   from the domain. */
	var emailPat=/^(.+)@(.+)$/
	/* The following string represents the pattern for matching all special
	   characters.  We don't want to allow special characters in the address. 
	   These characters include ( ) < > @ , ; : \ " . [ ]    */
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	/* The following string represents the range of characters allowed in a 
	   username or domainname.  It really states which chars aren't allowed. */
	var validChars="\[^\\s" + specialChars + "\]"
	/* The following pattern applies if the "user" is a quoted string (in
	   which case, there are no rules about which characters are allowed
	   and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
	   is a legal e-mail address. */
	var quotedUser="(\"[^\"]*\")"
	/* The following pattern applies for domains that are IP addresses,
	   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
	   e-mail address. NOTE: The square brackets are required. */
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	/* The following string represents an atom (basically a series of
	   non-special characters.) */
	var atom=validChars + '+'
	/* The following string represents one word in the typical username.
	   For example, in john.doe@somewhere.com, john and doe are words.
	   Basically, a word is either an atom or quoted string. */
	var word="(" + atom + "|" + quotedUser + ")"
	// The following pattern describes the structure of the user
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	/* The following pattern describes the structure of a normal symbolic
	   domain, as opposed to ipDomainPat, shown above. */
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	
	
	/* Finally, let's start trying to figure out if the supplied address is
	   valid. */
	
	/* Begin with the coarse pattern to simply break up user@domain into
	   different pieces that are easy to analyze. */
	var matchArray=emailStr.match(emailPat)
	if (matchArray==null) {
	  /* Too many/few @'s or something; basically, this address doesn't
	     even fit the general mould of a valid e-mail address. */
		//alert("Email address seems incorrect (check @ and .'s)")
		return false
	}
	var user=matchArray[1]
	var domain=matchArray[2]
	
	// See if "user" is valid 
	if (user.match(userPat)==null) {
	    // user is not valid
	    //alert("The username doesn't seem to be valid.")
	    return false
	}
	
	/* if the e-mail address is at an IP address (as opposed to a symbolic
	   host name) make sure the IP address is valid. */
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) {
	    // this is an IP address
		  for (var i=1;i<=4;i++) {
		    if (IPArray[i]>255) {
		        //alert("Destination IP address is invalid!")
			return false
		    }
	    }
	    return true
	}
	
	// Domain is symbolic name
	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
		//alert("The domain name doesn't seem to be valid.")
	    return false
	}
	
	/* domain name seems valid, but now make sure that it ends in a
	   three-letter word (like com, edu, gov) or a two-letter word,
	   representing country (uk, nl), and that there's a hostname preceding 
	   the domain or country. 
	   OF EEN VIERLETTER WOORD! zoals info
	   */
	
	/* Now we need to break up the domain to get a count of how many atoms
	   it consists of. */
	var atomPat=new RegExp(atom,"g")
	var domArr=domain.match(atomPat)
	var len=domArr.length
	if (domArr[domArr.length-1].length<2 || 
	    domArr[domArr.length-1].length>4) {
	   // the address must end in a two letter or three letter word. OF EEN VIERLETTER WOORD
	   //alert("The address must end in a three-letter domain, or two letter country.")
	   return false
	}
	
	// Make sure there's a host name preceding the domain.
	if (len<2) {
	   var errStr="This address is missing a hostname!"
	   //alert(errStr)
	   return false
	}
	
	// If we've gotten this far, everything's valid!
	return true;
}


