jQuery(function ($) {
	var casestudy = {
		message: null,
		init: function () {
			$('.casestudy-form a.casestudy').click(function (e) {
				e.preventDefault();

				// load the casestudy form using ajax
                                //alert(baseurl+"/data/casestudy.php");
				$.get(baseurl+"/data/casestudy.php", function(data){
					// create a modal dialog with the data
					$(data).modal({
						closeHTML: "<a href='#' title='Close' class='modal-close'>&nbsp;</a>",
						position: ["5%",],
						overlayId: 'casestudy-overlay',
						containerId: 'casestudy-container',
						onOpen: casestudy.open,
						onShow: casestudy.show,
						onClose: casestudy.close
					});
				});
			});
		},
		open: function (dialog) {
			// add padding to the buttons in firefox/mozilla
			if ($.browser.mozilla) {
				$('#casestudy-container .casestudy-button').css({
					'padding-bottom': '2px'
				});
			}
			// input field font size
			if ($.browser.safari) {
				$('#casestudy-container .casestudy-input').css({
					'font-size': '.9em'
				});
			}

			// dynamically determine height
			var h = 200;
			if ($('#casestudy-subject').length) {
				h += 26;
			}
			if ($('#casestudy-cc').length) {
				h += 22;
			}

			var title = $('#casestudy-container .casestudy-title').html();
			$('#casestudy-container .casestudy-title').html('Case Study Request');
			dialog.overlay.fadeIn(0, function () {
				dialog.container.fadeIn(0, function () {
					dialog.data.fadeIn(0, function () {
						$('#casestudy-container .casestudy-content').animate({
							height: h
						}, function () {
							$('#casestudy-container .casestudy-title').html(title);
							$('#casestudy-container form').fadeIn(0, function () {
								//$('#casestudy-container #casestudy-name').focus();

								/*$('#casestudy-container .casestudy-cc').click(function () {
									var cc = $('#casestudy-container #casestudy-cc');
									cc.is(':checked') ? cc.attr('checked', '') : cc.attr('checked', 'checked');
								});*/

								// fix png's for IE 6
								if ($.browser.msie && $.browser.version < 7) {
									$('#casestudy-container .casestudy-button').each(function () {
										if ($(this).css('backgroundImage').match(/^url[("']+(.*\.png)[)"']+$/i)) {
											var src = RegExp.$1;
											$(this).css({
												backgroundImage: 'none',
												filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="' +  src + '", sizingMethod="crop")'
											});
										}
									});
								}
							});
							$('#casestudy-container #casestudy-name').focus();
						});
					});
				});
			});
		},
		show: function (dialog) {
			$('#casestudy-container .casestudy-send').click(function (e) {
                                
				e.preventDefault();
				// validate form                
				if (casestudy.validate()) {
					var msg = $('#casestudy-container .casestudy-message');
					msg.fadeOut(function () {
						msg.removeClass('casestudy-error').empty();
					});
					$('#casestudy-container .casestudy-title').html('Case Study Request');
					$('#casestudy-container form').fadeOut(0);
					$('#casestudy-container .casestudy-content').animate({
						height: '80px'
					}, function () {
						$('#casestudy-container .casestudy-loading').fadeIn(0, function () {
							$.ajax({
								url: baseurl+'/data/casestudy.php',
								data: $('#casestudy-container form').serialize() + '&action=send',
								type: 'post',
								cache: false,
								dataType: 'html',
								success: function (data) {
                                                                        //alert(data);
									$('#casestudy-container .casestudy-loading').fadeOut(0, function () {
										$('#casestudy-container .casestudy-title').html('Case Study Request');
										msg.html(data).fadeIn(0);
                                                                                
									});
                                                                       
								},
								error: casestudy.error
                                                                
							});
						});
					});
				}
				else {                    
					if ($('#casestudy-container .casestudy-message:visible').length > 0) {
//						var msg = $('#casestudy-container .casestudy-message div');
//						msg.fadeOut(0, function () {
//							msg.empty();
//							casestudy.showError();
//							msg.fadeIn(0);
//						});         
					}
					else {                       
//                        $('#casestudy-container .casestudy-message').animate({
//                            height: 'auto'
//                        }, casestudy.showError);    
					}					
				}
                               
			});
                        $('#casestudy-container').css('z-index','100000002');
		},
		close: function (dialog) {
			$('#casestudy-container .casestudy-message').fadeOut();
			$('#casestudy-container .casestudy-title').html('Case Study Request');
			$('#casestudy-container form').fadeOut(0);
			$('#casestudy-container .casestudy-content').animate({
				height: 40
			}, function () {
				dialog.data.fadeOut(0, function () {
					dialog.container.fadeOut(0, function () {
						dialog.overlay.fadeOut(0, function () {
							$.modal.close();
						});
					});
				});
			});
		},
		error: function (xhr) {
			alert(xhr.statusText);
                        //alert(xhr.responseText);
		},
		validate: function () {
			casestudy.message = '';
                        /*if (!$('#casestudy-container #casestudy-name').val()) {
				casestudy.message += 'Name is required.';
			}*/
                        var name = $('#casestudy-container #casestudy-name').val();
						name=name.replace(/^\s+|\s+$/g,"");
						$('#casestudy-container #casestudy-name').val(name);
                            $('#nameerrormsg').hide();
                           
                            if (!name) {
                                    casestudy.message += 'Name is required. ';
                                     $('#nameerrormsg').show();
                                     $('#nameerrormsg').html('Name is required. ');                                     
                                     //document.getElementById('nameerrormsg').innerHTML = 'Name is required. ';
                            }
                            else {                                 
                                    if (!casestudy.validateName(name)) {
                                            casestudy.message += 'Name is invalid. ';
                                       $('#nameerrormsg').show();
                                       $('#nameerrormsg').html('Name is invalid.');
                                       //document.getElementById(id).innerHTML = 'Name is invalid. ';
                                    }
                            }
                            //alert(document.getElementById('nameerrormsg').innerHTML);

                      /*var phone = $('#casestudy-container #casestudy-phone').val();
                            if (!phone) {
                                    casestudy.message += 'Phone Number is required. ';
                            }
                            else {
                                    if (!casestudy.validatePhone(phone)) {
                                            casestudy.message += 'Phone Number is invalid. ';
                                    }
                            }*/

			var email = $('#casestudy-container #casestudy-email').val();
                            
                            $('#emailerrormsg').hide();
                            if (!email) {
                                    casestudy.message += 'Email is required. ';
                                    $('#emailerrormsg').show();
                                    $('#emailerrormsg').html('Email is required.');
                            }
                            else if (!casestudy.validateEmail(email)) {
                                   casestudy.message += 'Email is invalid.';
                                   $('#emailerrormsg').show();
                                    $('#emailerrormsg').html('Email is invalid.');
                            }                       

			/*if (!$('#casestudy-container #casestudy-message').val()) {
				casestudy.message += 'Message is required.';
			}*/
            
			if (casestudy.message.length > 0) {
				return false;
			}
			else {
				return true;
			}
		},
                validateName: function(name){                   
                   if (!/^[ a-zA-Z.]+$/.test(name))
                       {                          
                           return false;
                       }

                    else
                           {                              
                              
                               return true;

                           }
                },
               	validateEmail: function (email) {
			var at = email.lastIndexOf("@");

			// Make sure the at (@) sybmol exists and  
			// it is not the first or last character
			if (at < 1 || (at + 1) === email.length)
				return false;

			// Make sure there aren't multiple periods together
			if (/(\.{2,})/.test(email))
				return false;

			// Break up the local and domain portions
			var local = email.substring(0, at);
			var domain = email.substring(at + 1);

			// Check lengths
			if (local.length < 1 || local.length > 64 || domain.length < 4 || domain.length > 255)
				return false;

			// Make sure local and domain don't start with or end with a period
			if (/(^\.|\.$)/.test(local) || /(^\.|\.$)/.test(domain))
				return false;

			// Check for quoted-string addresses
			// Since almost anything is allowed in a quoted-string address,
			// we're just going to let them go through
			if (!/^"(.+)"$/.test(local)) {
				// It's a dot-string address...check for valid characters
				//if (!/^[-a-zA-Z0-9!#$%*\/?|^{}`~&'+=_\.]*$/.test(local))
				if (!/^[a-zA-Z0-9\_\.]*$/.test(local))
					return false;
			}

			// Make sure domain contains only valid characters and at least one period
			if (!/^[-a-zA-Z0-9\.]*$/.test(domain) || domain.indexOf(".") === -1)
				return false;	

			return true;
		},
		showError: function () {
			$('#casestudy-container .casestudy-message')
				.html($('<div class="casestudy-error"></div>').append(casestudy.message))
				.fadeIn(0);
		}
	};

	casestudy.init();

});