var signupstr = "<h1 class='register-title'>Register New User </h1>"+
		"<div id='signupModal'><div class='message' ></div><form id='register' >"+
			"<div class='field'><label for='firstname'>First Name:&nbsp;</label><input type='text' id='firstname' class='required' name='firstname' tabindex='1001' /></div>"+
			"<div class='field'><label for='lastname'>Last Name:&nbsp;</label><input type='text'   id='lastname' class='required' name='lastname' tabindex='1002' /></div>"+
			"<div class='field'><label for='email'>Email Address:&nbsp;</label><input type='text'  id='email' class='required' name='email' tabindex='1003' /></div>"+
			"<div class='field'><label for='signuppasswd'>Password:&nbsp;</label><input type='password'  id='signuppasswd' class='required' name='signuppasswd' tabindex='1004' /></div>"+
			"<div class='field'><label for='tip'>&nbsp;</label><span id='tip' style='padding-left:25px; color:#bbb' >(at least 5 characters)</span></div>"+
			"<div class='field'><label for='passwdR'>Re-type Password:&nbsp;</label><input type='password' id='passwdR' class='required' name='passwdR' tabindex='1005' /></div>"+
			"<div class='field'><label for='phone'>Phone</label><input type='text' id='phone' class='required' name='phone' tabindex='1006' /></div>"+
			"<div class='select' style='display:inline'><input type='radio' id='phonetype' name='phonetype' value='home' tabindex='1007' checked>Home</input><input type='radio' name='phonetype' value='mobile'>Mobile</input>"+
			"</div>"+
			"<br>"+
			"<div class='select' >"+
			"<input type='checkbox' id='accept' name='accept' tabindex='1008' />I accept the <br /><a href=\"javascript:openPrivacyWindow('termspopup.php') \">Terms and conditions</a>"+
			"</div>"+
			"<div class='select'><input  type='checkbox' id='advertising' checked name='advertising' />Yes, I would like to receive periodic discounts and special offers via Email.</div>"+
			"<br />"+
			"<div class='select'><button id='create' type='submit'  class='register-button' "+
						"onclick=\"return validateSignup()\" tabindex='1009'>Create an account</button></div>"+
			"<br/>"+
		"<div class='greydashline' style='margin-top:10px;'><img src='../images/spacer.gif' width='0' height='0' border='0' /></div>"+
		"<h2>Already have an account?<span>&nbsp;&nbsp;<a href='#' style='font-size:10px' onclick=\"$.prompt.goToState('state1')\">Sign In</a></span></h2>"+
		"<div class='greydashline' style='margin-top:0px;'><img src='../images/spacer.gif' width='0' height='0' border='0' /></div>"+
		"<div class='notice'>We respect your privacy. We will NEVER sell or rent your personal information without your consent. To learn more about how may use information we collect to improve your experience, please read our <a href=\"javascript:openPrivacyWindow('termspopup.php') \">Online Privacy Statement</a>.</div>"+
		"</form></div>		";
					   

var loginstr = "<h1 class='contact-title'>Sign In</h1>"+
		"<div id='loginModal'><div class='message' ></div>"+
		"<form id='loginform' name='loginform' >"+
			"<div class='field'><label for='contact-email'>Email:</label><input type='text' id='loginemail' name='loginemail' tabindex='1001' /></div>"+
			"<div class='field'><label for='contact-passwd'>Password:</label><input type='password' id='contact-passwd' class='required' name='passwd' tabindex='1002' /></div><br>"+
			"<div class='link'><span style='padding-left:100px'><a href='#' onclick=\"$.prompt.goToState('state2')\">Forgot your password?</a></span><br /></div>"+
			"<label>&nbsp;</label>"+
			"<br/>"+
		"</form></div>";
		
var forgotstr = "<h1 class='contact-title'>Forgot Your Password?</h1>"+
			"<div id='forgotModal'><div class='message'>Please enter the Email address you use to Sign In below. We will Email a temporary password to that address. Once you have logged in, follow the steps in the Email to create a new password for your account.</div>"+
			"<form><div class='field'><label for='forgotemail'>Email Address:</label><input type='text' id='forgotemail' class='required' name='forgotemail' tabindex='1001' /></div></form>"+
			"<label>&nbsp;</label>"+
			"<br/></div>";
	
					   
function signupprompt(){
			var message = "";
				var temp = {
					state0: {
						html:signupstr,
						buttons: { Cancel: false},
						focus: 1,
						submit: function(v,m,f) {
									if(v) {
										$('#signupModal .message').empty().fadeIn(200);
										$('#signupModal .email').focus();
										if (f.email == "") {
											$('#signupModal .message').html("<div class='error'>Please enter Email</div>")
												.fadeIn(200);
											return false;
										}
										else {
											if ( !validateemail(f.email) ) {
												$('#signupModal .message').html("<div class='error'>Please enter valid Email</div>")
													.fadeIn(200);
												return false;
											}
										}										
										$.ajax({ 
												type: 'POST', 
												url: 'scripts/register.php', 
												data: $('form').serialize(), 
												cache: false, 
												dataType: 'html',
												success: function (data) { 
														if (isNaN(parseInt(data, 10))) { 
																$('#signupModal .message').html("<div class='error'>"+data+"</div>")
																	.fadeIn(200);
																return false;
														}	else {
															window.location.reload();
															return true;
														}
													} // END success:													
												})
												return false;
										
									}
									$.prompt.close()
								}
					},
					state1: {
						html:loginstr,
						buttons: {  Cancel: false, Login: true },
						focus: 1,
						submit: function(v,m,f) {
									if(v) {
										$('#loginModal .message').empty().fadeIn(200);
										$('#loginModal .loginemail').focus();
										if (f.loginemail == "") {
											$('#loginModal .message').html("<div class='error'>Please enter Email</div>")
												.fadeIn(200);
											return false;
										}
										else {
											if ( !validateemail(f.loginemail) ) {
												$('#loginModal .message').html("<div class='error'>Please enter valid Email</div>")
													.fadeIn(200);
												return false;
											}
										}										
										$.ajax({ 
												type: 'POST', 
												url: 'scripts/login.php', 
												data: $('form').serialize(), 
												cache: false, 
												dataType: 'html',
												success: function (data) { 
														if (isNaN(parseInt(data, 10))) { 
																$('#loginModal .message').html("<div class='error'>"+data+"</div>")
																	.fadeIn(200);
																return false;
														}	else {
															window.location.reload();
															return true;
														}
													} // END success:													
												})
												return false;
										
									}
									//$.prompt.close() // removed, closed panel on wrong email
								}
										
					},
					state2: {
						html:forgotstr,
						buttons: { Cancel: false, Submit: true },
						focus: 1,
						submit: function(v,m,f) {
									if(v) {
										$('#forgotModal .message').empty().fadeIn(200);
										$('#forgotModal .forgotemail').focus();
										if (f.loginemail == "") {
											$('#forgotModal .message').html("<div class='error'>Please enter Email</div>")
												.fadeIn(200);
											return false;
										}
										else {
											if ( !validateemail(f.forgotemail) ) {
												$('#forgotModal .message').html("<div class='error'>Please enter valid Email</div>")
													.fadeIn(200);
												return false;
											}
										}										
										$.ajax({ 
												type: 'POST', 
												url: 'scripts/forgot.php', 
												data: $('form').serialize(), 
												cache: false, 
												dataType: 'html',
												success: function (data) { 
														if (isNaN(parseInt(data, 10))) { 
																$('#forgotModal .message').html("<div class='error'>"+data+"</div>")
																	.fadeIn(200);
																return false;
														}	else {
															$.prompt.goToState('state3');
															return false;
														}
													} // END success:													
												})
												return false;
										
									}
									$.prompt.close()
								}
					},
					state3: {
						html: "<div class='message'>Thank you, you password will be sent to your Email address.</div>",
						buttons: {Ok: true},
						focus: 0,
						submit: function(v,m,f) { return true; }
					}
				}
				
				$.prompt(temp, {prefix: 'jqismooth'});
			}

function loginprompt(){
			var message = "";			
				var temp = {
					state0: {						
						html:loginstr,
						buttons: { Cancel: false, Login: true },						
						focus: 1,						
						submit: function(v,m,f) {
									if(v) {
										$('#loginModal .message').empty().fadeIn(200);
										$('#loginform .loginemail').focus();
										if (f.loginemail == "") {
											$('#loginModal .message').html("<div class='error'>Please enter Email</div>")
												.fadeIn(200);
											return false;
										}
										else {
											if ( !validateemail(f.loginemail) ) {
												$('#loginModal .message').html("<div class='error'>Please enter valid Email</div>")
													.fadeIn(200);
												return false;
											}
										}										
										$.ajax({ 
												type: 'POST', 
												url: 'scripts/login.php', 
												data: $('form').serialize(), 
												cache: false, 
												dataType: 'html',
												success: function (data) { 
														if (isNaN(parseInt(data, 10))) { 
																$('#loginModal .message').html("<div class='error'>"+data+"</div>")
																	.fadeIn(200);
																return false;
														}	else {
															window.location.reload();
															return true;
														}
													} // END success:													
												})
												return false;
										
									}
									//$.prompt.close() // removed, closed panel on wrong email
								}
										
					},
					state2: {
						html:forgotstr,
						buttons: { Cancel: false, Submit: true },
						focus: 1,
						submit: function(v,m,f) {
									if(v) {
										$('#forgotModal .message').empty().fadeIn(200);
										if (f.forgotemail == "") {
											$('#forgotModal .message').html("<div class='error'>Please enter Email</div>")
												.fadeIn(200);
											return false;
										}
										else {
											if ( !validateemail(f.forgotemail) ) {
												$('#forgotModal .message').html("<div class='error'>Please enter valid Email</div>")
													.fadeIn(200);
												return false;
											}
										}										
										$.ajax({ 
												type: 'POST', 
												url: 'scripts/forgot.php', 
												data: $('form').serialize(), 
												cache: false, 
												dataType: 'html',
												success: function (data) { 
														if (isNaN(parseInt(data, 10))) { 
																$('#forgotModal .message').html("<div class='error'>"+data+"</div>")
																	.fadeIn(200);
																return false;
														}	else {
															$.prompt.goToState('state3');
															return false;
														}
													} // END success:													
												})
												return false;
										
									}
									$.prompt.close()
								}
					},
					state3: {
						html: "<div class='message'>Thank you, you password will be sent to your Email address.</div>",
						buttons: {Ok: true},
						focus: 0,
						submit: function(v,m,f) { return true; }
					}
				}
				
				$.prompt(temp, { prefix: 'jqismooth' });
				
			}

function validateSignup() {
	message = '';

	var firstname = document.getElementById('firstname');	
	if ( firstname.value.length == 0) {
		message += 'First Name is required.<br /> ';
	}

	var lastname = document.getElementById('lastname');
	if ( lastname.value.length == 0 ) {
		message += 'Last Name is required.<br />';
	}

	var email = document.getElementById('email');
	if ( email.value.length == 0 ) {
		message += 'Email is required.<br /> ';
	} else {
		if (!validateemail(email.value)) {			
			message += 'Email is invalid.<br /> ';
		}
	}

	var passwd = document.getElementById('signuppasswd');
	if ( passwd.value.length == 0) {
			message += 'Password is required. <br />';
	} else {
			if (passwd.value.length < 5) {
					message += 'Password must be at least 5 characters.<br />';
			}
	}

	var passwdR = document.getElementById('passwdR');
	if ( passwdR.value.length == 0 ) {
		message += 'Please re-type your Password. <br />';
	} 
	
	if (passwd.value.length >0 && passwdR.value.length > 0) {
		if ( passwdR.value != passwd.value) {
			message += "The Passwords do not match.<br />";
		}
	}

	var phone = document.getElementById('phone');
	if (phone.value.length == 0) {
		message += 'Please enter a Phone Number. <br />';
	} else {
			if (!validatePhone(phone.value)) {
				message += 'Phone Number is invalid. <br />';
			}
	}

	var accept = document.getElementById('accept');
	if (accept.checked == false) {
		message += "Please accept the Terms and Conditions. <br />";
	}


	if (message.length > 0) {
		$('#signupModal .message').html("<div class='error'>"+message+"</div>").fadeIn(200);
		return false;
	} else {
		$.ajax({ 
				type: 'POST', 
				url: 'scripts/register.php', 
				data: $('form').serialize(), 
				cache: false, 
				dataType: 'html',
				success: function (data) { 
						if (isNaN(parseInt(data, 10))) { 
								$('#signupModal .message').html("<div class='error'>"+data+"</div>")
									.fadeIn(200);
								return false;
						}	else {
							window.location.assign("/welcome.php")
							return true;
						}
					} // END success:													
				})
				return false;
		
	}
	  
}

function validateemail(email) {
			var at = email.lastIndexOf("@");

			// Make sure the at (@) sybmol exists and  
			// it is not the first or last character
			if (at < 1 || (at + 1) === email.length)
				return false;

			// Make sure there aren't multiple periods together
			if (/(\.{2,})/.test(email))
				return false;

			// Break up the local and domain portions
			var local = email.substring(0, at);
			var domain = email.substring(at + 1);

			// Check lengths
			if (local.length < 1 || local.length > 64 || domain.length < 4 || domain.length > 255)
				return false;

			// Make sure local and domain don't start with or end with a period
			if (/(^\.|\.$)/.test(local) || /(^\.|\.$)/.test(domain))
				return false;

			// Check for quoted-string addresses
			// Since almost anything is allowed in a quoted-string address,
			// we're just going to let them go through
			if (!/^"(.+)"$/.test(local)) {
				// It's a dot-string address...check for valid characters
				if (!/^[-a-zA-Z0-9!#$%*\/?|^{}`~&'+=_\.]*$/.test(local))
					return false;
			}

			// Make sure domain contains only valid characters and at least one period
			if (!/^[-a-zA-Z0-9\.]*$/.test(domain) || domain.indexOf(".") === -1)
				return false;	

			return true;
}
function validatePhone(phone) {
		phone = phone.replace(/\s+/g, "");
		return phone.length > 9 &&	phone.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
}
