var tmpclient, client, season_client, difficulty_client, pieces_client, country_client, category_client, state_pieces_client, noncategory_pieces_client;
function timeoutRaised()
{
    alert("timeout");
}

function post_init(Async) {
   
	tmpclient = new HttpClient();
	tmpclient.init();
	tmpclient.isAsync = Async;
	tmpclient.responseXML = true;
	tmpclient.requestType= 'POST';
	tmpclient.timeout=1000;
	tmpclient.ontimeout = timeoutRaised;
	return tmpclient;
}


function set_callback(httpclient, elementTag, FormName, SelectName) {
   httpclient.callback = function() {
   var newDom = httpclient.xmlhttp.responseXML;
   var countries = newDom.getElementsByTagName(elementTag);
   var content = "doing callback" + countries.length + elementTag + SelectName + FormName + "<ul>";
   //document.forms[FormName][SelectName].options.length=0;
   while (document.forms[FormName][SelectName].childNodes.length > 0) {
		document.forms[FormName][SelectName].removeChild(document.forms[FormName][SelectName].childNodes[0])
	}
  // add_option(FormName, SelectName, "Not Defined", -1);
   for (var i = 0; i < countries.length; i++) {
     //  content += "<li>" + i + " " + countries[i].firstChild.nodeValue + "</li>";
    	if (countries[i].firstChild.hasAttributes()) {
			var selected=false;
			if ((countries[i].firstChild.attributes['selected']) && (countries[i].firstChild.attributes['selected'].value>0) ){
				selected=true;
			}
	        add_option(FormName, SelectName, countries[i].firstChild.nodeValue, countries[i].firstChild.attributes['id'].value, selected);
	    } else {
	       add_option(FormName, SelectName, countries[i].firstChild.nodeValue , i,false);
	     }
	}

  content += "</ul>";

 // document.getElementById('country').innerHTML = document.getElementById('country').innerHTML + content;

  }
  return httpclient;
  
}

function index_set_callback(httpclient, elementTag, FormName, SelectName) {
   httpclient.callback = function() {
   var newDom = httpclient.xmlhttp.responseXML;
   var countries = newDom.getElementsByTagName(elementTag);
   document.forms[FormName][SelectName].options.length=0;
   index_add_option(FormName, SelectName, "Not Defined", -1);
   for (var i = 0; i < countries.length; i++) {
     //  content += "<li>" + i + " " + countries[i].firstChild.nodeValue + "</li>";
		var current = countries[i].firstChild;
		var current_id = countries[i].attributes['id'];
    	//if (current.hasAttributes()) { 
	    index_add_option(FormName, SelectName, current.nodeValue, current_id.nodeValue);
	     //alert(current.nodeValue + current_id.nodeValue);
	    } 

 // document.getElementById('country').innerHTML = document.getElementById('country').innerHTML + content;

  }
 
  return httpclient;
  
}

function set_callback_pieces(httpclient, elementTag, FormName, SelectName) {
   httpclient.callback = function() {
   
   var newDom = httpclient.xmlhttp.responseXML;
   var countries = newDom.getElementsByTagName(elementTag);
   
   //var content = "doing callback" + countries.length + elementTag + SelectName + FormName + "<ul>";
   //document.forms[FormName][SelectName].options.length=0;
 // alert(FormName + " " + SelectName + " " + document.forms);
   
   while (document.forms[FormName][SelectName].childNodes.length > 0) {
		document.forms[FormName][SelectName].removeChild(document.forms[FormName][SelectName].childNodes[0])
	}
	var sel=0;
	var index=0;
  // add_option(FormName, SelectName, "Not Defined", -1);
   // alert(httpclient.xmlhttp.responseText);
   for (var i = 0; i < countries.length; i++) {
     //  content += "<li>" + i + " " + countries[i].firstChild.nodeValue + "</li>";
	//alert(FormName + " " + SelectName + " " + document.forms);
	 var test='';
	 var cont=false;
	 try { cont = countries[i].hasAttributes(); } catch (e) { cont=countries[i].attributes.length>0; }
	 	if (cont) {
			var selected=false;
	        if ((countries[i].attributes['selected']) && (countries[i].attributes['selected'].value>0) ){
				selected=true;
			}
			try {
	          index=add_option(FormName, SelectName, countries[i].firstChild.nodeValue, countries[i].attributes['id'].value, selected);
			} catch (e) {
				index=add_option(FormName, SelectName, countries[i].firstChild.nodeValue, countries[i].getAttribute('id'), selected);
			}
			if (selected) {
			     sel = index;
				 }
	    } else {
	       add_option(FormName, SelectName, countries[i].nodeValue, i,false);
	     }
	
	}
    
	//add_option(FormName,SelectName, sel, sel, 

  }
  
  return httpclient;
  
}

function create_start() {

client = post_init(true);
client = set_callback_pieces(client, "state", "create_card", "is_visiting[]");
client.makeRequest('service.php', "service=states"); 

season_client = post_init(true);
season_client = set_callback_pieces(season_client, "season", "create_card", "season_id");
season_client.makeRequest('service.php', "service=seasons&default=1");

difficulty_client = post_init(true);
difficulty_client = set_callback_pieces(difficulty_client, "difficulty", "create_card", "max_difficulty");
difficulty_client.makeRequest('service.php', "service=difficulty");

category_client = post_init(true);
category_client = set_callback_pieces(category_client, "category", "create_card", "category[]");
category_client.makeRequest('service.php', "service=categories");

pieces_client = post_init(true);
pieces_client = set_callback_pieces(pieces_client, "piece", "create_card", "centerblock");
pieces_client.makeRequest('service.php', "service=pieces");

//country_client = post_init(true);
//country_client = set_callback_pieces(country_client, "country", "create_card", "countries");
//country_client.makeRequest('service.php', "service=countries");


}

function add_piece_state_start() {

client = post_init(true);
client = set_callback_pieces(client, "state", "piece_state", "state_id");
client.makeRequest('service.php', "service=states");

season_client = post_init(true);
season_client = set_callback_pieces(season_client, "season", "piece_state", "season_id");
season_client.makeRequest('service.php', "service=seasons");
//pieces_client = post_init(true);
//pieces_client = set_callback_pieces(pieces_client, "state_association", "piece_state", "pieces");
//pieces_client.makeRequest('service.php', "service=state_pieces&inverse=1&state_id=" + state_id.selectedIndex);

difficulty_client = post_init(true);
difficulty_client = set_callback_pieces(difficulty_client, "difficulty", "piece_state", "difficulty");
//difficulty_client.makeRequest('service.php', "service=difficulty");

state_pieces_client = post_init(true);
state_pieces_client = set_callback_pieces(state_pieces_client, "state_association", "piece_state", "piece_state");
//state_pieces_client.makeRequest('service.php', "service=state_pieces&state_id=".document.forms['piece_state']['state_id'].selectedIndex);
//state_pieces_client.makeRequest('service.php', "service=pieces&state=" + state_id.selectedIndex);



var image = document.getElementById('thumb');
	image.style.display = 'none';


}

function add_piece_category_start() {

client = post_init(true);
client = set_callback_pieces(client, "category", "piece_category", "category_id");
client.makeRequest('service.php', "service=categories");

//pieces_client = post_init(true);
//pieces_client = set_callback_pieces(pieces_client, "state_association", "piece_state", "pieces");
//pieces_client.makeRequest('service.php', "service=state_pieces&inverse=1&state_id=" + state_id.selectedIndex);

category_pieces_client = post_init(true);
category_pieces_client = set_callback_pieces(category_pieces_client, "category_association", "piece_category", "piece_category");

noncategory_pieces_client = post_init(true);
noncategory_pieces_client = set_callback_pieces(noncategory_pieces_client, "category_association", "piece_category", "nonpiece_category");
//state_pieces_client.makeRequest('service.php', "service=state_pieces&state_id=".document.forms['piece_state']['state_id'].selectedIndex);
//state_pieces_client.makeRequest('service.php', "service=pieces&state=" + state_id.selectedIndex);

var image = document.getElementById('thumb');
	image.style.display = 'none';


}
function add_option(formname, selectname, display, value, selected) {

index = selectname.indexOf("[]");
if (index>0) {
       selectname = selectname.substring(0,index);
	 }
  var select_id = document.getElementById(selectname);

  var len = select_id.options.length;
  // document.forms[formname][selectname].options[document.forms[formname][selectname].options.length] = new Option(display, value);
   select_id.options[len] = new Option(display, value, false, selected);
   if (selected) {
     select_id.SelectedIndex=len;
	 }
	 return len;
}	   

function index_add_option(formname, selectname, display, value) {
   document.forms[formname][selectname].options[document.forms[formname][selectname].options.length] = new Option(display, value);
}       	          			

function state_change() {

    //var selected = document.forms["piece_state"]["state_id"].selectedIndex;
	//pieces_client.makeRequest('service.php', "service=pieces");
	//pieces_client.makeRequest('service.php', "service=pieces&state=" + selected);
	//state_pieces_client.makeRequest('service.php', "service=pieces&inverse=1&state="+selected);
	//content = "service=" + selected;
	var state_id = document.getElementById('state_id');
	var req = "service=state_pieces&inverse=1&state_id=" + state_id.options[state_id.selectedIndex].value;
	state_pieces_client.makeRequest('service.php', req);
//	pieces_client.makeRequest('service.php', "service=state_pieces&state_id=" + state_id.options[state_id.selectedIndex].value);
	//document.getElementById('country').innerHTML = content;
	var image = document.getElementById('thumb');
	image.style.display = 'none';
	
}
/*
function getMultiple(ob ) { 
       while (ob.selectedIndex != -1) { 
	          if (ob.selectedIndex != 0) mult.push(ob.options[ob.selectedIndex].value); 
			        ob.options[ob.selectedIndex].selected = false; 
		   } // You can use the arSelected array for further processing. 
		return mult;
}

function index_state_change() {
    var selected = document.forms["piece_state"]["state_id"].selectedIndex;
	//pieces_client.makeRequest('service.php', "service=pieces");
	pieces_client.makeRequest('service.php', "service=pieces&state=" + selected);
	state_pieces_client.makeRequest('service.php', "service=pieces&inverse=1&state="+selected);
	content = "service=" + selected;
	//document.getElementById('country').innerHTML = content;
}
*/
function piece_change() {
    var state_id = document.getElementById('state_id');
	var piece_id = document.getElementById('piece_state');
	var season_id = document.getElementById('season_id');
	var image = document.getElementById('thumb');
	var req = "service=difficulty&season_id=" + season_id.options[season_id.selectedIndex].value + "&piece_id=" + piece_id.options[piece_id.selectedIndex].value + "&state_id=" + state_id.options[state_id.selectedIndex].value;

	difficulty_client.makeRequest('service.php', req);	
	image.src = 'out_piece.php?piece_id=' + piece_id.options[piece_id.selectedIndex].value + '&thumb=1';
	
	image.alt = piece_id.options[piece_id.selectedIndex].value;
	image.style.display = 'inline';
}

function category_change() {
  
	var category_id = document.getElementById('category_id');
	var request = "service=category_pieces&category_id=" + category_id.options[category_id.selectedIndex].value;
	category_pieces_client.makeRequest('service.php', request);	
	request = request + "&inverse=1";
	noncategory_pieces_client.makeRequest('service.php', request);	
}
function piece_category_click() {
    	var category_id = document.getElementById('category_id');
	var piece_id = document.getElementById('nonpiece_category');
	var request = "service=category_pieces&category_id=" + category_id.options[category_id.selectedIndex].value;
	var addrequest = request + "&add=" + piece_id.options[piece_id.selectedIndex].value;
	
	category_pieces_client.makeRequest('service.php', addrequest);	
	request = request + "&inverse=1";
	noncategory_pieces_client.makeRequest('service.php', request);	
}

function difficulty_change() {
	var state_id = document.getElementById('state_id');
	var piece_id = document.getElementById('piece_state');
	var season_id = document.getElementById('season_id');
	var difficulty_id = document.getElementById('difficulty');
	var request = "service=difficulty&season_id=" + season_id.options[season_id.selectedIndex].value + "&piece_id=" + piece_id.options[piece_id.selectedIndex].value + "&state_id=" + state_id.options[state_id.selectedIndex].value  + "&difficulty_id=" + difficulty_id.options[difficulty_id.selectedIndex].value;
	difficulty_client.makeRequest('service.php', request);	
}
function piece_state_select() {
	//document.forms["piece_state"]["pieces"].selectedIndex = -1;
}

function pieces_select() {
	//document.forms["piece_state"]["piece_state"].selectedIndex = -1;
}

function piece_category_piece_change(fieldname) {
   	var piece_id = document.getElementById(fieldname);
	var image = document.getElementById('thumb');
	image.src = 'out_piece.php?piece_id=' + piece_id.options[piece_id.selectedIndex].value + '&thumb=1';
	
	image.alt = piece_id.options[piece_id.selectedIndex].value;
	image.style.display = 'inline';
}


