/*********************************************************
 * LOGIN AND PASSWORDS
 *********************************************************/

function cbk_doLogin(errors) {
	doing_ajax = false;
	if (!errors) {
		//window.location=URL+"smartlogin.php";
                parent.location.href = URL+"smartlogin.php";
	} else {
		$("#callback-login").html(errors);
	}
}

/*********************************************************
 * USER
 *********************************************************/
function cbk_doSignUp(errors)
{
	doing_ajax = false;
	if(!errors)
		window.location=URL+"signup-done";
    else
    {
        $("#callback-signup").html('');

        var checktill = errors.search(/~~/i);

        if(checktill != -1)
        {
            var finderror   = errors.split('~~');
            var highlight   = finderror[0];
        }

        if(typeof(highlight) == "undefined")
            highlight = false;

        if(highlight)
        {
            switch(parseInt(highlight))
            {
                case 1:
                    $("#error_first_name").show();
                    $("#error_first_name").html(finderror[1]);
                    $("#error_first_name").fadeOut(100).fadeIn(100);
                    $("#firstname").css('background-color', '#FFFFCC');
                    break;
                case 2:
                    $("#error_last_name").show();
                    $("#error_last_name").html(finderror[1]);
                    $("#error_last_name").fadeOut(100).fadeIn(100);
                    $("#lastname").css('background-color', '#FFFFCC');
                    break;
                case 3:
                    $("#error_email").show();
                    $("#error_email").html(finderror[1]);
                    $("#error_email").fadeOut(100).fadeIn(100);
                    $("#email").css('background-color', '#FFFFCC');
                    break;
                case 4:
                    $("#error_pass").show();
                    $("#error_pass").html(finderror[1]);
                    $("#error_pass").fadeOut(100).fadeIn(100);
                    $("#password1").css('background-color', '#FFFFCC');
                    break;
                case 5:
                    $("#error_pass").show();
                    $("#error_pass").html(finderror[1]);
                    $("#error_pass").fadeOut(100).fadeIn(100);
                    $("#password1").css('background-color', '#FFFFCC');
                    $("#error_confirm_pass").show();
                    $("#error_confirm_pass").html(finderror[1]);
                    $("#error_confirm_pass").fadeOut(100).fadeIn(100);
                    $("#password2").css('background-color', '#FFFFCC');
                    break;
            }
        }
	}
}

function cbk_doUpdatePassword(errors) {
	doing_ajax = false;
	if (!errors) {
		$("#callback-password").html("<p class='Ok'>&raquo; Password updated!</p>");
		$("#old_password").val("");
		$("#new_password").val("");
		var methodDashboard = "window.location='"+URL_CPANEL+"dashboard';";
		setTimeout(methodDashboard, 500);
	} else {
		$("#callback-password").html(errors);
	}
		$("#callback-password").fadeOut(100).fadeIn(100);
}

function cbk_doUpdateProfile(errors) {
	doing_ajax = false;
	if (!errors) {
		$("#callback-profile").html("<p class='Ok'>&raquo; Profile updated!</p>");
		var methodDashboard = "window.location='"+URL_CPANEL+"dashboard';";
		setTimeout(methodDashboard, 500);
		//history.go(0);
	} else {
		$("#callback-profile").html(errors);
	}
		$("#callback-profile").fadeOut(100).fadeIn(100);
}

/*********************************************************
 * ISSUING
 *********************************************************/
//20100730: mayank modify this function to show the proper text with field highlighting..
function cbk_sendGift(errors)
{
	doing_ajax = false;
	if (!errors)
		history.go(0);
    else
    {
        $("#callback").html('');
        var findhighlight  = errors.search(/~~/i);
        if(parseInt(findhighlight) == -1)
        {
            $('html, body').animate({scrollTop: 0}, 'slow');
            $("#callbackerror").show();
            $("#callbackerror").html(errors);
            $("#callbackerror").fadeOut(100).fadeIn(100);
        }
        else
        {
            var errorhighlight = errors.split("~~");
            switch(parseInt(errorhighlight[0]))
            {
                case 1:
                    $("#error_phonenumber").show();
                    $("#error_phonenumber").html(errorhighlight[1]);
                    $("#error_phonenumber").fadeOut(100).fadeIn(100);
                    $("#phonenumber_first").css('background-color', '#FFFFCC');
                    $("#phonenumber_second").css('background-color', '#FFFFCC');
                    $("#phonenumber_third").css('background-color', '#FFFFCC');
                    $("#phonenumber_first").focus();
                    break;
                case 2:
                    $("#error_from").show();
                    $("#error_from").html(errorhighlight[1]);
                    $("#error_from").fadeOut(100).fadeIn(100);
                    $("#fromName").css('background-color', '#FFFFCC');
                    $("#fromName").focus();
                    break;
                case 3:
                    $("#error_message").show();
                    $("#error_message").html(errorhighlight[1]);
                    $("#error_message").fadeOut(100).fadeIn(100);
                    $("#text_message").css('background-color', '#FFFFCC');
                    $("#text_message").focus();
                    break;
                case 4:
                    $("#error_giftee_email").show();
                    $("#error_giftee_email").html(errorhighlight[1]);
                    $("#error_giftee_email").fadeOut(100).fadeIn(100);
                    $("#giftee_email").css('background-color', '#FFFFCC');
                    $("#giftee_email").focus();
                    break;
            }
        }
	}
}

/**
 *  @author: Mayank Gupta on 20100817.
 *  @desc: function retrieve errors from AJAX response then show them one by one on the UI if they exists otherwise the next page is shown.
 *  @params: resonse of AJAX.
 *  @return: new page on success otherwise showing error one by one on the express-checkout page.
 */
function cbk_doExpressCheckout(errors)
{
    doing_ajax = false;
	$("#callback-expresscheckout").html('');
    $("#bottom_callback-expresscheckout").html('');

    if (errors.substring(0,3)=="ok_")
    {
        $("#callback-expresscheckout").html('');
        var c = errors.replace(/ok_/,"");
        $("#callback-expresscheckout-container").html(c);
    }
    else
    {
		var errorObject = JSON.parse(errors);
		validator.showErrors(errorObject);
		// validator.focusInvalid not working, so scroll
		$('html, body').animate({scrollTop: 0}, 'slow');
    }
}

/**
 *  @author: Mayank Gupta on 20100817.
 *  @desc: function retrieve errors from AJAX response then show them one by one on the UI if they exists otherwise the next page is shown.
 *  @params: resonse of AJAX.
 *  @return: new page on success otherwise showing error one by one on the thickbox page.
 */
function cbk_doValidateSintaxCellphone(errors)
{
    doing_ajax = false;

    phonenumber=$("#first_cellphone-popup").val()+$("#second_cellphone-popup").val()+$("#third_cellphone-popup").val();

    if(!errors)
        window.location=URL+"redeem/"+phonenumber;
    else
    {
        $("#first_cellphone-popup").css('background-color', '#FFFFCC');
        $("#second_cellphone-popup").css('background-color', '#FFFFCC');
        $("#third_cellphone-popup").css('background-color', '#FFFFCC');
        $("#first_cellphone-popup").focus();
        $("#callback-cellphone-validation").html(errors);
    }
}

/**
 *  @author: Mayank Gupta on 20100818.
 *  @desc: function retrieve errors from AJAX response then show them one by one on the UI if they exists otherwise success message is shown.
 *  @params: resonse of AJAX.
 *  @return: On success Message sent otherwise showing error one by one on the contact us page.
 */
function cbk_doSendContact(data)
{
    doing_ajax = false;
    if(data == 1)
    {
        $("#callback").css({'color':'#ff0000'});
        $("#callback").html("<center><br />Message sent!</center>");
        $("#name").val("");
        $("#comments").val("");
        $("#email").val("");
        $("#captcha_code").val("");
        document.getElementById('captcha').src = '/captcha?' + Math.random();
    }
    else
    {
        $("#callback").html('');
        var checktill = data.search(/~~/i);

        if(checktill != -1)
        {
            var finderror   = data.split('~~');
            var highlight   = finderror[0];
        }

        if(typeof(highlight) == "undefined")
            highlight = false;

        if(highlight)
        {
            switch(parseInt(highlight))
            {
                case 1:
                    $("#error_contact_name").show();
                    $("#error_contact_name").html(finderror[1]);
                    $("#error_contact_name").fadeOut(100).fadeIn(100);
                    $("#name").css('background-color', '#FFFFCC');
                    break;
                case 2:
                    $("#error_contact_email").show();
                    $("#error_contact_email").html(finderror[1]);
                    $("#error_contact_email").fadeOut(100).fadeIn(100);
                    $("#email").css('background-color', '#FFFFCC');
                    break;
                case 3:
                    $("#error_contact_comments").show();
                    $("#error_contact_comments").html(finderror[1]);
                    $("#error_contact_comments").fadeOut(100).fadeIn(100);
                    $("#comments").css('background-color', '#FFFFCC');
                    break;
                case 4:
                    $("#error_contact_captcha").show();
                    $("#error_contact_captcha").html(finderror[1]);
                    $("#error_contact_captcha").fadeOut(100).fadeIn(100);
                    $("#captcha_code").css('background-color', '#FFFFCC');
                    break;
            }
        }
        
    }
}

/*********************************************************
 * CREDIT CARD
 *********************************************************/
function cbk_doAddCreditCard(errors) {
	doing_ajax = false;
	if (!errors) {
		history.go(0);
	} else {
		$("#callback-addcreditcard").html(errors);
		$("#callback-addcreditcard").fadeOut(100).fadeIn(100);
	}
}



function cbk_doAddFunds(errors) {
	doing_ajax = false;
	if (!errors) {
		history.go(0);
	} else {
		$("#callback-addfunds").html(errors);
		$("#callback-addfunds").fadeOut(100).fadeIn(100);
	}
}

function cbk_doDeleteCard(errors,card_id) {
	doing_ajax = false;
	if (!errors) {
		history.go(0);
	} else {
		$("#callback-deletecard"+card_id).html(errors);
		$("#callback-deletecard"+card_id).fadeOut(100).fadeIn(100);
	}
}

function cbk_doAddAutomaticRecharge(errors) {
	doing_ajax = false;
	if (!errors) {
		$("#callback").html("<p class='Ok'>&raquo; Auto Recharge successfully added!</p>");
		history.go(0);
	} else {
		$("#callback").html(errors);
		$("#callback").fadeOut(100).fadeIn(100);
	}

}

/*********************************************************
 * CELLPHONE
 *********************************************************/

function cbk_doAddCellphone(errors) {
	doing_ajax = false;
	if (!errors) {
		$("#callback").html("<p class='Ok'>&raquo; Mobile Number added!</p>");
		var methodDashboard = "window.location='"+URL_CPANEL+"profile#cellphone';";
		setTimeout(methodDashboard, 500);
		//history.go(0);
	} else {
		$("#callback").html(errors);
		$("#callback").fadeOut(100).fadeIn(100);
	}
}

 function cbk_doValidateCellphone(errors) {
	doing_ajax = false;
	if (!errors) {
		$("#callback-pending").html("<p class='Ok'>&raquo; Validation ok!</p>");
		var methodDashboard = "window.location='"+URL_CPANEL+"dashboard#cellphone';";
		setTimeout(methodDashboard, 500);
	} else {
		$("#callback-pending").html(errors);
		$("#callback-pending").fadeOut(100).fadeIn(100);
	}

}
