function changeIn(id, data) {
	document.getElementById(id).disabled = false;
	document.getElementById(id).innerHTML = "";
		
	var s = document.getElementById(id);
        
    var val = new Array();
    var option = new Array();
    var tempVal = new Array();
    
    var opt = document.createElement('OPTION');
	opt.setAttribute('value', 0);
	opt.innerHTML = 'Все модели';
	s.appendChild(opt);
	    
    arr = data.split('</option>');
    
    arr2 = data.split(' selected');
    sel = arr2[0].substring(arr2[0].lastIndexOf('=') + 1);
    sel = sel.replace(/\"/g, "");
    
    for (i=0; i< arr.length-1; i++)
    {
	    option[i] = arr[i].substring(arr[i].lastIndexOf('>') + 1);
	    tempVal = arr[i].split('"');
	    val[i] = tempVal[1];
	    
	    var opt = document.createElement('OPTION');
	    opt.setAttribute('value', val[i]);
	    if (val[i] == sel*1)
	    	opt.selected = true;
	    
	    opt.innerHTML = option[i];
	    s.appendChild(opt);
    }
}

function changeInMan(id, data) {
	document.getElementById(id).disabled = false;
	document.getElementById(id).innerHTML = "";
		
	var s = document.getElementById(id);
        
    var val = new Array();
    var option = new Array();
    var tempVal = new Array();
    
    var opt = document.createElement('OPTION');
	opt.setAttribute('value', 0);
	opt.innerHTML = 'Все производители';
	s.appendChild(opt);
	    
    arr = data.split('</option>');
    
    arr2 = data.split(' selected');
    sel = arr2[0].substring(arr2[0].lastIndexOf('=') + 1);
    sel = sel.replace(/\"/g, "");
    
    for (i=0; i< arr.length-1; i++)
    {
	    option[i] = arr[i].substring(arr[i].lastIndexOf('>') + 1);
	    tempVal = arr[i].split('"');
	    val[i] = tempVal[1];
	    
	    var opt = document.createElement('OPTION');
	    opt.setAttribute('value', val[i]);
	    if (val[i] == sel*1)
	    	opt.selected = true;
	    
	    opt.innerHTML = option[i];
	    s.appendChild(opt);
    }
}

function MEGAchangeIn(id, data) {
	if (data=='') {
		dis = ' disabled';
	} else {
		dis = '';
	}
	inte = parseInt(id) + 1;
	document.getElementById('models_'+id).innerHTML = '<select name="model_id[' + id + ']" id="models[' + id + ']" onchange="showHideSimple(\'mark' + inte + '\');"' + dis + '><option value="0">Выберите модель</option>' + data + '</select>';
}

function changeInSimple(id, data) {
	document.getElementById(id).innerHTML = data;
}

function changeInputSimple(id, data) {
	document.getElementById(id).value = data;
}

function makeRequest(id, phpfunc, func, div) {
	var http_request = false;
	if (window.XMLHttpRequest) {
		http_request = new XMLHttpRequest();
		if (http_request.overrideMimeType) {
			http_request.overrideMimeType('text/xml');
		}
	} else if (window.ActiveXObject) {
		try {
			http_request = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try {
				http_request = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e) {}
		}
	}
	if (!http_request) {
		alert('Не вышло :( Невозможно создать экземпляр класса XMLHTTP ');
		return false;
	}

	http_request.onreadystatechange = function() {alertContents(http_request, func, div);};
	http_request.open('GET', '/ajax/'+id+'/'+phpfunc, true);
	http_request.send(null);
}
function updateOrderState(order_id){
  var http_request = false;
  try {
    http_request=new XMLHttpRequest();
  } catch (e) {
    try {
      http_request=new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        http_request=new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
        alert("Your browser does not support AJAX!");
        return false;
      }
    }
  } 
  http_request.open("POST", "/scripts/ajax.php", false);
  http_request.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
  http_request.send('&order_id=' + order_id + '&action=lastUpdate');
  if (http_request.status == 200) {
    document.getElementById('last_update_' + order_id).innerHTML = '{last_update}';
  } else {
    return;
  } 
//  $.ajax({
//    type: 'GET',
//    dataType: 'html',
//    url: '/scripts/ajax.php',
//    data: {
//      order_id: order_id,
//      action: 'last_update'
//    },
//    success: function(){
//      $('td#"last_uptdate_'+order_id).text('{last_update}')
//    }
//  });  
}

function alertContents(http_request, func, div) {
	if (http_request.readyState == 4) {
		if (http_request.status == 200) {
			if (func !== null) {
				eval(func+'("'+div+'", http_request.responseText)');
			}
		} else {
			alert('С запросом возникла проблема.');
		}
	}
	delete http_request;
}

function sendDiscount() {
	rid = document.getElementById('discr').value;
	num = document.getElementById('disc').value;
	uid = document.getElementById('uid').value;
	if (num!='') {
		makeRequest(num, 'changeDiscount/'+rid+'/'+uid, 'madeChages', num);
	}
}

function madeChages(num) {
	document.getElementById('disc').value = '';
	if (document.getElementById('discr').options[document.getElementById('discr').selectedIndex].text.indexOf('(') > -1) {
		res = document.getElementById('discr').options[document.getElementById('discr').selectedIndex].text.replace(/(\d+)/g, num);
		document.getElementById('discr').options[document.getElementById('discr').selectedIndex].text = res;
	}
	else {
		 document.getElementById('discr').options[document.getElementById('discr').selectedIndex].text = document.getElementById('discr').options[document.getElementById('discr').selectedIndex].text + ' ( ' + num + '% )';
	}
	document.getElementById('discr').selectedIndex = document.getElementById('discr').selectedIndex+1;
}

function removeElement(id) {
	document.getElementById(id).style.display = 'none';
}
