// Code for window popups

var popupStatus=0;

$(function(){  
	$("#enewsletter").click(function(){  
		centerPopup($('#enewsletterpopup'));  
		loadPopup($('#enewsletterpopup'));  
	});
	
	$("#contactus").click(function(){  
		centerPopup($('#contactuspopup'));  
		loadPopup($('#contactuspopup'));  
	}); 

	$("#sendtoafriend").click(function(){  
		centerPopup($('#sendtoafriendpopup'));  
		loadPopup($('#sendtoafriendpopup'));  
	}); 
	
	$("#buyimages").click(function(){  
		centerPopup($('#buyimagespopup'));  
		loadPopup($('#buyimagespopup'));  
	});
	
	$("#compareyachts").click(function(){  
		centerPopup($('#compareyachtspopup'));  
		loadPopup($('#compareyachtspopup'));  
	});
	
	
	//CLOSING POPUP  
	//Click the x event!  
	$(".popupclose").click(function(){  
		disablePopup();  
	});
	
	//Click out event!  
	$("#backgroundpopup").click(function(){  
		disablePopup();  
	});
	  
	//Press Escape event
	$(document).keypress(function(e){  
		if(e.keyCode==27 && popupStatus==1){  
			disablePopup();  
		}  
	});

	// Restrict to alphanumeric characters
	$('#emailnewslettersignup input[type=text]').alphanumeric({allow:".,@ "});

	// Set up form prompts
	$('#emailnewslettersignup input[name=firstname]').form_prompt('First name*');  
	$('#emailnewslettersignup input[name=lastname]').form_prompt('Last name*');  
	$('#emailnewslettersignup input[name=company]').form_prompt('Company/Yacht name*');  
	$('#emailnewslettersignup input[name=position]').form_prompt('Title/Position');  
	$('#emailnewslettersignup input[name=telephone]').form_prompt('Phone number');  
	$('#emailnewslettersignup input[name=email]').form_prompt('Email*');  
	
	$('#emailcontactus input[name=firstname]').form_prompt('First name*');  
	$('#emailcontactus input[name=lastname]').form_prompt('Last name*');  
	$('#emailcontactus input[name=company]').form_prompt('Company/Yacht name*');  
	$('#emailcontactus input[name=position]').form_prompt('Title/Position');  
	$('#emailcontactus input[name=telephone]').form_prompt('Phone number');  
	$('#emailcontactus input[name=email]').form_prompt('Email*');  
	$('#emailcontactus textarea').form_prompt('Request');  

	$('#emailsendtoafriend input[name=senderemail]').form_prompt('Email of sender');  
	$('#emailsendtoafriend input[name=recipients]').form_prompt('Emails of recipients (split addresses with a comma)');  
	$('#emailsendtoafriend textarea').form_prompt('Comments');  
	$('#emailsendtoafriend input[name=captcha]').form_prompt('Enter captcha code');  
	
	$('#userlogin input[name=username]').form_prompt('Username');  
	$('#userlogin input[name=password]').form_prompt('Password');
	
	$('#userlogin2 input[name=username]').form_prompt('Username');  
	$('#userlogin2 input[name=password]').form_prompt('Password');  
	
	$('#buyimagesform input[name=firstname]').form_prompt('First name*');  
	$('#buyimagesform input[name=lastname]').form_prompt('Last name*');  
	$('#buyimagesform input[name=company]').form_prompt('Company/Yacht name*');  
	$('#buyimagesform input[name=position]').form_prompt('Title/Position');  
	$('#buyimagesform input[name=telephone]').form_prompt('Phone number');  
	$('#buyimagesform input[name=email]').form_prompt('Email*'); 	
	$('#buyimagesform input[name=captcha]').form_prompt('Enter captcha code');  
	
});  

function loadPopup(popup){  
    // Loads popup only if it is disabled  
    if (popupStatus==0) {  
		$("#backgroundpopup").css({'opacity':'0.7'});  
		$("#backgroundpopup").fadeIn("slow");  
		popup.fadeIn("slow");  
    	popupStatus=1;		
		if ($('#contactuscanvas').is(":visible")) {
			writecaptcha(document.getElementById('contactuscanvas'));	
		}
		if ($('#sendcanvas').is(":visible")) {
			writecaptcha(document.getElementById('sendcanvas'));	
		}
    }  
}  
	
function disablePopup(){  
    // Disables popup only if it is enabled  
    if (popupStatus==1) {  
		$("#backgroundpopup").fadeOut("slow");  
		$(".popup").fadeOut("fast", function() {
			resetPopups();	
		});  
		popupStatus=0;  
    }  
}  
	
//centering popup  
function centerPopup(popup){  
    var windowWidth=document.documentElement.clientWidth;  
    var windowHeight=document.documentElement.clientHeight;  
    var popupHeight=popup.height();  
    var popupWidth=popup.width();  

    //centering  
    popup.css({'top':windowHeight/2-popupHeight/2, 'left':windowWidth/2-popupWidth/2});  
    $("#backgroundpopup").css({'height':windowHeight});  
} 

function resetPopups() {
	// Reset popup windows to their default views
	$('#emailnewslettersignedup').hide();
	$('#emailnewslettersignedupproblem').hide();
	$('#emailnewsletterprocessing').hide();
	$('#newslettersignupcontainer').show();	
	$('#emailnewslettersignup input[type=text]').val("");
	$('#emailnewslettersignup input[type=text]').css('border','1px solid #888888');
	
	$('#emailcontactussuccess').hide();
	$('#emailcontactusproblem').hide();
	$('#emailcontactusprocessing').hide();
	$('#contactuscontainer').show();	
	$('#emailcontactus input[type=text]').val("");
	$('#emailcontactus textarea').val("");
	$('#emailcontactus input[type=text]').css('border','1px solid #888888');
	
	$('#emailsendtoafriendsent').hide();
	$('#emailsendtoafriendproblem').hide();
	$('#emailsendprocessing').hide();
	$('#emailafriendcontainer').show();	
	$('#emailsendtoafriend input[type=text]').val("");
	$('#emailsendtoafriend textarea').val("");
	$('#emailsendtoafriend input[type=text]').css('border','1px solid #888888');
	
	$('#userloginsuccess').hide();
	$('#userloginproblem').hide();
	$('#userloginprocessing').hide();
	$('#userlogincontainer').show();	
	$('#userlogin input[type=text]').val("");
	$('#userlogin input[type=text]').css('border','1px solid #888888');
	
	$('#userlogin2success').hide();
	$('#userlogin2problem').hide();
	$('#userlogin2processing').hide();
	$('#userlogin2container').show();	
	$('#userlogin2 input[type=text]').val("");
	$('#userlogin2 input[type=text]').css('border','1px solid #888888');
	
	$('#buyimagessuccess').hide();
	$('#buyimagesproblem').hide();
	$('#buyimagesprocessing').hide();
	$('#buyimagescontainer').show();	
	$('#buyimagesform input[type=text]').val("");
	$('#buyimagesform input[type=text]').css('border','1px solid #888888');
	
	$('.form-prompt-text').show();
}

function contactussubmit() {
	// Validate contact us form
	var validated=true;
	$('#emailcontactus input[type=text]').css('border','1px solid #888888');
	
	if ($('#emailcontactus input[name=firstname]').val()=="") {
		$('#emailcontactus input[name=firstname]').css('border','1px solid #f00');
		validated=false;
	}
	if ($('#emailcontactus input[name=lastname]').val()=="") {
		$('#emailcontactus input[name=lastname]').css('border','1px solid #f00');
		validated=false;
	}
	if ($('#emailcontactus input[name=company]').val()=="") {
		$('#emailcontactus input[name=company]').css('border','1px solid #f00');
		validated=false;
	}
	if ($('#emailcontactus input[name=email]').val()=="") {
		$('#emailcontactus input[name=email]').css('border','1px solid #f00');
		validated=false;
	}
	if ($('#emailcontactus input[name=captcha]').val()=="") {
		$('#emailcontactus input[name=captcha]').css('border','1px solid #f00');
		validated=false;
	}
	
	if (validated) {
		$('#contactuscontainer').fadeOut("normal", function() {
			$('#emailcontactusprocessing').show();
			var dataString=$('#emailcontactus').serialize();

			$.ajax({
				type: "POST",
				url: "/ajax/emailcontactus.asp",
				data: dataString,
				cache: false,
				async: true,
				success: function(data, textStatus, XMLHttpRequest) {
					if(textStatus=="success" && data=="success") {
						$('#emailcontactusprocessing').hide();
						$('#emailcontactussuccess').fadeIn("slow");
					} else {
						$('#emailcontactusprocessing').hide();
						$('#emailcontactusproblem').fadeIn("slow");
					}
					if(textStatus=="success") {
						if (data=="success") {
							$('#emailcontactusprocessing').hide();
							$('#emailcontactussuccess').fadeIn("slow");
						} else {
							// Captcha error (could check for data="captchaerror" but we'll assume that's what happened
							$('#emailcontactusprocessing').hide();
							$('#emailcontactus input[name=captcha]').css('border','1px solid #f00');
							$('#contactuscontainer').fadeIn("slow");
						}
					} else {
						$('#emailcontactusprocessing').hide();
						$('#emailcontactusproblem').fadeIn("slow");
					}
				},
				error: function() {
					$('#emailcontactusprocessing').hide();
					$('#emailcontactusproblem').fadeIn("slow");
				}
			});
		});
	}
	
	return false;
}

function userloginsubmit() {
	// Validate user login form
	var validated=true;
	$('#userlogin input[type=text]').css('border','1px solid #888888');
	
	if ($('#userlogin input[name=username]').val()=="") {
		$('#userlogin input[name=username]').css('border','1px solid #f00');
		validated=false;
	}
	if ($('#userlogin input[name=password]').val()=="") {
		$('#userlogin input[name=password]').css('border','1px solid #f00');
		validated=false;
	}
	
	if (validated) {
		$('#userlogincontainer').fadeOut("normal", function() {
			$('#userloginprocessing').show();
			var dataString=$('#userlogin').serialize();

			$.ajax({
				type: "POST",
				url: "/ajax/userlogin_popup.asp",
				data: dataString,
				cache: false,
				async: true,
				success: function(data, textStatus, XMLHttpRequest) {
					if(textStatus=="success") {
						if (data=="success") {
							$('#userloginprocessing').hide();
							$('#userloginsuccess').fadeIn("slow");
						} else {
							$('#userloginprocessing').hide();
							$('#userloginproblem').fadeIn("slow");
							$('#userlogincontainer').fadeIn("slow");
						}
					} else {
						$('#userloginprocessing').hide();
						$('#userloginproblem').fadeIn("slow");
					}
				},
				error: function() {
					$('#userloginprocessing').hide();
					$('#userloginproblem').fadeIn("slow");
				}
			});
		});
	}
	
	return false;
}

function userlogin2submit() {
	// Validate user login form
	var validated=true;
	$('#userlogin2problem').fadeOut("fast");
	$('#userlogin2 input[type=text]').css('border','1px solid #888888');
	
	if ($('#userlogin2 input[name=username]').val()=="") {
		$('#userlogin2 input[name=username]').css('border','1px solid #f00');
		validated=false;
	}
	if ($('#userlogin2 input[name=password]').val()=="") {
		$('#userlogin2 input[name=password]').css('border','1px solid #f00');
		validated=false;
	}
	
	if (validated) {
		$('#userlogin2container').fadeOut("normal", function() {
			$('#userlogin2processing').show();
			var dataString=$('#userlogin2').serialize();

			$.ajax({
				type: "POST",
				url: "/ajax/userlogin2_popup.asp",
				data: dataString,
				cache: false,
				async: true,
				success: function(data, textStatus, XMLHttpRequest) {
					if(textStatus=="success") {
						if (data=="success" || data=="success_packageok") {
							$('#userlogin2processing').hide();
							if (data=="success_packageok") {
								$('#userlogin2success_packageok').fadeIn("slow");
							} else {
								$('#userlogin2success').fadeIn("slow");
							}							
						} else {
							$('#userlogin2processing').hide();
							$('#userlogin2problem').fadeIn("slow");
							$('#userlogin2container').fadeIn("slow");
						}
					} else {
						$('#userlogin2processing').hide();
						$('#userlogin2problem').fadeIn("slow");
					}
				},
				error: function() {
					$('#userlogin2processing').hide();
					$('#userlogin2problem').fadeIn("slow");
				}
			});
		});
	}
	
	return false;
}

function buyimagessubmit() {
	// Validate buy images form
	var validated=true;
	$('#buyimagesform input[type=text]').css('border','1px solid #888888');
	
	if ($('#buyimagesform input[name=firstname]').val()=="") {
		$('#buyimagesform input[name=firstname]').css('border','1px solid #f00');
		validated=false;
	}
	if ($('#buyimagesform input[name=lastname]').val()=="") {
		$('#buyimagesform input[name=lastname]').css('border','1px solid #f00');
		validated=false;
	}
	if ($('#buyimagesform input[name=company]').val()=="") {
		$('#buyimagesform input[name=company]').css('border','1px solid #f00');
		validated=false;
	}
	if ($('#buyimagesform input[name=email]').val()=="") {
		$('#buyimagesform input[name=email]').css('border','1px solid #f00');
		validated=false;
	}
	//if ($('#buyimagesform input[name=captcha]').val()=="") {
		//$('#buyimagesform input[name=captcha]').css('border','1px solid #f00');
		//validated=false;
	//}

	if (validated) {
		$('#buyimagescontainer').fadeOut("normal", function() {
			$('#buyimagesprocessing').show();
			var dataString=$('#buyimagesform').serialize();

			$.ajax({
				type: "POST",
				url: "/ajax/emailbuyimages.asp",
				data: dataString,
				cache: false,
				async: true,
				success: function(data, textStatus, XMLHttpRequest) {
					if(textStatus=="success") {
						if (data=="success") {
							$('#buyimagesprocessing').hide();
							$('#buyimagessuccess').fadeIn("slow");
						} else {
							$('#buyimagesprocessing').hide();
							$('#buyimagesproblem').fadeIn("slow");
							$('#buyimagescontainer').fadeIn("slow");
						}
					} else {
						$('#buyimagesprocessing').hide();
						$('#buyimagesproblem').fadeIn("slow");
					}
				},
				error: function() {
					$('#buyimagesprocessing').hide();
					$('#buyimagesproblem').fadeIn("slow");
				}
			});
		});
	}
	
	return false;
}

function writecaptcha(captchacanvas) {
	if (captchacanvas) {
		captchacode=$('#sendcc').html();
		if ($.browser.msie && !supports_canvas) {
			captchacanvas=G_vmlCanvasManager.initElement(captchacanvas);
		}
		if (captchacanvas.getContext) {
			captchagraphics=captchacanvas.getContext('2d');
		}
		captchagraphics.clearRect(0,0,captchagraphics.canvas.width,captchagraphics.canvas.height);
	
		var texttheme=["#222222","#001AFF","#3B00FF","#F7F7F","#3F00FF"];
		var textfont=["Arial","Times New Roman"];
		captchagraphics.font="21px '"+textfont[ (Math.random() * 2 >>  0)]+"'";						
		captchagraphics.strokeStyle='#f00';
		captchagraphics.strokeStyle=texttheme[ (Math.random() * 5 >> 0)];
		//if ($.browser.msie)
		//	captchagraphics.strokeText(captchacode, 5, 5);
		//else
			captchagraphics.strokeText(captchacode, 5, 21);
	}
}
function supports_canvas() {
	return !! document.createElement('canvas').getContext
}
