// form logic
swfobject.registerObject("FlashID");

  $(document).ready(function(){					 
	$.validator.addMethod("first", function(value, element) {  
	return this.optional(element) ||  /^[a-zA-Z. ]{1,}$/i.test(value);  
	}, "You Forgot to Fill Out This Field");
	
	$.validator.addMethod("last", function(value, element) {  
	return this.optional(element) ||  /^[a-zA-Z. ]{1,}$/i.test(value);  
	}, "You Forgot to Fill Out This Field");
		
	$.validator.addMethod("addy", function(value, element) {  
	return this.optional(element) ||  /^[ ]{0,}[a-zA-Z0-9._-]{1,}[._ -]{0,}[0-9a-zA-Z.#-]{1,}[._ -]{0,}[0-9a-zA-Z.#-]{1,}[._ -]{0,}[0-9a-zA-Z.#-]{1,}[._ -]{0,}[a-zA-Z0-9._ -]{0,}$/i.test(value);  
	}, "Address Requires More Characters");

	$.validator.addMethod("apt", function(value, element) {  
	return this.optional(element) ||  /^[ ]{0,}[^"\r\n]{1,}[ ]{0,}[0-9._ -]{0,}$/i.test(value);
	}, "Please enter a valid Apt/Suite");
	
	$.validator.addMethod("city", function(value, element) {  
	return this.optional(element) ||  /^[ ]{0,}[a-zA-Z.]{3,}[a-zA-Z0-9._ -]{0,}$/i.test(value);
	}, "You Forgot to Fill Out This Field");	
	
	/*
	$.validator.addMethod("zip", function(value, element) {
          var validCountry = $('input[name=ValidCountry]').val();
          if( validCountry == "UNKNOWN" ||
              validCountry == "true" ) {
            return this.optional(element) ||  /^[0-9]{5,}$/i.test(value);
          }
          return 0;
	}, "Please enter a valid 5 Digit US ZIP Code");  
	*/

	$.validator.addMethod("zip", function(value, element) {
    return this.optional(element) ||  /^[0-9]{5}$/i.test(value);    
	}, "Please enter a valid 5 Digit US ZIP Code");  
	
   	$.validator.addMethod("phone", function(value, element) {  
	return this.optional(element) ||  /^[(][0-9]{3,}[)][ ][0-9]{3,}[-][0-9]{4,}$/i.test(value);  
	}, "Phone Requires 10 Digits");	

	$.validator.addMethod("email2", function(value, element) {  
	return this.optional(element) ||  /^[a-zA-Z0-9._-]{1,}[@]{1,}[a-zA-Z0-9._-]{1,}[.]{1,}[a-zA-Z0-9._-]{3,}$/i.test(value);  
	}, "This is Not a Valid Email Address");		

	$("#myform").validate(
	  {
	  errorElement: "em",
	  success: function(label) {
	    if (label.parent('span').length) 
              label.parent('span').removeClass("error");
	    label.text(label.next('b').text()+" is Ok!").addClass("success");
	  },
	  errorPlacement: function(error, element) {
		 if (element.attr("name") == "SState") {
			error.insertAfter(element);
			element.parent().addClass("error");
		 }
		 else
			 error.insertAfter(element);
	  },
	  ignoreTitle: true,
	  focusInvalid: false,
	  rules: {
	    First: "required first",
	    Last: "required last",
	    SAddress1: "required addy",
	    SAddress2: "apt",
	    SCity: "required city",
	    SState: "required",
	    SZip: "required zip",
	    Phone1: "required phone",
	    Email: "required email2"
	  }
	});

	$("#myform input, #myform select").live('focus', function() {																						
	if($(this).nextAll('em').length)  
		$(this).next('i').remove()
		else {
		$(this).after('<i>'+$(this).attr("title")+'</i>')
		}
	}).live('blur', function() {
	$(this).nextAll('i').remove()																							
    });
});

$("input[name=Phone1]").focus(function() {
  var mask = "(999) 999-9999";
  $(this).unmask(mask).select().mask(mask,{completed:function(){$('input[name=Email]').focus();}});
});

/*
$("input[name=SZip]").focus(function() {
  var mask = "99999";
  $(this).unmask(mask).select().mask(mask,{completed:function(){
    var protocol = (("https:" == document.location.protocol) ? "https://" : "http://");
    doAjax(protocol+'local.yahooapis.com/MapsService/V1/geocode?appid=d5vfkvvV34HSC275njpbeWaRu8vCMCZQGMExGZRMoIxDbT6VVxjO2iKSwUHfh3s-&zip='+$(this).val());
  }});
});	
*/

$("input[type=submit]").click(function(){
  $('#warning').empty();
  $("#myform em").each(function() {
    $('#warning').append($(this).text() + '\n');					 
  });

  if ($('#warning').not(':empty')) {
  }
});

function doXml(xml) {
  var foundUS = false;

  $(xml).find('Result').each(function(){
    var country = $(this).find('Country').text();  
    if(country=="US"){
      $('input[name=ValidCountry]').val("true");
      var city = $(this).find('City').text();
      var state = $(this).find('State').text();

      $('input[name=SCity]').val(city);
      $('option[value="'+state+'"]').attr("selected", "selected");
      $("#myform").validate().element( "input[name=SCity]" );		
      $("#myform").validate().element( "select" );
      $('input[name=Phone1]').focus();
      foundUS = true;
      return;
    }
  })

  if( !foundUS ) {
    $('input[name=ValidCountry]').val("false");
    $("#myform").validate().element( "input[name=SZip]" );
  }  
}
	
function doAjax(url){
  if(url.match('^http')){
    $.getJSON("http://query.yahooapis.com/v1/public/yql?"+
	      "q=select%20*%20from%20xml%20where%20url%3D%22"+
	      encodeURIComponent(url)+
	      "%22&format=xml'&callback=?",function(data){if(data.results[0]) doXml(data.results[0]);}
    )
  }
}
