$(document).ready(function(){
	
//	$(".lightbox_create_brief").each(function(){
//		
//		$(this).fancybox({
//				'padding'			: 0,
//				'autoScale'			: false,
//				'transitionIn'		: 'none',
//				'transitionOut'		: 'none'
//		});
//	});
	
	$(".lightbox_create_brief").fancybox({
			'padding'			: 0,
			'autoScale'			: false,
			'transitionIn'		: 'none',
			'transitionOut'		: 'none'
	});
	
	$(".lightbox_header_create_brief").fancybox({
			'padding'			: 0,
			'autoScale'			: false,
			'transitionIn'		: 'none',
			'transitionOut'		: 'none'
	});
	
	
	$(".ajax_contact_us").click(function(){
		
		$(".error").each(function(){
			$(this).removeClass('error');
		});
			
		var error = 0;
		var set_focus = "";
	
		var name;
		if((name = $("#name").val()) == ""){
			error = 1;
			$('#p_name').addClass("error");
			if(set_focus == ""){
				set_focus = 'name';
			}
		}
		
		var phone;
		if((phone = $("#phone").val()) == ""){
			error = 1;
			$('#p_phone').addClass("error");
			if(set_focus == ""){
				set_focus = 'phone';
			}
		}
		
		var email;
		email = $("#email").val();
		valid = Validate_Email_Address(email);
		if(valid == false){
			error = 1;
			$('#p_email').addClass("error");
			if(set_focus == ""){
				set_focus = 'email';
			}
		}
		
		var company;
		company = $("#company").val();
		
		var country;
		country = $("#country").val();
		
	
		var message;
		message = $("#message").val();
		
		if(error == 1){
			$(".ajax_contact_us").attr("disabled", false);
		}
		else{
			
			$.ajax({
				type: "POST",
				url: "/ajax/ajax_contact_us.php",
				data: "name="+escape(name)+"&email="+escape(email)+"&phone="+escape(phone)+"&company="+escape(company)+"&country="+escape(country)+"&message="+escape(message),
				success: function(html){
					$("#div_contact_us").html(html);
					scroll(0,0);
				},
				async: true
			});
		}
	});
});

function Validate_String(string, return_invalid_chars){
	var valid_chars = '1234567890-_.^~abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
	var invalid_chars = '';

	if(string == null || string == '')
	return(true);

	//For every character on the string.
	for(index = 0; index < string.length; index++){
		char = string.substr(index, 1);

		//Is it a valid character?
		if(valid_chars.indexOf(char) == -1){
			//If not, is it already on the list of invalid characters?
			if(invalid_chars.indexOf(char) == -1){
				//If it's not, add it.
				if(invalid_chars == '')
				invalid_chars += char;
				else
				invalid_chars += ', ' + char;
			}
		}
	}

	//If the string does not contain invalid characters, the function will return true.
	//If it does, it will either return false or a list of the invalid characters used
	//in the string, depending on the value of the second parameter.
	if(return_invalid_chars == true && invalid_chars != ''){
		var last_comma = invalid_chars.lastIndexOf(',');

		if(last_comma != -1){
			invalid_chars = invalid_chars.substr(0, $last_comma) + ' and ' + invalid_chars.substr(last_comma + 1, invalid_chars.length);
		}
		return(invalid_chars);
	}
	else
	return(invalid_chars == '');
}

function Validate_Email_Address(email_address){
	//Assumes that valid email addresses consist of user_name@domain.tld
	var at = email_address.indexOf('@');
	var dot = email_address.indexOf('.');

	if(at == -1 ||
	dot == -1 ||
	dot == 0 ||
	dot == email_address.length - 1)
	return(false);

	var user_name = email_address.substr(0, at);
	var domain_name = email_address.substr(at + 1, email_address.length);

	if(Validate_String(user_name) === false ||
	Validate_String(domain_name) === false)
	return(false);

	return(true);
}
