/*jQuery.fn.log = function (msg) {
	if ($.browser.mozilla) {
		console.log("%s: %o", msg, this);
	}
	return this;
};*/

/*Shadowbox.init({
	skipSetup: true, 
	clearCache: function() {
		each(S.cache, function(obj) {
			if( obj.el) {
				S.lib.removeEvent(obj.el, 'click', handleClick);
				try {
					delete obj.el.shadowboxCacheKey;
				} catch(e) {
					if (obj.el.removeAttribute) {
						obj.el.removeAttribute('shadowboxCacheKey');
					}
				}
			}
		});
		S.cache = [];
	}
});*/

function fetchPage(page, selector) {
	$.ajax({
		type	: "GET",
		url		: "/page.php",
		data	: "page=" + page + "&key=" + key(10000),
		success	: function(html){
			$(selector).empty();
			$(selector).append(html);
		}
	});
}

function key(number) {
	return Math.floor(Math.random() * number);
}

function fetchGallery(width) {
	$.ajax({
		type	: "GET",
		url		: "/gallery.php",
		data	: "width=" + width + "&key=" + key(10000),
		success	: function(html){
			
			$("#gallery").empty();
			$("#gallery").append(html);
			
			Shadowbox.clearCache();
			
			$("#albums").ImageOverlay({
				overlay_text_color: '#fff' 
			});
		}
	});
}

function fetchAlbum(gallery_id, width) {
	$.ajax({
		type	: "GET",
		url		: "/gallery.php",
		data	: "width=" + width + "&gallery_id=" + gallery_id + "&key=" + key(10000),
		success	: function(html){
			
			$("#gallery").empty();
			$("#gallery").append(html);
			
			$("#images").ImageOverlay({
				overlay_text_color: '#fff' 
			});
			
			Shadowbox.setup("#gallery li a", {
				gallery:	"Gallery"
			});
		}
	});
}

function fetchMenu(position, selector) {
	$.ajax({
		type	: "GET",
		url		: "/menu.php",
		data	: "position=" + position + "&key=" + key(10000),
		success	: function(html){
			
			$(selector).empty();
			$(selector).append(html);
			
			if (position == 'top') {
				$("ul.sf-menu").supersubs({ 
					minWidth:    12, 
					maxWidth:    27, 
					extraWidth:  1    
				}).superfish({
					dropShadows : false
				});
			}
			
			if (position == 'left') {
				$("ul.sf-menu").superfish({
					dropShadows : false
				});
			}
			
		}
	});
}

function fetchAdverts(page, position, selector) {
	$.ajax({
		type	: "GET",
		url		: "/adverts.php",
		data	: "page=" + page + "&position=" + position + "&key=" + key(10000),
		success	: function(html){
			$(selector).empty();
			$(selector).append(html);
		}
	});
}

function fetchListing(limit) {
	var selector = '#listing';
	$.ajax({
		type	: "GET",
		url		: "/listing.php",
		data	: "limit=" + limit + "&key=" + key(10000),
		success	: function(html){
			$(selector).empty();
			$(selector).append(html);
			
			Shadowbox.setup(".listing", {
				gallery:	"listing"
			});
			
		}
	});
}

function fetchAgents(limit) {
	var selector = '#agents';
	$.ajax({
		type	: "GET",
		url		: "/agents.php",
		data	: "limit=" + limit + "&key=" + key(10000),
		success	: function(html){
			$(selector).empty();
			$(selector).append(html);

			Shadowbox.setup(".listing", {
				gallery:	"listing"
			});

		}
	});
}

function fetchComments(limit) {
	var selector = '#comments';
	$.ajax({
		type	: "GET",
		url		: "/comments.php",
		data	: "limit=" + limit + "&key=" + key(10000),
		success	: function(html){
			$(selector).empty();
			$(selector).append(html);
		}
	});
}

function fetchLogin(selector) {
	$.ajax({
		type	: "GET",
		url		: "/login.php",
		data	: "key=" + key(10000),
		success	: function(html){
			$(selector).empty();
			$(selector).append(html);
		}
	});
}

function fetchSlideshow(name, width, height, selector) {
	
	$.ajax({
		type	: "GET",
		url		: "/slideshow.php",
		data	: "name=" + name + "&width=" + width + "&height=" + height + "&key=" + key(10000),
		success	: function(html){
			
			$(selector).empty();
			$(selector).append(html);
			
			$(selector).cycle({
				fx:     'fade', 
				timeout: 8000, 
				//next:   '#slideshow', 
				pause:   1,
				random:  1
				//before:  onBefore, 
				//after:   onAfter 
			});
		}
	});
}

function fetchBasket() {
	var selector = '#basket';
	$(selector).show();
	$.ajax({
		type	: "GET",
		url		: "/basket.php",
		data	: "&key=" + key(10000),
		success	: function(html){
			$(selector).empty();
			$(selector).append(html);
		}
	});
}

function emptyBasket() {
	var selector = '#basket';
	$.ajax({
		type	: "GET",
		url		: "/basket.php",
		data	: "empty=1&key=" + key(10000),
		success	: function(html){
			$(selector).empty();
			$(selector).append(html);
		}
	});
}

function fetchSpecials() {
	var selector = '#specials';
	$(selector).show();
	$.ajax({
		type	: "GET",
		url		: "/specials.php",
		data	: "&key=" + key(10000),
		success	: function(html){
			$(selector).empty();
			$(selector).append(html);
		}
	});
}

function fetchSignup() {
	
	var selector = "#signup";
	
	$.ajax({
		type	: "GET",
		url		: "signup.php",
		data	: "key=" + key(10000),
		success	: function(html){
			$(selector).empty();
			$(selector).append(html);
		}
	});
}

function echeck(str) {

	var at = "@";
	var dot = ".";
	var lat = str.indexOf(at);
	var lstr = str.length;
	var ldot = str.indexOf(dot);
	
	if (str.indexOf(at) ==- 1) { return false; }
	if (str.indexOf(at) ==- 1 || str.indexOf(at) == 0 || str.indexOf(at) == lstr) { return false; }
	if (str.indexOf(dot) ==- 1 || str.indexOf(dot) == 0 || str.indexOf(dot) == lstr) { return false; }
	if (str.indexOf(at, (lat + 1)) !=- 1) { return false; }
	if (str.substring(lat - 1, lat) == dot || str.substring(lat + 1, lat + 2) == dot) { return false; }
	if (str.indexOf(dot, (lat + 2)) ==- 1) { return false; }
	if (str.indexOf(" ") !=- 1 ) { return false; }
	 
	return true;
}

function isInteger (s) {
	var i;
	
	if (isEmpty(s))
	if (isInteger.arguments.length == 1) return 0;
	else return (isInteger.arguments[1] == true);
	
	for (i = 0; i < s.length; i++) {
		var c = s.charAt(i);
		if (!isDigit(c)) return false;
	}
	
	return true;
}

function isEmpty(s) {
	return ((s == null) || (s.length == 0))
}

function isDigit (c) {
	return ((c >= "0") && (c <= "9"))
}

function validateSignup() {
		
	// first_name
	if (document.getElementById("first_name").value.length < 1) {
		alert("Please enter a valid first name.");
		document.getElementById("first_name_label").style.color = "red";
		document.getElementById("first_name").focus();
		return false;
	} else {
		document.getElementById("first_name_label").style.color = "black";
	}
	
	// last_name
	if (document.getElementById("last_name").value.length < 1) {
		alert("Please enter a valid last name.");
		document.getElementById("last_name_label").style.color = "red";
		document.getElementById("last_name").focus();
		return false;
	} else {
		document.getElementById("last_name_label").style.color = "black";
	}
	
	// mobile_phone
	if (!isInteger(document.getElementById("mobile_phone").value)) {
		alert("Please enter a valid mobile phone number.");
		document.getElementById("mobile_phone_label").style.color = "red";
		document.getElementById("mobile_phone").focus();
		return false;
	} else {
		document.getElementById("mobile_phone_label").style.color = "black";
	}
	
	// work_phone
	if (!isInteger(document.getElementById("work_phone").value)) {
		alert("Please enter a valid work phone number.");
		document.getElementById("work_phone_label").style.color = "red";
		document.getElementById("work_phone").focus();
		return false;
	} else {
		document.getElementById("work_phone_label").style.color = "black";
	}
	
	// home_phone
	if (!isInteger(document.getElementById("home_phone").value)) {
		alert("Please enter a valid home phone number.");
		document.getElementById("home_phone_label").style.color = "red";
		document.getElementById("home_phone").focus();
		return false;
	} else {
		document.getElementById("home_phone_label").style.color = "black";
	}
	
	// email_1 (validate)
	if (echeck(document.getElementById("email_1").value) == false) {
		alert("Please enter a valid email address.");
		document.getElementById("email_1_label").style.color = "red";
		document.getElementById("email_1").focus();
		return false;
	} else {
		document.getElementById("email_1_label").style.color = "black";
	}

	// email_2
	if (document.getElementById("email_2").value != document.getElementById("email_1").value) {
		alert("Please make sure the email addresses are the same.");
		document.getElementById("email_2_label").style.color = "red";
		document.getElementById("email_2").focus();
		return false;
	} else {
		document.getElementById("email_2_label").style.color = "black";
	}
	
	// home_address
	if (document.getElementById("home_address").value.length < 10) {
		alert("Please enter a valid home address.");
		document.getElementById("home_address_label").style.color = "red";
		document.getElementById("home_address").focus();
		return false;
	} else {
		document.getElementById("home_address_label").style.color = "black";
	}
	
	// postal_address
	if (document.getElementById("postal_address").value.length < 10) {
		alert("Please enter a valid postal address.");
		document.getElementById("postal_address_label").style.color = "red";
		document.getElementById("postal_address").focus();
		return false;
	} else {
		document.getElementById("postal_address_label").style.color = "black";
	}
	
	if (document.getElementById("txtCaptcha").value.length == 5) {
		
		$.ajax({
			type	: "POST",
			url		: "/control/captcha/captcha.php",
			data	: "txtCaptcha=" + document.getElementById("txtCaptcha").value + "&key=" + key(10000),
			success	: function(html){
				$("#result").empty();
				$("#result").append(html);
				
				if (html == "FAIL") {
					alert("The code you entered was incorrect. Please try again.");
					$("#imgCaptcha").attr("src", "/control/captcha/image.php?key=" + key(10000));
				} else {
					//document.deal.submit();
					//AJAX SUBMIT CODE
					var selector = "#signup";
					//$(selector).fadeOut(1000);
					
					$.ajax({
						type	: "POST",
						url		: "signup_submit.php",
						data	: "first_name=" + $("#first_name").val() + 
								  "&last_name=" + $("#last_name").val() + 
								  "&mobile_phone=" + $("#mobile_phone").val() + 
								  "&work_phone=" + $("#work_phone").val() + 
								  "&home_phone=" + $("#home_phone").val() + 
								  "&email_1=" + $("#email_1").val() + 
								  "&home_address=" + $("#home_address").val() + 
								  "&postal_address=" + $("#postal_address").val() + 
								  "&txtCaptcha=" + $("#txtCaptcha").val() + 
								  "&key=" + key(10000),
						success	: function(html){
							$(selector).empty();
							$(selector).append(html);
							//$(selector).fadeIn(1000);
						}
					});
					
				}
			}
		});
		return false;
	} else {
		alert("Please enter the exact code on the image into the text box and try again.");
		document.getElementById("txtCaptcha").focus();
		return false;
	}
}