//20100805: mayank modify this file to adjust the phone number according to new partitioned phone number(3-3-4).
var indexActual = -1;
var classOver   = "on";
var classOut    = "off";
var result      = new Array();

function submitEnterGifting(event, id)
{
	if (!$("#callback-typeahead").is(":visible"))
        submitEnter(event, id);
}

function doAddressBookTypeahead(event, id_phonenumber_first, id_phonenumber_second, id_phonenumber_third)
{
    if(typeof("adbkCaption")=="undefined")
        return;
    
    if(adbkCaption.length==0)
        return;

    var firstphonenumber  = $("#"+id_phonenumber_first).val();
    var secondphonenumber = $("#"+id_phonenumber_second).val();
    var thirdphonenumber  = $("#"+id_phonenumber_third).val();

    if(firstphonenumber == 'xxx')
        firstphonenumber = '';
    if(secondphonenumber == 'xxx')
        secondphonenumber = '';
    if(thirdphonenumber == 'xxxx')
        thirdphonenumber = '';

    var phone = firstphonenumber+secondphonenumber+thirdphonenumber;
	phone = phone.replace(/[^0-9a-z]/gi, '');

	var re = new RegExp(phone,'gi');

    var html = "";
    delete result;
    window.result = new Array();
    for(var i=0; i<adbkCaption.length; i++)
    {
        if (adbkCaption[i].replace(/\s+/g,'').match(re) || adbkValue[i].replace(/\s+/g,'').match(re))
        {
            html = html + "<li id=\""+adbkValue[i]+"\" onmouseover=\"this.className='"+classOver+"'\" onmouseout=\"this.className='"+classOut+"'\" onclick=\"setPhone("+adbkValue[i]+", '"+id_phonenumber_first+"', '"+id_phonenumber_second+"', '"+id_phonenumber_third+"')\">"+adbkCaption[i]+"</li>";
            window.result.push(adbkValue[i]);
        }
    }
    if (html=="" || phone=="")
    {
        indexActual=-1;
        $("#callback-typeahead").hide();
        return;
    }

	html = "<ul>"+html+"</ul>";
    
    $("#callback-typeahead").html(html);
    $("#callback-typeahead").show();
	setPosition(id_phonenumber_first, id_phonenumber_second, id_phonenumber_third);
	
	// Bind Keys
	useKeyboard(event.keyCode, id_phonenumber_first, id_phonenumber_second, id_phonenumber_third);

    //20100805: mayank to autohide after 60 seconds.
    setTimeout("$(\"#callback-typeahead\").hide();",60000);
}

function setPosition(id1, id2, id3)
{
	var t = $('#'+id1).offset().top+21;
	var l = $('#'+id1).offset().left;
    
	$('#callback-typeahead').css({
	    top:t+'px',
	    left:l+'px'
	});
}

function setPhone(phone, id1, id2, id3)
{
	phone = phone.toString()
	var p1 = phone.substr(0,3);
	var p2 = phone.substr(3,3);
	var p3 = phone.substr(6,4);

    $("#"+id1).val(p1);
    $("#"+id2).val(p2);
    $("#"+id3).val(p3);
	
	$("#callback-typeahead").hide();
	indexActual=-1;
}

function useKeyboard(key, id1, id2, id3)
{
	var up     = 38;
	var down   = 40;
	var enter  = 13;
	var escape = 27;
	var phone  = "";
	
	if (key!=up && key!=down && key!=enter && key!=escape) return;   
	
	if (key==enter)
    {
		phone = getActualPhoneIndex();
		setPhone(phone, id1, id2, id3);
		return;
	}
	
	if (key==escape)
    {
		$("#callback-typeahead").hide();
		return;
	}
	
	if (key==down)
    {
		if (indexActual<(result.length-1)) indexActual++;
		setPhoneStyle();
	}
	
	if (key==up)
    {
		if (indexActual>0) indexActual--;
		setPhoneStyle();
	}
}

function getActualPhoneIndex()
{
	if (indexActual<0 && indexActual>=result.length) return "";
	return result[indexActual];
}

function setPhoneStyle()
{
	var phone = getActualPhoneIndex();
	$("#"+phone).attr("className", classOver);
}
