var warning_color = "#FFCCCC";


function focusLabels() {
  if (!document.getElementsByTagName) return false;
  var labels = document.getElementsByTagName("label");
  for (var i=0; i<labels.length; i++) {
    if (!labels[i].getAttribute("for")) continue;
    labels[i].onclick = function() {
      var id = this.getAttribute("for");
      if (!document.getElementById(id)) return false;
      var element = document.getElementById(id);
      element.focus();
    }
  }
}


function resetFields(whichform) {
  for (var i=0; i<whichform.elements.length; i++) {
    var element = whichform.elements[i];
    if (element.type == "submit") continue;
    if (!element.defaultValue) continue;
    element.onfocus = function() {
    if (this.value == this.defaultValue) {
      this.value = "";
     }
    }
    element.onblur = function() {
      if (this.value == "") {
        this.value = this.defaultValue;
      }
    }
  }
}



function validateForm(whichform) {
//function validateForm() {
	//var whichform = document.myform;
	
	resetColors(whichform);
  for (var i=0; i<whichform.elements.length; i++) {
    var element = whichform.elements[i];
    if (element.className.indexOf("required") != -1) {
      if (!isFilled(element)) {
        alert("Please fill in the "+element.name+" field.");
        element.style.backgroundColor=warning_color;
        return false;
      }
    }
    if (element.className.indexOf("email") != -1) {
      if (!isEmail(element)) {
        alert("The "+element.name+" field must be a valid email address.");
        element.style.backgroundColor=warning_color;
        return false;
      }
    }
  }
  return true;
}

function isFilled(field) {
  if (field.value.length < 1 || field.value == field.defaultValue) {
    return false;
  } else {
    return true;
  }
}

function isEmail(field) {
  if (field.value.indexOf("@") == -1 || field.value.indexOf(".") == -1) {
    return false;
  } else {
    return true;
  }
}

function resetColors(theForm) {
  for (var i=0; i<theForm.elements.length; i++) {
  	if ( theForm.elements[i].className.search(/required/) == -1 ) continue;
  	theForm.elements[i].style.backgroundColor="#FFFFFF";
  }
}

function showPhone() {
	$("#myphone").slideDown();
	$("#phone").addClass("required");
} // end showPhone()

function hidePhone() {
	$("#myphone").slideUp();
	$("#phone").removeClass("required");
} // end showPhone()

$(document).ready(function(){
	$("#primary a[@href$='contact_form.php']")
		.click(function(){
			$("#contactform:hidden").slideDown("slow");
			return false;
		})
		.parent().after("<div id='contactform' style='display:none'><\/div>");
	$("#contactform").load("contactform_general.html");
});

function prepareForms() {
    var thisform = $("#myform");
   // resetFields(thisform[0]);
    thisform.submit(function() {
      //return validateForm(this);
		$.get('contactform.php', $(this).find('input').serialize(), function(data)
		  {
			$('#contactform').html(data);
		  });
		return false;
    });
}


