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)) {
		alert("Please, type your valid email");
		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() {
	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").val() +
	        		"&comments=" + $("#comments").val() +
	        		"&email=" + $("#email").val() + "&captcha=" + $("#captcha_code").val()
	        		,
	        success: function(data) {
                        if(data == 1)
                        {
                            doing_ajax = false;
                            $("#callback").css({'color':'#ff0000'});
                            $("#callback").html("<center><br />Message sent!</center>");
                            $("#name").val("");
                            $("#comments").val("");
                            $("#email").val("");
                            $("#captcha_code").val("");
                            document.getElementById('captcha').src = '/securimage_show.php?' + Math.random();
                        }
                        else
                        {
                            doing_ajax = false;
                            $("#callback").css({'color':'#ff0000'});
                            $("#callback").html("<br />"+data);
                        }
	        }
	} );

}

function doSignUp() {

	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").val() +
	        		"&lastname=" + $("#lastname").val() +
	        		"&email=" + $("#email").val() +
	        		"&password1=" + $("#password1").val() +
	        		"&password2=" + $("#password2").val()
	        		,
	        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);
		        }
	        }
	} );
}

/*********************************************************
 * ISSUING
 *********************************************************/
//20100616: mayank to chage this function for parsing xml data properly.
function getGiftsForHomepage(mode)
{
	if (doing_ajax) return;	doing_ajax = true;
	$("#page-nav").css("visibility", "hidden");
        $("#products").html("<br><br><img class='NoBorder' src='"+URL+"images/icons/icon-ajax.gif'><br><a href='#' id='imgForFocus'></a><br><br>");
	$.ajax({
	type: "GET",
	url: URL_AJAX + "ajax.homepage.gifts.php",
	data: 	"mode=" + mode,
	success: function(data)
	{
		if($(data).find("gift").length == 0)
		{
			alert('No feature gift available');
			$("#products").html("");
			doing_ajax = false;
			return false;
		}
		xmlobj = data;
		productBlock(data,0);
		doing_ajax = false;
		tb_init("a.thickbox2");
	}
	});
}

/*
 * 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;
    $("#page-nav").css("visibility", "hidden");
    $("#products").html("<br><br><img class='NoBorder' src='"+URL+"images/icons/icon-ajax.gif'><br><a href='#' id='imgForFocus'></a><br><br>");
    $.ajax({
    type: "GET",
    url: URL_AJAX + "ajax.pricerange.gifts.php",
    data: "start_price="+startprice+"&end_price="+endprice,
    success: function(data)
    {
        if($(data).find("gift").length == 0)
        {
            alert('No Gifts is in certain range.');
            $("#products").html("");
            doing_ajax = false;
            return false;
        }
        xmlobj = data;
        productBlock(data,0);
        doing_ajax = false;
        tb_init("a.thickbox2");
    }
    });

}

//20100616: mayank write functions for paging the xml result.
function productBlock(data,no,selection)
{
    try
    {
        if(typeof(selection)=='undefined')
            selection = false;
    }
    catch(err)
    {
        //do nothing;
    }
    if(data == null)
        data = xmlobj;

    $("#products").html("");
    var htm = "";
    var i   = no + 1;
    var uplimit = no + 6;
    var x = 0;
    $(data).find("gift").each(function()
    {
        var xmlno           = parseInt($(this).attr("no"));
        var gift_id         = $(this).attr("id");
        var price           = $(this).attr("price");
        var title           = $(this).attr("title");
        var vendor          = $(this).attr("vendor");

        var modified_title  = titleTwoLines(title);
        if(xmlno >= i)
        {
            x++;

            if(x == 3 || x == 4)
                htm += "<div class=\"product second\">";
            else
                htm += "<div class=\"product\">";
            htm += "<a href=\""+URL+"viewgift/"+gift_id+"?width=550&amp;height=200\" class=\"thickbox2\" title=\"click for gift details\" alt=\"click for gift details\"><img src=\""+URL+"images/icons/small/"+gift_id+".png\" class=\"NoBorder\"/></a>";
            htm += "<div class=\"box\">";
            htm += ""+modified_title+"</a> <br /> <label style=\"font-weight:normal\">by "+vendor+"</label>  <a href=\""+URL+"viewgift/"+gift_id+"?width=550&amp;height=200\" class=\"thickbox2\" title=\"click for gift details\" alt=\"click for gift details\">view detail</a><br />";
            htm += "</div>";
            htm += "<div class=\"cost box\">$ "+price+"</div>";
            htm += "<a href=\""+URL_CPANEL+"send/"+gift_id+"/\"><span></span></a>";
            htm += "</div>";

            if(i == uplimit)
            {
                return false;
            }
            i +=1;

        }
    });
    if(x==3)
            htm += "<div class=\"product second\"></div>";
    $("#products").html(htm);

    //20100630: mayank to implement the paging according to new design.

        $("#page-nav").html("");
        var glength = data.getElementsByTagName("gift").length;
        var pagecount = Math.ceil(glength/6);
        var dd = "";
        if(glength > 6)
        {
            prevgifts = uplimit -(6+6);
            if(prevgifts >= 0)
            {
                if(selection == false)
                    var prevcolor = 1;
                else
                    prevcolor = selection - 1;
                dd += "<a href=\"javascript:void(0);\" onclick=\"showPreviousGifts("+prevcolor+")\" title=\"Previous\"><span class=\"prevbutton\"></span></a>";
            }
            else
                dd += "<span class=\"prevbutton\"></span>";

            if(pagecount >= 1)
            {
                dd += "<div id=\"numbering\">";
                for(j=1;j<=pagecount;j++)
                {
                    var giftnumber = (j-1) * 6;
                    //alert(selection);
                    if(j == 1  && (selection == false || selection == 1))
                        dd += "<a href=\"javascript:void(0);\" onclick=\"showNumberGifts("+giftnumber+","+j+")\" title=\""+j+"\"><span id=\"id_"+j+"\" class=\"selected\">"+j+"</span></a>";
                    else if(j == selection)
                        dd += "<a href=\"javascript:void(0);\" onclick=\"showNumberGifts("+giftnumber+","+j+")\" title=\""+j+"\"><span id=\"id_"+j+"\" class=\"selected\">"+j+"</span></a>";
                    else
                        dd += "<a href=\"javascript:void(0);\" onclick=\"showNumberGifts("+giftnumber+", "+j+")\" title=\""+j+"\"><span id=\"id_"+j+"\">"+j+"</span></a>";
                }
                dd += "</div>";
            }

            if(glength > uplimit)
            {
                nextgifts = uplimit;
                if(selection == false)
                    var nextcolor = 2;
                else
                    nextcolor = selection + 1;
                dd += "<a href=\"javascript:void(0);\" onclick=\"showNextGifts("+nextcolor+")\" title=\"Next\"><span class=\"nxtbutton\"></span></a>";
            }
            else
                dd += "<span class=\"nxtbutton\"></span>";
        }
        if(dd !="" && pagecount>1)
        {
            $("#page-nav").css("visibility", "visible");
            $("#page-nav").html(dd);
        }

}

//20100616: mayank write function for paging the xml result.
function showNextGifts(selected)
{
	productBlock(xmlobj,nextgifts,selected);
        tb_init("a.thickbox2");
}

//20100616: mayank write function for paging the xml result.
function showPreviousGifts(selected)
{
	productBlock(xmlobj,prevgifts,selected);
        tb_init("a.thickbox2");
}

//20100616: mayank to add <br> tag in title if title contains character between 11 to 22.
function titleTwoLines(title)
{
    Title = $.trim(title);

    // only broke for less 22 chars
    var broke = 22;
    if (Title.length>=broke) return Title;

    // only broke until char 10
    broke = 10;
    var pos = Title.indexOf(" ");
    if (pos<=broke) return Title;

    if (pos!=false) return Title.substr(0,pos) +"<br>"+ Title.substr(pos); else return Title;
}

//20100630: mayank to show the GIfts by number selected.
function showNumberGifts(pagenumber,selected)
{
    productBlock(xmlobj,pagenumber,selected);
    tb_init("a.thickbox2");
}

function sendGift() {

	if (doing_ajax) return;	doing_ajax = true;

	$("#callback").html("<img class='NoBorder' src='"+URL+"images/icons/icon-ajax.gif'>");
	$("#callback").show();

	var mess = escape($("#text_message").val());
	var from = escape($("#fromName").val());
	$.ajax( {
	        type: "POST",
	        url: URL_AJAX + "ajax.gift.send.php",
	        data: 	"gift_id=" + $("#gift_id").val() +
	        		"&phonenumber=" + $("#phonenumber").val() + "&from=" + from +
	        		"&message=" + mess + "&promcode=" + $("#promcode").val()+ "&usecc=" + $("#usecc").attr("checked")
	        		,
	        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/";
	        }
	} );
}

function doValidateCellphone2(phone) {

	if (doing_ajax) return;	doing_ajax = true;
        //alert(phone);
	$.ajax( {
	        type: "POST",
	        url: URL_AJAX + "ajax.cellphone.validate2.php",
	        data: 	"&phonenumber=" + phone,
	        success: function(data) {
                   doing_ajax = false;
                   //alert('');
                    if (data!="") {
                        alert(data);
                        return false;
                    }else{
                        return true;
                    }
                }
	} );

}


function doGoToIssuing(id_gift, id_phonenumber) {
	val = isValidPhone($("#"+id_phonenumber).val());
	if(!val){
		alert('Please enter a valid mobile number');
		$("#"+id_phonenumber).focus();
		return false;
	}

        boo = doValidateCellphone2(val);
        if(boo){
            //alert(data);
            return false;
        }
	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);
	        	tb_init('a.thickbox2');
	        	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);
	        	tb_init("a.thickboxDinamyc");
	        }
	} );

}

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() {

	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: 	"c=" + $("#cellphone-popup").val()
	        		,
	        success: function(errors) {
	        	doing_ajax = false;
	        	if (!errors) window.location=URL+"redeem/"+$("#cellphone-popup").val();
	        	else $("#callback-cellphone-validation").html(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);
	        	}
	        }
	} );
}


/*********************************************************
 * EXPRESS CHECKOUT
 *********************************************************/

function doExpressCheckout() {
	//alert(URL_AJAX);
	//return;
	if (doing_ajax) return;	doing_ajax = true;

	$("#callback-expresscheckout").html("<img class='NoBorder' src='"+URL+"images/icons/icon-ajax.gif'>");
	$("#callback-expresscheckout").show();

	var mess = escape($("#text_message").val());

	$.ajax( {
	        type: "POST",
	        url: URL_AJAX + "ajax.expresscheckout.php",
	        data:
				"account-firstname=" 	+ $("#account-firstname").val() +
				"&account-lastname=" 	+ $("#account-lastname").val() +
				"&account-email=" 		+ $("#account-email").val() +
				"&account-password=" 	+ $("#account-password").val() +
				"&cc-ccnum=" 			+ $("#cc-ccnum").val() +
				"&cc-cardtype_id=" 		+ $("#cc-cardtype").val() +
				"&cc-expmonth=" 		+ $("#cc-expmonth").val() +
				"&cc-expyear=" 			+ $("#cc-expyear").val() +
				"&cc-firstname=" 		+ $("#cc-firstname").val() +
				"&cc-lastname=" 		+ $("#cc-lastname").val() +
				"&cc-address1="			+ $("#cc-address1").val() +
				"&cc-address2=" 		+ $("#cc-address2").val() +
				"&cc-city=" 			+ $("#cc-city").val() +
				"&cc-state_id=" 		+ $("#cc-state_id").val() +
				"&cc-zipcode=" 			+ $("#cc-zipcode").val() +
				"&cc-preferred=" 			+ $("#cc-preferred").val() +
				"&gift-id=" 			+ $("#gift-id").val() +
				"&gift-phonenumber=" 	+ $("#gift-phonenumber").val() +
				"&from=" 			+ $("#from").val() +
				"&message=" 			+ mess +
				"&terms=" 		+ $("#terms").val() +
				"&promcode=" 			+ $("#promcode").val()
	        		,
	        success: function(errors) {
	        	doing_ajax = false;
				if (errors.substring(0,3)=="ok_") {
					var c = errors.replace(/ok_/,"");
	        		$("#callback-expresscheckout-container").html(c);
	        	} else {
	        		$("#callback-expresscheckout").html(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>");

	$.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('');
					alert($(data).find("error").text());
					$('#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!");
			}
			}
	} );		
}


