// JavaScript Document


function equalHeight(group) {

	tallest = 0;
    group.each(function() {
		// first, clear any css height setting
		$(this).css('height', 'auto');
		// then get the height of the tallest element
        thisHeight = $(this).height();
        if(thisHeight > tallest) {
            tallest = thisHeight;
        }
    });
	// set all elements to that height
    group.height(tallest);
}

$(document).ready(function() {
	if ($('.row1')) {
	equalHeight($('.row1'));
	equalHeight($('.row2'));
	}
});


var	g_purchase_info_rules = {};
var g_purchase_confirm_rules = {};

	$(document).ready(function() {
		if ($("#purchase")) {
			
			// make sure everything is unchecked
			$("#purchase input[type='radio']").attr("checked", "");
			$("#purchase input[type='checkbox']").attr("checked", "").attr("disabled", "disabled");
	
			
			// attach the on click even for radio buttons
			$("#purchase input[type='radio']").click(function() {
								
				
				// get the ID of the associated checkbox
				var checkbox_id  = "software_pkg_management_" + $(this).attr("value");
	
				/* START DIRTY HACK 
				if ($(this).attr("value") == 1 || $(this).attr("value") == 11 || $(this).attr("value") == 17) {
					document.getElementById('purchase_form').setAttribute('action', '../../evaluate/evaluate-info.php');
				} else {
					document.getElementById('purchase_form').setAttribute('action', '../../evaluate/purchase-info.php');
				}
				 END DIRTY HACK */
				
				// disable the os dropdown if it's a windows box
				switch($(this).attr('value')) {

					case '17':
					case '19':
					case '21':
					case '23':
					case '25':
						$('#software_os').attr('disabled', 'disabled');
						break;
					default:
						$('#software_os').attr('disabled', '');
						break;
				}
	
				// enable the related checkbox
				$("#" + checkbox_id).attr("disabled", "");
				
				// disabled and uncheck all other checkboxes
				$("#purchase input[type='checkbox'][id!='" + checkbox_id + "']").attr("disabled", "disabled").attr("checked", "");
															  
			});	
	
			
			$('#purchase_form').submit(function() {
				//alert(window.location.href);

				if ($("input[type='radio']:checked").length == 0) {
					alert('Please select at least one product');
					return false;
				}
												
				if ($("#software_os").attr('value') == 'Solaris' && $("#software_architecture").attr("value") == '64') {
					alert('Please contact us regarding availability of a 64bit Solaris version');
					return false;
				}
				// check for solaris
				//console.log($("#software_os").attr('value') + "\n" + $("#software_architecture").attr("value"));
				return true;
			});
		}		
		
		/************************ PHONE NUMBER ADDITION **********************/
		jQuery.validator.addMethod("phoneUS", function(phone_number, element) {
			phone_number = phone_number.replace(/\s+/g, ""); 
			return this.optional(element) || phone_number.length > 9 &&
				phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
		}, "Please specify a valid phone number");

		/********************** EVALUATE INFO VALIDATION *********************/
		
		if ($("#evaluate_info_form") || $("#case_study_form") || $("#wp_form")) {
		
			$("#evaluate_info_form, #case_study_form, #wp_form").validate({
					highlight: function(element, errorClass) {
						 //$(element).addClass(errorClass);
						 $(element.form).find("span[htmlfor=" + element.id + "]").addClass(errorClass);
					},
					unhighlight: function(element, errorClass) {
						 //$(element).removeClass(errorClass);
						 $(element.form).find("span[htmlfor=" + element.id + "]").removeClass(errorClass);
					},
					errorPlacement: function(error, element) {
						 error.appendTo( element.next("span") );
					},
					errorElement: "span",
					errorClass: "invalid",
					rules: {
						name: {
							required: false
						},
						company: {
							required: false	
						},
						phone: {
							required: false
						},
						email: {
							required: true,
							email: true
						},
						confirm_email: {
							email: true,
							equalTo: "#email"
						}
					}										
			});
		}
		
		/********************** PURCHASE INFO VALIDATION *********************/
		if ($("#purcashe_info_form")) {
			$("#purchase_info_form").validate({
					highlight: function(element, errorClass) {
						 //$(element).addClass(errorClass);
						 $(element.form).find("span[htmlfor=" + element.id + "]").addClass(errorClass);
					},
					unhighlight: function(element, errorClass) {
						 //$(element).removeClass(errorClass);
						 $(element.form).find("span[htmlfor=" + element.id + "]").removeClass(errorClass);
					},
					errorPlacement: function(error, element) {
						 error.appendTo(element.next("span"));
					},
					errorElement: "span",
					errorClass: "invalid",
					rules: g_purchase_info_rules
											
			});	
		}
		
		
		/********************** CONTACT FORM VALIDATION *********************/
		if ($("#contactform")) {
			$("#contactform").validate({
					highlight: function(element, errorClass) {
						 //$(element).addClass(errorClass);
						 $(element.form).find("span[htmlfor=" + element.id + "]").addClass(errorClass);
					},
					unhighlight: function(element, errorClass) {
						 //$(element).removeClass(errorClass);
						 $(element.form).find("span[htmlfor=" + element.id + "]").removeClass(errorClass);
					},
					errorPlacement: function(error, element) {
						 error.appendTo(element.next("span"));
					},
					errorElement: "span",
					errorClass: "invalid",
					rules: {
						first_name: {
							required: true
						},
						last_name: {
							required: true	
						},
						company: {
							required: true	
						},
						email: {
							required: true,
							email: true
						}
					}										
			});	
		}
		
		/********************** SUPPORT FORM VALIDATION *********************/
		if ($("#supportform")) {
			$("#supportform").validate({
					highlight: function(element, errorClass) {
						 //$(element).addClass(errorClass);
						 $(element.form).find("span[htmlfor=" + element.id + "]").addClass(errorClass);
					},
					unhighlight: function(element, errorClass) {
						 //$(element).removeClass(errorClass);
						 $(element.form).find("span[htmlfor=" + element.id + "]").removeClass(errorClass);
					},
					errorPlacement: function(error, element) {
						 error.appendTo(element.next("span"));
					},
					errorElement: "span",
					errorClass: "invalid",
					rules: {
						name: {
							required: true
						},
						phone: {
							required: true	
						},
						company: {
							required: true	
						},
						license_key:
						{
							required:true
						},
						'00N50000001uHBd': {
							required: true
						},
						subject: {
							required: true
						},
						email: {
							required: true,
							email: true
						}
					}										
			});	
		}
		
		if ($("#purchase_confirm_form")) {
			$("#purchase_confirm_form").validate( {
					highlight: function(element, errorClass) {
						 //$(element).addClass(errorClass);
						 $(element.form).find("span[htmlfor=" + element.id + "]").addClass(errorClass);
					},
					unhighlight: function(element, errorClass) {
						 //$(element).removeClass(errorClass);
						 $(element.form).find("span[htmlfor=" + element.id + "]").removeClass(errorClass);
					},
					errorPlacement: function(error, element) {
						 error.appendTo(element.next("span"));
					},
					errorElement: "span",
					errorClass: "invalid",
				rules: g_purchase_confirm_rules
/*
 {
					cc_first_name: {
						required: true	
					},
					cc_last_name: {
						required: true
					},
					cc_street1: {
						required: true	
					},
					cc_city: {
						required: true	
					},
					cc_state: {
						required: true
					},
					cc_zip: {
						required: true
					},
					cc_card_type: {
						required: true
					},
					cc_number: {
						required: true
					}
				}
*/	
			});
		}
		
		/*************** EVALUATION FORM ****************/
		if ($("#evaluate_form")) {
			$("#evaluate_form").submit(function() {
		
					if ($("#software_os").attr("value") == 'Solaris' && $("input[name=software_pkg]:checked").attr("value").indexOf('64') > 0) {
		
						alert('Please contact us regarding availability of a 64bit Solaris version');
						return false;
					} else {
						return true;	
					}
			});
		}
		
	});	
	
