var defaultValues = { //register defaultValues here
	moveFirstname:"First Name",
	moveLastname:"Last Name",
	moveDate:"mm/dd/yyyy",
	moveTocity:"city",
    moveToCity:"City",
    moveFromzip:"Zip",
    moveFromZip:"ZIP Code",
	moveEmail:"my email address",
	movePhoneno:"my phone number",
	locSerWhat:'(ex:pizza)',
	locSerWhere:'City, ST or ZIP',
	regEmail:"Email Address",
	regEmail2:"Re-Type Your Email Address",
    regZip:"ZIP Code"
};

function isValidEmailAddress(emailAddress) {
    var pattern = new RegExp(/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i);
    return pattern.test(emailAddress);
}

function recordGoogleAnalyticsClick(link, category, action) {
  try {
      _gaq.push(['_trackEvent', category, action]);
      if (link) {
          setTimeout('document.location.href = "' + link.href + '"', 100);
      }
   }catch(err){
      //noop
   }
}

$(document).ready(	function() {
	var loc = window.location.toString();
	if (loc.indexOf('/my-deals') == -1) {
		$("select#theSelectSearch option[selected]").removeAttr("selected");
        $("select#theSelectSearch option[value='1']").attr("selected", "selected");
		$('#searchKeyword').attr('value', 'Search articles');
	}
	$("#theSelectSearch").change(function(){
		var foo = document.getElementById('theSelectSearch');
		var indx = foo.selectedIndex;
		if (indx == 0 && $('#searchKeyword').attr('value') == 'Search articles' ) {
			$('#searchKeyword').attr('value', 'Search deals');
		} else if (indx == 1 && $('#searchKeyword').attr('value') == 'Search deals' ){
			$('#searchKeyword').attr('value', 'Search articles');
		}
	});

	$("#searchKeyword").keyup(function(e){
      if(e.keyCode == 13) {
        doSearch();
      }
	});

	$("#searchDealsBtn").click(function(){
        doSearch();
	});

    function doSearch() {
 		if ($('#searchKeyword').attr('value') == 'Search deals' || $('#searchKeyword').attr('value') == '' || $('#searchKeyword').attr('value') == 'Search articles' ) {
		} else {
			var foo = document.getElementById('theSelectSearch');
			var indx = foo.selectedIndex;
			var searchKeyword = $('#searchKeyword').attr('value');
			if (indx == 0) {
				//var theURL = $('#searchDealsURL').text();
                var theURL = mydealsSearchPath + encodeURIComponent(searchKeyword) + mydealsSearchQuery;
				window.location = theURL;
			} else {
				$('#_77_keywords2').attr('value', searchKeyword);
				$('#searchSubmitForm2').submit();
			}
		}
    }

	$("#searchKeyword").click(function(){
		if (($('#searchKeyword').attr('value') == 'Search deals') || ($('#searchKeyword').attr('value') == 'Search articles')) {
			$('#searchKeyword').attr('value', '');
		}
	});


	var mmBtId = mmGetBtId().toString();

	$(".btIdSection").each(function() {
		var theSection = $(this).html();
		var replaced = theSection.replace(/thisBtId/g, mmBtId);
		$(this).html(replaced);
	});

    $("#moveDate").datepicker({showOn: 'both', buttonImage: '/Mover-portlet/images/caledarLogo.gif', buttonImageOnly: true});

    var isPwdBlockSelected = $('isPwdBlockSelectedValueFromServer').attr('value');
    //alert(isPwdBlockSelected);
    if (isPwdBlockSelected == "display") {
        $('#changePassBlock').show();
        $('#changePassPng').hide();
    }
    $("#changePassPng").click(function(){
        $('#changePassBlock').show();
        $('#changePassPng').hide();
        $('#isPwdBlockSelected').attr('value', 'display');
    });

    $(".hasDefVal").each(function() {
        var idd = $(this).attr('id');
        var thisVal = $(this).attr('value');
        var theVal = "";
        if ($(this).attr('value') == '') {
            jQuery.each(defaultValues, function(key, value) {
                if (key == idd) {
                    theVal = value;
                    return;
                }
            });
        }
        if (theVal != "") {
            $(this).attr('value', theVal);
        }
    });

    $(".hasDefVal").click(function(){
        var idd = $(this).attr('id');
        var thisVal = $(this).attr('value');
        var thisRef = this;
        jQuery.each(defaultValues, function(key, value) {
            if (key == idd && value == thisVal) {
                $(thisRef).attr('value', '');
                return;
            }
        });
    });

    $(".hasDefVal").blur(function(){
        var idd = $(this).attr('id');
        var thisVal = $(this).attr('value');
        var theVal = "";
        if ($(this).attr('value') == '') {
            jQuery.each(defaultValues, function(key, value) {
                if (key == idd) {
                    theVal = value;
                    return;
                }
            });
        }
        if (theVal != "") {
            $(this).attr('value', theVal);
        }
    });

    $("form").submit(function(){
      /*  if ($(this).attr('action') == "/my-neighborhood") {
            var WHAT = $("#locSerWhat").attr("value");
            var WHERE = $("#locSerWhere").attr("value");
            jQuery.cookie("WHAT", WHAT);
            jQuery.cookie("WHERE", WHERE);
        }   */
        $(this).find('.hasDefVal').each(function(){
            var idd = $(this).attr('id');
            var thisVal = $(this).attr('value');
            var thisRef = this;
            jQuery.each(defaultValues, function(key, value) {
                if (key == idd && value == thisVal) {
                    $(thisRef).attr('value', '');
                    return;
                }
            });
        });
    });

	/* if (jQuery.cookie("WHAT") != 'null') {
        $("#locSerWhat").attr("value", jQuery.cookie("WHAT"));
        $("#locSerWhere").attr("value", jQuery.cookie("WHERE"));
        jQuery.cookie("WHAT", null);
        jQuery.cookie("WHERE", null);

    }     */

    //email registration
    var clicked = false;
     $("#yourEmail").click(function(){
         var em = $("#yourEmail").attr("value");
        if (!clicked || em == 'email@example.com') {
          $("#yourEmail").attr('value', '');
        }
          clicked = true;
    });

     $("#yourEmail").blur(function(){
         if ($(this).attr('value') == '') {
             $("#yourEmail").attr('value', 'email@example.com');
         }
    });

    $('#em_sup_wrapper form').submit(function() {
        var em = $("#yourEmail").attr("value");
        if (!isValidEmailAddress(em) || em == 'email@example.com') {
            $(".emError").show();
            $("#yourEmail").attr('value', 'email@example.com');
            clicked = false;
            return false;
        } else {
            return true;
        }
    });

    var errs = $(".err").html(); var trimmed = '' ;
   if (errs) {
	    trimmed = errs.replace(/^\s+|\s+$/g, '') ;
	}
   if ($('span[class="portlet-title-text"]').html() == 'Sign In') {
       var totErrs = "";
        $(".portlet-msg-error").each(function(){
             totErrs += $(this).html() + "<br/>";
        });
       var cont =  $('#sign_in_wrapper').html();
       $('#main-content').html('<div id="sign_in_wrapper">' + cont + '</div');
        if (trimmed != '') {
            $(".err").html(totErrs);
        }
   }
    if (trimmed != '') {
	    $(".err").show();
	}
    var auto_hre = $('.get_auto_href').attr('href');
    $('auto_href').attr('href', auto_hre);
    if($('#joinFree').length == 0) {
         $('#join_it_is_free').hide();
    }



});



function mmPopDial(	title, subTitle, mmBody, footer){ 
return '<div id="mmDig_wrapper" style="z-index:9999">\
<table border="0" cellspacing="0" cellpadding="0">\
  <tr>\
    <td width="18" height="18" id="corn_1">&nbsp;</td>\
    <td height="18" id="shad_top">&nbsp;</td>\
    <td width="22" height="18" id="corn_2"><img src="/mymove-theme/images/myMove/buttons_logos/X_.png" class="mmX"></td>\
  </tr>\
  <tr>\
    <td width="18" id="shad_left"><img src="/mymove-theme/images/myMove/buttons_logos/ie.png" width="18" height="18"></td>\
    <td>\
    <div class="mmDigData">\
    <div class="mmDigTytle">'+ title + '</div>\
    <div class="mmDigSubTytle">' + subTitle  + '</div>\
    <div class="mmDigForm">'+ mmBody  +'</div>\
    <div class="mmDigFooter">'+ footer  +'</div>\
    </div>\
    </td>\
    <td width="22" id="shad_right">&nbsp;</td>\
  </tr>\
  <tr>\
    <td width="18" height="22" id="corn_4">&nbsp;</td>\
    <td height="22" id="shad_bottom">&nbsp;</td>\
    <td width="22" height="22" id="corn_3">&nbsp;</td>\
  </tr>\
</table>\
</div>';
}

function mmGetSessionData(attrName){
    var value = '';
    url = "/delegate/mymovesessionservlet?sessionAttr="+ attrName;
    $.ajax({
      url: url,
      dataType: 'json',
      async: false,
      success: function(data) {
        value = data.mm_attr;
      }
    });
    return value;
}

function mmGetBtId(){
    return mmGetSessionData('MYMOVE_BTID');
}
