	$(function() {
		var name = $("#name"),
			email_address = $("#email_address"),
			phone_number = $("#phone_number"),
			business_type = $("#business_type"),
			company_name = $("#company_name"),
			message = $("#message"),
			
			allFields = $([]).add(name).add(email_address).add(phone_number).add(business_type).add(company_name).add(message),
			tips = $("#validateTips");

		function updateTips(t) {
			tips.text(t).effect("highlight",{},1500);
		}

		function checkLength(o,n,min,max) {

			if ( o.val().length > max || o.val().length < min ) {
				o.addClass('ui-state-error');
				updateTips("Length of " + n + " must be between "+min+" and "+max+".");
				return false;
			} else {
				return true;
			}

		}

		function checkRegexp(o,regexp,n) {

			if ( !( regexp.test( o.val() ) ) ) {
				o.addClass('ui-state-error');
				updateTips(n);
				return false;
			} else {
				return true;
			}

		}
		
		$("#dialog").dialog({
					bgiframe: true,
			resizable: false,
			autoOpen: false,
			height: 550,
			width: 500,
			modal: true,
			buttons: {
				'Submit': function() {
					var bValid = true;
					allFields.removeClass('ui-state-error');

					bValid = bValid && checkLength(name,"Name",3,20);
					bValid = bValid && checkLength(email_address,"Email",6,255);
					bValid = bValid && checkRegexp(name,/^[a-z]+$/i,"First Name must consist of letters");
					bValid = bValid && checkRegexp(email_address,/^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i,"eg. username@domain.com");
					bValid = bValid && checkLength(message,"Message",20,500);

					if (bValid) {
					

 //document.forms["register"].submit();
 
$("#dialog").fadeOut("slow", function() {
	makeRequest("dynamic_contact_us.php","?step=Submit&name="+name.val()+"&email_address="+email_address.val()+"&phone_number="+phone_number.val()+"&business_type="+business_type.val()+"&company_name="+company_name.val()+"&message="+message.val());
	$('#dialog').dialog('removebutton', 'Submit');
	$('#dialog').dialog('removebutton', 'Cancel');
	//makeRequest("forms/register_modal.php","?step=Submit&first_name="+first_name.val()+"&last_name="+last_name.val()+"&email_address="+email_address.val()+"&confirm_email_address="+confirm_email_address.val()+"&password="+password.val()+"&confirm_password="+confirm_password.val()+"&house_name_or_number="+house_name_or_number.val()+"&street="+street.val()+"&town_or_province="+town_or_province.val()+"&city_or_state="+city_or_state.val()+"&post_zip_code="+post_zip_code.val()+"&country="+country.val()+"&mobile_number="+mobile_number.val()+"&landline_number="+landline_number.val()+"&newsletter="+newsletter.val(), function() {
    
		$('#dialog').html(duplicate_email) 
			$('#dialog').dialog('addbutton', 'Close', newButtonClickFunction); 
			$("#dialog").fadeIn("slow");
		});

	
  function newButtonClickFunction() {
   $(this).dialog('close'); 
   }
   
 //$("form#user_<?php echo $games_id; ?>").load("update_vote.php?game_id=<?php echo $games_id; ?>&vote=" + value + "&type=<?php echo $list; ?>");

					}
				},
				Cancel: function() {
					$(this).dialog('close');
				}
			},
			close: function() {
				allFields.val('').removeClass('ui-state-error');
			}
		});
		
		//add a button
		$.extend($.ui.dialog.prototype, { 
        'addbutton': function(buttonName, func) {
                var buttons = this.element.dialog('option', 'buttons');
                buttons[buttonName] = func;
                this.element.dialog('option', 'buttons', buttons);
        }
}); 

//remove a button
$.extend($.ui.dialog.prototype, {
        'removebutton': function(buttonName) {
                var buttons = this.element.dialog('option', 'buttons');
                delete buttons[buttonName];
                this.element.dialog('option', 'buttons', buttons);
        }
}); 
		
		
		$('.contact_us').click(function() {
			$('#dialog').dialog('open');
			return false;
		})

		$('.cart-buttons').hover(
			function(){ 
				$(this).addClass("ui-state-hover"); 
			},
			function(){ 
				$(this).removeClass("ui-state-hover"); 
			}
		).mousedown(function(){
			$(this).addClass("ui-state-active"); 
		})
		.mouseup(function(){
				$(this).removeClass("ui-state-active");
		});
	});

	var http_request = false;
	var duplicate_email=false;
	
   function makeRequest(url, parameters) {
      http_request = false;
      if (window.XMLHttpRequest) { // Mozilla, Safari,...
         http_request = new XMLHttpRequest();
         if (http_request.overrideMimeType) {
         	// set type accordingly to anticipated content type
            //http_request.overrideMimeType('text/xml');
            http_request.overrideMimeType('text/html');
         }
      } else if (window.ActiveXObject) { // IE
         try {
            http_request = new ActiveXObject("Msxml2.XMLHTTP");
         } catch (e) {
            try {
               http_request = new ActiveXObject("Microsoft.XMLHTTP");
            } catch (e) {}
         }
      }
      if (!http_request) {
         alert('Cannot create XMLHTTP instance');
         return false;
      }
     // http_request.onreadystatechange = alertContents;
      http_request.open('GET', url + parameters, false);
      http_request.send(null);
	  
	  if (http_request.status == 200) {
            //alert(http_request.responseText);
            duplicate_email = http_request.responseText;
         }
   }

//   function alertContents() {
 //     if (http_request.readyState == 4) {
 //        if (http_request.status == 200) {
  //          //alert(http_request.responseText);
   //         duplicate_email = http_request.responseText;
    //     }
     // }
  // }
   
   function get(obj) {
      var getstr = "?";
      for (i=0; i<obj.childNodes.length; i++) {
         if (obj.childNodes[i].tagName == "INPUT") {
            if (obj.childNodes[i].type == "text") {
               getstr += obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";
            }
            if (obj.childNodes[i].type == "checkbox") {
               if (obj.childNodes[i].checked) {
                  getstr += obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";
               } else {
                  getstr += obj.childNodes[i].name + "=&";
               }
            }
            if (obj.childNodes[i].type == "radio") {
               if (obj.childNodes[i].checked) {
                  getstr += obj.childNodes[i].name + "=" + obj.childNodes[i].value + "&";
               }
            }
         }   
         if (obj.childNodes[i].tagName == "SELECT") {
            var sel = obj.childNodes[i];
            getstr += sel.name + "=" + sel.options[sel.selectedIndex].value + "&";
         }
         
      }
     // makeRequest('get.php', getstr);
   }
