/***********************************************/
/*                Trim Function                */
/***********************************************/
String.prototype.trim = function() { 
	return jQuery.trim(this);
}

function promoVideoClick() {
	//Track in google analytics (EVENT)
	_gaq.push(['_trackEvent', 'Videos', 'Viewed', 'How It Works']);
}

function clientPortalVideoClick() {
	//Track in google analytics (EVENT)
	_gaq.push(['_trackEvent', 'Videos', 'Viewed', 'Client Portal']);
}

function compareSolutionsClick() {
	//Track in google analytics (EVENT)
	_gaq.push(['_trackEvent', 'Images', 'Viewed', 'Solution Comparison']);
}

function speedGraphClick() {
	//Track in google analytics (EVENT)
	_gaq.push(['_trackEvent', 'Images', 'Viewed', 'Speed Difference']);
}

/***********************************************/
/*              On Document Ready              */
/***********************************************/
$j(document).ready(function() {
	// Enable the swapimage plugin
	$j.swapImage(".swapimage");
	
	//Overide page numbers on cycle feature with custom text
	$j("div#featurenav").children("a").each(function() {
		if($j(this).html() == "1") {
			$j(this).html("Managed VPS Hosting");		
		} else if($j(this).html() == "2") {
			$j(this).html("Our Client Portal");
		}
	});
	
	//Get in touch form
	$j("#form_submit").click(function() {
		if(!$j("#form_name, #form_email, #form_notes").isEmpty() && $j("#form_email").isEmail()) {
			$j("#contact-container :input").each(function() {
				$j(this).attr("disabled", true);
			});
			
			Request.ajax("/custom-php/process-contact-form.php", {
				name: $j("#form_name").val(),
				email: $j("#form_email").val(),
				notes: $j("#form_notes").val()
			});
			
			//Track in google analytics (GOAL)
			_gaq.push(['_trackPageview', '/contact-complete.php']);
			
			$j("#contact-container").html("<h3 style=\"margin-top: 15px;\"><img src=\"/art/tick.png\" alt=\"Success\" style=\"margin-right: 5px;\" />Thanks!</h3>We successfully received your web inquiry and a representative will be responding shortly.");
		}
	});
	
	//Sign-up form
	$j("#signup_submit").click(function() {
		if(!$j("#signup_solution, #signup_location, #signup_first_name, #signup_last_name, #signup_email, #signup_address, #signup_city, #signup_state, #signup_zipcode, #signup_country").isEmpty() && $j("#signup_email").isEmail() && $j("#signup_terms_of_service").isChecked()) {
			$j("#sign-up-form :input").each(function() {
				$j(this).attr("disabled", true);
			});
			
			$j("#signup_submit").val("Please Wait...");
			
			Request.ajax("/custom-php/process-signup-form.php", {
				solution: $j("#signup_solution").val(),
				location: $j("#signup_location").val(),
				first_name: $j("#signup_first_name").val(),
				last_name: $j("#signup_last_name").val(),
				company: $j("#signup_company").val(),
				email: $j("#signup_email").val(),
				address: $j("#signup_address").val(),
				city: $j("#signup_city").val(),
				state: $j("#signup_state").val(),
				zipcode: $j("#signup_zipcode").val(),
				country: $j("#signup_country").val(),
				promo: $j("#signup_promo").val(),
				notes: $j("#signup_notes").val()
			}, function(p_response) {
				if(p_response == "success") {
					//Track in google analytics (GOAL)
					_gaq.push(['_trackPageview', '/sign-up-complete.php']);
				
					$j(".entry").html("<h4><img src=\"/art/tick.png\" alt=\"Welcome To 619Cloud\" style=\"margin-right: 5px;\" />Welcome To 619Cloud!</h4><div style=\"font-size: 14px; margin-top: 10px;\"><p>We successfully received your sign up, and will be e-mailing you shortly at <i><strong>" + $j("#signup_email").val() + "</strong></i>.</p><p>Our e-mail will have a variety of questions to help us customize and personalize your 619Cloud solution to your exact needs and specifications. Our questions will include your intended solutions purpose, what technologies/software you require, your estimated traffic, and which domains you wish to initially host.</p><p>We look forward to building and serving your cloud hosting needs. If you have any questions or comments, please feel free to e-mail us at <a href=\"mailto:sales@619cloud.com\">sales@619cloud.com</a>.</p><p>Thank You!</p></div>");				
				}
			});
		}
	});
	
	//Dns Sign-up form
	$j("#dns_signup_submit").click(function() {
		if(!$j("#signup_first_name, #signup_last_name, #signup_email, #signup_address, #signup_city, #signup_state, #signup_zipcode, #signup_country, #signup_domain").isEmpty() && $j("#signup_email").isEmail() && $j("#signup_terms_of_service").isChecked()) {
			$j("#dns-sign-up-form :input").each(function() {
				$j(this).attr("disabled", true);
			});
			
			$j("#dns_signup_submit").val("Please Wait...");
			
			Request.ajax("/custom-php/process-dns-signup-form.php", {
				first_name: $j("#signup_first_name").val(),
				last_name: $j("#signup_last_name").val(),
				company: $j("#signup_company").val(),
				email: $j("#signup_email").val(),
				address: $j("#signup_address").val(),
				city: $j("#signup_city").val(),
				state: $j("#signup_state").val(),
				zipcode: $j("#signup_zipcode").val(),
				country: $j("#signup_country").val(),
				domain: $j("#signup_domain").val(),
				notes: $j("#signup_notes").val()
			}, function(p_response) {
				if(p_response == "success") {
					//Track in google analytics (GOAL)
					_gaq.push(['_trackPageview', '/dns-sign-up-complete.php']);
				
					$j(".entry").html("<h4><img src=\"/art/tick.png\" alt=\"Thanks\" style=\"margin-right: 5px;\" />Thanks!</h4><div style=\"font-size: 14px; margin-top: 10px;\"><p>We successfully received your DNS hosting sign up, and will be e-mailing you shortly at <i><strong>" + $j("#signup_email").val() + "</strong></i>.</p><p>We look forward to serving your DNS hosting needs. If you have any questions or comments, please feel free to e-mail us at <a href=\"mailto:sales@619cloud.com\">sales@619cloud.com</a>.</p><p>Thank You!</p></div>");				
				}
			});
		}
	});
});

/***********************************************/
/*              AJAX Request Object            */
/***********************************************/
var Request = {
	//// Test if an object is empty
	isEmptyObject: function(obj) {
		for(var i in obj) {
			return false;
		}
		return true;
	},
	//// Make an ajax request
	// p_server_side_page: Ajax page to call
	// p_parameters: Arguments to pass to the server side page
	// p_callback: Handler to call on success
	// p_options (Optional): Additional configuration options 
	//   -- onWorking: Element to update with spinner image while ajax call is working
	//   -- dataType:{"xml","html","script","json","jsonp","text"} Data type of return object
	//   -- type:{"POST","GET"} Request type
	//   -- timeout:{int} Timeout in milliseconds before fail
	//   -- cache:{true/false} Whether to allow caching or not
	//   -- async:{true/false} Whether to send the request asynchronous, if you need synchronous set to false.
	ajax: function(p_server_side_page, p_parameters, p_callback, p_options) {
		// Set defaults
		var data_type = "text";
		var type = "post";
		var timeout = 30000;
		var cache = false;
		var async = true;

		// Check option parameters
		if (typeof p_options != "undefined") {
			if (typeof (p_options.onWorking) != "undefined") {
				p_options.onWorking.ajaxStart(function() {
					$j(this).html("<img src=\"/art/ajax-working.gif\" alt=\"Working...\" />");
				}).ajaxStop(function() {
					$j(this).html("");
					
					// Unbind the start
					$j(this).unbind("ajaxStart");
					
					// Unbine the stop
					$j(this).unbind("ajaxStop");
				});
			}

			if (typeof (p_options.dataType) != "undefined") {
				data_type = p_options.dataType;
			}

			if (typeof (p_options.type) != "undefined") {
				type = p_options.type;
			}

			if (typeof (p_options.timeout) != "undefined") {
				timeout = p_options.timeout;
			}

			if (typeof (p_options.cache) != "undefined") {
				cache = p_options.cache;
			}

			if (typeof (p_options.async) != "undefined") {
				async = p_options.async;
			}
		}
		
		//Force a `GET` request, if the parameters is null, undefined, or empty.
		if(typeof p_parameters == "undefined" || p_parameters == null) {
			type = "get";					
		} else {
			if(this.isEmptyObject(p_parameters)) {
				type = "get";
			}
		}

		// Make ajax request
		$j.ajax({ url: p_server_side_page,
			data: p_parameters,
			success: p_callback,
			dataType: data_type,
			type: type,
			timeout: timeout,
			cache: cache,
			async: async,
			error: function(XMLHttpRequest, textStatus, errorThrown) { /*NOTHING*/ }
		});
	}
}

/***********************************************/
/*           jQuery Custom Extensions          */
/***********************************************/
// Check for jQuery
if (jQuery) (function() {
	// Extend the functions for elements
	$j.extend($j.fn, {
		isEmpty: function() {
			var empty = false;
			
			$j(this).each(function() {
				// Select
				if(this.tagName == "SELECT") {
					if($j(this).children("option").length > 0 && $j(this).val().length > 0) {
						$j(this).removeClass("error");
					} else {
						$j(this).addClass("error");
						empty = true;
					}	
				} 
				// Text
				else {
					if($j(this).val().length > 0) {
						$j(this).removeClass("error");
					} else {
						$j(this).addClass("error");
						empty = true;
					}
				}
			});
			
			return empty;
		},
		isEmail: function() {
			var email = true;
			
			$j(this).each(function() {
				var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{1,5})$/;
				
				if(reg.test($j(this).val()) == false) {
					$j(this).addClass("error");
					email = false;
				} else {
					$j(this).removeClass("error");
				}
			});
			
			return email;
		},
		isChecked: function() {
			var checked = false;
			
			$j(this).each(function() {
				if($j(this).attr("checked")) {
					$j(this).parent().removeClass("error");
					checked = true;	
				} else {
					$j(this).parent().addClass("error");
				}
			});
			
			return checked;
		},
		upperCase: function() {
			$j(this).each(function() {
				var obj = this;
				
				$j(this).bind("keyup", function() {
					$j(obj).val($j(element.val().toUpperCase()));
				});
			});
		}
	});
})(jQuery);

/***********************************************/
/*              Get URL Parameters             */
/***********************************************/
function getUrlParams() {
    var params = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    
    for(var i = 0; i < hashes.length; i++) {
        hash = hashes[i].split('=');
        params.push(hash[0]);
        params[hash[0]] = hash[1];
    }
    
    return params;
}
