var xmlobj = "";
var nextgifts = prevgifts = 0;
/*********************************************************
 * LOGIN AND PASSWORDS
 *********************************************************/

function doLogin() {

	if (doing_ajax) return;	doing_ajax = true;

	$("#callback-login").html("<img class='NoBorder' src='"+URL+"images/icons/icon-ajax-gray-green.gif'>");


	$.ajax( {
	        type: "POST",
	        url: URL_AJAX + "ajax.user.login.php",
	        data: "email=" + $("#loginemail").val() + "&password=" + $("#loginpassword").val() + "&remember=" + $("#remember").attr("checked"),
	        success: function(data) { cbk_doLogin(data);}
	} );
}

function doResendEmailValidation(user_id) {

	if (doing_ajax) return;	doing_ajax = true;

	$("#callback-login").html("<img class='NoBorder' src='"+URL+"images/icons/icon-ajax-gray-green.gif'>");

	$.ajax( {
	        type: "POST",
	        url: URL_AJAX + "ajax.user.resend.validation.php",
	        data: "user_id=" + user_id,
	        success: function(data) {
	        	$("#callback-login").html(data);
	        	doing_ajax = false;
	        }
	} );
}

function doResetPassword(user_id) {
	if (doing_ajax) return;	doing_ajax = true;

	$("#callback-login").html("<img class='NoBorder' src='"+URL+"images/icons/icon-ajax-gray-green.gif'>");
	$.ajax( {
	        type: "POST",
	        url: URL_AJAX + "ajax.user.reset.password.php",
	        data: "user_id=" + user_id,
	        success: function(data) {
	        	$("#callback-login").html(data);
	        	doing_ajax = false;
	        }
	} );
}

function doResetPasswordByEmail() {

	var email = $("#loginemail").val();
	if (!is_valid_email(email)) {
		$("#callback-login").html("We don't recognize that address, can you double-check it?");
		return;
	}

	if (doing_ajax) return;	doing_ajax = true;
	$("#callback-login").html("<img class='NoBorder' src='"+URL+"images/icons/icon-ajax-gray-green.gif'>");
	$.ajax( {
	        type: "POST",
	        url: URL_AJAX + "ajax.user.reset.password-by-email.php",
	        data: "email=" + email,
	        success: function(data) {
	        	$("#callback-login").html(data);
	        	doing_ajax = false;
	        }
	} );
}


function doChangePassword() {
	if (doing_ajax) return;	doing_ajax = true;

	$("#callback").html("<img class='NoBorder' src='"+URL+"images/icons/icon-ajax.gif'>");
	$.ajax( {
	        type: "POST",
	        url: URL_AJAX + "ajax.user.change.password.php",
	        data: "password1=" + $("#password1").val() + "&password2=" + $("#password2").val(),
	        success: function(data) {
	        	$("#callback").html(data);
	        	doing_ajax = false;
	        }
	} );
}


/*********************************************************
 * USER
 *********************************************************/

function doSendContact()
{
    
    var name        = $("#name").val();
    var email       = $("#email").val();
    var comments    = $("#comments").val();
    var captchacode = $("#captcha_code").val();

    var errors = new Array();

    if(name == '')
        errors[errors.length] = '1~~Name is a required field.';

    if(email == '')
        errors[errors.length] = '2~~Email is a required field.';

    if(email != 'email address' && (!(email.indexOf(".") > 2) || !(email.indexOf("@") > 0)))
        errors[errors.length] = '2~~Email address appears incorrect, please double-check it.';

    if(comments == '')
        errors[errors.length] = '3~~Comments is a required field.';

    if(captchacode == '')
        errors[errors.length] = '4~~Code is a required field.';

    if(errors.length > 0)
    {
        for(var i=0; i<errors.length; i++)
        {
            var checktill = errors[i].search(/~~/i);

            if(checktill != -1)
            {
                var finderror   = errors[i].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;
                }
            }
        }
        return false;
    }

	if (doing_ajax) return; doing_ajax = true;

	$("#callback").html("<center><img class='NoBorder' src='"+URL+"images/icons/icon-ajax.gif'></center>");

	$.ajax( {
	        type: "POST",
	        url: URL_AJAX + "ajax.contact.php",
	        data: 	"name=" + name +
	        		"&comments=" + comments +
	        		"&email=" + email + "&captcha=" + captchacode
	        		,
	        success: function(data)
            {
                //20100818: mayank to write the callback function to handle the AJAX response properly.
                cbk_doSendContact(data);
	        }
	} );

}

function doSignUp()
{
    var firstname = $("#firstname").val();
    var lastname  = $("#lastname").val();
    var email     = $("#email").val();
    var password1 = $("#password1").val();
    var password2 = $("#password2").val();

    var errors = new Array();

    if(firstname == '')
        errors[errors.length] = '1~~First name is a required field.';

    if(lastname == '')
        errors[errors.length] = '2~~Last name is a required<br>field.';

    if(email == '')
        errors[errors.length] = '3~~Email is a required field.';

    if(email != '' && (!(email.indexOf(".") > 2) || !(email.indexOf("@") > 0)))
        errors[errors.length] = '3~~Email address appears incorrect, please double-check it.';

    if(password1 == '')
        errors[errors.length] = '4~~Password is a required field.';

    if(password2 == '')
        errors[errors.length] = '5~~Confirm password is a required field.';

    if(password1 != '' && password1 != password2)
        errors[errors.length] = '6~~Password and confirm password should be same.';

    if(errors.length > 0)
    {
        for(var i=0; i<errors.length; i++)
        {
            var checktill = errors[i].search(/~~/i);

            if(checktill != -1)
            {
                var finderror   = errors[i].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_confirm_pass").show();
                        $("#error_confirm_pass").html(finderror[1]);
                        $("#error_confirm_pass").fadeOut(100).fadeIn(100);
                        $("#password2").css('background-color', '#FFFFCC');
                        break;
                    case 6:
                        $("#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;
                }
            }
        }
        return false;
    }

	if (doing_ajax) return;	doing_ajax = true;
	$("#callback-signup").html("<center><img class='NoBorder' src='"+URL+"images/icons/icon-ajax-big.gif'></center>");

	$.ajax({
	        type: "POST",
	        url: URL_AJAX + "ajax.user.signup.php",
	        data: 	"firstname=" + firstname +
	        		"&lastname=" + lastname +
	        		"&email=" + email +
	        		"&password1=" + password1 +
	        		"&password2=" + password2
	        		,
	        success: function(data)
            {
                cbk_doSignUp(data);
            }
	});
}

function doUpdatePassword() {
	if (doing_ajax) return;	doing_ajax = true;

	$("#callback-password").html("<center><img class='NoBorder' src='"+URL+"images/icons/icon-ajax.gif'></center>");
	$.ajax( {
	        type: "POST",
	        url: URL_AJAX + "ajax.user.update.password.php",
	        data: 	"old_password=" + $("#old_password").val() +
	        		"&new_password=" + $("#new_password").val()
	        		,
	        success: function(data) { cbk_doUpdatePassword(data);}
	} );
}

function doUpdateProfile() {

	if (doing_ajax) return;	doing_ajax = true;

	$("#callback-profile").html("<center><img class='NoBorder' src='"+URL+"images/icons/icon-ajax-big.gif'></center>");

	$.ajax( {
	        type: "POST",
	        url: URL_AJAX + "ajax.user.update.profile.php",
	        data: 	"firstname=" + $("#firstname").val() +
	        		"&lastname=" + $("#lastname").val() +
	        		"&address1=" + $("#address1").val() +
	        		"&address2=" + $("#address2").val() +
	        		"&state_id=" + $("#state_id").val() +
	        		"&city=" + $("#city").val() +
	        		"&zipcode=" + $("#zipcode").val() +
	        		"&email=" + $("#email").val() +
                    "&email2=" + $("#email2").val()
	        		,
	        success: function(data) { cbk_doUpdateProfile(data);}
	} );
}

function doDeleteAccount() {
	if (doing_ajax) return;	doing_ajax = true;

	$("#callback-account").html("<center><img class='NoBorder' src='"+URL+"images/icons/icon-ajax.gif'></center>");

	$.ajax( {
	        type: "POST",
	        url: URL_AJAX + "ajax.user.delete.account.php",
	        data: "agree=" + $("#agree").attr("checked"),
	        success: function(errors) {
		       	doing_ajax = false;
	        	if (!errors) {
		        	$("#callback").html("<center><h5>Account Cancelled!</h5></center>");
		        	$("#callback").fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100);
		        	setTimeout("history.go(0);",1000);
		        } else {
		        	$("#callback-account").html(errors);
		        	$("#callback-account").fadeOut(100).fadeIn(100).fadeOut(100).fadeIn(100);
		        }
	        }
	} );
}

/*********************************************************
 * Home Page
 *********************************************************/
// Featured gifts.  Accesses /homepage/products.php.
function getGiftsForHomepage()
{
    if (doing_ajax) return;	doing_ajax = true;

    $("#products").html("<br><br><img class='NoBorder' src='"+URL+"images/icons/icon-ajax.gif'><br><a href='#' id='imgForFocus'></a><br><br>");
	$("#products").load('/includes/homepage/products.php', 'mode=featured', function() {
		doing_ajax = false;
	});
}

/*
 * 20100705: mayank to get Gifts for certain Price Range.
 * @params: integer startprice (from 0), integer endprice (upto 15)
 * @response: return gifts in Grid view.
 **/
function getGiftsForRange(startprice,endprice)
{
    if (doing_ajax) return;	doing_ajax = true;

	$("#products").html("<br><br><img class='NoBorder' src='"+URL+"images/icons/icon-ajax.gif'><br><a href='#' id='imgForFocus'></a><br><br>");
	$("#products").load('/includes/homepage/products.php', { mode: 'range', start_price: startprice, end_price: endprice }, function() {
		doing_ajax = false;
	});
}


/*********************************************************
 * Issuing
 *********************************************************/

//20100730: mayank to modify the function implementing validation with text and field highlighting.
function sendGift()
{
    var phonenumber  = $("#phonenumber").val().replace(/[\(\-\) ]/g, "");;
    var from              = $("#from").val();
    var mess              = $("#text_message").val();
    var giftee_email	  = $("#giftee_email").val();
    var type			  = $("#type").val();
    
    var errors = new Array();

    if(phonenumber.length != 10)
        errors[errors.length] = '1~~Enter your ten digit mobile number.';

    if(from == '' || from == 'Enter your Name')
        errors[errors.length] = '2~~From is a required field.';

    if(mess == '' || mess == 'Write your own Message')
        errors[errors.length] = '3~~Message is a required field.';

	if(giftee_email.indexOf('email address') >= 0) {
		giftee_email = '';
	}
    if( giftee_email != '' && (!(giftee_email.indexOf(".") > 2) || !(giftee_email.indexOf("@") > 0)))
        errors[errors.length] = '4~~Your giftee\'s email address appears incorrect, please double-check it.';

    if(errors.length > 0)
    {
        for(var i=0; i<errors.length; i++)
        {
            var checktill = errors[i].search(/~~/i);

            if(checktill != -1)
            {
                var finderror   = errors[i].split('~~');
                var highlight   = finderror[0];
            }

            if(typeof(highlight) == "undefined")
                highlight = false;

            if(highlight)
            {
                var checkhash = finderror[1].search(/##/i);

                if(checkhash != -1)
                {
                    var findtextbox      = finderror[1].split('##');
                    var textboxselection = findtextbox[0];
                    var errormsg         = findtextbox[1];
                }
                else
                    errormsg = finderror[1];

                if(typeof(textboxselection) == "undefined")
                    textboxselection = false;

                switch(parseInt(highlight))
                {
                    case 1:
                        $("#error_phonenumber").show();
                        $("#error_phonenumber").html(errormsg);
                        $("#error_phonenumber").fadeOut(100).fadeIn(100);
                        $("#phonenumber").css('background-color', '#FFFFCC');
                        break;
                    case 2:
                        $("#error_from").show();
                        $("#error_from").html(errormsg);
                        $("#error_from").fadeOut(100).fadeIn(100);
                        $("#fromName").css('background-color', '#FFFFCC');
                        break;
                    case 3:
                        $("#error_message").show();
                        $("#error_message").html(errormsg);
                        $("#error_message").fadeOut(100).fadeIn(100);
                        $("#text_message").css('background-color', '#FFFFCC');
                        break;
                    case 4:
                        $("#error_giftee_email").show();
                        $("#error_giftee_email").html(errormsg);
                        $("#error_giftee_email").fadeOut(100).fadeIn(100);
                        $("#giftee_email").css('background-color', '#FFFFCC');
                        break;
                }
            }
        }
    return false;
    }

    if (doing_ajax) return;	doing_ajax = true;
	$("#callback").html("<img class='NoBorder' src='"+URL+"images/icons/icon-ajax.gif'>");
	$("#callback").show();

	$.ajax(
    {
        type: "POST",
        url: URL_AJAX + "ajax.gift.send.php",
        data: 	"gift_id=" + $("#gift_id").val() +
                "&phonenumber=" + phonenumber + "&from=" + escape(from) +
                "&message=" + escape(mess) + "&promcode=" + $("#promcode").val()+ 
                "&usecc=" + $("#usecc").attr("checked") + "&email=" + escape(giftee_email) + "&type=" + type
                ,
        success: function(data)
        {
            cbk_sendGift(data);
        }
	});
}

function sendGiftStarbucks() {

	if (doing_ajax) return;	doing_ajax = true;

	$("#callbackError").html("<img class='NoBorder' src='"+URL+"images/icons/icon-ajax.gif'>");
	$("#callbackError").show();

	var gift_id = $("input[@name='starbucks_id']:checked").val();

	var mess = $("#ddTexts").val();
	if(mess == 'other'){
		mess = $("#customText").val();
	}

	$.ajax( {
	        type: "POST",
	        url: URL_AJAX + "ajax.gift.sendStarbucks.php",
	        data: 	"&phonenumber=" + $("#phonenumber").val() +
	        		"&from=" + $("#fromName").val() +
	        		"&message=" + mess +
	        		"&product=" + gift_id,
	        success: function(data) {
					doing_ajax = false;
					//$("#callbackError").html(data);

					if(data == 'ok')
						$("#callback").html("<center><br><br><img src='"+URL+"images/icons/icon-ok.png'><br><br><h2>Your gift has been sent</h2></center>");
					else
						$("#callbackError").html(data);
				}
	} );
}

function setPopulateNumber(phone) {

	if (doing_ajax) return;	doing_ajax = true;

	$.ajax( {
	        type: "POST",
	        url: URL_AJAX + "ajax.gift.populate.php",
	        data: 	"phone=" + phone,
	        success: function(data) {
	        	window.location=URL_CPANEL+"gifts/";
	        }
	} );
}

//20100804: mayank to modify this function to proper accept the phone number and validate them.
function doGoToIssuing(id_gift, id_phonenumber_first, id_phonenumber_second, id_phonenumber_third,thickboxid)
{
    if(typeof(thickboxid) == "undefined")
        thickboxid = false;
    
    var firstphonenumber  = $("#"+id_phonenumber_first).val();
    var secondphonenumber = $("#"+id_phonenumber_second).val();
    var thirdphonenumber  = $("#"+id_phonenumber_third).val();
    
    if(firstphonenumber == '' || firstphonenumber == 'xxx' || firstphonenumber.length != 3)
    {
        if(thickboxid)
        {
            $("#callback_thichbox").show();
            $("#callback_thichbox").html('Enter your ten digit mobile number.');
            $("#callback_thichbox").fadeOut(100).fadeIn(100);
        }
        else
        {
            $('html, body').animate({scrollTop: 0}, 'slow');
            $("#callback").show();
            $("#callback").html('Enter your ten digit mobile number.');
            $("#callback").fadeOut(100).fadeIn(100);
        }
        $("#"+id_phonenumber_second).css('background-color', '');
        $("#"+id_phonenumber_third).css('background-color', '');
        $("#"+id_phonenumber_first).css('background-color', '#FFFFCC');
        $("#"+id_phonenumber_first).focus();
        return false;
    }
    if(secondphonenumber == '' || secondphonenumber == 'xxx' || secondphonenumber.length != 3)
    {
        if(thickboxid)
        {
            $("#callback_thichbox").show();
            $("#callback_thichbox").html('Enter your ten digit mobile number.');
            $("#callback_thichbox").fadeOut(100).fadeIn(100);
        }
        else
        {
            $('html, body').animate({scrollTop: 0}, 'slow');
            $("#callback").show();
            $("#callback").html('Enter your ten digit mobile number.');
            $("#callback").fadeOut(100).fadeIn(100);
        }
        $("#"+id_phonenumber_third).css('background-color', '');
        $("#"+id_phonenumber_first).css('background-color', '');
        $("#"+id_phonenumber_second).css('background-color', '#FFFFCC');
        $("#"+id_phonenumber_second).focus();
        return false;
    }
    if(thirdphonenumber == '' || thirdphonenumber == 'xxxx' || thirdphonenumber.length != 4)
    {
        if(thickboxid)
        {
            $("#callback_thichbox").show();
            $("#callback_thichbox").html('Enter your ten digit mobile number.');
            $("#callback_thichbox").fadeOut(100).fadeIn(100);
        }
        else
        {
            $('html, body').animate({scrollTop: 0}, 'slow');
            $("#callback").show();
            $("#callback").html('Enter your ten digit mobile number.');
            $("#callback").fadeOut(100).fadeIn(100);
        }
        $("#"+id_phonenumber_first).css('background-color', '');
        $("#"+id_phonenumber_second).css('background-color', '');
        $("#"+id_phonenumber_third).css('background-color', '#FFFFCC');
        $("#"+id_phonenumber_third).focus();
        return false;
    }
    var phonenumber = firstphonenumber+secondphonenumber+thirdphonenumber;
    
	var val = isValidPhone(phonenumber);
	if(!val)
    {
        if(thickboxid)
        {
            $("#callback_thichbox").show();
            $("#callback_thichbox").html('Enter your ten digit mobile number.');
            $("#callback_thichbox").fadeOut(100).fadeIn(100);
        }
        else
        {
            $('html, body').animate({scrollTop: 0}, 'slow');
            $("#callback").show();
            $("#callback").html('Enter your ten digit mobile number.');
            $("#callback").fadeOut(100).fadeIn(100);
        }
        $("#"+id_phonenumber_first).css('background-color', '#FFFFCC');
        $("#"+id_phonenumber_second).css('background-color', '#FFFFCC');
        $("#"+id_phonenumber_third).css('background-color', '#FFFFCC');
		$("#"+id_phonenumber_first).focus();
		return false;
	}

    if (doing_ajax) return;	doing_ajax = true;
    if(thickboxid)
    {
        $("#callback_thichbox").html("<img class='NoBorder' src='"+URL+"images/icons/icon-ajax.gif'>");
        $("#callback_thichbox").show();
    }
    else
    {
        $("#callback").html("<img class='NoBorder' src='"+URL+"images/icons/icon-ajax.gif'>");
        $("#callback").show();
    }
    $.ajax({
        type: "POST",
        url: URL_AJAX + "ajax.cellphone.validate2.php",
        data: 	"&phonenumber=" + val,
        success: function(data)
        {
            doing_ajax = false;
            if(data!="")
            {
                if(thickboxid)
                {
                    $("#callback_thichbox").html(data);
                    $("#callback_thichbox").fadeOut(100).fadeIn(100);
                }
                else
                {
                    $('html, body').animate({scrollTop: 0}, 'slow');
                    $("#callback").html(data);
                    $("#callback").fadeOut(100).fadeIn(100);
                }
                $("#"+id_phonenumber_first).css('background-color', '#FFFFCC');
                $("#"+id_phonenumber_second).css('background-color', '#FFFFCC');
                $("#"+id_phonenumber_third).css('background-color', '#FFFFCC');
                $("#"+id_phonenumber_first).focus();
                return false;
            }
            else
            {
                if(thickboxid)
                   $("#callback_thichbox").html('');
                else
                    $("#callback").html('');
                var url = URL_CPANEL + "send/" + id_gift + "/" + val;
                window.location = url;
            }
        }
    });
}

function getHistoryInit() {
	var phrase = $("phrase").val();
	var category = $("category").val();

	getHistory(phrase, category, 0, 0);
}

function getHistoryPaging(page) {
	getHistory("", "", page, 1);
}

function getHistory(phrase, category, page, mode) {

	if (doing_ajax) return;	doing_ajax = true;

	$("#callback").html("<center><p style='width: 680px;'><br><br><br><img class='NoBorder' src='"+URL+"images/icons/icon-ajax-big.gif'><br><font color=black>Getting History...</font><br><br></p></center>");

	$.ajax( {
	        type: "POST",
	        url: URL_AJAX + "ajax.gift.history.php",
	        data: 	"phrase=" 		+ $("#phrase").val() +
	        		"&category="	+ $("#category").val() +
	        		"&page=" 		+ page +
	        		"&mode="		+ mode
	        		,
	        success: function(data) {
	        	$("#callback").html(data);
	        	doing_ajax = false;
	        }
	} );

}

function getAccountHistory(page) {

	if (doing_ajax) return;	doing_ajax = true;

	$("#callback").html("<center><br><br><br><img class='NoBorder' src='"+URL+"images/icons/icon-ajax-big.gif'><br><font color=black>Getting History...</font><br><br></center>");

	$.ajax( {
	        type: "POST",
	        url: URL_AJAX + "ajax.account.history.php",
	        data: 	"&page="+ page,
	        success: function(data) {
	        	$("#callback").html(data);
	        	doing_ajax = false;
	        }
	} );

}

function redeemGift() {
	if (doing_ajax) return;	doing_ajax = true;

	$("#callback-redeemPopup").html("<center><img class='NoBorder' src='"+URL+"images/icons/icon-ajax.gif'></center>");

	$.ajax( {
	        type: "POST",
	        url: URL_AJAX + "ajax.redeem.php",
	        data: 	"redeemCode=" + $("#redeemCode").val(),
	        success: function(errors) {
	        	if (errors.substring(0,3)=="ok_") {
	        		$("#formReedemCode").html("<tr><td>"+errors.replace(/ok_/,"")+"</td></tr>");
	        		$("#callback-redeemPopup").html("");
	        		$("#formReedemCode").removeClass("Form");
	        	} else {
	        		$("#callback-redeemPopup").html(errors);
	        	}
	        	doing_ajax = false;
	        }
	} );
}

/*********************************************************
 * FRIENDS
 *********************************************************/

function getFriends() {

	$.ajax( {
	        type: "POST",
	        url: URL_AJAX + "ajax.user.getfriends.php",
	        success: function(data) { $("#callback-friends").html(data); }
	} );

}

function getFriendsPaging(page) {

	$("#callback").html("<center><img class='NoBorder' src='"+URL+"images/icons/icon-ajax-big.gif'></center>");
	$("#callback").show();

	$.ajax( {
	        type: "POST",
	        url: URL_AJAX + "ajax.user.getfriends.paging.php",
	        data: 	"page=" + page,
	        success: function(data) {
	        	$("#callback").html(data);
	        }
	} );

}

function doFriendAdd() {

	if (doing_ajax) return;	doing_ajax = true;
	$("#callback-friend").show();
	$("#callback-friend").html("<img class='NoBorder' src='"+URL+"images/icons/icon-ajax.gif'>");

	$.ajax( {
	        type: "POST",
	        url: URL_AJAX + "ajax.user.friend.add.php",
	        data: 	"fname=" + $("#fname").val() +
	        		"&lname=" + $("#lname").val() +
	        		"&nickname=" + $("#nickname").val() +
	        		"&cellphone=" + $("#cellphone").val()
	        		,
	        success: function(errors) {
	        	doing_ajax = false;
				if (!errors) {
					getFriendsPaging(1);
					$("#fname").val("");
					$("#lname").val("");
					$("#nickname").val("");
					$("#cellphone").val("");
					$("#fname").focus();
					$("#callback-friend").html("<font color=blue>Friend successfully added!</font>");
					$("#callback-friend").fadeOut(5000);
				} else {
					$("#callback-friend").html(errors);
				}
	        }
	} );
}

function doFriendEdit(id) {

	if (doing_ajax) return;	doing_ajax = true;

	$("#callback-friendPopup").html("<img class='NoBorder' src='"+URL+"images/icons/icon-ajax.gif'>");

	$.ajax( {
	        type: "POST",
	        url: URL_AJAX + "ajax.user.friend.edit.php",
	        data: 	"fname=" + $("#fnamePopup").val() +
	        		"&lname=" + $("#lnamePopup").val() +
	        		"&nickname=" + $("#nicknamePopup").val() +
	        		"&cellphone=" + $("#cellphonePopup").val() +
	        		"&id=" + id
	        		,
	        success: function(errors) {
	        	doing_ajax = false;
				if (!errors) {
					$("#callback-friendPopup").html("<font color=blue>User successfully updated!</font>");
					tb_remove();
					getFriendsPaging(1);
				} else {
					$("#callback-friendPopup").html(errors);
				}
	        }
	} );
}

function doFriendDelete(id) {

	var answer = confirm("Are you sure you want to delete this contact?");
	if(!answer) return;

	$.ajax( {
	        type: "POST",
	        url: URL_AJAX + "ajax.user.friend.delete.php",
	        data: 	"id=" + id,
	        success: function(errors) {
	        	$("#friend_"+id).slideUp("slow", function () {$("#friend_"+id).remove();} );
	        	getFriendsPaging(1);
	        }
	} );
}
/*********************************************************
 * CREDIT CARD
 *********************************************************/

 function doAddCreditCard() {

	if (doing_ajax) return;	doing_ajax = true;

	$("#callback-addcreditcard").html("<center><img class='NoBorder' src='"+URL+"images/icons/icon-ajax.gif'><br><font color=black>Adding Credit Card...</font></center>");
	$("#callback-addcreditcard").show();

	$.ajax( {
	        type: "POST",
	        url: URL_AJAX + "ajax.creditcard.add.php",
	        data: 	"ccnum=" 		+ $("#ccnum").val() +
	        		"&cardtype_id="	+ $("#cardtype").val() +
	        		"&expmonth=" 	+ $("#expmonth").val() +
	        		"&expyear=" 	+ $("#expyear").val() +
	        		"&firstname=" 	+ $("#cc-firstname").val() +
	        		"&lastname=" 	+ $("#cc-lastname").val() +
	        		"&preferred="	+ $("#preferred").attr("checked") +
	        		"&address1=" 	+ $("#address1").val() +
	        		"&address2=" 	+ $("#address2").val() +
	        		"&state_id=" 	+ $("#state_id").val() +
	        		"&city=" 		+ $("#city").val() +
	        		"&zipcode=" 	+ $("#zipcode").val()
	        		,
	        success: function(data) { cbk_doAddCreditCard(data);}
	} );
}

function doAddFunds() {

	if (doing_ajax) return;	doing_ajax = true;

	$("#callback-addfunds").html("<center><img class='NoBorder' src='"+URL+"images/icons/icon-ajax.gif'><br></center>");
	$("#callback-addfunds").show();

	$.ajax( {
	        type: "POST",
	        url: URL_AJAX + "ajax.creditcard.add.funds.php",
	        data: 	"card_id=" 	+ $("#creditcard").val() +
	        		"&amount=" 	+ $("#amount").val() +
	        		"&password="+ $("#password").val()
	        		,
	        success: function(data) { cbk_doAddFunds(data);}
	} );
}

function doAddFundsFromIssuing() {

	if (doing_ajax) return;	doing_ajax = true;

	$("#callback").html("<center><img class='NoBorder' src='"+URL+"images/icons/icon-ajax.gif'></center>");
	$("#callback").show();

	$.ajax( {
	        type: "POST", url: URL_AJAX + "ajax.creditcard.add.funds.by.issuing.php",
	        success: function(data) {
	        	doing_ajax = false;
	        	if (data.length>0) $("#callback").html(data);
	        	else sendGift();
	        }
	} );
}

function doAddAutomaticRecharge(card_id) {

	if (doing_ajax) return;	doing_ajax = true;

	$("#callback").html("<img class='NoBorder' src='"+URL+"images/icons/icon-ajax.gif'>");
	$("#callback").show();

	$.ajax( {
	        type: "POST",
	        url: URL_AJAX + "ajax.creditcard.add.autorecharge.php",
	        data: 	"card_id=" 	+ card_id +
	        		"&recharge_amount=" + $("#amount").val() +
	        		"&threshold=" + $("#threshold").val()
	        		,
	        success: function(data) { cbk_doAddAutomaticRecharge(data);}
	} );
}

function doDeleteAutomaticRecharge(auto_id) {

	if (doing_ajax) return;	doing_ajax = true;

	$("#callback-remove").html("<img class='NoBorder' src='"+URL+"images/icons/icon-ajax.gif'>");
	$("#callback-remove").show();

	$.ajax( {
	        type: "POST",
	        url: URL_AJAX + "ajax.creditcard.delete.autorecharge.php",
	        data: 	"auto_id=" 	+ auto_id,
	        success: function(data) {
	        	doing_ajax = false;
	        	$("#callback-remove").hide();
	        	$("#callback-remove-ok").html("<td><h5>&raquo; Automatic Recharge Successfully removed!</h5></td>");
	        	$("#callback-remove-ok").fadeOut(100).fadeIn(100);
	        }
	} );
}

function doDeleteCard(card_id) {
	if (doing_ajax) return;	doing_ajax = true;
	$("#callback-deletecard"+card_id).html("<center><img class='NoBorder' src='"+URL+"images/icons/icon-ajax.gif'><br><font color=black> Deleting Credit Card...</font></center>");
	$("#callback-deletecard"+card_id).show();

	$.ajax( {
	        type: "POST",
	        url: URL_AJAX + "ajax.creditcard.delete.php",
	        data: 	"card_id=" 	+ card_id,
	        success: function(data) { cbk_doDeleteCard(data,card_id);}
	} );
}

function doPreferredCard(card_id) {
	if (doing_ajax) return;	doing_ajax = true;

	$.ajax( {
	        type: "POST",
	        url: URL_AJAX + "ajax.creditcard.set.preferred.php",
	        data: 	"card_id=" 	+ card_id,
	        success: function(data) { doing_ajax = false; }
	} );
}


/*********************************************************
 * CELLPHONE
 *********************************************************/

function doAddCellphone() {

	if (doing_ajax) return;	doing_ajax = true;

	$("#callback").html("<img class='NoBorder' src='"+URL+"images/icons/icon-ajax.gif'>");
	$("#callback").show();

	$.ajax( {
	        type: "POST",
	        url: URL_AJAX + "ajax.cellphone.add.php",
	        data: 	"phonenumber=" + $("#phonenumber").val() +
	        		"&cellcarrier_id=" + $("#cellcarrier_id").val()
	        		,
	        success: function(data) { cbk_doAddCellphone(data);}
	} );
}

function doValidateCellphone(phone_id) {

	if (doing_ajax) return;	doing_ajax = true;

	$("#callback-pending").html("<img class='NoBorder' src='"+URL+"images/icons/icon-ajax.gif'>");
	$("#callback-pending").show();

	$.ajax( {
	        type: "POST",
	        url: URL_AJAX + "ajax.cellphone.validate.php",
	        data: 	"validationcode=" + $("#validationcode_"+phone_id).val() +
	        		"&phonenumber=" + $("#phonenumber_"+phone_id).val()
	        		,
	        success: function(data) { cbk_doValidateCellphone(data);}
	} );

}


function doValidateSintaxCellphone()
{
    var firstphonenumber  = $("#first_cellphone-popup").val();
    var secondphonenumber = $("#second_cellphone-popup").val();
    var thirdphonenumber  = $("#third_cellphone-popup").val();

    if(firstphonenumber == '' || firstphonenumber == 'xxx' || firstphonenumber.length != 3)
    {
        $("#callback-cellphone-validation").html('Please enter your ten digit mobile number.');
        $("#callback-cellphone-validation").fadeOut(100).fadeIn(100);
        $("#second_cellphone-popup").css('background-color', '');
        $("#third_cellphone-popup").css('background-color', '');
        $("#first_cellphone-popup").css('background-color', '#FFFFCC');
        $("#first_cellphone-popup").focus();
        return false;
    }
    if(secondphonenumber == '' || secondphonenumber == 'xxx' || secondphonenumber.length != 3)
    {
        $("#callback-cellphone-validation").html('Please enter your ten digit mobile number.');
        $("#callback-cellphone-validation").fadeOut(100).fadeIn(100);
        $("#third_cellphone-popup").css('background-color', '');
        $("#first_cellphone-popup").css('background-color', '');
        $("#second_cellphone-popup").css('background-color', '#FFFFCC');
        $("#second_cellphone-popup").focus();
        return false;
    }
    if(thirdphonenumber == '' || thirdphonenumber == 'xxxx' || thirdphonenumber.length != 4)
    {
        $("#callback-cellphone-validation").html('Please enter your ten digit mobile number.');
        $("#callback-cellphone-validation").fadeOut(100).fadeIn(100);
        $("#second_cellphone-popup").css('background-color', '');
        $("#first_cellphone-popup").css('background-color', '');
        $("#third_cellphone-popup").css('background-color', '#FFFFCC');
        $("#third_cellphone-popup").focus();
        return false;
    }

    var phonenumber = firstphonenumber + secondphonenumber + thirdphonenumber;
	if (doing_ajax) return; doing_ajax = true;

	$("#callback-cellphone-validation").html("<img class='NoBorder' src='"+URL+"images/icons/icon-ajax.gif'>");

	$.ajax( {
	        type: "POST",
	        url: URL_AJAX + "ajax.cellphone.sintax.validation.php",
	        data: 	"phonenumber=" + phonenumber
            ,
	        success: function(errors)
            {
	        	//20100817: mayank: callback function to maintain the consistency of the site.
                cbk_doValidateSintaxCellphone(errors);
	        }
	} );

}

function doResendValidationCode(phone_id) {

	if (doing_ajax) return;	doing_ajax = true;

	$("#callback-pending").html("<img class='NoBorder' src='"+URL+"images/icons/icon-ajax.gif'>");
	$("#callback-pending").show();

	$.ajax( {
	        type: "POST",
	        url: URL_AJAX + "ajax.cellphone.resend.php",
	        data: 	"id=" + phone_id,
	        success: function(errors) {
	        	 doing_ajax = false;
	        	if (!errors) $("#callback-pending").html("&raquo; Code sent.");
	        	else $("#callback-pending").html("&raquo; "+errors);
	        }
	} );

}

function doPreferredCellphone(phone_id) {
	if (doing_ajax) return;	doing_ajax = true;

	$.ajax( {
	        type: "POST",
	        url: URL_AJAX + "ajax.cellphone.set.preferred.php",
	        data: 	"phone_id=" + phone_id,
	        success: function(data) { doing_ajax = false; }
	} );
}

function doDeleteCellphone(phone_id) {

	var answer = confirm("Are you sure?");
	if(!answer) return;

	if (doing_ajax) return;	doing_ajax = true;
	$("#callback-cellphone").html("<img src='"+URL+"images/icons/icon-ajax-big.gif'>");

	$.ajax( {
	        type: "POST",
	        url: URL_AJAX + "ajax.cellphone.delete.php",
	        data: 	"phone_id=" + phone_id,
	        success: function(errors) {
	        	doing_ajax = false;
	        	if (!errors) {
	        		var methodDashboard = "window.location='"+URL_CPANEL+"profile';";
	        		setTimeout(methodDashboard, 500);
	        	} else {
	        		$("#callback-cellphone").html(errors);
	        	}
	        }
	} );
}

function doExpressCheckout() {
	if (doing_ajax) return; doing_ajax = true;
	if(!validator.form()) {
		return;
	}
    var from                = $("#from").val();
	var textmessage         = $("#text_message").val();
    var accountfirstname    = $("#account-firstname").val();
    var accountlastname     = $("#account-lastname").val();
    var accountemail        = $("#account-email").val();
    var cccardtype          = $("#cc-cardtype").val();
    var ccccnum             = $("#cc-ccnum").val();
	var cc_cvv				= $("#cc-cvv").val();
    var ccexpmonth          = $("#cc-expmonth").val();
    var ccexpyear           = $("#cc-expyear").val();
    var ccfirstname         = $("#cc-firstname").val();
    var cclastname          = $("#cc-lastname").val();
    var ccaddress1          = $("#cc-address1").val();
    var cccity              = $("#cc-city").val();
    var ccstateid           = $("#cc-state_id").val();
    var cczipcode           = $("#cc-zipcode").val();
    var termscondition      = $("#terms").val();
    var accountpass1        = $("#account-password1").val();
    var accountpass2        = $("#account-password2").val();

	from = alltrim(from);
    textmessage = alltrim(textmessage);
    accountpass1 = alltrim(accountpass1);
    accountpass2 = alltrim(accountpass2);
	cc_cvv = alltrim(cc_cvv);


	$("#callback-expresscheckout").html(
		"<img class='NoBorder' src='"+URL+"images/icons/icon-ajax.gif'>");
	$("#callback-expresscheckout").show();
    $("#bottom_callback-expresscheckout").html(
		"<img class='NoBorder' src='"+URL+"images/icons/icon-ajax.gif'>");
    $("#bottom_callback-expresscheckout").show();

	var mess = escape(textmessage);
	$.ajax( {
	        type: "POST",
	        url: URL_AJAX + "ajax.expresscheckout.php",
	        data:
				"account-firstname=" 	+ accountfirstname +
				"&account-lastname=" 	+ accountlastname +
				"&account-email=" 		+ accountemail +
				"&account-password1=" 	+ accountpass1 +
                "&account-password2=" 	+ accountpass2 +
				"&cc-ccnum=" 			+ ccccnum +
				"&cc-cvv="				+ cc_cvv +
				"&cc-cardtype_id=" 		+ cccardtype +
				"&cc-expmonth=" 		+ ccexpmonth +
				"&cc-expyear=" 			+ ccexpyear +
				"&cc-firstname=" 		+ ccfirstname +
				"&cc-lastname=" 		+ cclastname +
				"&cc-address1="			+ ccaddress1 +
				"&cc-address2=" 		+ $("#cc-address2").val() +
				"&cc-city=" 			+ cccity +
				"&cc-state_id=" 		+ ccstateid +
				"&cc-zipcode=" 			+ cczipcode +
				"&cc-preferred=" 		+ $("#cc-preferred").val() +
				"&gift-id=" 			+ $("#gift-id").val() +
				"&gift-phonenumber=" 	+ $("#gift-phonenumber").val() +
                "&gift-email="          + $("#gift-email").val() +
				"&from="                + from +
				"&message=" 			+ mess +
				"&terms="               + termscondition +
				"&promcode=" 			+ $("#promcode").val()
	        		,
	        success: function(errors)
            {
                cbk_doExpressCheckout(errors);
	        }
	} );
}

function getGoogleMap() {
	if (doing_ajax) return;	doing_ajax = true;

	$("#callback-error").html("<img class='NoBorder' src='"+URL+"images/icons/icon-ajax.gif'>");

	$.ajax( {
	        type: "POST",
	        url: URL_AJAX + "ajax.googlemap.get.php",
	        data: 	"zipcode=" + $("#zipcode").val(),
	        success: function(errors) {
	        	if (errors.substring(0,3)=="ok_") {
	        		var c = errors.replace(/ok_/,"");
	        		$("#callback-map").html(c);
	        		$("#callback-error").html("");
	        	} else {
	        		$("#callback-error").html(errors);
	        	}
	        	doing_ajax = false;
	        }
	} );
}



function doNoMail(id) {
	var e = $("#item-"+id).attr("checked");

	$.ajax( {
	        type: "POST",
	        url: URL_AJAX + "ajax.nomail.set.php",
	        data: 	"item=" + id + "&e="+e
	} );
}


function applyPromoCode()
{
	var pcode = $('#promcode').val();
	if($.trim(pcode) == '')
	{
		alert('please enter promocode');
		$('#special_rates').hide();
		return;
	}
	var g_id = $('#gift_id').val();
	if (doing_ajax) return;	doing_ajax = true;
    $("#special_rates").html("<center><img class='NoBorder' src='"+URL+"images/icons/icon-ajax.gif'></center>");
    $('#special_rates').show();
    
	$.ajax( {
	        type: "POST",
	        url: URL_AJAX + "promocode.php",
	        data: 	"pcode=" + pcode + "&g_id="+g_id,
			dataType : 'xml',
			success: function(data){
				if($(data).find("error").length==1)
				{
					$('#promcode').val('');
					var error = $(data).find("error").text();
					if(error == "Promo code not applicable for this gift") {
						error = "Promo code - out of stock.";
					}
					alert(error);
					$('#special_rates').hide();
				}
				else
				{
					var p = $.trim($(data).find("gift").text());
					var str = "Total price of the gift after applying <br/>promocode is : <strong>$</strong>";
					if(p.indexOf('.')!= -1)
						$('#special_rates').show().html(str+p);
					else
						$('#special_rates').show().html(str+'<strong>'+p+'.00</strong>');
				}
				doing_ajax = false;
			}
	} );
}

function verify_code()
{
	var last4 = $("#last4").val();
	if(last4 == "")
	{
		alert("please enter your last 4 digit of phone no");
		return false;
	}
	var code = $('#sms-code').val();
	$.ajax( {
	        type: "POST",
	        url: URL_AJAX + "detailed_message.php",			
	        data: 	"last4=" + last4 + "&sms_code="+code,			
			success: function(data){					
			
			if($(data).find("code").length == 1  && $(data).find("code").attr("value") != 0)
			{
				alert($(data).find("message").attr('value'));
				window.location.href = URL;
				return false;
			}
			else
			{
				var htm = '<img src="'+$(data).find("image").attr("value")+'" alt="image" title="gift">';
				$('#image').html(htm);
				$('#messageHead').html($(data).find("messageHead").attr("value")+'!');
				var message_text = $(data).find("messageBody").attr("value");
				if(message_text)
				{
					
					var index = message_text.indexOf('$NEW_LINE$');
					while(index != -1)
					{					
						message_text = message_text.replace('$NEW_LINE$','<br/>');						
						index = message_text.indexOf('$NEW_LINE$');					
					}
					var str = message_text;			
					
					th_str = '<a href="http://www.giiv.com" style="color:#669900; text-decoration:none"><strong>www.giiv.com</strong></a>';					
					str = str.replace("www.giiv.com",th_str);					
					
					var tempurl = 'http://www.giiv.com/t/';
					var cnt = str.indexOf(tempurl);					
					if(cnt != -1)
					{
						var lastcnt = tempurl.length + 14;	
						var url = str.substr(cnt,lastcnt);
						str = str.replace(url,'');
						th_str = '<a href="'+url+'" style="color:#669900"><strong>click here</strong></a>';
						str = str.replace("click here",th_str);						
					}
					
					$('#custom_meesage').html(str);								
					$("#TB_overlay").css('display','none');
					$("#TB_window").css('display','none');
				}
				else
					alert("Please try later!");
			}
			}
	} );		
}

var doing_god=false;

// GOD functionality pushed to a future release.
// This function is not used.
function getGiftOfTheDay() {
	if(doing_god) return; doing_god=true;
	
	$.ajax({
	type: "GET",
	url: URL_AJAX + "ajax.homepage.gifts.php",
	data: 	"mode=4",
	success: function(data) {
            
		if($(data).find("gift").length == 0)
		{
                    parsed = $(data);
                    $("#gift-of-the-day-title").html(parsed.find("name").text());
                    $("#gift-of-the-day-desc").html(parsed.find("longdesc").text());
                    $("#gift-of-the-day-price").html("$"+parsed.find("price").text());
                    $("#gift-of-the-day-icon").attr('src', parsed.find("icon").text());
                    
                    $("#gift-of-the-day a").each(function() {
                        $(this).attr('href', "cpanel/send/"+parsed.find("id").text());
                    });
                    $('#gift-of-the-day').fadeIn("slow");
                    doing_ajax = false;

                    return false;
		} 
	}
	});	
}

/**
 * @author: Mayank Gupta:20100818.
 * @desc: This function is check for the phone number entered in the look up your gift section.
 * @param: Phone number retrieve from the caller page dynamically.
 * @return: If valid phone number then forward to another page otherwise show the errors.
 */
function goToRedeem()
{
    var phonenumber = $("#phonenumber").val().replace(/[\(\-\) ]/g, "");

    if(phonenumber.length != 10)
    {
        $("#phonecallback").html('<br>Enter your ten digit mobile number.');
        $("#phonecallback").css({"color":"red","font-family":"arial","font-size":"9pt","font-weight":"bold"});
        $("#phonecallback").fadeOut(100).fadeIn(100);
        $("#phonenumber").css('background-color', '#FFFFCC');
        $("#phonenumber").focus();
        return false;
    }

    if (doing_ajax) return; doing_ajax = true;
    $("#phonecallback").html("<br><img class='NoBorder' src='"+URL+"images/icons/icon-ajax.gif'>");
    $.ajax({
        type: "POST",
        url: URL_AJAX + "ajax.cellphone.validate2.php",
        data: 	"&phonenumber=" + phonenumber,
        success: function(data)
        {
            doing_ajax = false;
            if(data!="")
            {
                $("#phonecallback").html(data);
                $("#phonecallback").css({"color":"red","font-family":"arial","font-size":"9pt","font-weight":"bold"});
                $("#phonecallback").fadeOut(100).fadeIn(100);
                $("#phonenumber").css('background-color', '#FFFFCC');
                $("#phonenumber").focus();
                return false;
            }
            else
            {
                $("#phonecallback").css({"color":"","font-family":"","font-size":"","font-weight":""});
                $("#phonecallback").html('Did you receive a gift from someone?<br />You can look it up by entering your mobile number.');
                window.location = URL+"redeem/"+phonenumber;
            }
        }
    });
}

