// JavaScript Document
$(document).ready(function(){
	$("input:text.form_label, textarea.form_label").each(function(){
		if($(this).val().length == 0){
			$(this).val($(this).attr('title'));
			$(this).addClass('empty');
			$("label[for="+$(this).attr("id")+"]").parent().remove();
		}
		//$("label[for="+$(this).attr("id")+"]").remove();
		$(".inline").css('display','inline');
	});
	$("input:text, .required").each(function(){
		$(this).css("border", "#7F9DB9 1px solid");				  
	});
	$("input:text.form_label, textarea.form_label").focus(function(){
		if($(this).hasClass('not_empty')){
			return;
		} else {
			$(this).addClass('dim');
		}
	});
	$("input:text.form_label, textarea.form_label").keydown(function(){
		if($(this).hasClass('dim')){
			$(this).val('');
			$(this).addClass('not_empty');
			$(this).removeClass('empty');
			$(this).removeClass('dim');
		}
	});
	$("input:text.form_label, textarea.form_label").change(function(){
		$(this).addClass('not_empty');
		$(this).removeClass('empty');
		$(this).removeClass('dim');
	});
	$("input:text.form_label, textarea.form_label").blur(function(){
		if($(this).val().length == 0 || $(this).hasClass('empty')){
			$(this).removeClass('not_empty');
			$(this).removeClass('dim');
			$(this).addClass('empty');
			$(this).val($(this).attr('title'));
			if($(this).hasClass("required")){
				$(this).css("border", "#C00 2px solid");	
			}
		} else {
			$(this).addClass('not_empty');
			$(this).removeClass('empty');
			/// Border Not required
			$(this).css("border", "#7F9DB9 1px solid");
		}
	});
	$("form").submit(function(){
		$form_status = true;
		$(".form_label.empty").each(function(){
			$(this).val('');
		});
		$first_missing_element = false;
		$(".required").each(function(){
			if($(this).val().length == 0){
				if($first_missing_element == false){
					$first_missing_element = true;
					$id = $(this).attr("id");
					$form_status = false;
				}
				$(this).val($(this).attr('title'));
				$(this).css("border", "#C00 2px solid");
			}
		});
		$(".validate-email").each(function(){
			$regex = new RegExp('^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$');
			if(!$regex.test($(this).val())){
				$(this).css("border", "#C00 2px solid");
				if($first_missing_element == false){
					$first_missing_element = true;
					$id = $(this).attr("id");
					$form_status = false;
				}
			}			
		});

		if(!$form_status){
			$element = document.getElementById($id);
			$pos = findPos($element);
			window.scrollTo($pos[0],$pos[1]);
		}
		if(!$form_status){
			alert("Please verify all highlighted fields ");	
		}
		return $form_status;
	});
	// End Form Label
	$(".other_container").toggle();
	//
	$("form #title").change(function(){
		if($(this).val()=='Other'){
			if($("#other").size()==0){
				// Element is Not in the DOM Create Element
				$(".other_container").toggle();
			} else {
				$(".other_container").toggle();
			}
			$("#other").trigger("focus");
		} else {
			$("#other").val("");
			$(".other_container").hide();
		}
	});
	$(".validate-email").blur(function(){
		$regex = new RegExp('^[a-zA-Z0-9._-]+@[a-zA-Z0-9._-]+\.([a-zA-Z]{2,4})$');
		if(!$regex.test($(this).val())){
			$(this).css("border", "#C00 2px solid");	
		} else {
			$(this).css("border", "#7F9DB9 1px solid");
		}
	});
	
	$(".showNextSibling").each(function(){
		if($(this).val() === '0'){
			$(this).parent().parent().next().hide();	
		}
	});
	
	$(".showNextSibling").click(function(){
		if($(this).val() === '0'){
			$(this).parent().parent().next().hide();	
		} else {
			$(this).parent().parent().next().show();	
		}
	});
});
function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		do {
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		} while (obj = obj.offsetParent);
		return [curleft,curtop];
	}
}
