﻿var _sCookieName = "chk.prefs.ropu";
var _dCookieExpireDate = new Date();
_dCookieExpireDate.setUTCFullYear( _dCookieExpireDate.getUTCFullYear()+5 );
var _oCookieOptions = {
	expiresAt: _dCookieExpireDate
}

//document.domain = "chk.com"; //SP2010 hates this!

//<a id="click-on-load" href="/Owners/Registration/Pages/SignUp.aspx?iframe=true&width=510&height=510" rel="prettyPhoto" >Sign up for our newsletters!</a>

$(document).ready( function(){

	if( $("html.ie6").length != 1 ){

		initLoadPopup();
		$("#popup-content .buttonRow").append(('<ul id="validationList"></ul>'));
		
	
		//Set ignore cookie on No Thanks link
		$("#no-popup-thanks").click( function(){
			$.cookies.set(_sCookieName, "false", _oCookieOptions );
			$.prettyPhoto.close()
		});
		$("#ask-again-later").click( function(){
			$.cookies.del(_sCookieName);
			$.prettyPhoto.close()
		});
		
		$("#popup-content .sp-group-parent .sp-group").eq(0).addClass("first");
		$("#popup-content .sp-group-parent .sp-group:last").addClass("last");
		
		if ($("#popup-content span.success").length > 0)
		{
			$.cookies.set(_sCookieName, "false", _oCookieOptions );
			var success = $("#popup-content span.success");
			success.siblings('fieldset').css('display', 'none');
			success.after( $("#footer-socials").addClass("success") );
			$("#footer-socials li.label").text("You can also find us on:");
			$("#footer-socials ul").append('<li class="label">by searching Chesapeake Energy.</li>');
			//parent.removePopupCookieControls();
			$("#popupCookieControls", window.parent.document).remove();
		}
	
		RollThroughTheLabelsAndDefineTerms();
		ToggleMailingAddressRequirements()
		ToggleOwnerNumberRequirements();
		RollThroughRequiredFieldsAndColorThem();
		
		$('input[type=text]').blur(function(){
			RollThroughRequiredFieldsAndColorThem();
		});
	
		$("#popup-content input[type=checkbox]").each( 
			function(i)
			{
				var thisLabel = $("#popup-content label[for=" + $(this).attr("id") + "]");
				//alert(thisLabel.text())
				if( thisLabel.text() == "The Play" || thisLabel.text() == "Community Ties" || thisLabel.text() == "Yes! I want to receive a free Chesapeake gift." ) 
				{
					$(this).click( function() { ToggleMailingAddressRequirements() } )
				}
			}
		);
		
		$("#popup-content .royalty input[type=radio]").click( 
			function()
			{
				ToggleOwnerNumberRequirements();
			}
		);
	
		$("#popup-content .buttonRow input").eq(0).click( 
			function()
			{
				return DoValidation();
			}
		);
		
		if ( $(".safari #popup-content").length > 0 ){
			$(".safari fieldset.name li input:text").css("width","119px");
			$(".safari fieldset.giftOption li label").css("width","250px");
			$(".safari fieldset.regional li label").css("width","170px");
			$(".safari fieldset.print li label").css("width","170px");
			$(".safari fieldset.cngOptions li label").css("width","170px");
		}
	}
});

function DoValidation(){
	
	var valid = true;
	
	$("#validationList").empty();
	
	var email = "";
	var cmail = "";
	
	$("#popup-content input[type=text]").each( 
		function(i)
		{
			var thisLabel = $("#popup-content label[for=" + $(this).attr("id") + "]");
			var trimmedLabelText = trim12(thisLabel.text());
			if(trimmedLabelText.indexOf("*") > -1)
			{
				if( trim12( $(this).val() ) == '' ) 
				{
					$(this).css("background-color","#ffa");
					$("#validationList").append('<li>'+trimmedLabelText+'  must contain a value.</li>');
					valid = false;
				}
			}
			if(trimmedLabelText.indexOf("*Email") > -1){
				email = trim12( $(this).val() );
				if(!isEmail(email) && email != "" ){
					$(this).css("background-color","#ffa");
					$("#validationList").append('<li>*Email must be a valid address.</li>');
					valid = false;
				}
			}
			if(trimmedLabelText.indexOf("*Confirm Email") > -1){
				cmail = trim12( $(this).val() );
				if(!isEmail(cmail) && cmail != "" ){
					$(this).css("background-color","#ffa");
					$("#validationList").append('<li>*Confirm Email must be a valid address.</li>');
					valid = false;
				}

				if(email != cmail){
					$(this).css("background-color","#ffa");
					$("#validationList").append('<li>*Email and *Confirm Email values must match.</li>');
					valid = false;
				}
			}
			
		}
	);
	
	ShowValidationList();
	
	return valid;
}

function initLoadPopup() {
	
	// Do not modify DOM if we're in edit mode
	if ($("html").hasClass("editmode")) {
		return;
	}
	
	// Only process if 
	if ($('#click-on-load').length == 0) {
		return;
	}

	// Select web parts
	$("#click-on-load").each( function() {

		// Attempt cookie retrieval
		var sCookieVal = $.cookies.get( _sCookieName );
		if (sCookieVal == null){ $(this).click(); }
	});
	
	var cookieTrolls = '<span id="popupCookieControls">'
	cookieTrolls += '<a id="no-popup-thanks" href="javascript:;">No thanks, please don\'t ask again.</a> | '
	cookieTrolls += '<a id="ask-again-later" href="javascript:;">Not now, ask me again later.</a></span>'
	$(".pp_fade .pp_details").prepend(cookieTrolls);
}



function RollThroughTheLabelsAndDefineTerms(){
	$("#popup-content label span").each( 
		function(i)
		{
			DefineTerm($(this), "royalty owner", "A royalty owner is someone who is currently receiving royalty payments from a producing Chesapeake well.");
			DefineTerm($(this), "mineral owner", "A mineral owner is someone who owns mineral rights, in this case oil or natural gas.");
			DefineTerm($(this), "Royalty Owner Number", "Your Chesapeake Owner Number can be found in the bottom left-hand corner of your revenue check stub.");
		}		
	);
}

function DefineTerm(sourceObject, term, definition){
	if( trim12(sourceObject.text().toLowerCase()).indexOf(term.toLowerCase()) > -1){
		sourceObject.addClass("defined").attr("title", definition);
	}
}

function RollThroughRequiredFieldsAndColorThem(){
	$("#popup-content input[type=text]").each( 
		function(i)
		{
			var trimmedLabelText = trim12($("#popup-content label[for=" + $(this).attr("id") + "]").text());
			if(trimmedLabelText.indexOf("*") > -1)
			{
				if( trim12( $(this).val() ) == '' ) $(this).css("background-color","#ffd");
				else $(this).css("background-color","#fff");
			}
			else $(this).css("background-color","#fff");
		}
	);
}

function ToggleMailingAddressRequirements(){
	
	if( AddressIsRequired() )
	{
		
		$("#popup-content input[type=text]").each( 
			function(i)
			{
				var thisLabel = $("#popup-content label[for=" + $(this).attr("id") + "]");
				var trimmedLabelText = trim12(thisLabel.text());
				if( trimmedLabelText == "Address 1" || trimmedLabelText == "*Address 1" || 
					trimmedLabelText == "City/State/Zip" || trimmedLabelText == "*City/State/Zip" ||
					trimmedLabelText == "Zip" || trimmedLabelText == "*Zip" ) 
				{
					switch(trimmedLabelText)
					{
						case "Address 1":
							thisLabel.text("*Address 1");
							break;
						case "City/State/Zip":
							thisLabel.text("*City/State/Zip");
							break;
						case "Zip":
							thisLabel.text("*Zip");
							break;
					}
				}
			}
		);
	}
	else
	{
		$("#popup-content input[type=text]").each( 
			function(i)
			{
				var thisLabel = $("#popup-content label[for=" + $(this).attr("id") + "]");
				var trimmedLabelText = trim12(thisLabel.text());
				if( trimmedLabelText == "Address 1" || trimmedLabelText == "*Address 1" || 
					trimmedLabelText == "City/State/Zip" || trimmedLabelText == "*City/State/Zip" ||
					trimmedLabelText == "Zip" || trimmedLabelText == "*Zip" ) 
				{
					switch(trimmedLabelText)
					{
						case "*Address 1":
							thisLabel.text("Address 1");
							break;
						case "*City/State/Zip":
							thisLabel.text("City/State/Zip");
							break;
						case "*Zip":
							thisLabel.text("Zip");
							break;
					}
				}
			}
		);
	}
	RollThroughRequiredFieldsAndColorThem();
}

function ToggleOwnerNumberRequirements(){
	
	if( OwnerNumberIsRequired() )
	{
		
		$("#popup-content .royalty input[type=text]").each( 
			function(i)
			{
				var thisLabel = $("#popup-content label[for=" + $(this).attr("id") + "]");
				var trimmedLabelText = trim12(thisLabel.text());
				//alert(trimmedLabelText);
				if( trimmedLabelText == "Royalty Owner Number" ) thisLabel.children("span").eq(0).text("*Royalty Owner Number");
			}
		);
	}
	else
	{
		$("#popup-content .royalty input[type=text]").each( 
			function(i)
			{
				var thisLabel = $("#popup-content label[for=" + $(this).attr("id") + "]");
				var trimmedLabelText = trim12(thisLabel.text());
				//alert(trimmedLabelText);
				if( trimmedLabelText == "*Royalty Owner Number" ) thisLabel.children("span").eq(0).text("Royalty Owner Number");
			}
		);
	}
	RollThroughRequiredFieldsAndColorThem();
}



function ShowValidationList(){
	var validationList = $("#validationList");
	if( validationList.children("li").size() > 0 ) validationList.fadeIn().fadeTo(4000,1,function(){ $(this).fadeOut(); })
	validationList.click(function(){ $(this).stop().fadeOut(100); });
}

function AddressIsRequired()
{
	var result = false;
	$("#popup-content input[type=checkbox]").each( 
		function(i)
		{
			var thisId = $(this).attr("id");
			var thisLabel = $("#popup-content label[for="+thisId+"]");
			
			if( thisLabel.text() == "The Play" || thisLabel.text() == "Community Ties" || thisLabel.text() == "Yes! I want to receive a free Chesapeake gift." )  
			{
				if($(this).attr('checked')) 
				{ 
					//alert('! ' + thisLabel.text() + ' : ' + $(this).attr('checked') );
					result = true; 
				}
			}
		}
	);
	return result;
}

function OwnerNumberIsRequired()
{
	var result = false;
	var royaltyRadioName = $("#popup-content .royalty input:radio").eq(0).attr("name"); 
	if( $("#popup-content .royalty input:radio[name="+royaltyRadioName+"]:checked").val() == "Yes" ) 
		result = true;
	return result;
}

function AddressIsProvided()
{
	var result = true;
	$("#popup-content input[type=text]").each( 
		function(i)
		{
			var thisId = $(this).attr("id");
			var thisLabel = $("#popup-content label[for="+thisId+"]");
			var trimmedLabelText = trim12(thisLabel.text());
			
			//console.log ('label', trimmedLabelText);

			if( trimmedLabelText == "Address 1" || trimmedLabelText == "*Address 1" || 
				trimmedLabelText == "City/State/Zip" || trimmedLabelText == "*City/State/Zip" ||
				trimmedLabelText == "Zip" || trimmedLabelText == "*Zip" ) 
			{
				
				//console.log ('value', $(this).val() );
				
				if( trim12( $(this).val() ) == '' ) 
				{ 
					result = false;
				}
			}
		}
	);
	return result;
}

function ValidateMailingAddress()
{
	var valid = true;
	$("#popup-content input[type=text]").each( 
		function(i)
		{
			var thisLabel = $("#popup-content label[for=" + $(this).attr("id") + "]");
			var trimmedLabelText = trim12(thisLabel.text());
			
			//console.log ('label', trimmedLabelText);

			if( trimmedLabelText == "Address 1" || trimmedLabelText == "*Address 1" || 
				trimmedLabelText == "City/State/Zip" || trimmedLabelText == "*City/State/Zip" ||
				trimmedLabelText == "Zip" || trimmedLabelText == "*Zip" ) 
			{
				if( trim12( $(this).val() ) == '' ) 
				{ 
					valid = false;
				}
			}
		}
	);
	return valid;
}

function isEmail(address) {
	return (address.toLowerCase().search(/^([a-z]+)([a-z0-9\-\_\.]{1,100})([a-z0-9]+)\@([a-z0-9]+)([a-z0-9\-\.]*)([a-z0-9]+)\.([a-z]{2,6})$/) != -1);
}

