﻿//	Moonstone Interactive Javascript Library for forms
//	Requires jQuery, flowplayer tab library
	//Initialize when ready
	$(document).ready(function() { 
		if (msiFormInit()) { 
			$('#aspnetForm').validate({
				rules: {
					Lead_stInterest_1:{
						validCheckbox1: true	
					},
					Lead_stInterest_2:{
						validCheckbox2: true	
					}, 
					Assistyou_1:{
						validCheckAssist1: true	
					},
					Assistyou_2:{
						validCheckAssist2: true	
					},
					Assistyou_3:{
						validCheckAssist2: true	
					},
					Assistyou_4:{
						validCheckAssist2: true	
					},
					Assistyou_5:{
						validCheckAssist2: true	
					},
					LookingFor:{
						validLookingFor: true

					}
					
				
				}/*,
				errorClass: function(error, element){
					if ( element.attr('type') == 'checkbox' ){
						alert('class');
					}
				}
				*/
			
			});
		}
	});
	
	function msiFormInit() {
		setOnDemandPickLists();
		setWizardTabs();
		setValidationClasses();
		setLandingPage();
		setRollovers();
		extendVisible();
		setCheckBoxes();
		return true;
	}
	
	function setCheckBoxes(){
		$('input[type="checkbox"]').bind('click', function(){
			$('input[type="checkbox"]').removeClass('error');
			$('input[type="checkbox"] ~ label').css("display", "none");
			$('#form_contents h4').css('color', '#666666');

		});
	}
	
	
	//Rollover function for images
	function setRollovers() {
		$('.jqRollOver').hover(function() {
			//Use "name" attribute for rollover image
			var currentImg = $(this).attr('src');
			$(this).attr('src', $(this).attr('name'));
			$(this).attr('name', currentImg);
		}, function() {
			var currentImg = $(this).attr('src');
			$(this).attr('src', $(this).attr('name'));
			$(this).attr('name', currentImg);
		});
		return true;
	}
	
	//Test function
	function isVisible(e) {
		var r = $('#'+e).is(':visible');
		alert('Is "' + e + '" Visible?: ' + r);
		r = $('#'+e).is(':reallyvisible');
		alert('Is "' + e + '" Really Visible?: ' + r);
		return r;
	}
	
	//Is item REALLY visible
	function extendVisible() {
		jQuery.extend(
			jQuery.expr[ ":" ], 
			{ reallyvisible : function (a) { return !(jQuery(a).is(':hidden') || jQuery(a).parents(':hidden').length); }}
		);
		return true;
	}
	
	//On Demand Pick Lists
	function setOnDemandPickLists() {
		$('#Account_PrimaryBillToCountry').load('/GetPickListOptions.aspx?recordtype=Lead&fieldname=Country');
		$('#Account_PrimaryBillToState').load('/GetPickListOptions.aspx?recordtype=Lead&fieldname=StateProvince');
		$('#Account_PrimaryShipToCountry').load('/GetPickListOptions.aspx?recordtype=Lead&fieldname=Country');
		$('#Account_PrimaryShipToState').load('/GetPickListOptions.aspx?recordtype=Lead&fieldname=StateProvince');
		$('#Contact_PrimaryCountry').load('/GetPickListOptions.aspx?recordtype=Lead&fieldname=Country');
		$('#Contact_PrimaryStateProvince').load('/GetPickListOptions.aspx?recordtype=Lead&fieldname=StateProvince');
		$('#Contact_MrMrs').load('/GetPickListOptions.aspx?recordtype=Lead&fieldname=MrMrs');
		$('#Account_AccountType').load('/GetPickListOptions.aspx?recordtype=Account&fieldname=AccountType');
		$('#Account_stPrimaryRegion').load('/GetPickListOptions.aspx?recordtype=Account&fieldname=Region');
		$('#Account_plPurchase_products_Interest').load('/GetPickListOptions.aspx?recordtype=Account&fieldname=plPrimary_Product_Interest');
		$('#Account_plPurchase_products_Through').load('/GetPickListOptions.aspx?recordtype=Account&fieldname=plPurchase_products_Through');
//		$('#Account_Industry').load('/GetPickListOptions.aspx?recordtype=Account&fieldname=Industry');
		$('#Lead_StateProvince').load('/GetPickListOptions.aspx?recordtype=Lead&fieldname=StateProvince');
		$('#Lead_Country').load('/GetPickListOptions.aspx?recordtype=Lead&fieldname=Country');

		return true;
	}
	
	//Landing page specific setting
	function setLandingPage() {
		$('#Lead_stInterest_1').click(function() {
				$('#jqLandingAddress').toggle();
			}
		);
		return true;
	}
	
	//Add additional validation classes
	function setValidationClasses () {
	
	//Add custom method to allow validation of selects where default option's value=none.  This is to 
		//Work around Ektron editor's moronic habit of removing blank select values
		$.validator.addMethod("notNone",
			function(value,element) {
				var r = true;
				var isvisible = $(element).is(':reallyvisible');
				if (isvisible && value == 'none'){
					r = false;
				}
				return r; 
			}
			,"Selection Required");
		$.validator.addMethod("NumericOnly",
		function(value,element) {
			var r = true;
			var isvisible = $(element).is(':visible');
			var regex = /^\d{1,10}\.?\d{0}$/;
			if (isvisible)
			{
			if (value.search(regex) ==-1) {
				r = false;
			  } else {
				r = true;
			  }
			}
			
			return r; 
		}
		,"NumericOnly");
		
		//Switch out other fields based on status of country
		$("#Account_PrimaryBillToProvince").hide();
		$("#Account_PrimaryBillToProvince").removeClass("vldRequired");
		$("#Account_PrimaryShipToProvince").hide();
		$("#Account_PrimaryShipToProvince").removeClass("vldRequired");
		$("#Contact_PrimaryProvince").hide();
		$("#Contact_PrimaryProvince").removeClass("vldRequired");
		$("#Account_PrimaryShipToState").show();
		$("#Account_PrimaryBillToState").show();
		$("#Contact_PrimaryStateProvince").show();
		$("#Lead_Province").hide();
		$("#Lead_StateProvince").show();  

		
		$.validator.addMethod("switchCountry",function(value,element) {
			if (value == 'USA') {
				if (element.name == 'Lead_Country') {  
					$("#Lead_StateProvince").show();
					$("#Lead_StateProvince").addClass("vldSelect");
					$("#Lead_Province").hide();
					$("#Lead_Province").removeClass("vldRequired");					
					$("#Lead_ZipCode").addClass("vldRequired"); 
					$('#Lead_Province').attr('value', '');
				}
				if (element.name == 'Account_PrimaryBillToCountry') {    
					$("#Account_PrimaryBillToState").show();
					$("#Account_PrimaryBillToState").addClass("vldSelect");
					$("#Account_PrimaryBillToProvince").hide();
					$("#Account_PrimaryBillToProvince").removeClass("vldRequired");					
					$("#Account_PrimaryBillToPostalCode").addClass("vldRequired"); 
					$('#Account_PrimaryBillToProvince').attr('value', '');
				}
				if (element.name == 'Account_PrimaryShipToCountry') {    
					$("#Account_PrimaryShipToState").show();
					$("#Account_PrimaryShipToState").addClass("vldSelect");
					$("#Account_PrimaryShipToProvince").hide();
					$("#Account_PrimaryShipToPostalCode").addClass("vldRequired"); 
					$("#Account_PrimaryShipToProvince").removeClass("vldRequired");					
					$('#Account_PrimaryShipToProvince').attr('value', '');
				}
				if (element.name == 'Contact_PrimaryCountry') {    
					$("#Contact_PrimaryStateProvince").show();
					$("#Contact_PrimaryStateProvince").addClass("vldSelect");
					$("#Contact_PrimaryProvince").hide();
					$("#Contact_PrimaryProvince").removeClass("vldRequired");					
					$("#Contact_PrimaryZipCode").addClass("vldRequired"); 
					$('#Contact_PrimaryProvince').attr('value', '');
				}	
				$("#Lead_Province").hide();
				$("#Lead_StateProvince").show();
			} else {
				if (element.name == 'Lead_Country') {  
					$("#Lead_StateProvince").hide();
					$("#Lead_StateProvince").removeClass("vldSelect");
					$("#Lead_Province").show();
					$("#Lead_Province").removeClass("vldRequired");// provinence optional
					$("#Lead_ZipCode").removeClass("vldRequired"); // zipcode optional
					$('#Lead_StateProvince ').attr('value', '');
				}
				if (element.name == 'Account_PrimaryBillToCountry') {
					$("#Account_PrimaryBillToState").hide();
					$("#Account_PrimaryBillToState").removeClass("vldSelect");
					$("#Account_PrimaryBillToProvince").show();
					$("#Account_PrimaryBillToProvince").removeClass("vldRequired");// provinence optional
					$("#Account_PrimaryBillToPostalCode").removeClass("vldRequired"); // zipcode optional
					$('#Account_PrimaryBillToState ').attr('value', '');

				}
				if (element.name == 'Account_PrimaryShipToCountry') {    
					$("#Account_PrimaryShipToState").hide();
					$("#Account_PrimaryShipToState").removeClass("vldSelect");
					$("#Account_PrimaryShipToProvince").show();
					$("#Account_PrimaryShipToProvince").removeClass("vldRequired"); // provinence optional
					$("#Account_PrimaryShipToPostalCode").removeClass("vldRequired"); // zipcode optional
					$('#Account_PrimaryShipToState ').attr('value', '');
				}
				if (element.name == 'Contact_PrimaryCountry') {    
					$("#Contact_PrimaryStateProvince").hide();
					$("#Contact_PrimaryStateProvince").removeClass("vldSelect");
					$("#Contact_PrimaryProvince").show();
					$("#Contact_PrimaryProvince").removeClass("vldRequired"); // provinence optional					
					$("#Contact_PrimaryZipCode").removeClass("vldRequired"); // zipcode optional
					$('#Contact_PrimaryStateProvince ').attr('value', '');
				}
				$("#Lead_Province").show();
				$("#Lead_StateProvince").hide();
			}
			return true;
		},'');
		
		
		$.validator.addMethod("validCheckbox2", function(value, element) { 
			if($('input[@name=Lead_stInterest_2]:checked').size() == 0 && $('input[@name=Lead_stInterest_1]:checked').size() == 0){
				
			
			}
			else{
				return true;
			}
		}, '&nbsp;');
		
		$.validator.addMethod("validCheckbox1", function(value, element) { 
			if($('input[@name=Lead_stInterest_2]:checked').size() == 0 && $('input[@name=Lead_stInterest_1]:checked').size() == 0){
				
				$('#form_contents h4').css('color', 'red');
			}
			else{
				return true;
			}
		}, '&nbsp;');
		
		$.validator.addMethod("validCheckAssist1", function(value, element) { 
		if($('input[@name=Assistyou_5]:checked').size() == 0 && $('input[@name=Assistyou_4]:checked').size() == 0 && $('input[@name=Assistyou_3]:checked').size() == 0 && $('input[@name=Assistyou_2]:checked').size() == 0 && $('input[@name=Assistyou_1]:checked').size() == 0){
		
			
				
				$('#form_contents h4').css('color', 'red');
			}
			else{
				return true;
			}
		}, '&nbsp;');
		
		$.validator.addMethod("validCheckAssist2", function(value, element) { 
			if($('input[@name=Assistyou_5]:checked').size() == 0 && $('input[@name=Assistyou_4]:checked').size() == 0 && $('input[@name=Assistyou_3]:checked').size() == 0 && $('input[@name=Assistyou_2]:checked').size() == 0 && $('input[@name=Assistyou_1]:checked').size() == 0){
				
			
			}
			else{
				return true;
			}
		}, '&nbsp;');

		$.validator.addMethod("validLookingFor", function(value, element) { 
			if($('input[@name=LookingFor]:checked:eq(0)').size() == 0 && $('input[@name=LookingFor]:checked:eq(1)').size() == 0){
				$('input[@name=LookingFor]').addClass('error');
				$('#lookingForLabel').css('color', 'red');
				
			}
			else{
				$('#lookingForLabel').css('color', 'black');
				return true;
			}
		}, '&nbsp;');
	
		//All validators should have custom classes to add additional functionality
		$.validator.addClassRules({
			vldSelect: {
				required: function(element) { return $(element).is(':reallyvisible');},
				notNone: function(element) { return $(element).is(':reallyvisible');}
			},
			vldReqNumeric: {
			required: function(element) { return $(element).is(':visible');},
			NumericOnly: function(element) { return $(element).is(':visible');}
			},
			/*
			vldCheckbox:{
				required: function(element){
					if($('input[@name=Lead_stInterest]:checked').size() == 0){
						alert('Please Make A Selection for fieldName');
					}
					
				}
			},
			*/
			vldZip: {
				digits: true,
				minlength: 5,
				maxlength: 5
			},
			vldCountry: {
				required:  function(element) { return $(element).is(':reallyvisible');},
				switchCountry: true
			},
			vldRequired: {
				required:  function(element) { return $(element).is(':reallyvisible');}
			},
			
			hideDisplay: {
				required:  function(element) { return $(element).is(':reallyvisible');}
			}
		});
		
		
		return true;
	}

	
	
	//Setup wizard tabs for forms
	function setWizardTabs() {
	
		// get container for the wizard and initialize its exposing
		var wizard = $("#jqToolsWizard");
		
		if ( wizard.size() ) {
			//.expose({color: '#789', lazy: true});

			// enable exposing on the wizard
			//wizard.click(function() {
			//	$(this).expose().load();
			//});

			
			// enable tabs that are contained within wizard
			$("ul.jqToolsTabs", wizard).tabs($("div.jqToolsPanes > div", wizard), function(index) {

				/* now we are inside the onBeforeClick event */
	
				//SANDY - code from example commented out, but this is where we should call validation
				//		and return true/false
				
				if (!$('form:visible').valid()){
							return false;
				}
				return true;
				
				// everything is ok. remove possible red highlight from the terms
				//terms.parent().removeClass("error");
			});

			

			// get handle to the tabs API
			var api = $("ul.jqToolsTabs", wizard).tabs(0);
/*
			api.onClick(function() {
				if (!$('form:visible').valid()){
							return;
				}
					
				
			});
			*/

			// "next tab" button
			$("button.next", wizard).click(function() {
				//alert('event');
				$("button.next").addClass('next');
				if (!$('form:visible').valid()){
						//alert('invalid');
						return;
					}
				else{
					//alert('valid');
					api.next();
				}
				return false;
			});

			// "previous tab" button
			$("button.prev", wizard).click(function() {
				api.prev();
				return false;
			});
	

		}
		return true;
	}

	function toggleOn(id) {
		document.getElementById(id).style.display="block";
		return true;
	}

	function toggleOff(id) {
		document.getElementById(id).style.display="none";
		return true;
	}
$(function() {
$('.rollover').hover(function() {
		var currentImg = $(this).attr('src');
		$(this).attr('src', $(this).attr('hover'));
		$(this).attr('hover', currentImg);
	}, function() {
		var currentImg = $(this).attr('src');
		$(this).attr('src', $(this).attr('hover'));
		$(this).attr('hover', currentImg);
	});
});