var $ = jQuery.noConflict();

this.sendform = function(){	

	this.validate = function(){
		$("span.error").remove();
		
		var email = $("#email").val();
		var phone = $("#Phone").val();
			
		var valid = true;
		//first name
		if($("#First_Name").val() == "") {
			error($("#First_Name"),"Please tell us your first name.")
			valid = false;
		};	
		//last name
		if($("#Last_Name").val() == "") {
			error($("#Last_Name"),"Please tell us your last name.")
			valid = false;
		};			
		//email
		if(!checkEmail(email)) {
			error($("#email"),"Please enter a valid email address.")
			valid = false;
		};		
		//captcha
		if(!jcap()) {
			error($("#uword"),"Please enter the correct code.")
			valid = false;
		};		
		//phone
		/*if(!checkPhone(phone)) {
			error($("#Phone"),"Please enter your phone number.")
			valid = false;
		};*/					
		return valid;
	};
	
	this.validateQuick = function(){
		$("span.error").remove();
		
		var email = $("#email").val();
			
		var valid = true;
		//first name
		if($("#Name").val() == "") {
			error($("#Name"),"Please tell us your name.")
			valid = false;
		};	
		//email
		if(!checkEmail(email)) {
			error($("#email"),"Please enter a valid email address.")
			valid = false;
		};			
		//message
		if($("#Comments").val() == "") {
			error($("#Comments"),"Please write your comments.")
			valid = false;
		};			
		//captcha
		if(!jcap()) {
			error($("#uword"),"Please enter the correct code.")
			valid = false;
		};			
		return valid;
	};
	
	this.checkEmail = function(str){
	  var regEx = /^[^@]+@[^@]+.[a-z]{2,}$/;
	  return (str.search(regEx) != -1);
	};	
	this.checkPhone = function(str){
	  var regEx = /[\d\s_]{6,}/;
	  return (str.search(regEx) != -1);
	};				
	
	this.error = function(obj,text){
		var parent = $(obj).parent();
		parent.append("<span class=\"error\">"+ text +"</span>");
		$("span.error",parent).hide().show("slow");
	};
	
	$("#registration_form").submit(function(){
		return validate();
	});
	
	$("#quick_form").submit(function(){
		return validateQuick();
	});
	
};

this.sendemail = function(){	

	this.validate = function(name, email, phone){
		$("span.error").remove();
		var valid = true;
		//name
		if(name == "") {
			error($("#name"),"Please tell us your name.")
			valid = false;
		};	
		//email
		if(!checkEmail(email)) {
			error($("#email"),"We need a valid email address.")
			valid = false;
		};			
		//phone
		if(!checkPhone(phone)) {
			error($("#phone"),"We need your phone number.")
			valid = false;
		};					
		return valid;
	};
	
	this.checkEmail = function(str){
	  var regEx = /^[^@]+@[^@]+.[a-z]{2,}$/;
	  return (str.search(regEx) != -1);
	};	
	this.checkPhone = function(str){
	  var regEx = /[\d\s_]{6,}/;
	  return (str.search(regEx) != -1);
	};				
	
	this.error = function(obj,text){
		var parent = $(obj).parent();
		parent.append("<span class=\"error\">"+ text +"</span>");
		$("span.error",parent).hide().show("slow");
	};	
		
	this.send = function(name, phone, fax, email, company, related, contact_via, comments, referred_by, other_reference){	
	$("#contactForm button").remove();					
	$("#contactForm").append("<p class=\"success\">Sending...</p>");
	$("#contactForm .success").hide().fadeIn(800);
	var dataString = "name=" + name + "&phone=" + phone + "&fax=" + fax + "&email=" + email + "&company=" + company + "&related=" + related + "&contact_via=" + contact_via + "&comments=" + comments + "&referred_by=" + referred_by + "&other_reference=" + other_reference;
	$.ajax({
			type: "POST",
			url: "/wp-content/themes/ca4it/sendemail.php",
			data: dataString,
			success: function(data) {
				$("#contactForm .success").remove();
				if(data == "ok"){
					$("#contactForm").append("<p class=\"success\"><strong>Thank you for your time!</strong> We've received your email and we'll reply as soon as possible.</p>");				
				} else {
					$("#contactForm").append("<p class=\"success\"><strong>There has been an error with sending your email.</strong> Please contact us at <a href='mailto:info@ca4it.com'>info@ca4it.com</a>. Sorry for the inconvenience.</p>");
				};
				$("#contactForm .success").hide().fadeIn(800);
			}	
		});
	};	

	$("#contactForm button").click(function(){																
		var name = $("#name").val();	
		var phone = $("#phone").val();	 
		var fax = $("#fax").val();	 		
		var email = $("#email").val();	 	
		var company = $("#company").val();	 
		var related = $("#related").val();	 
		var contact_via = $("#contact_via").val();	 
		var comments = $("#comments").val();	 	
		var referred_by = $("#referred_by").val();	
		var other_reference = $("#other_reference").val();			
		if(validate(name, email, phone)) send(name, phone, fax, email, company, related, contact_via, comments, referred_by, other_reference);
						
		return false;
	});	

};

this.liveimage = function(){
	var src = $("#image_live").attr("src");
	if(src){
		if(src.indexOf("online") != -1){
			$(".extra .three strong").addClass("online").text("Live support online");
			$("#home .extra .three strong").addClass("online").text("online");		
		};	
	};

};

this.blankwin = function(a){
	var hostname = window.location.hostname;
	hostname = hostname.replace("www.","").toLowerCase();
	this.check = function(obj){
		var href = obj.href.toLowerCase();
		return ((href.indexOf("http://")!=-1 || href.indexOf("https://")!=-1)&& href.indexOf(hostname)==-1) ? true : false;				
	};
	this.set = function(obj){
		obj.target = "_blank";
		obj.className = "external";
	};	
	if(check(a)) set(a);	
};

this.sidebuttons = function(a){
	$("#side .extra li").each(function(){
		var p = $("p",this)																	 
		$("a",this).hover(
			function(){
				$(p).show("fast");
			},
			function(){
				$(p).hide("fast");
			}
		)
	});
};


$(document).ready(function(){	
	sendform();
	liveimage();
	sidebuttons();
	$("a").each(function(){
		blankwin(this);
	});
	$("#slides").easySlider({
		auto: true,
		pause: 4000,
		continuous: true,
		numeric: true
	});
});


