$(function() {	
	$('#noJSmessage').hide(); // hides the top JS message across the site.

	$('#pass').keyup(function(e) {
		var strongRegex = new RegExp("^(?=.{8,})(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*\\W).*$", "g");
		var mediumRegex = new RegExp("^(?=.{7,})(((?=.*[A-Z])(?=.*[a-z]))|((?=.*[A-Z])(?=.*[0-9]))|((?=.*[a-z])(?=.*[0-9]))).*$", "g");
		var enoughRegex = new RegExp("(?=.{6,}).*", "g");
		if (false == enoughRegex.test($(this).val())) {
             $('#strength').html('Too short');
     	} else if (strongRegex.test($(this).val())) {
             $('#strength').className = 'ok';
             $('#strength').html('Strong!');
     	} else if (mediumRegex.test($(this).val())) {
             $('#strength').className = 'alert';
             $('#strength').html('Medium');
     	} else {
             $('#strength').className = 'error';
             $('#strength').html('Weak');
     	}
     	return true;
	});

	$('#emailAddress').keyup(function(e) {
    
    	var emailRegex = 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);
    
		if (emailRegex.test($(this).val())) {
             $('#emailAdd').className = 'ok';
             $('#emailAdd').html('Valid Email Address!');
     	} else {
             $('#emailAdd').className = 'error';
             $('#emailAdd').html('Invalid Email Address!');
     	}
     	return true;
	});

	$('#createsub').click(function() {
		var pvalid = false;
		var evalid = false;	

		// check if the password is okay
		var pword = $('#strength').text();
		if (pword == "Strong!" || pword == "Medium") {
			pvalid = true;
		}
	
		// check if the email is valid
		var email = $('#emailAdd').text();
		if (email == "Valid Email Address!") {
			evalid = true;
		}
	
		if (pvalid && evalid) {
			// submit the form
			$('#createform').submit();
		} else {
			return false;
		}
	});

	
	$('#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;
});
});

$(function() {
$('a.canmap').click(function() {
$(this).parent().parent().parent().hide(); return false;
});
});

$(function() {
$('a.facebook').hover(function() {
$('.fmsg').show();
$('.pls').hide();
},
function() {
$('.fmsg').hide();
$('.pls').show();
});
});

$(function() {
$('a.facebook').hover(function() {
$('.fmsg').show();
$('.pls').hide();
},
function() {
$('.fmsg').hide();
$('.pls').show();
});
});

$(function() {
$('a.facebook').hover(function() {
$('.fmsg').show();
$('.pls').hide();
},
function() {
$('.fmsg').hide();
$('.pls').show();
});
});


$(function() {
$('a.m').click(function() {
$(this).nextAll("div:first").show(); 
$(this).hide(); 
$(this).next('a.c').show(); return false;
});
});

$(function() {
$('a.c').click(function() {
$(this).nextAll("div:first").hide();
$(this).hide(); 
$(this).prev('a.m').show(); return false;
});
});


$(document).ready(function() {
$('.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;
        }
    });
});
});

$(document).ready(function() {
$('.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;
        }
    });
});
});

$(document).ready(function() {
$(".resel").change(function() {
    $(this).parent().parent().find('.red').submit();
});
});

$(document).ready(function() {
$('.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;
        }
    });
});
});

$(document).ready(function() {
$('.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;
   });

  $('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;
   });

  $('a.ppfma').click(function() {
       $(this).hide();
       $(this).next('.waita').show();
       $('form').submit(); return false;
   });

  $('a.edit1').click(function() {
       $('.editbox1').show(); return false;
   });

  $('a.edit2').click(function() {
       $('.editbox2').show(); return false;
   });

   $('a.cus').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();
   });
   
   	$('#advertsub').click(function() {
		$('#advertform').submit();
	});
	$('#checkoutsub').click(function() {
		$('#checkoutform').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();
				} 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;
	});
});