var cat_arr = Array('','Road Bikes','MTB Bikes','Gear');
var prov_arr = Array('','CNW','EP','FS','ACG','KZN','LP','MPL','NWC','WP');
//var prov_arr = Array('','NorthWest','Eastern Cape','Free State','Gauteng','KwaZulu -Natal','Limpopo','Mpumalanga','Northern Cape','Western Cape');

function create_form(iid,the_field,the_value){
    if(document.getElementById('item_' + iid + '_' + the_field)){
        var item_box = document.getElementById('item_' + iid + '_' + the_field);
        var form_name = 'item_' + iid + '_' + the_field + '_form';
        var html = '';
        html += '<form id="' + form_name + '" name="' + form_name + '" method="post" enctype="multipart/form-data">';
        switch(the_field){       
            case 'name':
                html += '<input type="text" value="' + the_value + '" name="the_value"/>';
            break;    
            case 'price':
                html += 'R<input type="text" value="' + the_value + '" name="the_value"/>';
            break;
            case 'descr':
                html += '<textarea name="the_value">' + the_value + '</textarea>';
            break; 
            case 'category':
                html += '<select name="the_value" style="width:155px;">';
                    for(i=0;i < cat_arr.length;i++){
                        var sel = '';
                        if(the_value == i){
                            sel = ' SELECTED ';
                        }
                        html += '<option ' + sel + 'value="' + i + '">' + cat_arr[i] + '</option>';
                    }                
                html += '</select>';
            break;  
            case 'prov':
                html += '<select name="the_value" style="width:155px;">';
                    for(i=0;i < prov_arr.length;i++){
                        var sel = '';
                        if(the_value == prov_arr[i]){
                            sel = ' SELECTED ';
                        }
                        html += '<option ' + sel + 'value="' + prov_arr[i] + '">' + prov_arr[i] + '</option>';
                    }                
                html += '</select>';
            break;                                 
            default:
            break;
        }
       html += '<input type="hidden" value="' + iid + '" name="the_iid"/>';
       html += '<input type="hidden" value="' + the_field + '" name="the_field"/>';
       html += '<input type="button" value="update" name="update_it" onclick="POSTitforme(\'' + form_name + '\');"/>';
       html += '</form>';
       item_box.innerHTML = html;
    }
}

function GETitforme(gstring){
    loadXMLDoc('/inc/ryr_data.inc.php'+gstring);
}

function POSTitforme(form_name){
   
        postXMLDoc('/second_hand_store/update_field',form_name);
}

//ajax functions START __________
var xmlhttp;
function loadXMLDoc(url){
    if (window.XMLHttpRequest){// code for Mozilla, etc.
    xmlhttp=new XMLHttpRequest();
    xmlhttp.onreadystatechange=xmlhttpChange;
    //alert(url);
    xmlhttp.open("GET",url,true);
    xmlhttp.send(null);
    }else if (window.ActiveXObject){// code for IE
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        if (xmlhttp){
            xmlhttp.onreadystatechange=xmlhttpChange;
            xmlhttp.open("GET",url,true);
            xmlhttp.send(null);
        }
    }
}
function postXMLDoc(url,formname){
    if (window.XMLHttpRequest){// code for Mozilla, etc.
    xmlhttp=new XMLHttpRequest();
        if (xmlhttp){
            var fields = new Array();
            if(formname){
                var ajax = formname;
            }
            //loop through form elements and retrieve field NAMEs and Values
            for (var x = 0; x < eval("document."+ajax+".elements.length"); x++){
                // join them into a string.
                eval("fields.push(document."+ajax+".elements[x].name+'='+document."+ajax+".elements[x].value)");
            }
            elem = 'errors';
            //sendf looks like "username=myusername&password=mypass"
            var sendf = fields.join('&');
            
            
            xmlhttp.open("POST", url, true);
            xmlhttp.onreadystatechange=xmlhttpChange;
            xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
//          xmlhttp.setRequestHeader('Content-Type', 'multipart/form-data');
            xmlhttp.send(sendf);                
        }
    }else if (window.ActiveXObject){// code for IE
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        if (xmlhttp){
            var fields = new Array();
            if(formname){
                var ajax = formname;
            }
            //loop through form elements and retrieve field NAMEs and Values
            for (var x = 0; x < eval("document."+ajax+".elements.length"); x++){
                // join them into a string.
                eval("fields.push(document."+ajax+".elements[x].name+'='+document."+ajax+".elements[x].value)");
            }
            elem = 'errors';
            //sendf looks like "username=myusername&password=mypass"
            var sendf = fields.join('&');
            
            
            xmlhttp.open("POST", url, true);
            xmlhttp.onreadystatechange=xmlhttpChange;
            xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
//          xmlhttp.setRequestHeader('Content-Type', 'multipart/x-www-form-urlencoded');

            
            xmlhttp.send(sendf);                
        }
    }
}   
function xmlhttpChange(){
    //alert(xmlhttp.status);
    if (xmlhttp.readyState==4){// if xmlhttp shows "loaded"
        if (xmlhttp.status==200){// if "OK"
            //alert(xmlhttp.responseText);          
            eval(xmlhttp.responseText);
            /*var temp = new Array();
            temp = xmlhttp.responseText.split('|');
            //alert('wilma-inner');
            //alert(temp[0]);
            
            if(temp[1]){
                //document.getElementById(temp[0]).innerHTML= temp[1];
                document.getElementById('wilma-inner').innerHTML= temp[1];
            }
            if(temp[2]){                
                eval(temp[2]);
            }*/
        }else{
            alert("Problem retrieving data")
        }
    }
}   
    
//ajax functions END __________
