
$(document).ready(function() {
    //bind click actions
    $('#submit_for_alerts').bind("click", register_alerts);
    $('#close_for_wallpaper').bind("click", hide_wallpaper_download_box );
    $('#submit_for_wallpaper').bind("click", confirm_wallpaper_download);
		
	 $('#mobile_number_for_alerts').mask("(999) 999-9999");
	 $('#mobile_number_for_wallpaper').mask("(999) 999-9999");
	
    //hide wallpaper download box
    $('#wallpaper_download_box').hide('fast');

    //add hover to wallpapers
    $('#wallpapers img').fadeTo('fast', 0.6);
    $('#wallpapers img').hover(function() {
        $(this).fadeTo('fast', 1.0);
    },
    function() {
        $(this).fadeTo('slow', 0.6);
    });

});

function show_message_box(section, message, text_color, fadeout) {
			$('#' + section + '_message_box').show('slow', function() { $(this).html(message); $(this).css('color', text_color);});
			if(fadeout > 0){
				setTimeout( function () { 
						$('#' + section + '_message_box').fadeOut('slow');
					}, fadeout);
			}
}

function register_alerts() {
		if (checkInternationalPhone(unformatPhone($('#mobile_number_for_alerts').val())) == false) {
        $('#mobile_number_for_alerts').focus();
        show_message_box('alerts', 'Invalid phone number.', 'red', 0);
        return false;
    }

    if ($('#check_terms_for_alerts:checked').val() == undefined) {
        $('#check_terms_for_alerts').focus();
        show_message_box('alerts', 'Please agree to terms and conditions.', 'red', 0);
        return false;
    }
	
		RegisterForAlerts(2007, unformatPhone($('#mobile_number_for_alerts').val()));
    show_message_box('alerts', 'Successfully registered for alerts.You should now receive a message on your phone.To confirm, please reply YES to each message.', 'green', 7000);

	 $(" .alerts_right input[type='checkbox']").each( function() {
		this.checked = false;
	 });

}

function hide_wallpaper_download_box(){
	$('#wallpaper_download_box').hide('slow');
	$('#wallpaper_result').hide('slow', function() { $('#wallpaper_result').html(''); });
	$('#wallpaper_message_box').html('');
	$('#check_terms_for_wallpaper').checked = false;
}


function show_wallpaper_download_box(id, msgtext){
	$('#wallpaper_download_box').show('slow', function () { $('#mobile_number_for_wallpaper').focus(); });
	$('#selected_wallpaper').html('Download wallpaper: ' + msgtext);
	$('#selected_wallpaper_id').val(id);

	$('#check_terms_wallpaper').attr('checked', false);

}



function confirm_wallpaper_download(){
	if(checkInternationalPhone(unformatPhone($('#mobile_number_for_wallpaper').val())) == false) {
		$('#mobile_number_for_wallpaper').focus();
		show_message_box('wallpaper', 'Invalid phone number', 'red', 0);
		return false;
	}
	if($('#check_terms_wallpaper:checked ').val() == undefined){
		$('#check_terms_wallpaper').focus();
		show_message_box('wallpaper', 'Please agree to terms and conditions.', 'red', 0);
		return false;
	}
	
	RequestContent($('#selected_wallpaper_id').val(), unformatPhone($('#mobile_number_for_wallpaper').val()));
	
	hide_wallpaper_download_box();
	show_message_box('wallpaper_result', 'You have successfully requested the wallpaper. You should now receive a message on your phone. To confirm, please reply YES to the message.', 'green', 7000);
	$('#wallpaper_message_box').fadeOut(5000);
}