$(document).ready
(
	function()
	{
	
		// based upon the 'activePage' variable placed at the top of the each page. 
		// add the id attribute to the child div that contains the dot class and set it's value to 'active' 
		$(activePage + ' div.dot').attr('id', 'active');
		
		// surprise, surprise, IE sucks once again and doesn't pickup the style for the #active selector
		// so just set the css value for the active page to show the orange dot as well
		$(activePage + ' div.dot').css('display', 'inline-block');
		
		//console.log(test);
		
		$('#cssdropdown li.headlink').hover
		(
			function() 
			{ 
				// display the dropdown menu
				$('ul', this).css('display', 'block');
				// display the orange dot if this target doesn't have the 'active' id value
				$('div.dot', this).not("#active").css('display', 'inline-block');
			},
			function() 
			{
				// hide the dropdown menu
				$('ul', this).css('display', 'none');
				// remove the orange dot if this target doesn't have the 'active' id value
				$('div.dot', this).not("#active").css('display', 'none');
			}
		);
	}
);

jQuery.fn.getCheckboxVal = function()
{ 
	var vals = []; 
	var i = 0; 
	this.each(function()
	{ 
		vals[i++] = jQuery(this).val(); 
	}); 
	return vals; 
}

function replaceit(kronosForm) 
{
  var tempString = "";
  tempString = escape(document.kronosForm.ADA_Needs.value);
  var findString = "%22";
  var replaceString = "";
  tempString = replaceAll(tempString,findString,replaceString);
  findString = "%0A";
  replaceString = " ";
  tempString = replaceAll(tempString,findString,replaceString);
  findString = "%0D";
  replaceString = " ";
  tempString = replaceAll(tempString,findString,replaceString);

  document.kronosForm.ADA_Needs.value = unescape(tempString);

  return;
}


function replaceAll(oldStr,findStr,repStr) 
{
  var srchNdx = 0;
  var newStr = "";
  while (oldStr.indexOf(findStr,srchNdx) != -1)
  {
    newStr += oldStr.substring(srchNdx,oldStr.indexOf(findStr,srchNdx));

    newStr += repStr;
    srchNdx = (oldStr.indexOf(findStr,srchNdx) + findStr.length);
  }
  newStr += oldStr.substring(srchNdx,oldStr.length);
  return newStr;
}

	function validateRequiredFields()
	{
		var requiredFormFieldsValid = true;
		var firstFocusControl = null;
		var highlightColor = '#FFBDBD';
		
		var salutation = $("select[name='~Salutation']");
		if(salutation.val().length <= 0)
		{
			salutation.parent().parent().css('background-color', highlightColor);
			requiredFormFieldsValid = false;
			if(firstFocusControl==null)
				firstFocusControl = salutation;
		}
		else
		{
			salutation.parent().parent().css('background-color', 'white');
		}
		
		var firstName = $("input[name='~First_Name']");
		if(firstName.val().length <= 0)
		{
			firstName.parent().parent().css('background-color', highlightColor);
			requiredFormFieldsValid = false;
			if(firstFocusControl==null)
				firstFocusControl = firstName;
		}
		else
		{
			firstName.parent().parent().css('background-color', 'white');
		}
		
		var lastName = $("input[name='~Last_Name']");
		if(lastName.val().length <= 0)
		{
			lastName.parent().parent().css('background-color', highlightColor);
			requiredFormFieldsValid = false;
			if(firstFocusControl==null)
				firstFocusControl = lastName;
		}	
		else
		{
			lastName.parent().parent().css('background-color', 'white');
		}
		
		var title = $("input[name='~Title']");
		if(title.val().length <= 0)
		{
			title.parent().parent().css('background-color', highlightColor);
			requiredFormFieldsValid = false;
			if(firstFocusControl==null)
				firstFocusControl = title;
		}	
		else
		{
			title.parent().parent().css('background-color', 'white');
		}		
		
		var company = $("input[name='~Company']");
		if(company.val().length <= 0)
		{
			company.parent().parent().css('background-color', highlightColor);
			requiredFormFieldsValid = false;
			if(firstFocusControl==null)
				firstFocusControl = company;
		}	
		else
		{
			company.parent().parent().css('background-color', 'white');
		}		
		
		var address = $("input[name='~Address']");
		if(address.val().length <= 0)
		{
			address.parent().parent().css('background-color', highlightColor);
			requiredFormFieldsValid = false;
			if(firstFocusControl==null)
				firstFocusControl = address;
		}	
		else
		{
			address.parent().parent().css('background-color', 'white');
		}		
		
		var city = $("input[name='~City']");
		if(city.val().length <= 0)
		{
			city.parent().parent().css('background-color', highlightColor);
			requiredFormFieldsValid = false;
			if(firstFocusControl==null)
				firstFocusControl = city;
		}	
		else
		{
			city.parent().parent().css('background-color', 'white');
		}
		
		var zip = $("input[name='~Zip']");
		if(zip.val().length <= 0)
		{
			zip.parent().parent().css('background-color', highlightColor);
			requiredFormFieldsValid = false;
			if(firstFocusControl==null)
				firstFocusControl = zip;
		}	
		else
		{
			zip.parent().parent().css('background-color', 'white');
		}
		
		var country = $("select[name='~Country']");
		if(country.val().length <= 0)
		{
			country.parent().parent().css('background-color', highlightColor);
			requiredFormFieldsValid = false;
			if(firstFocusControl==null)
				firstFocusControl = country;
		}
		else
		{
			country.parent().parent().css('background-color', 'white');
		}
		
		var phone = $("input[name='~Phone']");
		if(phone.val().length <= 0)
		{
			phone.parent().parent().css('background-color', highlightColor);
			requiredFormFieldsValid = false;
			if(firstFocusControl==null)
				firstFocusControl = phone;
		}
		else
		{
			phone.parent().parent().css('background-color', 'white');
		}
		
		var email = $("input[name='Email']");
		if(email.val().length <= 0)
		{
			email.parent().parent().css('background-color', highlightColor);
			requiredFormFieldsValid = false;
			if(firstFocusControl==null)
				firstFocusControl = email;
		}
		else
		{
			//email.parent().parent().css('background-color', 'white');
			if(validateEmail(email.val()))
			{
				email.parent().parent().css('background-color', 'white');
			}
			else
			{
				email.parent().parent().css('background-color', highlightColor);
				requiredFormFieldsValid = false;
				if(firstFocusControl==null)
					firstFocusControl = email;
			}
		}
		
		var emergencyContactName = $("input[name='~Emergency_Contact']");
		if(emergencyContactName.val().length <= 0)
		{
			emergencyContactName.parent().parent().css('background-color', highlightColor);
			requiredFormFieldsValid = false;
			if(firstFocusControl==null)
				firstFocusControl = emergencyContactName;
		}
		else
		{
			emergencyContactName.parent().parent().css('background-color', 'white');
		}
		
		var emergencyContactPhone = $("input[name='~Emergency_Contact_Phone']");
		if(emergencyContactPhone.val().length <= 0)
		{
			emergencyContactPhone.parent().parent().css('background-color', highlightColor);
			requiredFormFieldsValid = false;
			if(firstFocusControl==null)
				firstFocusControl = emergencyContactPhone;
		}
		else
		{
			emergencyContactPhone.parent().parent().css('background-color', 'white');
		}
		
		var industry = $("select[name='~Industry_Type']");
		if(industry.val().length <= 0)
		{
			industry.parent().parent().css('background-color', highlightColor);
			requiredFormFieldsValid = false;
			if(firstFocusControl==null)
				firstFocusControl = industry;
		}
		else
		{
			industry.parent().parent().css('background-color', 'white');
		}
		
		var functionalRole = $("select[name='~Functional_Role']");
		if(functionalRole.val().length <= 0)
		{
			functionalRole.parent().parent().css('background-color', highlightColor);
			requiredFormFieldsValid = false;
			if(firstFocusControl==null)
				firstFocusControl = functionalRole;
		}
		else
		{
			functionalRole.parent().parent().css('background-color', 'white');
		}
		
		var functionalLevel = $("select[name='~Functional_Level']");
		if(functionalLevel.val().length <= 0)
		{
			functionalLevel.parent().parent().css('background-color', highlightColor);
			requiredFormFieldsValid = false;
			if(firstFocusControl==null)
				firstFocusControl = functionalLevel;
		}
		else
		{
			functionalLevel.parent().parent().css('background-color', 'white');
		}
		
		var howDidYouHearAboutKronos = $("select[name='~How_did_you_hear']")
		if(howDidYouHearAboutKronos.val().length <= 0)
		{
			howDidYouHearAboutKronos.parent().parent().css('background-color', highlightColor);
			requiredFormFieldsValid = false;
			if(firstFocusControl==null)
				firstFocusControl = howDidYouHearAboutKronos;
		}
		else
		{
			howDidYouHearAboutKronos.parent().parent().css('background-color', 'white');
		}
		
		var howDidYouHearAboutKronosRepName = $("input[name='~How_did_you_hear-RepName']")
		if(howDidYouHearAboutKronosRepName.val().length <= 0)
		{
			howDidYouHearAboutKronosRepName.parent().parent().css('background-color', highlightColor);
			requiredFormFieldsValid = false;
			if(firstFocusControl==null)
				firstFocusControl = howDidYouHearAboutKronosRepName;
		}
		else
		{
			howDidYouHearAboutKronosRepName.parent().parent().css('background-color', 'white');
		}
		
		var numberOfEmployees = $("select[name='Number_Employees']")
		if(numberOfEmployees.val().length <= 0)
		{
			numberOfEmployees.parent().parent().css('background-color', highlightColor);
			requiredFormFieldsValid = false;
			if(firstFocusControl==null)
				firstFocusControl = numberOfEmployees;
		}
		else
		{
			numberOfEmployees.parent().parent().css('background-color', 'white');
		}
		
		var isFirstYear = $("select[name='~First_Year']")
		if(isFirstYear.val().length <= 0)
		{
			isFirstYear.parent().parent().css('background-color', highlightColor);
			requiredFormFieldsValid = false;
			if(firstFocusControl==null)
				firstFocusControl = isFirstYear;
		}
		else
		{
			isFirstYear.parent().parent().css('background-color', 'white');
		}
		
		var isUsedInMultipleCountries = $("select[name='~Use_Kronos_in_Multiple_Countries']")
		if(isUsedInMultipleCountries.val().length <= 0)
		{
			isUsedInMultipleCountries.parent().parent().css('background-color', highlightColor);
			requiredFormFieldsValid = false;
			if(firstFocusControl==null)
				firstFocusControl = isUsedInMultipleCountries;
		}
		else
		{
			isUsedInMultipleCountries.parent().parent().css('background-color', 'white');
		}
		
		var isPolicyAccepted = $('#cancellationPolicy');
		//console.log(isPolicyAccepted);
		if(!isPolicyAccepted.is(':checked'))
		{
			isPolicyAccepted.parent().parent().css('background-color', highlightColor);
			requiredFormFieldsValid = false;
			if(firstFocusControl==null)
				firstFocusControl = isPolicyAccepted;			
		}
		else
		{
			isPolicyAccepted.parent().parent().css('background-color', 'white');
		}
		
		var productFamily = $("input[name='~Product_Family']");
		if(validateProductFamilySelections()!=true)
		{
			productFamily.parent().parent().css('background-color', highlightColor);
			requiredFormFieldsValid = false;
			if(firstFocusControl==null)
				firstFocusControl = isPolicyAccepted;			
		}
		else
		{
			productFamily.parent().parent().css('background-color', 'white');
		}
		
		if(!requiredFormFieldsValid)
			firstFocusControl.focus();
		
		if(requiredFormFieldsValid)
		{
		
			// add throbber because IE sucks balls and won't display an animated gif properly
			document.getElementById("throbber").setAttribute("src","http://www.kronos.com/microsites/Spring2011Summit/img/body/wait.gif");
			setTimeout('document.getElementById("throbber").src = "http://www.kronos.com/microsites/Spring2011Summit/img/body/wait.gif"', 200); 
		
			// display modal dialog and wait for Eloqua to redirect us to the thank you page
			$("#processing").modal({minHeight:150, opacity:75, overlayCss: {backgroundColor:"#000"}});
			
			document.kronosForm.Email.name = "~Email"
			return requiredFormFieldsValid;
		}
		else
		{
			return requiredFormFieldsValid;
		}

	}
	
	function validateProductFamilySelections()
	{
		var productArray = []
		productArray = $("input[name='~Product_Family']:checked").getCheckboxVal()
		if(productArray.length > 0)
			return true;
		else
			return false;
	}
	
	function validateEmail($email) 
	{
	var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
	if( !emailReg.test( $email ) ) {
		return false;
	} else {
		return true;
	}
}


