function getUrlVars() {
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
    
    return vars;
}

var winHeight = $(window).height();
var winWidth = $(window).width();

function showModalWindow(id) {
    var mask = "#mask";
    
    $(mask).css('height',winHeight);
    $(mask).css('width',winWidth);
    
    $(mask).css('top',0);
    $(mask).css('left',0);
    
    $(id).css('top',winHeight/2 - ($(id).height() + 10 + 10)/2);
    $(id).css('left',winWidth/2 - ($(id).width() + 10 + 10)/2);
    
    $(mask).fadeTo("fast",0.8);
    $(id).fadeIn("fast");
}

function defaultFields(id) {
    var defaultColor = $(id).css('color');
    var defaultValue = $(id).val();
    
    $(id).focus(function() {
        if($(this).val() == defaultValue) {
	    $(this).css('color','#000');
	    $(this).val('');
	}
    }).blur(function() {
	if($(this).val() == "") {
	    $(this).css('color',defaultColor);
	    $(this).val(defaultValue);
	}
    });
}

function sendFeedback() {
    $('#feedback,#email,#sendfeedback').attr('disabled','disabled');
    $('#contactmodal #modalresult').html('<img src="http://cdn.zuupy.com/images/loading.gif">').show();
    $.ajax({
	type: "POST",
	url: "/feedback/",
	data: { message:$('#contactmodal #modaldefault #feedback').val(), email:$('#contactmodal #modaldefault #email').val() },
	success: function(data) {
	    switch(data) {
		case '0':
		    $('#contactmodal #modalresult').html('<span style="color:#669900;">Your feedback has been sent successfully!</span>');
		    $('#feedback,#email,#sendfeedback').removeAttr('disabled');
		    setTimeout("$('#mask').click()",2000);
		    break;
		case '1':
		    $('#contactmodal #modalresult').html('<span style="color:#cc0000;">Please enter a longer message.</span>');
		    $('#feedback,#email,#sendfeedback').removeAttr('disabled');
		    break;
		case '2':
		    $('#contactmodal #modalresult').html('<span style="color:#cc0000;">Please enter a valid email address.</span>');
		    $('#feedback,#email,#sendfeedback').removeAttr('disabled');
		    break;
	    }
	},
	error: function(XMLHttpRequest, textStatus, errorThrown) {
	    $('#contactmodal #modalresult').html('<span style="color:#cc0000;">Error sending feedback.</span>');
	    $('#feedback,#email,#sendfeedback').removeAttr('disabled');
	}
    });
}

function loginBeta() {
    $('#betaemail,#betapassword,#betaemail2,#betapassword2,#betalogin').attr('disabled','disabled');
    $('#loginmodal #modalresult').html('<img src="http://cdn.zuupy.com/images/loading.gif">').show();
    $.ajax({
	type: "POST",
	url: "/account/login/",
	data: { username:$('#betaemail').val(),password:$('#betapassword').val() },
	success: function(data) {
	    switch(data)
	    {
	    case '0':
		if(getUrlVars()['redirect']) {
		    window.location = getUrlVars()['redirect'];
		}
		else {
		    window.location = "/account/";
		}
		break;
	    case '1':
		$('#loginmodal #modalresult').html('<span style="color:#cc0000;">Please enter both your email and password.</span>');
		$('#betaemail,#betapassword,#betaemail2,#betapassword2,#betalogin').removeAttr('disabled');
		break;
	    case '2':
		$('#loginmodal #modalresult').html('<span style="color:#cc0000;">Your email and password combination is invalid. Note that passwords are case-sensitive.</span>');
		$('#betaemail,#betapassword,#betaemail2,#betapassword2,#betalogin').removeAttr('disabled');
		break;
	    }
	},
	error: function(XMLHttpRequest, textStatus, errorThrown) {
	    $('#loginmodal #modalresult').html('<span style="color:#cc0000;">Error logging in.</span>');
	    $('#betaemail,#betapassword,#betaemail2,#betapassword2,#betalogin').removeAttr('disabled');
	}
    });
}

function newBeta() {
    $('#betaemailnew,#betasignup').attr('disabled','disabled');
    $('#betamodal #modalresult').html('<img src="http://cdn.zuupy.com/images/loading.gif">').show();
    $.ajax({
        type: "POST",
        url: "/account/register/",
        data: { email:$('#betaemailnew').val() },
        success: function(data) {
	    switch(data)
	    {
	    case '0':
		window.location = "/betasuccess/";
		break;
	    case '1':
		$('#betamodal #modalresult').html('<span style="color:#cc0000;">You have entered an invalid email address.</span>');
		$('#betaemailnew,#betasignup').removeAttr('disabled');
		break;
	    case '2':
		$('#betamodal #modalresult').html('<span style="color:#cc0000;">This email has been registered and is active. Please log in using this email.</span>');
		$('#betaemailnew,#betasignup').removeAttr('disabled');
		    setTimeout("$('#mask').click()",3000);
		break;
	    case '3':
		$('#betamodal #modalresult').html('<span style="color:#cc0000;">This email has been registered before but not activated yet. Another email containing the activation code has been resent to your email.</span>');
		$('#betaemailnew,#betasignup').removeAttr('disabled');
		    setTimeout("$('#mask').click()",3000);
		break;
	    }
	},
	error: function(XMLHttpRequest, textStatus, errorThrown) {
	    $('#betamodal #modalresult').html('<span style="color:#cc0000;">Error registering account.</span>');
	    $('#betaemailnew,#betasignup').removeAttr('disabled');
	}
    });
}

function requestPassword() {
    $('#forgetpasswordemail,#requestpassword').attr('disabled','disabled');
    $('#forgetpasswordmodal #modalresult').html('<img src="http://cdn.zuupy.com/images/loading.gif">').show();
    $.ajax({
	type: "POST",
	url: "/account/forgetpassword/",
	data: { email:$('#forgetpasswordmodal #modaldefault #forgetpasswordemail').val() },
	success: function(data) {
	    switch(data) {
		case '0':
		    $('#forgetpasswordmodal #modalresult').html('<span style="color:#669900;">We have sent a password reset link to your email address. Please check your inbox for instructions on how to reset your password.</span>');
		    $('#forgetpasswordemail,#requestpassword').removeAttr('disabled');
		    setTimeout("$('#mask').click()",3000);
		    break;
		case '1':
		    $('#forgetpasswordmodal #modalresult').html('<span style="color:#cc0000;">Sorry, there is no user with this email address.</span>');
		    $('#forgetpasswordemail,#requestpassword').removeAttr('disabled');
		    break;
		case '2':
		    $('#forgetpasswordmodal #modalresult').html('<span style="color:#cc0000;">This email has been registered before but not activated yet. Another email containing the activation code has been resent to your email.</span>');
		    $('#forgetpasswordemail,#requestpassword').removeAttr('disabled');
		    setTimeout("$('#mask').click()",3000);
		    break;
		case '3':
		    $('#forgetpasswordmodal #modalresult').html('<span style="color:#cc0000;">Please enter a valid email address.</span>');
		    $('#forgetpasswordemail,#requestpassword').removeAttr('disabled');			
		    break;
	    }
	},
	error: function(XMLHttpRequest, textStatus, errorThrown) {
	    $('#forgetpasswordmodal #modalresult').html('<span style="color:#cc0000;">Error sending password request.</span>');
	    $('#forgetpasswordemail,#requestpassword').removeAttr('disabled');
	}
    });
}

function goToByScroll(id){
    $('html,body').animate({scrollTop: $("#" + id).offset().top},1000);
}

$(document).ready(function() {
    $('.textbox2').focus(function() {
	$(this).hide();
	$(this).next().show().focus();
    });
    
    $('.textboxodd').blur(function() {
	if($(this).val() == '') {
	    $(this).hide();
	    $(this).prev().show();
	}
    });
    
    $('#betaemail,#betapassword,#betaemail2,#betapassword2,#betalogin').keypress(function(e) {
	if(e.which == 13) {
	    loginBeta();
	}
    });
    
    $('#betalogin').click(function(e) {
	loginBeta();
    });
    
    $('#betaemailnew,#betasignup').keypress(function(e) {
	if(e.which == 13) {
	    newBeta();
	}
    });
    
    $('#betasignup').click(function(e) {
        newBeta();
    });
    
    $('#forgetpasswordemail,#requestpassword').keypress(function(e) {
	if(e.which == 13) {
	    requestPassword();
	}
    });
    
    $('#requestpassword').click(function() {
	requestPassword();
    });
    
    $('#email,#sendfeedback').keypress(function(e) {
	if(e.which == 13) {
	    sendFeedback();
	}
    });
    
    $('#sendfeedback').click(function() {
	sendFeedback();
    });
    
    $('#watchvideo').click(function(e) {
	$('#video').html('<object width="530" height="325">'+
			    '<param name="movie" value="http://www.youtube.com/v/lMhoZ-SbjK0&hl=en&fs=1&rel=0&autoplay=1"></param>'+
			    '<param name="allowFullScreen" value="true"></param>'+
			    '<param name="allowscriptaccess" value="always"></param>'+
			    '<embed src="http://www.youtube.com/v/lMhoZ-SbjK0&hl=en&fs=1&rel=0&autoplay=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="530" height="325"></embed>'+
			'</object>');
	showModalWindow('#video');
    }); /* 425 by 344 */
    
    $('a[name=contact]').click(function(e) {
	e.preventDefault();
	showModalWindow('#contactmodal');
    });
    
    $('a[name=forgetpassword]').click(function(e) {
	e.preventDefault();
	$('#loginmodal').hide();
	showModalWindow('#forgetpasswordmodal');
    });
    
    $('a[name=beta]').click(function(e) {
	e.preventDefault();
	showModalWindow('#betamodal');
    });
    
    $('a[name=login]').click(function(e) {
	e.preventDefault();
	showModalWindow('#loginmodal');
    });
    
    $('#signupnow,#tryitnow,#tryitnow2,#tryitnow3').click(function() {
	$('a[name=beta]').click();
    });
    
    $('#cancel,#mask,.closemodal').click(function(e) {
	$('#video').html('');
	$('#mask,#video,#__ss_4559437,.modal').hide();
	$('.modal #modalresult').hide();
	$('.modal #modaldefault').show();
    });
    
    $('.cd_home_ss').click(function(e) {
	id = $(this).attr('id');
	showModalWindow('#'+id+'_modal');
    });
    
    $('.seeademo').click(function(e) {
	showModalWindow('#demo_modal');
    });
    
    $('td#left h3,td#right h3').click(function() {	
	$('td#left h4, td#right h4').hide();
	$(this).next('h4').slideToggle('fast');
    });
    
    //Zuupy CrowdDeals
    $('.howitworksquestion').click(function() {	
	$('.howitworksanswer').hide();
	$(this).next('.howitworksanswer').slideToggle('fast');
    });
    
    //$('#cd_subcontentheader').click(function() {
	//$('#cd_subcontent').slideToggle();
    //});
    
    $('.bfmtop').click(function() {
	$('.bfmcontent').slideToggle('fast');
    });
    
    $('#bubbleclose').click(function() {
	$('#bubble').hide();
    });
    
    $('#seeaquickdemo').click(function() {
	window.location = '/demo/';
    });
    
    $('#affiliate').click(function() {
        $('#contactustable').hide();
        $('#contactheader').html('Earn money by referring customers to us.');
        $('#contactmessage').html('We are currently rolling out an affiliate program that pays out a <b>100% commission rate</b> for each new customer signed up. For instance, if you referred to us 10 prospects who subscribe to our US$29.95/mo plan, we pay you US$299.50. We also pay out incremental commissions of US$1.00 per registration of a free trial account, i.e. if you sign up 10 new free trial users for us, we pay you US$10.00 whether they upgrade or not. Drop us a message below, and we will get back to you with more details.<br><br><b>Alternatively, simply sign up for an affiliate account now to start earning commissions: <a href="http://affiliates.zuupy.com" target="_blank">http://affiliates.zuupy.com</a>!</b>');
	$('a[name=contact]').click();	
    });
});
