function AjaxRequest(url, callback_function) {
  var req = null;
  
  if(window.XMLHttpRequest)
	{
		try{req=new XMLHttpRequest();
	} catch(e)
		{ req=false; }
	}
	
	if( req == null )
	{
	  var msxmlhttp = new Array(
	    'Msxml2.XMLHTTP.5.0',
	    'Msxml2.XMLHTTP.4.0',
	    'Msxml2.XMLHTTP.3.0',
	    'Msxml2.XMLHTTP',
	    'Microsoft.XMLHTTP');
	  for (var i = 0; i < msxmlhttp.length; i++) {
	    try {
	      req = new ActiveXObject(msxmlhttp[i]);
	      break;
	    } catch (e) {
	      req = null;
	    }
	  }
	}

  if (req != null) {
    req.open("GET", url, true);
    req.setRequestHeader('Content-Type','application/x-www-form-urlencoded; charset=UTF-8');
    req.onreadystatechange = function() {
      if ((req.readyState == 4)) {
      	if( req.status == 200  && callback_function != null )
      	{
        	 // alert("ok " + req.status);
           var json = req.responseText.parseJSON();
           var callback = callback_function + '(json)';
           eval(callback);
        }
        else
        {
        	// alert(req.status);
        }
      }
    }
    req.send(null);
//    delete req;
  }
  return true;
}

function UpdateForm(objects) {
  document.forms['add_frm']['fetch_meta'].disabled = false;
  for (var i in objects) {
    if (i != "toJSONString") {
      if (document.forms['add_frm'][i]) {
        document.forms['add_frm'][i].value = objects[i];
      }
    }
  }
}

function FetchMeta() {
  var url = document.forms['add_frm']['url'].value;
  if( url.indexOf("http://") == -1 && url.indexOf("https://") == -1 ) { url="http://"+url; }
  if( url.length <= 8 ) { alert('Please enter a valid url'); return; } 
  // document.forms['add_frm']['fetch_meta'].disabled = true; 
  AjaxRequest('ajax.php?cmd=fetch_meta&url=' + url, 'UpdateForm');
}

function UpdateSubCategory(objects) {
  document.getElementById(objects['div_tag']).innerHTML = objects['select_box'];
}

function FetchSubCategory(cat, selected_cat, div_tag) {
  document.getElementById(div_tag).innerHTML = "loading...";
  AjaxRequest('ajax.php?cmd=fetch_sub_category&cat=' + cat + "&selected_cat=" + selected_cat+ "&div_tag=" + div_tag, 'UpdateSubCategory');
}

function UpdatePricing(objects) {
  document.getElementById("listing_type_div").innerHTML = objects;
  if( document.forms['add_frm']['cat'].options[0].value == "0" )
  {
     document.forms['add_frm']['cat'].options[0] = null;
  }
  document.getElementById('paymentGatewayDiv').style.display = (!document.getElementById('periodprice').disabled && document.getElementById('periodprice').selectedIndex > 0 ? 'block' : 'none');
  document.getElementById('reciprocalUrlDiv').style.display = (!document.getElementById('periodprice').disabled && document.getElementById('periodprice').selectedIndex == 0 ? 'block' : 'none');
}

function FetchPricing() {
  var cat = document.forms['add_frm']['cat'].options[document.forms['add_frm']['cat'].selectedIndex].value;
  var cat1 = document.forms['add_frm']['add_cat1'].options[document.forms['add_frm']['add_cat1'].selectedIndex].value;
  var cat2 = document.forms['add_frm']['add_cat2'].options[document.forms['add_frm']['add_cat2'].selectedIndex].value;
  if (document.forms['add_frm']['periodprice']) {
    var periodprice = document.forms['add_frm']['periodprice'].options[document.forms['add_frm']['periodprice'].selectedIndex].value;
  }
  document.getElementById("listing_type_div").innerHTML = "loading...";
  AjaxRequest('ajax.php?cmd=fetch_pricing&cat=' + cat + "&cat1=" + cat1 + "&cat2=" + cat2 + "&periodprice=" + periodprice, 'UpdatePricing');
}
