/*
$.validator.setDefaults({
	submitHandler: function() { alert("submitted!"); }
});
*/

$.metadata.setType("attr", "validate");

var countryList;


$().ready(function() {
	// validate the comment form when it is submitted
	
	$.validator.setDefaults({
   		debug: false
	})
	
	var errorContainer = $('div.errorwarning');
	 $("#contact").bind("invalid-form.validate", function(e, validator) {
                  var err = validator.numberOfInvalids();
                  if (err) {
                    errorContainer.show();
                  } else {
                    errorContainer.hide();
                  }
                }).validate({
				rules: {
					State: {
						required: function(element) {
							return $("#Country").val() == 'United-States'
							}
					},
					
					PostalCode: {
						maxlength: 10,
						required: function(element) {
							return $("#Country").val() == 'United-States'
							}
						
					}
					
				}
				
				});
	
	$.get("/assets/scripts/common/data/countries.json", function(data, textStatus) {
       
	   // this will give us an array of objects
        countryList = JSON.parse(data);
		$("#Country").fillSelect(countryList);
		});	
			
	$("#Country").change(onSelectChangeCountryList);
	$("#State").change(onSelectChangeUSStateList);
	
	$('#PhoneNumber').numeric({allow:".,-"});
	$('#PostalCode').numeric({allow:"-"});
	
	setReferrer();
	setRedirect();
	
	// validate the form when it is submitted
	
});

$.fn.clearForm = function() {
  return this.each(function() {
    var type = this.type, tag = this.tagName.toLowerCase();
    if (tag == 'form')
      return $(':input',this).clearForm();
    if (type == 'text' || type == 'password' || tag == 'textarea')
      this.value = '';
    else if (type == 'checkbox' || type == 'radio')
      this.checked = false;
    else if (tag == 'select')
      this.selectedIndex = 0;
  });
  
};

function resetFrm() {

	if (confirm('Are you sure you want to reset this form, all data will be lost?')) {
		$('#contact').clearForm();
	}

}

function onSelectChangeCountryList(){     
    var selected = $("#Country option:selected");       
    if(selected.val() == "United-States"){
		$.get("/assets/scripts/common/data/us_states.json", function(data, textStatus) {
		
			// this will give us an array of objects
			var usStateListArray = JSON.parse(data);	
			$("#State").fillSelect(usStateListArray);
			$("#us_states_para").show('fast'); 
			$("#postal_code_para").show('fast');
			$("#State").focus();
		});
    } else {
		$("#us_states_para").hide('fast'); 
		$("#PostalCode").val('');
		$("#postal_code_para").hide('fast'); 
	}
	$("#Country").valid();
		
}  

function onSelectChangeUSStateList(){
	$("#State").valid();
	$("#State").blur(function () {
		//$("#Comments").focus();
		$("#PostalCode").focus();
	 });
	
}

//Sets a hidden input field to the referring url. If there is no referring url, a default line is set instead.
function setReferrer() {

	if (document.referrer == "")
	{
		$("#referer").val("Bookmark / Direct");
	} else {
		$("#referer").val(document.referrer);
	}
		
}

//Sets a hidden input field to the redirect url. 
function setRedirect() {
	var thank_you_page_path = "wcm/thank_you.page";
	var domain_path;

	if (document.domain == "")
	{
		domain_path = "www.rohmhaas.com";
	} else {
		domain_path = location.host;
	}
	url = "http://" + domain_path + "/" + thank_you_page_path; 
	$("#redirect").val(url);

}

function preprocessFormField() {

	$("#Name").val($("#FirstName").val() + " " + $("#LastName").val());
	//$("#Country").val($("#Country option:selected").val());
	//$("#State").val($("#State option:selected").val());


	// iterate over Country	
    for(var x=0; x < countryList.length; x++) {
		var item = countryList[x];
		
		if (item.value == $("#Country option:selected").val()) 
		{
			$("#Region").val(item.region);
			
			break;
		}		
	}
}


function setbg(color)
{
	$("#Comments").css("background",color);
}
