
// THIS JAVASCRIPT FUNCTION IS USED TO CREATE DYNAMIC COMBOS USING AJAX //
/**
 * Sweadi
 * AJAX Combo Updater
 *
 * LICENSE: The source file is subject to GNU Public License
 * You can Modify, Delete, Use this Class in Personal and Commerical Projects
 * 
 *
 * @author     Asad Mehmood <sweadi@gmail.com>
 * @copyright  2009 sweadi
 * @version    v1.0.1 02/13/2009 16:18:46 PM
 
 */
 

/**
 * Create a Dynamic Drop Down List by Changing the value of One
 *
 * @param table name of the table from which the values are to be selected
 * @param value the field name that will be option value
 * @param label the field name that will be label for the option
 * @param con_field The name of field which will be used in where clause for filtering records
 * @param con_value The value of the condition field
 * @param sel_field Option Value that needs to be selected
 * @param order Which Order the list needs to be created if empty will be ASCENDING
*/

var select_id;
var selected_val;
function check(table, value, label, con_field, con_value, sel_id,  order, selected)
    {
        
			 select_id 		= sel_id;
             selected_val	= selected;
             
             if (window.XMLHttpRequest)
			 { 
				 http_request = new XMLHttpRequest();
				
			 }
			 else if (window.ActiveXObject)
			 { 
				 http_request = new ActiveXObject("Microsoft.XMLHTTP");
			 }
			 

			url ='http://www.jimex.co.jp/backoffice/ajax/combo.ajax.php?action=comboxml&table='+table+'&value='+value+'&label='+label+'&con_field='+con_field+'&con_value='+con_value+'&order='+order;
            //window.open(url);

			 try
			 {
				http_request.open('GET', url,true)
			 } 
			 catch(err){alert(err.toString());}
			 
			 http_request.onreadystatechange = handleResponse;     	
			 
			 try
			 {
				http_request.send(null);		
			}catch(err){alert(err.toString());}
 }


function handleResponse()
{
	if(http_request.readyState != 4)
	{
			document.getElementById('loadingDiv').innerHTML = '<img src="http://www.jimex.co.jp/backoffice/theme/images/loading.gif" border="0" />';
			showDiv('loadingDiv');
    }
	if (http_request.readyState == 4)
	{
    xmlDoc=http_request.responseXML;
				 
	var newOpt = new Option("Select","", false);
	
	document.getElementById(select_id).length = 0;
    document.getElementById(select_id).options[0] = newOpt;
	
		for( i=0; i<xmlDoc.getElementsByTagName("SEL").length;i++)
		{
			 value = xmlDoc.getElementsByTagName("VALUE")[i].firstChild.data;
			 data  = xmlDoc.getElementsByTagName("LABEL")[i].firstChild.data;
			
			if(selected_val == value)
			{
			 	flag = true;
			}
			
			else
			{
				flag = false;
			}
			
	
			 var newOpt = new Option(data,value, flag);
			 
			 document.getElementById(select_id).options[i+1] = newOpt;
		}
    document.getElementById(select_id).value = selected_val; // This line is for I.E 7
	hideDiv("loadingDiv");
    }
}

