// JavaScript Document

$(document).ready(function(){

	var view = $.query.get('view');
	if (view=='privacy'){
		//$("#privacy").load("/boxy.asp?Action=ShowProjects&UserID="+UserID);
		var box = new Boxy($("#privacy"), {modal: true});
	}

	if (view=='sweepsrules'){
		//$("#privacy").load("/boxy.asp?Action=ShowProjects&UserID="+UserID);
		var box = new Boxy($("#terms"), {modal: true});
	}
	
	$('#terms_link').click(function(){
		//$("#terms").load("/boxy.asp?Action=ShowProjects&UserID="+UserID);
		var box = new Boxy($("#terms"), {modal: true});
	});

	$('#terms_closedv a').click(function(){
		$("#terms").hide();
	});
	
	$('#privacy_link').click(function(){
		//$("#privacy").load("/boxy.asp?Action=ShowProjects&UserID="+UserID);
		var box = new Boxy($("#privacy"), {modal: true});
	});

	$('#privacy_closedv a').click(function(){
		$("#privacy").hide();
	});
	
	
	//Thank You Page – Coupon					JxwkCPWV9AIQ84TtzgM
	//Thank You Page – Sweeps					CUlRCO2W9AIQ84TtzgM
	//Thank You Page – Coupon/Sweeps			qTl7COWX9AIQ84TtzgM
	//Link to Store Locator – Coupon				_CJSCN2Y9AIQ84TtzgM
	//Link to KM Site – Coupon						X-wuCNWZ9AIQ84TtzgM
	//Link to FB – Coupon							xg1SCM2a9AIQ84TtzgM
	//Link to Twitter – Coupon						TgITCMWb9AIQ84TtzgM
	//Email to Friend – Coupon						NBKkCL2c9AIQ84TtzgM
	//Coupon Landing Page						FtH-CKWf9AIQ84TtzgM
	//Print link on Coupon Landing – Coupon		JWGNCI2i9AIQ84TtzgM
	//Find Store Link on Coupon Landing - Coupon	kxpACIWj9AIQ84TtzgM
	
	if ($('#sendemail_fm').length==1){
		adConversion('ThankYou');
	}else{
		adConversion('Home'); //fire an AdWords conversion just for visiting the home page
	}

	$('.out').click(function(){
		//alert('out: '+$(this).attr('href'));
		_gaq.push(['_trackPageview',$(this).attr('href')]);
		
		if ($(this).attr('href').indexOf('facebook')>1) {
			//alert('adConversion: Facebook');
			adConversion('Facebook');
		}

		if ($(this).attr('href').indexOf('twitter')>1) {
			//alert('adConversion: Twitter');
			adConversion('Twitter');
		}

		if ($(this).attr('href').indexOf('www.kellymoore.com')>1) {
			//alert('adConversion: KellyMoore');
			adConversion('KellyMoore');
		}
		
		//return false;
	});
	
	
	
	$("#signup_fm").submit(function(){
		if ($("input#FirstName").val().toUpperCase()=="")
		{alert("Please enter a value for the \"First Name\" field.");$("input#FirstName").focus();return false;}
		if ($("input#FirstName").val().toUpperCase().length<2)
		{alert("Please enter at least 2 characters in the \"First Name\" field.");$("input#FirstName").focus();return false;}
		if ($("input#FirstName").val().toUpperCase().length>50)
		{alert("Please enter at most 50 characters in the \"First Name\" field.");$("input#FirstName").focus();return false;}
		if (!isValidInput($("input#FirstName").val().toUpperCase()))
		{alert("Please enter only letter, whitespace and \"-.\" characters in the \"First Name\" field.");$("input#FirstName").focus();return false;}
		
		if ($("input#LastName").val().toUpperCase()=="")
		{alert("Please enter a value for the \"Last Name\" field.");$("input#LastName").focus();return false;}
		if ($("input#LastName").val().toUpperCase().length<2)
		{alert("Please enter at least 2 characters in the \"Last Name\" field.");$("input#LastName").focus();return false;}
		if ($("input#LastName").val().toUpperCase().length>50)
		{alert("Please enter at most 50 characters in the \"Last Name\" field.");$("input#LastName").focus();return false;}
		if (!isValidInput($("input#LastName").val().toUpperCase()))
		{alert("Please enter only letter, whitespace and \"-.\" characters in the \"Last Name\" field.");$("input#LastName").focus();return false;}
		
		if ($("input#Phone").val().toUpperCase()=="")
		{alert("Please enter a value for the \"Phone\" field.");$("input#Phone").focus();return false;}
		if ($("input#Phone").val().toUpperCase().length<10)
		{alert("Please enter at least 10 characters in the \"Phone\" field.");$("input#Phone").focus();return false;}
		if ($("input#Phone").val().toUpperCase().length>15)
		{alert("Please enter at most 15 characters in the \"Phone\" field.");$("input#Phone").focus();return false;}
		if (!(/^[0-9( )-]{10,}$/.test($("input#Phone").val().toUpperCase())))
		{alert("Please enter only digit and \"-\" characters in the \"Phone\" field.");$("input#Phone").focus();return false;}
		
		if ($("input#Email").val().toUpperCase()=="")
		{alert("Please enter a value for the \"Email\" field.");$("input#Email").focus();return false;}
		if (!isEmail($("input#Email").val().toUpperCase()))
		{alert("Please enter a valid email address");$("input#Email").focus();return false;}

		
		if ($("input#Address1").val().toUpperCase()=="")
		{alert("Please enter a value for the \"Address\" field.");$("input#Address1").focus();return false;}
		if ($("input#Address1").val().toUpperCase().length<5)
		{alert("Please enter at least 5 characters in the \"Address\" field.");$("input#Address1").focus();return false;}
		if ($("input#Address1").val().toUpperCase().length>50)
		{alert("Please enter at most 50 characters in the \"Address\" field.");$("input#Address1").focus();return false;}
		if (!(/^([a-zA-Z0-9]|[ .,-]|[#]){1,}$/.test($("input#Address1").val().toUpperCase())))
		{alert("Please enter only letter, digit, whitespace and \"-.,#\" characters in the \"Address\" field.");$("input#Address1").focus();return false;}
		
		if ($("input#City").val().toUpperCase()=="")
		{alert("Please enter a value for the \"City\" field.");$("input#City").focus();return false;}
		if ($("input#City").val().toUpperCase().length<2)
		{alert("Please enter at least 2 characters in the \"City\" field.");$("input#City").focus();return false;}
		if ($("input#City").val().toUpperCase().length>25)
		{alert("Please enter at most 25 characters in the \"City\" field.");$("input#City").focus();return false;}
		if (!(/^[a-z A-Z]{2,}$/.test($("input#City").val().toUpperCase())))
		{alert("Please enter only letter in the \"City\" field.");$("input#City").focus();return false;}
		
		if ($("input#State").val().toUpperCase()=="")
		{alert("Please enter a value for the \"State\" field.");$("input#State").focus();return false;}
		if ($("input#State").val().toUpperCase().length<2)
		{alert("Please enter at least 2 characters in the \"State\" field.");$("input#State").focus();return false;}
		if ($("input#State").val().toUpperCase().length>25)
		{alert("Please enter at most 25 characters in the \"State\" field.");$("input#State").focus();return false;}
		if (!(/^[a-zA-Z]{2,}$/.test($("input#State").val().toUpperCase())))
		{alert("Please enter only letter in the \"State\" field.");$("input#State").focus();return false;}
		
		if ($("input#PostalCode").val().toUpperCase()=="")
		{alert("Please enter a value for the \"Zip Code\" field.");$("input#PostalCode").focus();return false;}
		if ($("input#PostalCode").val().toUpperCase().length<5)
		{alert("Please enter at least 5 characters in the \"Zip Code\" field.");$("input#PostalCode").focus();return false;}
		if ($("input#PostalCode").val().toUpperCase().length>15)
		{alert("Please enter at most 15 characters in the \"Zip Code\" field.");$("input#PostalCode").focus();return false;}
		if (!(/^([0-9]|[ -]){2,}$/.test($("input#PostalCode").val().toUpperCase())))
		{alert("Please enter only digit, whitespace and \"-\" characters in the \"Zip Code\" field.");$("input#PostalCode").focus();return false;}
		

		//if ($("select#Occupation").val()=="")
		//{alert("Please enter a value for the Category field.");$("input#Occupation").focus();return false;}

		//if ($("input#Agree:checked").val()==null)
		//{alert("Please check \"I Agree\".");$("input#Agree").focus();return false;}
		if ($('#CustomerType').val()==''){alert('Please select a Customer Type');$('#CustomerType').focus();return false;}

		if ($("input#State").val().toUpperCase()!="CA" && $("input#State").val().toUpperCase()!="AR" && $("input#State").val().toUpperCase()!="CA" && $("input#State").val().toUpperCase()!="ID" && $("input#State").val().toUpperCase()!="NV" && $("input#State").val().toUpperCase()!="OK" && $("input#State").val().toUpperCase()!="OR" && $("input#State").val().toUpperCase()!="TX" && $("input#State").val().toUpperCase()!="WA") {
			alert('Only residents in Arkansas, California, Idaho, Nevada, Oklahoma, Oregon, Texas and Washington may participate in this Sweepstakes.');
			return false;
		}
		
		if ($('#CommercialAccount2').attr('checked')==true) {
		    $('#signup_fm').append('<input type="hidden" name="_targetemail" value="test@rbgweb.com,gswartz@rbgadvertising.com">');
		    //$('#signup_fm').append('<input type="hidden" name="_fieldOrder" value="FirstName,LastName,CustomerType,Company,Phone,Email,Address1,City,State,PostalCode,CommercialAccount">');
		}

	});
	
	
	
	// locations.asp
	$("#fapform").submit(function(){
		if($.trim($("#fapform input[name='zip']").val())=='' && $.trim($("#fapform input[name='address2']").val())=='' && $.trim($("#fapform input[name='city']").val())=='' && $.trim($("#fapform input[name='state']").val())=='')
		{alert("Please enter your zip code or your address.");$("#fapform input[name='zip']").focus();return false;}
		else
		{
			if($.trim($("#fapform input[name='zip']").val())!='')
			{
				if(!isValidDigit($("#fapform input[name='zip']").val()))
				{alert("Please enter a valid zip code.");$("#fapform input[name='zip']").focus();return false;}
			}
			else
			{
				if($.trim($("#fapform input[name='city']").val())=='')
				{alert("Please enter a value for the \"City\" field.");$("#fapform input[name='city']").focus();return false;}
				if($.trim($("#fapform input[name='state']").val())=='')
				{alert("Please enter a value for the \"State\" field.");$("#fapform input[name='state']").focus();return false;}
			}
		}
	});
	
	
	$("#fapform2").submit(function(){
		if($.trim($("#fapform2 input[name='zip']").val())=='' && $.trim($("#fapform2 input[name='address2']").val())=='' && $.trim($("#fapform2 input[name='city']").val())=='' && $.trim($("#fapform2 input[name='state']").val())=='')
		{alert("Please enter your zip code or your address.");$("#fapform2 input[name='zip']").focus();return false;}
		else
		{
			if($.trim($("#fapform2 input[name='zip']").val())!='')
			{
				if(!isValidDigit($("#fapform2 input[name='zip']").val()))
				{alert("Please enter a valid zip code.");$("#fapform2 input[name='zip']").focus();return false;}
			}
			else
			{
				if($.trim($("#fapform2 input[name='city']").val())=='')
				{alert("Please enter a value for the \"City\" field.");$("#fapform2 input[name='city']").focus();return false;}
				if($.trim($("#fapform2 input[name='state']").val())=='')
				{alert("Please enter a value for the \"State\" field.");$("#fapform2 input[name='state']").focus();return false;}
			}
		}
	});

	
	$("#findarep").submit(function(){
		if($.trim($("#findarep input[name='zip']").val())=='')
		{alert("Please enter your zip code.");$("#findarep input[name='zip']").focus();return false;}
		if(!isValidDigit($("#findarep input[name='zip']").val()) || $("#findarep input[name='zip']").val().length != 5)
		{alert("Please enter a valid zip code.");$("#findarep input[name='zip']").focus();return false;}
	});
	
	$("#findarep_contact_fm").submit(function(){
		if($.trim($("#findarep_contact_fm input[name='Name']").val())=='')
		{alert("Please enter a value for the \"Name\" field.");$("#findarep_contact_fm input[name='Name']").focus();return false;}
		
		if($.trim($("#findarep_contact_fm input[name='Email']").val())=='')
		{alert("Please enter a value for the \"Email\" field.");$("#findarep_contact_fm input[name='Email']").focus();return false;}
		if (!isEmail($("#findarep_contact_fm input[name='Email']").val()))
		{alert("Please enter a valid email address.");$("#findarep_contact_fm input[name='Email']").focus();return false;}
		
		if($.trim($("#findarep_contact_fm textarea[name='Comments']").val())=='')
		{alert("Please enter a value for the \"Comments\" field.");$("#findarep_contact_fm textarea[name='Comments']").focus();return false;}
	});
	
	
	$("#emailmapinfo_fm").submit(function(){
		if($.trim($("#emailto").val())=='')
		{alert("Please enter a recipient.");$("#emailto").focus();return false;}
		if (!isEmail($("#emailto").val()))
		{alert("Please enter a valid recipient.");$("#emailto").focus();return false;}
		
		if($.trim($("#emailfrom").val())=='' || !isEmail($("#emailfrom").val()))
		{alert("From email address is not valid.");$("#emailfrom").focus();return false;}
	});

});







function comments_add(id)	//document.getElementById('blog_comment_form_'+id).scrollIntoView(false);
{$('#blog_comment_form_'+id).toggle(0,function(){$('#blog_comment_form_'+id+' textarea').focus();});}

function comments_show(id)
{$('#blog_comments_'+id).toggle(0,function(){document.getElementById('blog_comments_'+id).scrollIntoView(false);});}

function comments_scroll(id)
{
	var strUrl,strWID;
	strUrl=window.location.href;
	if(strUrl.indexOf("#w")>0)
	{
		strWID=strUrl.substring(strUrl.indexOf("#w")+2);
		if(strWID=='')
		{$('#blog_comment_form_'+id).show();document.getElementById('blog_comment_form_'+id).scrollIntoView(false);$('#blog_comment_form_'+id+' textarea').focus();}
		else
		{$('#blog_comments_'+strWID).show();document.getElementById('blog_comments_'+strWID).scrollIntoView(false);}
	}
}

function comments_check(id)
{
	$("#comment_fm_"+id).submit(function(){
		if($.trim($("form#comment_fm_"+id+" input[name='author']").val())=="")
		{alert("Please enter a value for \"your name\" field.");$("form#comment_fm_"+id+" input[name='author']").focus();return false;}
		if($.trim($("form#comment_fm_"+id+" input[name='link']").val())=="")
		{alert("Please enter a value for \"email\" field.");$("form#comment_fm_"+id+" input[name='link']").focus();return false;}
		if(!isEmail($("form#comment_fm_"+id+" input[name='link']").val()))
		{alert("Please enter a valid email address.");$("form#comment_fm_"+id+" input[name='link']").focus();return false;}
		if($.trim($("form#comment_fm_"+id+" textarea[name='content']").val())=="")
		{alert("Please enter a value for \"comments\" field.");$("form#comment_fm_"+id+" textarea[name='content']").focus();return false;}
	});
}


function adConversion(conversionType){

	//alert('AdWords Conversion Pixel: '+conversionType);

	switch(conversionType)
	{
	case "Home":
	  adConversionPixel('72WACJWS9AIQ84TtzgM');
	  break;
	case "ThankYou":
	  adConversionPixel('FtH-CKWf9AIQ84TtzgM');
	  break;
	case "PrintPrint":
	  adConversionPixel('JWGNCI2i9AIQ84TtzgM');
	  break;
	case "Twitter"://
	  adConversionPixel('TgITCMWb9AIQ84TtzgM');
	  break;
	case "Facebook":
	  adConversionPixel('xg1SCM2a9AIQ84TtzgM');
	  break;
	case "KellyMoore":
	  adConversionPixel('X-wuCNWZ9AIQ84TtzgM');
	  break;
	  
	case "StoreLocator":
	  adConversionPixel('_CJSCN2Y9AIQ84TtzgM');
	  break;
	case "StoreLocatoronCoupon":
	  adConversionPixel('kxpACIWj9AIQ84TtzgM');
	  break;

	case "Email":
	  adConversionPixel('NBKkCL2c9AIQ84TtzgM');
	  break;

	case "RegCoupon":
	  adConversionPixel('JxwkCPWV9AIQ84TtzgM');
	  break;
	case "RegSweeps":
	  adConversionPixel('CUlRCO2W9AIQ84TtzgM');
	  break;

	case "RegCouponSweeps":
	  adConversionPixel('qTl7COWX9AIQ84TtzgM');
	  break;
	  
	default:
	  //code to be executed if n is different from case 1 and 2
	}
}

function adConversionPixel(label){
var adConversionPixel = '<img height="1" width="1" style="border-style:none;" alt="" src="http://www.googleadservices.com/pagead/conversion/970670707/?label='+label+'&amp;guid=ON&amp;script=0"/>'
$('body').append(adConversionPixel);
}
