/*
 * contactable 1.2.1 - jQuery Ajax contact form
 *
 * Copyright (c) 2009 Philip Beel (http://www.theodin.co.uk/)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 *
 * Revision: $Id: jquery.contactable.js 2010-01-18 $
 *
 */
 
 
function getCookie(c_name)
{
	if (document.cookie.length>0)
	  {
	  c_start=document.cookie.indexOf(c_name + "=");
	  if (c_start!=-1)
	    {
	    c_start=c_start + c_name.length+1;
	    c_end=document.cookie.indexOf(";",c_start);
	    if (c_end==-1) c_end=document.cookie.length;
	    return unescape(document.cookie.substring(c_start,c_end));
	    }
	  }
	return "";
}
 
 
//extend the plugin
(function($){

	//define the new for the plugin ans how to call it	
	$.fn.login = function(options) {
		//set default options  
		var defaults = {
			name: 'Name',
			email: 'E-mail address',
			message : 'Message',
			subject : 'Contact me!',
			recievedMsg : '<h2>Thanks for leaving a message</h2>',
			notRecievedMsg : '<h2>Something went wrong...</h2><p>Sorry but your message could not be sent. <br />Make sure you entered a valid e-mail address.</p> <p>Please <a href="#" onclick="window.location.reload();">try again</a>.</p>',
			disclaimer: '* All fields are required',
			hideOnSubmit: true
		};

		//call in the default otions
		var options = $.extend(defaults, options);
		//act upon the element that is passed into the design    
		return this.each(function(options) {
			
			// checken of er al cookie bestaat van username
			var username = getCookie('fvf_username');
			if(username != null && username != '') {
				var init_username = username;
			} else {
				var init_username = '';
			}
			
			// checken of er al cookie bestaat van password
			var password = getCookie('fvf_password');
			if(password != null && password != '') {
				var init_password = password;
			} else {
				var init_password = '';
			}
			
			//construct the form
			var loginform = '<form id="loginForm" action="/ZoneProcess.aspx?ZoneID=51&amp;OID={module_oid}&amp;OTYPE={module_otype}" method="post" name="catseczoneform98004"> \
				<div id="loading"></div> \
				<div id="callback"></div> \
				<div class="holder"> \
					<!-- \
					<select id="selectZone" onchange="changeSecureTarget(this.options[this.selectedIndex].value)"> \
						<option value="51">Ledenzone</option> \
						<option value="2039">RVB</option> \
					</select> \
					--> \
					<label for="SZUsername">Gebruikersnaam</label> \
					<input type="text" maxlength="255" id="SZUsername" name="Username" class="cat_textbox_small" value="'+init_username+'" /> \
					<br /> \
					<label for="SZPassword">Paswoord</label>  \
					<input type="password" maxlength="255" id="SZPassword" name="Password" class="cat_textbox_small" value="" /> \
					<!--<input type="checkbox" id="RememberMe" name="RememberMe" /><label for="RememberMe">Onthouden</label>--> \
					<br /> \
					<input type="submit" value="aanmelden" class="button" /> \
					<br /><br /> \
					<!--<input type="checkbox" id="RememberMe" name="RememberMe" /> Onthouden | --><a href="/paswoord-vergeten">Paswoord vergeten?</a> \
				</div> \
				</form>';
			
			$(this).html('<div id="login_btn"></div>');
			$(this).append(loginform);
			
			//show / hide function
			$('div#login_btn').toggle(function() {
				$('#overlay').css({display: 'block'});
				$(this).animate({"marginLeft": "-=5px"}, "fast"); 
				$('#loginForm').animate({"marginLeft": "-=0px"}, "fast");
				$(this).animate({"marginLeft": "+=367px"}, "slow"); 
				$('#loginForm').animate({"marginLeft": "+=370px"}, "slow"); 
			}, 
			function() {
				$('#loginForm').animate({"marginLeft": "-=370px"}, "slow");
				$(this).animate({"marginLeft": "-=367px"}, "slow").animate({"marginLeft": "+=5px"}, "fast"); 
				$('#overlay').css({display: 'none'});
			});
			
			/*
			//validate the form 
			$("#loginForm").validate({
				//set the rules for the fild names
				rules: {
					name: {
						required: true,
						minlength: 2
					},
					email: {
						required: true,
						email: true
					},
					msg: {
						required: true,
						minlength: 2
					}
				},
				//set messages to appear inline
				messages: {
					name: "",
					email: "",
					msg: ""
				},			
				messages: {
					name: "Please fill out your name",
					email: "Please provide a valid e-mail address",
					comment: "Please provide your message"
				},
				
				submitHandler: function() {
					$('.holder').hide();
					$('#contact h2').hide();
					$('#loading').show();
					$.post('mail.php',{name:$('#name').val(), email:$('#email').val(), msg:$('#msg').val()},
					function(data){
						$('#loading').css({display:'none'}); 
						if(data == 'success') {
							$('#callback').show().append(defaults.recievedMsg);
							if(defaults.hideOnSubmit == true) {
								//hide the tab after successful submition if requested
								$('#loginForm').animate({dummy:1}, 5000).animate({"marginLeft": "-=430px"}, "slow");
								$('div#login_btn').animate({dummy:1}, 5000).animate({"marginLeft": "-=427px"}, "slow").animate({"marginLeft": "+=5px"}, "fast"); 
								$('#overlay').css({display: 'none'});
							}
						}
						else {
							$('#callback').show().append(defaults.notRecievedMsg);
						}
					});		
				}
			});
			*/
		});
	};
})(jQuery);


