/**
 * The action for keyup and mouseup events on "Name" field
 *
 * @return void
 */
self.tipUp = function (el, keepAddressId)
{
    if (1 == arguments.length) {
        keepAddressId = false;
    }
    if(el.value.length > 2){
        $('AddressSuggestionsLinkBox').style.display='block';
		 openAddressSuggestions();
    }
    manageAddressId(keepAddressId);
}

/**
 * Fill or clear the hidden address id according to the selected tip "Type" and tip "Name"
 *
 * @return void
 */
self.manageAddressId = function (keepAddressId)
{
    if (!$('Name').valueByPopup) {
        $('Name').valueByPopup = $('Name').value;
        $('TravelTipForm_AddressId').valueByPopup = $('TravelTipForm_AddressId').value;
        $('Type').valueByPopup = $('Type').value;
        $('Name').disabledByDefault = $('Name').disabled;
        $('Name').backgroundColorByDefault = $('Name').style.backgroundColor;
    }
    if (($('Name').valueByPopup != $('Name').value) || $('Type').valueByPopup != $('Type').value) {
        if (false == keepAddressId) {
            $('TravelTipForm_AddressId').value = '';
        }
        $('Name').disabled = false;
        $('Name').style.backgroundColor = "#FFFFFF";
    } else {
        $('TravelTipForm_AddressId').value = $('TravelTipForm_AddressId').valueByPopup;
        $('Name').disabled = $('Name').disabledByDefault;
        $('Name').style.backgroundColor = $('Name').backgroundColorByDefault;
        //$('AddressSuggestionsLinkBox').style.display='none';
    }
}

/**
 * Disable the traveltip address fields that cannot be changed
 *
 * @return void
 */
self.updateTraveltip = function()
{
   if (!document.forms.travelTipForm) {
       return;
   }
   var form = document.forms.travelTipForm;
   var fields = new Array("Street", "Phone", "Email", "Url", "Name");

   var addrId = -1;
   var i;
   var idFld = document.forms.travelTipForm.AddressId;

   if (idFld.value != "") {
       addrId = idFld.value;
   }

   if (addrId > 0) {
       for (i = 0; i < fields.length; i ++) {
           if (form.elements[fields[i]].value != "") {
               form.elements[fields[i]].disabled=true;
               form.elements[fields[i]].style.backgroundColor = "#CCCCCC";
           }
       }
   }
}

/**
 * Clear the traveltip address fields that cannot be changed
 *
 * @return void
 */
self.clearTraveltip = function()
{
   if (!document.forms.travelTipForm) {
       return;
   }
   var form = document.forms.travelTipForm;
   var fields = new Array("Street", "Phone", "Email", "Url", "Name");

   var i;

   for (i = 0; i < fields.length; i ++) {
       form.elements[fields[i]].disabled=false;
       form.elements[fields[i]].style.backgroundColor = "#FFFFFF";
   }
}

self.setPrice = function(price)
{
    price_save = price;
    switch (price) {
        case 'bad':
			if ($('Price').value!=price) {
            	$('goodPriceImg').src = '/images/icons/tip_price_good_n.gif';
            	$('badPriceImg').src = '/images/icons/tip_price_bad.gif';
			} else {
			    $('goodPriceImg').src = '/images/icons/tip_price_good_n.gif';
            	$('badPriceImg').src = '/images/icons/tip_price_bad_n.gif';
				price_save="";
			}
            break;
        case 'good':
			if ($('Price').value!=price) {
            	$('goodPriceImg').src = '/images/icons/tip_price_good.gif';
            	$('badPriceImg').src  = '/images/icons/tip_price_bad_n.gif';
			} else {
				$('goodPriceImg').src = '/images/icons/tip_price_good_n.gif';
            	$('badPriceImg').src  = '/images/icons/tip_price_bad_n.gif';
				price_save="";
			}
            break;
    }
	$('Price').value = price_save;
}

self.setPerformance = function(performance)
{
	performance_save = performance;
    switch (performance) {
        case 'bad':
			if ($('Performance').value!=performance) {
            	$('badPerformanceImg').src = '/images/icons/tip_performance_bad.gif';
            	$('middlePerformanceImg').src = '/images/icons/tip_performance_middle_n.gif';
            	$('excellentPerformanceImg').src = '/images/icons/tip_performance_excellent_n.gif';
			} else {
				$('badPerformanceImg').src = '/images/icons/tip_performance_bad_n.gif';
            	$('middlePerformanceImg').src = '/images/icons/tip_performance_middle_n.gif';
            	$('excellentPerformanceImg').src = '/images/icons/tip_performance_excellent_n.gif';
				performance_save="";
			}
            break;
        case 'middle':
			if ($('Performance').value!=performance) {
            	$('badPerformanceImg').src = '/images/icons/tip_performance_bad_n.gif';
            	$('middlePerformanceImg').src = '/images/icons/tip_performance_middle.gif';
            	$('excellentPerformanceImg').src = '/images/icons/tip_performance_excellent_n.gif';
			} else {
				$('badPerformanceImg').src = '/images/icons/tip_performance_bad_n.gif';
            	$('middlePerformanceImg').src = '/images/icons/tip_performance_middle_n.gif';
            	$('excellentPerformanceImg').src = '/images/icons/tip_performance_excellent_n.gif';
				performance_save="";
			}
            break;
        case 'excellent':
			if ($('Performance').value!=performance) {
            	$('badPerformanceImg').src = '/images/icons/tip_performance_bad_n.gif';
            	$('middlePerformanceImg').src = '/images/icons/tip_performance_middle_n.gif';
            	$('excellentPerformanceImg').src = '/images/icons/tip_performance_excellent.gif';
			} else {
				$('badPerformanceImg').src = '/images/icons/tip_performance_bad_n.gif';
            	$('middlePerformanceImg').src = '/images/icons/tip_performance_middle_n.gif';
            	$('excellentPerformanceImg').src = '/images/icons/tip_performance_excellent_n.gif';
				performance_save="";
			}
            break;
    }
	$('Performance').value = performance_save;
}

