function create_object() {
    var http_request = false;
    if(window.XMLHttpRequest) {
        http_request = new XMLHttpRequest();
        if (http_request.overrideMimeType) {
            http_request.overrideMimeType('text/html; charset=windows-1251');
        }
    }
    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('Failed to create object of class XMLHTTP');
        return false;
    }
    else { return http_request; }
}
 
function make_request_select(url) {
    http_request = create_object();
    http_request.onreadystatechange = function() { paste_response_select(http_request); };
    http_request.open('GET', url, true);
    http_request.send(null);
}

function make_request_add(url) {
    http_request = create_object();
    http_request.onreadystatechange = function() { paste_response_add(http_request); };
    http_request.open('GET', url, true);
    http_request.send(null);
}
 
function make_request_calculate(url) {
    http_request = create_object();
    http_request.onreadystatechange = function() { paste_response_calculate(http_request); };
    http_request.open('GET', url, true);
    http_request.send(null);
}

function parseArgs(args) {
	response = 0;
	line = args.toString();
	ar_line = line.split(":END");
	for(i = 0; i < ar_line.length; i++) {
		qnumber = ar_line[i].toString().substring(ar_line[i].toString().indexOf("---")+3, ar_line[i].toString().indexOf(":"));
		qcheck = ar_line[i].toString().substring(ar_line[i].toString().indexOf(":")+1, ar_line[i].toString().length);
		if(qnumber != "") {
			switch(qcheck) {
				case "N":
					if(document.getElementById("input_" + qnumber).checked) {
						response++;
					}
				break;
				case "Y":
					if(!document.getElementById("input_" + qnumber).checked) {
						response++;
					}
				break;
				case "notempty":
					if(document.getElementById("input_" + qnumber).value == "") {
						reponse++;
					}
				break;
				case "empty":
					if(document.getElementById("input_" + qnumber).value != "") {
						reponse++;
					}
				break;
				default:
					el = document.getElementById("nalog");
					for(f = 0; f < el.length; f++) {
						if(el.elements[f].id == "input_" + qnumber) {
							if(el.elements[f].value == qcheck) {
								if(!el.elements[f].checked) {
									response++;
								}
							}
						}
					}
				break;
			}
		}
	}
	return response;
}

function switchInit(id, qnumber, init_value) {
	switch(id) {
		case "N":
			if(!document.getElementById("input_" + qnumber).checked) {
				init_value = "N";
			}
		break;
		case "Y":
			if(document.getElementById("input_" + qnumber).checked) {
				init_value = "Y";
			}
		break;
		case "notempty":
			if(document.getElementById("input_" + qnumber).value != "") {
				init_value = "notempty";
			}
		break;
		case "empty":
			if(document.getElementById("input_" + qnumber).value == "") {
				init_value = "empty";
			}
		break;
		default:
		break;
	}
	return init_value;
}

function AssignNewValues(args) {
	conditions = args.split("---");
	for(i = 0; i < conditions.length; i++) {
		if(conditions[i] != "") {
			scon = conditions[i].toString().substring(0, conditions[i].toString().indexOf("CONDITION:"));
			scon = scon.replace(/(^\s+)|(\s+$)/g, "");
			nvalue = conditions[i].toString().substring(conditions[i].toString().indexOf("CONDITION:")+10, conditions[i].toString().length);
			nvalue = nvalue.replace(/(^\s+)|(\s+$)/g, "");
			ar_scon = scon.split("&");
			errors = 0;
			for(s = 0; s < ar_scon.length; s++) {
				field = ar_scon[s].toString().substring(0, ar_scon[s].toString().indexOf("="));
				field = field.replace(/(^\s+)|(\s+$)/g, "");
				value = ar_scon[s].toString().substring(ar_scon[s].toString().indexOf("=")+1, ar_scon[s].toString().length);
				value = value.replace(/(^\s+)|(\s+$)/g, "");
				switch(value) {
					case "N":
						if(document.getElementById("input_" + field).checked) {
							errors++;
							alert("N " + errors);
						}
					break;
					case "Y":
						if(!document.getElementById("input_" + field).checked) {
							errors++;
							alert("Y " + errors);
						}
					break;
					case "notempty":
						if(document.getElementById("input_" + field).value == "") {
							errors++;
							alert("notempty " + errors);
						}
					break;
					case "empty":
						if(document.getElementById("input_" + field).value != "") {
							errors++;
							alert("empty " + errors);
						}
					break;
					default:
						el = document.getElementById("nalog");
						for(f = 0; f < el.length; f++) {
							if(el.elements[f].id == "input_" + field) {
								if(el.elements[f].value == value) {
									if(!el.elements[f].checked) {
										errors++;
									}
								}
							}
						}
					break;
				}
			}
			if(errors == 0) {
				sfield = nvalue.toString().substring(0, nvalue.toString().indexOf("="));
				sfield = sfield.replace(/(^\s+)|(\s+$)/g, "");
				svalue = nvalue.toString().substring(nvalue.toString().indexOf("=")+1, nvalue.toString().length);
				svalue = svalue.replace(/(^\s+)|(\s+$)/g, "");
				document.getElementById("input_" + sfield).value = svalue;
			}
		}
	}
}


function paste_response_select(http_request) {
    if (http_request.readyState == 4) {
        if (http_request.status == 200) {
		if(http_request.responseText != "") {
			response = http_request.responseText.toString();
			question_number = response.substring(response.indexOf("THISQUESTION:")+13, response.indexOf("---"));
			question_number = question_number.replace(/(^\s+)|(\s+$)/g, "");
		        targs = document.getElementById("input_" + question_number).title;
				if(targs != "") {
					AssignNewValues(targs);
				}
			init_value = response.substring(response.indexOf("THISVALUE")+9, response.length);
			init_value = init_value.replace(/(^\s+)|(\s+$)/g, "");
			response = response.replace(response.slice(0, response.indexOf("---")), "");
			response = response.replace(response.slice(response.indexOf("THISVALUE")), "");
			response = response.replace(/(^\s+)|(\s+$)/g, "");
			possible_values = response.split("---");
			possible_values = possible_values.slice(0);
			total_values = new Array();
			total_numbers = new Array();
			for(i = 0; i < possible_values.length; i++) {
				values = "";
				value_id = possible_values[i].toString().substring(0, possible_values[i].toString().indexOf(":"));
				values = possible_values[i].toString().substring(possible_values[i].toString().indexOf(":")+1, possible_values[i].toString().length);
				values = values.replace(/(^\s+)|(\s+$)/g, "");
				if(value_id.length > 0) {
					total_values[value_id] = values.split("|");
					total_numbers[i] = value_id;
				}
			}
			for(s in total_numbers) {
				id = total_numbers[s];
				if(id.length > 0) {
					nline = new Array();
					nline = total_values[id];
					ncheck = switchInit(id, question_number, init_value);
					if(id == ncheck) {
						for(g = 0; g < nline.length; g++) {
							args = "";
						        args = document.getElementById("input_" + nline[g]).alt;
							if(args) {
								args_check = parseArgs(args);
							} else {
								args_check = 0;
							}
							if(args_check == 0) {
								x = document.getElementById(nline[g]);
								x2 = document.getElementById(nline[g] + "_f");
								if(x) {
									x.style.display = "block";
								}
								if(x2) {
									x2.style.display = "block";
								}
							}
						}
					} else {
						for(e = 0; e < nline.length; e++) {
							z = document.getElementById(nline[e]);
							z2 = document.getElementById(nline[e] + "_f");
							if(z) {
								z.style.display = "none";
							}
							if(z2) {
								z2.style.display = "none";
							}
						}
					}
				}
			}
		}
	}
    }
}
	
function paste_response_add(http_request) {
    if (http_request.readyState == 4) {
        if (http_request.status == 200) {
		tbl = document.getElementById("asdf");
		tblrows = tbl.rows;
		newrow = tbl.insertRow(tblrows.length - 3);
		newrow.innerHTML = http_request.responseText;
	}
    }
}

function paste_response_calculate(http_request) {
    if (http_request.readyState == 4) {
        if (http_request.status == 200) {
		response = http_request.responseText.toString();
		id = response.substring(0, response.indexOf("="));
		fvalue = response.substring(response.indexOf("=") + 1, response.length);
		document.getElementById(id).value = fvalue;
	}
    }
}

function AjaxGetCurrencyRate(url) {
    http_request = create_object();
    http_request.onreadystatechange = function() { paste_response_field(http_request); };
    http_request.open('GET', url, true);
    http_request.send(null);
}

function AjaxGetConvertedAmount(url) {
    http_request = create_object();
    http_request.onreadystatechange = function() { paste_response_field(http_request); };
    http_request.open('GET', url, true);
    http_request.send(null);
}

function paste_response_field(http_request) {
    if (http_request.readyState == 4) {
        if (http_request.status == 200) {
		if(http_request.responseText != "") {
			response = http_request.responseText.toString();
			newfield = response.substring(response.indexOf("FIELD:")+6, response.indexOf("---VALUE:"));
			newvalue = response.substring(response.indexOf("---VALUE:")+9, response.length);
			document.getElementById("input_" + newfield).value = newvalue;
//			document.getElementById("input_" + newfield).disabled = true;
		}
	}
    }
}

function CheckValue(id) {
	value = document.getElementById("input_" + id).value;
	alert(value);
}

function GetCurrencyRate(fdate, fcurrency, ffield) {
	try {
		sdate = document.getElementById("input_" + fdate).value;	
		scurrency = document.getElementById("input_" + fcurrency).value;
		sfield = document.getElementById("input_" + ffield).value;
		AjaxGetCurrencyRate('/tools/soft/currency.php?date=' + sdate + '&currency=' + scurrency + '&field=' + ffield);
	}
	catch(err) {
		alert(err);
	}
}

function GetConvertedAmount(frate, famount, ffield) {
	try {
		srate = document.getElementById("input_" + frate).value;	
		samount = document.getElementById("input_" + famount).value;
		sfield = document.getElementById("input_" + ffield).value;
		AjaxGetConvertedAmount('/tools/soft/convert.php?rate=' + srate + '&amount=' + samount + '&field=' + ffield + '&action=amount');
	}
	catch(err) {
		alert(err);
	}
}

function GetConvertedAmountMinus(frate, famount, ffield) {
	try {
		srate = document.getElementById("input_" + frate).value;	
		samount = document.getElementById("input_" + famount).value;
		sfield = document.getElementById("input_" + ffield).value;
		AjaxGetConvertedAmount('/tools/soft/convert.php?rate=' + srate + '&amount=' + samount + '&field=' + ffield + '&action=minus');
	}
	catch(err) {
		alert(err);
	}
}

function GetConvertedTaxRate(frate, famount, ffield) {
	try {
		srate = document.getElementById("input_" + frate).value;	
		samount = document.getElementById("input_" + famount).value;
		sfield = document.getElementById("input_" + ffield).value;
		AjaxGetConvertedAmount('/tools/soft/convert.php?rate=' + srate + '&amount=' + samount + '&field=' + ffield + '&action=tax');
	}
	catch(err) {
		alert(err);
	}
}

function requestCalculate(data) {
	try {
		keys = new Array();
		values = new Array();
		fields = data.split("&");
		for(i = 0; i < fields.length; i++) {
			keys[i] = fields[i].toString().substring(0, fields[i].toString().indexOf("="));
			field_name = fields[i].toString().substring(fields[i].toString().indexOf("=")+1, fields[i].toString().length);
			values[i] = document.getElementById("input_" + field_name).value;
		}
		var data_string = "";
		for(s = 0; s < keys.length; s++) {
			alert(keys[s]);
			if(s == 0) {
				data_string += "?" + keys[s] + "=" + values[s];
			} else {
				data_string += "&" + keys[s] + "=" + values[s];
			}
		}
		make_request_calculate('/tools/soft/calculate.php' + data_string);
	}
	catch(err) {
		alert(err);
	}
}

function formSubmit() {
	document.getElementById("nalog").submit();
}

