$(document).ready(function() {
						   
/* $(function() {
	$('#content.home').cycle();
	}); */
$(function() {
	$('.homeRotate').cycle();
	});

$(function() {
	$('#pinLocate').cycle();
	});

$("#taflink, #why-captcha, #prize-list, #pins-played, #forgot-your-password, #fblink, #twitlink").live('click', function() {
	popUpById($(this).attr("id"));
	page($(this).attr("id") + '-popup');
});

$(".official-rules").live('click', function() {
	popUpById('official-rules');
	page('official-rules-popup');
});

$("#popUp .closePopup").live('click', function() {
	$('select').fadeIn(100);
	$("#popUp").fadeOut(300);
});

$("#registerFrm").submit(function() {
	$('#registerFrm div.frow').css('background-color', 'transparent').css('color', '#000000');
	$.ajax({
		type: "POST", 
		url: "forms_cb.php",
		data: $(this).serialize(),
		dataType: "json",
		success: function(data) {
			if (data.success == "FAIL")
			{
				errors = "";
				
				for (i in data.errors)
				{
					errors += "<li>" + data.errors[i] + "</li>";
					$('input[name=' + i + '], select[name=' + i + ']').parents('div.frow, div.frmCol3').css('background-color', '#ff0000').css('color', '#ffffff');
				}
				
				if ('extra' in data)
				{
					for (i in data.extra)
					{
						$('input[name=' + i + '], select[name=' + i + ']').parents('div.frow, div.frmCol3').css('background-color', '#ff0000').css('color', '#ffffff');
					}
				}
				
				popUpById("form-errors");
				$("#popUp ul.errors").html(errors);
				$("input[name=captcha_code]").val("");
				document.getElementById('captcha').src = './securimage/securimage_show.php?' + Math.random(); 
			} 
			else  if (data.success == "PASS") 
			{
				location.href = "congratulations.php";
			}
		}
	});
	return false; 
});

$('#registerBtn').click(function() {
	$("#registerFrm").submit();
});

$("#userloginFrm").submit(function() {
	dataString = $(this).serialize(); 
	$.ajax({
		type: "POST", 
		url: "forms_cb.php",
		data: dataString,
		dataType: "json",
		success: function(data) {
			if (data.success == "FAIL")
			{
				errors = "";
				
				for (i in data.errors)
				{
					errors += "<li>" + data.errors[i] + "</li>";
				}
				
				popUpById("form-errors");
				$("#popUp ul.errors").html(errors);
			} 
			else  if (data.success == "PASS") 
			{
				location.href = "spin.php";
			}
		}
	});
	return false; 
});

$('#userloginBtn').live('click', function() {
	$("#userloginFrm").submit();
});

$("#resetFrm").submit(function() {
	$.ajax({
		type: "POST", 
		url: "forms_cb.php",
		data: $(this).serialize(),
		dataType: "json",
		success: function(data) {
			
			if (data.success == "FAIL")
			{
				popUpById("password-reset");
				$("#popUp p.message").html(data.message);
				$("#popUp p.alert").html(data.alert);
			} 
			else  if (data.success == "PASS") 
			{
				location.href = "login.php?resetSuccess";
			}
		}
	});
	return false; 
});

$('#pwdResetBtn').live('click', function() {
	$("#resetFrm").submit();
});

$("#registerFrm input[name=first_name], #registerFrm input[name=last_name]").bind("keyup", function() 
{ 
	$(this).val($(this).val().replace(/^[\s]+/,'').replace(/[\s]+$/,'').replace(/[0-9]/ig,'')); 
});

$("#registerFrm input[name=postal_code1], #registerFrm input[name=postal_code2]").bind("keyup", function() 
{ 
	$(this).val($(this).val().replace(/^[\s]+/,'').replace(/[\s]+$/,'')); 
});

$("#registerFrm input[name=daytime_phone1], #registerFrm input[name=daytime_phone2], #registerFrm input[name=daytime_phone3]").bind("keyup", function() 
{ 
	$(this).val($(this).val().replace(/^[\s]+/,'').replace(/[\s]+$/,'').replace(/[a-z]/ig,'')); 
});

// end of doc ready func
});

$.fn.serializeObject = function()
{
   var o = {};
   var a = this.serializeArray();
   $.each(a, function() {
       if (o[this.name]) {
           if (!o[this.name].push) {
               o[this.name] = [o[this.name]];
           }
           o[this.name].push(this.value || '');
       } else {
           o[this.name] = this.value || '';
       }
   });
   return o;
}

function moveOnMax(field,nextFieldID){
  if(field.value.length >= field.maxLength){
    document.getElementById(nextFieldID).focus();
  }
}


function spinDone() // for flash
{
	$('#content .spinnerPrizes').hide();
	$('#content .spinnerPacks').css('top:340px');
}

function taf() // for flash
{
	popUpById('taflink');
}

function rules() // for flash
{
	popUpById('official-rules');
}

function popUpById(id)
{	
	popUp($("#" + id + "-window").html()); 
}

function popUp(html)
{
	$('#popUp .mask').css({'width':$(window).width(), 'height':$('html').height()});
	$("#popUp .window").html(html);
	$('select').fadeOut(300);
	$("#popUp").fadeIn(500);
}

function submitTafForm()
{
	dataString = $('#popUp .tafFrm').serialize();
	dataObject = $('#popUp .tafFrm').serializeObject();
	
	$.ajax({
		type: "POST", 
		url: "forms_cb.php",
		data: dataString,
		dataType: "json",
		success: function(data) {
			if (data.success == "FAIL")
			{
			    errors = "";
			    
			    for (i in data.errors)
			    {
				errors += "<li>" + data.errors[i] + "</li>";
			    }
			    
			    $("#popUp .window").html($('#form-errors-window').html());
			    $("#popUp ul.errors").html(errors);
			    $("#popUp .closePopup").click(function() {
				    $("#popUp .window").html($('#taflink-window').html());
				    $('#popUp form').populate(dataObject);
				    return false;
			    });
			}
			else  if (data.success == "PASS") 
			{
			    $("#popUp .window").html($("#taf-sent-window").html());
			}
		}
	});
}

function showTafAgain()
{
	$("#popUp .window").html($("#taflink-window").html());
	page('taflink-popup');
};

function submitRequestCodeForm()
{
	dataString = $('#popUp .requestCodeFrm').serialize();
	$.ajax({
		type: "POST", 
		url: "forms_cb.php",
		data: dataString,
		dataType: "json",
		success: function(data) {
			if (data.success == "FAIL")
			{
				
			} 
			else  if (data.success == "PASS") 
			{
				$("#popUp .requestCodeFrm").replaceWith("");
			}
			
			$("#popUp p.message").html(data.message);
			$("#popUp p.alert").html(data.alert);
		}
	});
};

function page(page)
{
    $.ajax({
	    type: "POST", 
	    url: "forms_cb.php",
	    data: { action: "page", page: page },
	    dataType: "json"
    });
}

