//JBM's ajax functions 1.3
$(document).ready(function() { 
	
	/*$('#analytics').load('analytics.php', function() {
	  $('#analytics').fadeIn('slow');
	});*/
	
	$(".clear").focus(function() {
		if ($(this).val() == "Your email address") {
			$(this).val('');
		}
	});

	//grab the email address from the newsletter form
	//send it through the constant contact w/s
	$("#newsletter form").submit(function(){
		//obtain the values
		var email = $("#ea").val();
		var cc_list = $("#cc_list_newsletter").val();
		id = 0;
		//validate the values
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		if( email == "" || email == "Your email address" || reg.test(email) == false ){
			jAlert('Please submit a valid email address');
			return false;
		}else{
			var datastr = 'email=' + email + '&cc_list=' + cc_list;
			setTimeout("constantcontact('"+datastr+"','"+id+"',true)",500);
			$("#ea").val("Your email address");
			return false;
		}
		return false;
	});
	
	//grab the email address and message from the contact form
	//send it through the constant contact w/s
	$("#contact form").submit(function(){
		//obtain the values
		var email = $("#email").val();
		var message = $("#message").val();
		var admin_email = $("#admin_email").val();
		var cc_list = $("#cc_list_contact").val();
		var subject = "Showlist Contact";
		id = 0;
		//validate the values
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		if( email == "" || email == "Your email address" || reg.test(email) == false || message == "" ){
			jAlert('Please submit a valid email address and fill out the form correctly');
			return false;
		}else{
			var datastr = 'email=' + email + '&message=' + message + '&subject=' + subject + '&admin_email=' + admin_email + '&cc_list=' + cc_list;
			setTimeout("mail('"+datastr+"','"+id+"')",500);
			setTimeout("constantcontact('"+datastr+"','"+id+"',false)",500);
			$("#email").val("Your email address");
			$("#message").val("Message sent!");
			return false;
		}
		return false;
	});
	
	//grab all the data from the giveaway form
	//mail the info to the site admin
	//send it through the constant contact w/s
	$(".giveaway form").submit(function(){
		//obtain the values
		var id = $(this.parentNode).attr('id');
		var fname = $("#fname"+id).val();
		var lname = $("#lname"+id).val();
		var email = $("#email"+id).val();
		var act = $("#act"+id).val();
		var city = $("#city"+id).val();
		var venue = $("#venue"+id).val();
		var date = $("#date"+id).val();
		var cc_list = $("#cc_list"+id).val();
		var list_id = $("#list_id"+id).val();
		var user_id = $("#user_id"+id).val();
		var admin_email = $("#admin_email_ga"+id).val();
		var subject = "Giveaway Signup";
		//validate the values
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		if( fname == "" || lname == "" || email == "" ){
			jAlert('Please submit your full name and email address');
			//alert('Please submit your full name and email address');
			return false;
		}else if( reg.test(email) == false ){
			jAlert('Please submit a valid email address', 'Something\'s wrong!');
			//alert('Please submit a valid email address');
			return false;
		}else{
			var datastr = 'id=' + id + '&fname=' + fname + '&lname=' + lname + '&email=' + email + '&act=' + act + '&city=' + city + '&venue=' + venue + '&date=' + date + '&subject=' + subject + '&admin_email=' + admin_email + '&cc_list=' + cc_list + '&list_id=' + list_id + '&user_id=' + user_id;
			
			datastr = datastr.replace(/'/gi, "\\'");
			
			setTimeout("mail('"+datastr+"','"+id+"')",500);
			setTimeout("giveawaydb('"+datastr+"','"+id+"')",500);
			setTimeout("constantcontact('"+datastr+"','"+id+"',false)",500);
			return false;
		}
		return false;
	});

	$('#public_date').datepicker();
	$('.public_input').tooltip({
	    left: -120 
	});
	$('.my').tooltip();
	
	$("#public_submission").submit(function(){
		//obtain the values
		var p_date = $('#public_date').val();
		var p_type = $('#public_type').val();
		var p_event = $('#public_event').val();
		var p_city = $('#public_city').val();
		var p_venue = $('#public_venue').val();
		var p_info = $('#public_info').val();
		var userID = $('#userID').val();
		var subject = "Public List Submission";
		
		var date = dateFormat(p_date, "yyyy-mm-dd");
		var display_date = dateFormat(p_date, "dddd, mmmm d");
		//validate the values
		var reg = /^([A-Za-z0-9_\-\.])/;
		if( p_date == "" || p_event == "" || p_city == "" || p_venue == "" || p_info == "" ){
			jAlert('Please provide all required fields (*)', 'Something\'s wrong!');
			return false;
		}else if( reg.test(p_event) == false || reg.test(p_city) == false || reg.test(p_venue) == false || reg.test(p_info) == false ){
			jAlert('Please make sure your submission is correct', 'Something\'s wrong!');
			return false;
		}else{
			var datastr = 'p_date=' + date + '&p_type=' + p_type + '&p_event=' + p_event + '&p_city=' + p_city + '&p_venue=' + p_venue + '&p_info=' + p_info + '&userID=' + userID;
			var table = '<tr><td nowrap=\"nowrap\" align=\"center\" class=\"light\"><span style=\"display:none\">' + p_date + '</span>' + display_date + '</td><td nowrap=\"nowrap\" align=\"center\" class=\"\">' + p_type + '</td><td nowrap=\"nowrap\" align=\"center\" class=\"light\">' + p_event + '</td><td nowrap=\"nowrap\" align=\"center\" class=\"\">' + p_city + '</td><td nowrap=\"nowrap\" align=\"center\" class=\"light\">' + p_venue + '</td><td align=\"center\" class=\"\"><input type=\"button\" value=\"Info\" onclick=\"window.open(&#39;' + p_info + '&#39;)\" class=\"square\" /></td></tr>';
			
			datastr = datastr.replace(/'/gi, "\\'");
			table = table.replace(/'/gi, "&#39;");
			setTimeout("publicdb('"+datastr+"','"+table+"')",500);
			return false;
		}
		return false;
	});
	
	$(".delete").click(function(){
		//obtain the value
		var public_id = ($(this).attr("id"));
		var datastr = 'public_id=' + public_id;
		setTimeout("public_delete('"+datastr+"')",500);
		return true;
	});
	
	$("#submit_myshowlist").submit(function(){
		//obtain the values
		var email = $('#user_email').val();
		var email_my = $('#email_my').val();
		var save_my = $('#save_my').val();
		var list_id = $('#list_id').val();
		var user_id = $('#user_id').val();
		var subject = "MyShowlist";
		
		//declare ye olde datastrings
		var myshowlistArray = new Array();
		
		//obtain the winning values
		var myshowlist = $('#myTable').children().find("input:checked");
		var i = 0;
		myshowlist.each(function(){
			var myshowlistid = $(this).attr("id");
			if( myshowlistid != "" )
				myshowlistArray[i] = myshowlistid;
			i++;
		});
		if( myshowlistArray == "" ){
			jAlert('Please choose the list items you would like on your list', 'Something\'s wrong!');
			return false;
		}
		var myshowlistSer = php_serialize(myshowlistArray);
		
		//validate the values
		var reg = /^([A-Za-z0-9_\-\.])/;
		if( email_my != "on" && save_my != "on" ){
			jAlert('Please choose either Email MyShowlist or Save MyShowlist', 'Something\'s wrong!');
			return false;
		}else{
			var datastr = 'email=' + email + '&email_my=' + email_my + '&save_my=' + save_my + '&list_id=' + list_id + '&user_id=' + user_id + '&myshowlist=' + myshowlistSer;
			
			setTimeout("myshowlistdb('"+datastr+"')",500);
			return false;
		}
		return false;
	});
	
});

//runs the mail function
function mail(datastr,id){
	$.ajax({
		type: "POST",
		url: "mail.php",
		data: datastr,
		cache: false,
		success: function(html){
			if( id == 0 ){ //set by the contact function, shows a separate alert
				jAlert('Your message was successfully sent', 'Thanks!');
			}else{
				jAlert('You have successfully entered to win a free pair of tickets through showlistLA.\n\nRandomly selected winners will be informed via the email address you provide, and your tickets will be waiting for you at the venue\'s will call on night of the show.\n\n<i>*By entering to win a giveaway you are also subscribing to our weekly newsletter for newly announced concert listings and giveaways. You can unsubscribe at any time. No spam, ever.</i>', 'Congratulations!');$('.giveaway#'+id).fadeOut('fast');
			}
		},
		error: function(html){
			if( id == 0 ){ //set by the contact function, shows a separate alert
				jAlert('There was an error with your submission, please re-submit', 'Something\'s wrong!');
			}else{
				jAlert('There was an error with your submission, please re-submit', 'Something\'s wrong!');$('.giveaway#'+id).fadeOut('fast');
			}
		}
	});
};

//inserts the public data into the database
function publicdb(datastr,table){
	$.ajax({
		type: "POST",
		url: "publicdb.php",
		data: datastr,
		cache: false,
		success: function(html){jAlert('Your public listing was successfully added', 'Thanks!');$('#tbody').prepend(table);},
		error: function(html){jAlert('There was an error with your submission, please re-submit', 'Something\'s wrong!');}
	});
};

//inserts the public data into the database
function public_delete(datastr){
	$.ajax({
		type: "POST",
		url: "public_delete.php",
		data: datastr,
		cache: false,
		success: function(html){jAlert('Your public listing was successfully removed, please refresh the page', 'Awesome!');},
		error: function(html){jAlert('There was an error with your deletion, please try again', 'Something\'s wrong!');}
	});
};

//inserts the giveaway data into the database
function giveawaydb(datastr,id){
	$.ajax({
		type: "POST",
		url: "giveawaydb.php",
		data: datastr,
		cache: false,
		success: function(html){},
		error: function(html){}
	});
};

//runs the mail function
function myshowlistdb(datastr){
	$.ajax({
		type: "POST",
		url: "myshowlistdb.php",
		data: datastr,
		cache: false,
		success: function(html){jAlert('You have successfully created your own Showlist', 'Thanks!');},
		error: function(html){jAlert('There was an error with your submission, please re-submit', 'Something\'s wrong!');}
	});
};

//sends data to the constant contact w/s
//only show this success/error alert if called from newsletter
function constantcontact(datastr,id,alert){
	$.ajax({
		type: "POST",
		url: "constantcontact.php",
		data: datastr,
		cache: false,
		success: function(html){
					if( alert == true){
						jAlert('You have successfully signed up for the showlistLA Newsletter', 'Thanks!');$('.giveaway#'+id).fadeOut('fast');
					};
				},
		error: function(html){
					if( alert == true){
						jAlert('There was an error with your submission, please re-submit', 'Something\'s wrong!');$('.giveaway#'+id).fadeOut('fast');
					};
				}
	});
};

function popUp(id){
var form = document.getElementById(id).style;
if( form.display == 'block' )
	$('.giveaway#'+id).fadeOut('fast');
else
	$('.giveaway#'+id).fadeIn('fast');
};

$.ajax({
  url: "analytics.php",
  cache: false,
  success: function(html){
    $("#analytics").append(html);
	$("#analytics").fadeIn('slow');
  }
});

function php_serialize(obj)
{
    var string = '';

    if (typeof(obj) == 'object') {
        if (obj instanceof Array) {
            string = 'a:';
            tmpstring = '';
            count = 0;
            for (var key in obj) {
                tmpstring += php_serialize(key);
                tmpstring += php_serialize(obj[key]);
                count++;
            }
            string += count + ':{';
            string += tmpstring;
            string += '}';
        } else if (obj instanceof Object) {
            classname = obj.toString();

            if (classname == '[object Object]') {
                classname = 'StdClass';
            }

            string = 'O:' + classname.length + ':"' + classname + '":';
            tmpstring = '';
            count = 0;
            for (var key in obj) {
                tmpstring += php_serialize(key);
                if (obj[key]) {
                    tmpstring += php_serialize(obj[key]);
                } else {
                    tmpstring += php_serialize('');
                }
                count++;
            }
            string += count + ':{' + tmpstring + '}';
        }
    } else {
        switch (typeof(obj)) {
            case 'number':
                if (obj - Math.floor(obj) != 0) {
                    string += 'd:' + obj + ';';
                } else {
                    string += 'i:' + obj + ';';
                }
                break;
            case 'string':
                string += 's:' + obj.length + ':"' + obj + '";';
                break;
            case 'boolean':
                if (obj) {
                    string += 'b:1;';
                } else {
                    string += 'b:0;';
                }
                break;
        }
    }
    return string;
};