/*
     Functions Description:
     Function Name               Description                                              Event(when function called)
     get_focus()         function would call when putting the curson on the input field.         onFocus
     lost_focus()        function would call when input field losts the focus.                   onBlur
     populate_values()   function to bring the values through Ajax                               onKeyUp
     fnEnd()             function to be called after ajax function return the values               ----
     changeStyle()       function to change the style of the div selected                        onMouseOver
     changePreviousStyle() function to recover the previous style of the div when focus lost     onMouseOut
     scroll_keys()        function to facilitate the up/down arrow keys to move on the populated onKeyDown
                          values.

/*****************************************************************************************/
var selectedDivId = 0;
var previousDivId = 0;
var divcount = 0;
var eFlag = 0;
var id    = 0;
var kn;
var backgroundColor = "#ffcc33"; // Use on changeStyle function

/*
	Function to populate Values
    @divid  id for div
    @typ=used for which field
    @fldname the field name for which the value is searching
    @fldval  key value to be searched for
    @dispfld=show the selected val
    This function could be changed as per the requirement.
*/
function populate_values(divid, typ, fldname, fldid, dispfld, selvalue) {
   //alert(document.getElementById(fldid).value.length);
	var key_len=document.getElementById(fldid).value.length;
		if(eFlag == 0)
		{
			get_focus(divid,fldname);
			kval=document.getElementById(fldid).value;
			//alert('../ajax.php?dispfld='+dispfld+'&tbl='+typ+'&autofill_catgid='+ kval);
			ajax('../ajax.php?dispfld='+dispfld+'&tbl='+typ+'&selvalue='+selvalue+'&autofill_catgid='+ kval +'', divid);
			
		}
		else
			return false;

}
/* Function to be called when the input field gets focused */
function get_focus(divid,caption)
{
	eval("document.getElementById('"+divid+"')").style.visibility="visible";
    eval("document.getElementById('"+divid+"')").innerHTML="Type to select a "+caption;
}
/* Function to be called when the input field losts focus */
function lost_focus(divid)
{
    eval("document.getElementById('"+divid+"')").style.visibility="hidden";
    eval("document.getElementById('"+divid+"')").innerHTML="";
    
}

/* Function to enabled the arrow keys to move up and down on the populated values
   Input params:
   @e -> event
   @fldid -> Field id where value of selected div to be returned.
   @divprefix -> the name of the div(without the iterator number) which would returned from ajax script.
   @divid -> populated area of divs
*/
function scroll_keys(e,fldid,divprefix,divid)
{
    var obj=window.event? event : e;       // event for IE ; e for Mozilla/Netscape/Opera
  	var key=obj.charCode? obj.charCode : obj.keyCode;
    var lastKeyCode = key;
    var curid   = parseInt(id);
    var lastid;
    var curDivId;
    var selectedDivId;
    var cflag;
    if(divcount==1)
    	cflag = 1;
    else
    	cflag = 0;
    if(divcount!=0)
      	lastid = parseInt(divcount)-1;
    else
    	return false;
    eFlag = 0;
    if(lastKeyCode == 38)
	{// Up arrow key
        var previd;
        eFlag = 1;
        if(curid <= lastid )
        {

            if(curid == 0)
            	previd = lastid;
            else
            	previd = parseInt(curid) - 1;
            curDivId  = divprefix+curid;
            if(cflag == 0)
            	changePreviousStyle(curDivId,divprefix);
        }
        selectedDivId = divprefix+previd;
        changeStyle(selectedDivId,divprefix);
        selectValue(fldid,selectedDivId);

      }
      else if(lastKeyCode == 40)
      {// Down arrow key
        var nextid;
        eFlag = 1;
        if(curid >= 0)
        {
            if(curid == lastid)
            	nextid = 0;
            else
            	nextid = parseInt(curid) + 1;
            curDivId  = divprefix+curid;
            if(cflag == 0)
            	changePreviousStyle(curDivId,divprefix);
        }
        selectedDivId = divprefix+nextid;
        changeStyle(selectedDivId,divprefix);
        selectValue(fldid,selectedDivId);

      }
      else if(lastKeyCode == 13)
      {// Enter key
      	eFlag = 1;
        var fldobj = document.getElementById(fldid);
        if(fldobj.value=="")
        	fldobj.value= eval("document.getElementById('"+selectedDivId+"')").innerHTML;
        lost_focus(divid,'','');
      }
      else
      {
      	eFlag = 0;
      }

}

/* Function to change the style of the div on mouse over event of keyup/down event occurs
   Input params:
   @divname -> div obj
   @fldname -> Field name where value of selected div to be returned.
   @divprefix -> the name of the div(without the iterator number) which returned from ajax script.
   Returns the selected div value to the text field
*/
function changeStyle(divid,divprefix)
{
// var fldobj = document.getElementById(fldid);
 var divobj = document.getElementById(divid);
 divobj.value= eval("document.getElementById('"+divid+"')").innerHTML;
   //selectedDivId = divid;
 //  id = parseInt(divid.replace(divprefix,""));    // getting div number
//   eval("document.getElementById('"+divid+"')").style.background = backgroundColor;
   return false;   // assigning new color to backgroud
  //fldobj.value= eval("document.getElementById('"+divid+"')").innerHTML;
}

/*   Returns the selected div value to the text field    */
function selectValue(fldid,divid,catgid,putcont)

{
	// alert(eval("document.getElementById('"+putcont+"')"));
	 var fldobj = document.getElementById(fldid);
     fldobj.value= eval("document.getElementById('"+divid+"')").innerHTML;
  eval("document.getElementById('"+putcont+"')").value= catgid;
     return false;
}

function putValue(tbox1,tbox1_val)
{
	document.getElementById(tbox1).value=tbox1_val;

     return false;
}
//for show option in add-content page
function show_opt1(selobj) {
	var fldobj = document.getElementById(selobj);
	if(fldobj.value=="title") {
		txtsubject.style.display = '';
		txtsubject.focus();
		return false;
	}
}

function addColumn(tblId,typ)
{
	var tblBodyObj = document.getElementById(tblId).tBodies[0];
	var newCell = tblBodyObj.rows[0].insertCell(-1);
	//newCell.width = "25%";
	newCell.align = "left";
	newCell.innerHTML='&nbsp; &nbsp; &nbsp;<input type="text" name="txt'+typ+'" value="Add '+typ+'" onFocus="if(this.value==\'Add '+typ+'\')this.value=\'\';">';
}
function deleteColumn(tblId)
{
	var allRows = document.getElementById(tblId).rows;
	//alert(allRows[0].cells.length);
	/*if (allRows[0].cells.length > 2) {
		allRows[0].deleteCell(-1);
	}*/
	for (var i=0; i<allRows[0].cells.length ; i++) {
		if (allRows[0].cells.length > 2) {
			allRows[0].deleteCell(-1);
		}
	}
}
function show_opt(sel_val) {
	deleteColumn('tblSample');
	if(sel_val=="title") {
		addColumn('tblSample',sel_val);
	}
	else if(sel_val=="url") {
		addColumn('tblSample',"vanity");
		addColumn('tblSample',sel_val);
		
	}
	else if(sel_val=="desc") {
		addColumn('tblSample',sel_val);
	}
	return true;
}
