YAHOO.example.ACFlatData = new function(){
    // Define a custom formatter function
    this.fnCustomFormatter = function(oResultItem, sQuery) {
        var sKey = oResultItem[0];
        var nQuantity = oResultItem[1];
        var sKeyQuery = sKey.substr(0, sQuery.length);
        var sKeyRemainder = sKey.substr(sQuery.length);
        var aMarkup = ["<div class='sample-result'><span class='sample-query'>",
            sKeyQuery,
            "</span>",
            sKeyRemainder,
            "</div>"];
        return (aMarkup.join(""));
    };
	
    // Instantiate one XHR DataSource and define schema as an array:
    //     ["Record Delimiter",
    //     "Field Delimiter"]
    this.oACDS = new YAHOO.widget.DS_XHR("load_contacts.php", ["\n", "\t"]);
    this.oACDS.responseType = YAHOO.widget.DS_XHR.TYPE_FLAT;
    this.oACDS.maxCacheEntries = 60;
    this.oACDS.queryMatchSubset = true;

    // Instantiate first AutoComplete
    this.oAutoComp0 = new YAHOO.widget.AutoComplete('statesinput0','statescontainer0', this.oACDS);
    this.oAutoComp0.queryDelay = 0;
    this.oAutoComp0.delimChar = ";";
    this.oAutoComp0.prehighlightClassName = "yui-ac-prehighlight";
    this.oAutoComp0.formatResult = this.fnCustomFormatter;

    // Instantiate second AutoComplete
    this.oAutoComp1 = new YAHOO.widget.AutoComplete('statesinput1','statescontainer1', this.oACDS);
    this.oAutoComp1.queryDelay = 0;
    this.oAutoComp1.delimChar = ";";
    this.oAutoComp1.prehighlightClassName = "yui-ac-prehighlight";
    this.oAutoComp1.formatResult = this.fnCustomFormatter;

    // Instantiate third AutoComplete
    this.oAutoComp2 = new YAHOO.widget.AutoComplete('statesinput2','statescontainer2', this.oACDS);
    this.oAutoComp2.queryDelay = 0;
    this.oAutoComp2.delimChar = ";";
    this.oAutoComp2.prehighlightClassName = "yui-ac-prehighlight";
    this.oAutoComp2.formatResult = this.fnCustomFormatter;

    // Instantiate forth AutoComplete
    this.oAutoComp3 = new YAHOO.widget.AutoComplete('statesinput3','statescontainer3', this.oACDS);
    this.oAutoComp3.queryDelay = 0;
    this.oAutoComp3.delimChar = ";";
    this.oAutoComp3.prehighlightClassName = "yui-ac-prehighlight";
    this.oAutoComp3.formatResult = this.fnCustomFormatter;

    // Instantiate fifth AutoComplete
    this.oAutoComp4 = new YAHOO.widget.AutoComplete('statesinput4','statescontainer4', this.oACDS);
    this.oAutoComp4.queryDelay = 0;
    this.oAutoComp4.delimChar = ";";
    this.oAutoComp4.prehighlightClassName = "yui-ac-prehighlight";
    this.oAutoComp4.formatResult = this.fnCustomFormatter;

    // Instantiate sixth AutoComplete
    this.oAutoComp5 = new YAHOO.widget.AutoComplete('statesinput5','statescontainer5', this.oACDS);
    this.oAutoComp5.queryDelay = 0;
    this.oAutoComp5.delimChar = ";";
    this.oAutoComp5.prehighlightClassName = "yui-ac-prehighlight";
    this.oAutoComp5.formatResult = this.fnCustomFormatter;
};





function switchTwoDiv(id1, id2) {
	switchDiv(id1);
	switchDiv(id2);
}
function switchDiv(id) {
	//safe function to hide an element with a specified id
	// only support IE 5+
	if (document.getElementById(id)) { 
		if(document.getElementById(id).style.display == 'none')
			document.getElementById(id).style.display = 'block';
		else
			document.getElementById(id).style.display = 'none';
	}
}


function reloadContacts(contacts)
{
	var f = document.getElementById(contacts);
	f.contentWindow.location.reload(true);
}


var del_handleSuccess = function(o){
        reloadContacts("contactFrame");
        alert("Email Address Successfully Deleted.");
        reloadContacts("contactFrame");
}

var del_handleFailure = function(o){
        alert("Failed to Delete Email Address.");
}

var del_callback =
{
  success:del_handleSuccess,
  failure:del_handleFailure
};



var add_handleSuccess = function(o){
        reloadContacts("contactFrame");
	alert("Email Address Successfully Added.");
	var contact = document.getElementById("newcontact");
	if(contact.value!=null&&contact.value!="")
		contact.value="";
	reloadContacts("contactFrame");
}

var add_handleFailure = function(o){
	alert("Failed to Add Email Address.");
}

var add_callback =
{
  success:add_handleSuccess,
  failure:add_handleFailure
};

function addContact(id)
{
	var contact = document.getElementById(id);
	if (contact) { 
		if(contact.value!=null&&contact.value!="")
		{
			var sUrl = "addcontact.php?newcontact="+contact.value;
			var request = YAHOO.util.Connect.asyncRequest('GET', sUrl, add_callback);
		}
		else
			alert("Please enter a valid contact email address!");
	}	
}

function deleteFromFrame(id)
{
        //safe function to hide an element with a specified id
        // only support IE 5+
        var myFrame = document.getElementById(id);
	var doc = myFrame.contentDocument;
	if (doc == undefined || doc == null)
        	doc = myFrame.contentWindow.document;
        var list = doc.getElementsByName("contacts[]");
        var total = 0;
	var contacts = "";
        if (list) {
                var max = list.length;
                for (var idx = 0; idx < max; idx++) {
                if (eval("list[" + idx + "].checked") == true) {
                    total += 1;
		    if(idx==0)
		        contacts=urlencode("contacts[]")+"="+urlencode(eval("list[" + idx + "].value"));
		    else
                        contacts += "&"+urlencode("contacts[]")+"="+urlencode(eval("list[" + idx + "].value"));
                   }
                }
        }
        if(total==0||contacts=="")
        {
                alert("Please select a contact first!");
                return;
        }else
        {
                var sUrl = "deletecontact.php?"+contacts;
                var request = YAHOO.util.Connect.asyncRequest('GET', sUrl, del_callback);
	}
}

function urlencode(str) {
	str = escape(str);
	str = str.replace('+', '%2B');
	str = str.replace('%20', '+');
	str = str.replace('*', '%2A');
	str = str.replace('/', '%2F');
	str = str.replace('@', '%40');
	return str;
}

function urldecode(str) {
	str = str.replace('+', ' ');
	str = unescape(str);
	return str;
}
