$(document).ready(function() {
	$('#noJSmessage').hide(); // hides the top JS message across the site.
	
	$('#accountsub').click(function() {
		$('#accountform').submit();
	});
		$('#reqpass').click(function() {
		$('#lostpassform').submit();
	});

	$('#profilesub, #editlink').click(function() {
		$('#profileform').submit();
	});
	$('#supportsub').click(function() {
		$('#supportform').submit();
	});
	$('#epsub').click(function() {
		$('#epform').submit();
	});
	$('#reportsub').click(function() {
		$('#repform').submit();
	});	
	$('#createsub').click(function() {
		$('#createform').submit();
	});	
	$('#subform').click(function() {
		if ($('#murl').val() == "" || $('#murl').val() == "http://www.your-web-address.com/your-page.htm") {
			$('#errorbox').text("Please enter a URL (web address).").addClass("errorbox");
			return false;
		} else {
			if ($('#mcat').val() == 0) {
				$('#errorbox').text("Please select a category.").addClass("errorbox");
				return false;
			} else {
				if ($('#nli_check').val() == 0) {
					$('#errorbox').text("You must login to earn credits (click again to shorten without login).").addClass("errorbox");
					$('#nli_check').val(1);
					return false;
				} else {	
					$('#shortenurl').submit();
				}
			}
		}
	});

/*
	$('#captcha').simpleCaptcha({
		numImages: 5
	});
*/

	$('a.share').hover(function() {
		$(this).parent().parent().css("background-color","#D6FFCD");}, 
	function () {
		$(this).parent().parent().css("background-color","#FFFFFF"); return false;
	});
	
	$('a.share').focus(function() {
		$(this).parent().parent().css("background-color","#D6FFCD"); return false;
	});
	$('a.share').blur(function() {
		$(this).parent().parent().css("background-color","#FFFFFF"); return false;
	});

	
	$('a.report').hover(function() {
		$(this).parent().parent().css("background-color","#FFDDDD");}, 
	function () {
		$(this).parent().parent().css("background-color","#FFFFFF"); return false;
	});
	$('a.report').focus(function() {
		$(this).parent().parent().css("background-color","#FFDDDD"); return false;
	});
	$('a.share').blur(function() {
		$(this).parent().parent().css("background-color","#FFFFFF"); return false;
	});

$('a.s').click(function() {
$(this).parent().next('.b').show(); return false;
});


$('a.canmap').click(function() {
$(this).parent().parent().parent().hide(); return false;
});


$('a.falnk').hover(function() {
$('.faspan').show();
$('.empty').hide();
},
function() {
$('.faspan').hide();
$('.empty').show();
});


$('a.twlnk').hover(function() {
$('.twspan').show();
$('.empty').hide();
},
function() {
$('.twspan').hide();
$('.empty').show();
});


$('a.gblnk').hover(function() {
$('.gbspan').show();
$('.empty').hide();
},
function() {
$('.gbspan').hide();
$('.empty').show();
});


$('a.dilnk').hover(function() {
$('.dispan').show();
$('.empty').hide();
},
function() {
$('.dispan').hide();
$('.empty').show();
});


$('a.delnk').hover(function() {
$('.despan').show();
$('.empty').hide();
},
function() {
$('.despan').hide();
$('.empty').show();
});


$('a.stlnk').hover(function() {
$('.stspan').show();
$('.empty').hide();
},
function() {
$('.stspan').hide();
$('.empty').show();
});

$('a.emlnk').hover(function() {
$('.emspan').show();
$('.empty').hide();
},
function() {
$('.emspan').hide();
$('.empty').show();
});


$('a.m').click(function() {
$(this).nextAll("div:first").show(); 
$(this).hide(); 
$(this).next('a.c').show(); return false;
});


$('a.c').click(function() {
$(this).nextAll("div:first").hide();
$(this).hide(); 
$(this).prev('a.m').show(); return false;
});


$('.order').each(function() {
    var default_value = this.value;
    $(this).css('color', '#808080'); 
    $(this).focus(function() {
        if(this.value == default_value) {
            this.value = '';
            $(this).css('color', '#000');
        }
    });
    $(this).blur(function() {
        if(this.value == '') {
            $(this).css('color', '#666');
            this.value = default_value;
        }
    });
});


$('.resel').each(function() {
    var default_value = this.value;
    $(this).css('color', '#808080'); 
    $(this).focus(function() {
        if(this.value == default_value) {
            this.value = '';
            $(this).css('color', '#000');
        }
    });
    $(this).blur(function() {
        if(this.value == '') {
            $(this).css('color', '#666');
            this.value = default_value;
        }
    });
});


$(".resel").change(function() {
    $(this).parent().parent().find('.red').submit();
});


$('.murl').each(function() {
    var default_value = this.value;
    $(this).css('color', '#808080'); 
    $(this).focus(function() {
        if(this.value == default_value) {
            this.value = '';
            $(this).css('color', '#000');
        }
    });
    $(this).blur(function() {
        if(this.value == '') {
            $(this).css('color', '#666');
            this.value = default_value;
        }
    });
});


$('.mcat').each(function() {
    var default_value = this.value;
    $(this).css('color', '#808080'); 
    $(this).focus(function() {
        if(this.value == default_value) {
            this.value = '';
            $(this).css('color', '#000');
        }
    });
    $(this).blur(function() {
        if(this.value == '') {
            $(this).css('color', '#666');
            this.value = default_value;
        }
    });
});

  $('.surl').focus(function(){
    
    this.select();
   return false;
   });

	$(".wait").hide();
	$(".waita").hide();


  $('a.ppfm').click(function() {
       $('a.ppfm').not(this).show();
			 $('.wait').not(this).hide();
       $(this).hide();
       $(this).next('.wait').show();
       $(this).parent('form').submit(); return false;
$.cookie('ordered', 'yes', { expires: 7 });       
   });

  $('a.ppfma').click(function() {
       $(this).hide();
       $(this).next('.waita').show();
       $('form').submit(); return false;
   });
$('a.ppfmb').click(function() {
       $(this).hide();
       $(this).next('.waita').show();
       $('checkoutform2').submit(); return false;
   });

  $('a.edit1').click(function() {
       $('.editbox1').show(); return false;
   });

  $('a.edit2').click(function() {
       $('.editbox2').show(); return false;
   });

   $('a.customshort').click(function() {
       $('#edbox').show();
       return false;
   });

  $('a.ca').click(function() {
       $(this).parent().parent().hide(); return false;
   });
   
   $('#edsave, #cosave, #clsave').click(function() {
   		var formid = $(this).attr('id');
   		formid = formid.substring(0,2);
   		$('#' + formid + 'form').submit();
   });
   
   	$('#lostpwsub').click(function() {
		$('#lostpwform').submit();
	});
	$('#advertsub').click(function() {
		$('#advertform').submit();
	});
	$('#checkoutsub').click(function() {
		$('#checkoutform').submit();
	});$('#checkoutsuba').click(function() {
		$('#checkoutform').submit();
                $.cookie('ordered', 'yes', { expires: 7 });

	});
	$('#checkoutsub2').click(function() {
		$('#checkoutform2').submit();
	});	
	$('#savecats').click(function(){
		var checkBoxes = $("input.catoptbox");
		var categories = "";
		var counter = 0;
		$.each(checkBoxes, function() {
			if ($(this).is(":checked")){
            	categories = categories + '_' + $(this).attr('value') + '_';
            	counter++;
        	}
		});
        	
        if (counter == 0) {
        	$('#caterr').text('Please select a category.').addClass('aderror');
        } else if (counter > 3) {
        	$('#caterr').text('Maximum of 3 categories.').addClass('aderror');
        } else {
        	$('#caterr').removeClass('aderror').text('');
        	$.get("/includes/updateCats.inc.php", {cats: categories}, function(data) {
				if (data) {					
					$("#selcats").text(data);
					$("div.b").hide();
					$("#caterr").removeClass();
				} else {
					$('#caterr').text('Error saving selected categories. Please try again.').addClass('aderror');
				}
        	});
        }

		return false;
	});
	
	$('#savelocs').click(function(){
		var checkBoxes = $("#loclist input");
		var locations = "";
		var counter = 0;
		$.each(checkBoxes, function() {
			if ($(this).is(":checked")){
            	locations = locations + '_' + $(this).attr('value') + '_';
    			counter++;
        	}
		});
        
		if (counter == 0) {
        	// do nothing
        } else if (counter == 9) {
			$('#sellocs').text('All');
			$("#loclist").hide();
		} else {
        	$.get("/includes/updateLocs.inc.php", {locs: locations}, function(data) {
				if (data) {					
					$("#sellocs").text(data);
					$("#loclist").hide();
				}
        	});
		}
        
		return false;
	});
	
	$('#cbform #check').click(function() {
		if ($(this).is(":checked")){
			var conf = confirm('Are you sure?');
			if (conf) {
				$('#cbform').submit();
			} else {
				return false;
			}
		}
	});
	
	$('a.delete').click(function() {
		var id = $(this).attr('id');
		var id = id.split('_');
		
		if (id[1] > 0 && id[2] > 0) {
			var conf = confirm("Are you sure?");
			if (conf) {
				$.get("includes/delButton.inc.php", {id: id[1], mid: id[2]}, function(data) {
					if (data) {					
						$("#button" + id[1]).fadeOut();
					} else {
						$("#msg").text("Error deleting button. Please try again or contact support.");
					}
    	    	});
			}
		}	
	});
});

