function strASCIIencode( str ) {
	var result = "str:";
	for (var i = 0; i < str.length; i++ ) {
		charCode =str.charAt(i).charCodeAt(0);
		if( charCode >= 1040 && charCode <= 1103 ) {
			charCode = str.charAt(i).charCodeAt(0) - 848;
		} else {
			charCode = str.charAt(i).charCodeAt(0);
		}
		result += charCode+":";	
	}
	return result;
	//return str;
}

function URLEncode( str )
{
	// The Javascript escape and unescape functions do not correspond
	// with what browsers actually do...
	var SAFECHARS = "0123456789" +					// Numeric
					"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +	// Alphabetic
					"abcdefghijklmnopqrstuvwxyz" +
					"-_.!~*'()";					// RFC2396 Mark characters
	var HEX = "0123456789ABCDEF";

	var plaintext = str;
	var encoded = "";
	for (var i = 0; i < plaintext.length; i++ ) {
		var ch = plaintext.charAt(i);
	    if (ch == " ") {
		    encoded += "+";				// x-www-urlencoded, rather than %20
		} else if (SAFECHARS.indexOf(ch) != -1) {
		    encoded += ch;
		} else {
		    var charCode = ch.charCodeAt(0);
			if (charCode > 255) {
			    alert( "Unicode Character '" 
                        + ch 
                        + "' cannot be encoded using standard URL encoding.\n" +
				          "(URL encoding only supports 8-bit characters.)\n" +
						  "A space (+) will be substituted." );
				encoded += "+";
			} else {
				encoded += "%";
				encoded += HEX.charAt((charCode >> 4) & 0xF);
				encoded += HEX.charAt(charCode & 0xF);
			}
		}
	} // for

	return encoded;
};


	function loadSelectFromXML(selectBox, xmldata, value_tag, text_tag) {
		selectBox.length = 0;
		var xd = new XMLDoc(xmldata, null); 
		var el = xd.docNode;
		var els = el.children;
		for(var e=0; e < els.length; e++)
		{
  				var ch = els[e];
				   if(ch.nodeType=='ELEMENT') {
						var elm = ch.children;
						for(a=0; a < elm.length; a++) {
								if( elm[a].tagName == value_tag )  var s_value =  trim(elm[a].children[0].content);
								if( elm[a].tagName == text_tag )  var s_text =  trim(elm[a].children[0].content);
						}
						selectBox.length = selectBox.length+1 ;
						selectBox.options[selectBox.length-1].text = s_text;
						selectBox.options[selectBox.length-1].value = s_value;
						//alert(s_value);
				  }
			
		}
	}
	
	function loadSelectPropertyTypesFromXML(selectBox, xmldata, value_tag, text_tag) {
		selectBox.length = 0;
		//alert(xmldata);
		var xd = new XMLDoc(xmldata, null); 
		var el = xd.docNode;
		var els = el.children;
		for(var e=0; e < els.length; e++)
		{
  				var ch = els[e];
				   if(ch.nodeType=='ELEMENT') {
						var elm = ch.children;
						for(a=0; a < elm.length; a++) {
								if( elm[a].tagName == value_tag )  var s_value =  trim(elm[a].children[0].content);
								if( elm[a].tagName == text_tag )  var s_text =  trim(elm[a].children[0].content);
						}
						selectBox.length = selectBox.length+1 ;
						selectBox.options[selectBox.length-1].text = s_text;
						selectBox.options[selectBox.length-1].value = s_value;
						//alert(s_value);
				  }
			
		}
	}
	
	function loadSelectFromXML2( xmldata, value_tag, text_tag,targetTable) {
		//selectBox.length = 0;
		var xd = new XMLDoc(xmldata, null); 
		var el = xd.docNode;
		var els = el.children;
		var targetTable1=targetTable;
		var tbl = document.getElementById(targetTable1);
		//alert(tblArticles.rows.length);
		var row = tbl.insertRow( tbl.rows.length );
		
		for(var e=0; e < els.length; e++)
		{
  				var ch = els[e];
				   if(ch.nodeType=='ELEMENT') {
						var elm = ch.children;
						for(a=0; a < elm.length; a++) {
								if( elm[a].tagName == value_tag )  var s_value =  trim(elm[a].children[0].content);
								if( elm[a].tagName == text_tag )  var s_text =  trim(elm[a].children[0].content);
						}
						
						row.insertCell( 0).innerHTML = '<input name=article[] value='+s_value+' type=checkbox id='+s_value+'>';
		//alert(s_value);
		row.insertCell( 1 ).innerHTML = s_text;
		//alert(s_text);
						/*
						selectBox.length = selectBox.length+1 ;
						selectBox.options[selectBox.length-1].text = s_text;
						selectBox.options[selectBox.length-1].value = s_value;
						*/
				  }
			
		}
	}
	
	function loadSelectFromXML3(selectBox, xmldata, value_tag, text_tag) {
		selectBox.length = 0;
		var xd = new XMLDoc(xmldata, null); 
		var el = xd.docNode;
		var els = el.children;
						selectBox.length = selectBox.length+1 ;
						selectBox.options[selectBox.length-1].text = "--------------------------------ALL--------------------------------";
						selectBox.options[selectBox.length-1].value = 0;
		for(var e=0; e < els.length; e++)
		{
  				var ch = els[e];
				   if(ch.nodeType=='ELEMENT') {
						var elm = ch.children;
						
						for(a=0; a < elm.length; a++) {
								if( elm[a].tagName == value_tag )  var s_value =  trim(elm[a].children[0].content);
								if( elm[a].tagName == text_tag )  var s_text =  trim(elm[a].children[0].content);
						}
						selectBox.length = selectBox.length+1 ;
						selectBox.options[selectBox.length-1].text = s_text;
						selectBox.options[selectBox.length-1].value = s_value;
						//alert(s_value);
				  }
			
		}
	}
	
	
	function loadSelectFromDB(selectBox , table, value, text, orderby, where) {
		var url = "loadselect.php?table="+table+
						"&value="+URLEncode(value)+
						"&text="+URLEncode(text)+
						"&orderby="+URLEncode(orderby)+
						"&where="+URLEncode(where);
		var xmldata = getXMLData(url);
		loadSelectFromXML(selectBox, xmldata, 'value', 'text');
	}
	function loadSelectFromDB(selectBox , table, value, text, orderby, where) {
		var url = "loadselect.php?table="+table+
						"&value="+URLEncode(value)+
						"&text="+URLEncode(text)+
						"&orderby="+URLEncode(orderby)+
						"&where="+URLEncode(where);
		var xmldata = getXMLData(url);
		loadSelectFromXML(selectBox, xmldata, 'value', 'text');
	}
	
	function loadSelectPropertyTypesFromDB(selectBox , city_id) {
		var url = "loadpropertytypesselect.php?city_id="+city_id;
		var xmldata = getXMLData(url);
		//alert(xmldata);
		loadSelectPropertyTypesFromXML(selectBox, xmldata, 'value', 'text');
	}
	function loadSelectFromDB3(selectBox , table, value, text, orderby, where) {
		var url = "loadselect.php?table="+table+
						"&value="+URLEncode(value)+
						"&text="+URLEncode(text)+
						"&orderby="+URLEncode(orderby)+
						"&where="+URLEncode(where);
		var xmldata = getXMLData(url);
		loadSelectFromXML3(selectBox, xmldata, 'value', 'text');
	}
	
	function loadSelectFromDB2( table, value, text, orderby, where,targetTable) {
		var url = "loadselect.php?table="+table+
						"&value="+URLEncode(value)+
						"&text="+URLEncode(text)+
						"&orderby="+URLEncode(orderby)+
						"&where="+URLEncode(where);
		var xmldata = getXMLData(url);
		var targetTable1=targetTable;
		loadSelectFromXML2( xmldata, 'value', 'text',targetTable1);
	}

	
	function makeVisible(id, state) {
		var element = document.getElementById(id);
		if( state == true) {
			element.style.display = '';
		} else {
			element.style.display = 'none';
		}
	}
	
	function getRow(xmldata) {
		//alert(xmldata);
		//return;
		if( xmldata == '' || xmldata == null ) return 'nodata';
		var result = new Array();
		var xd = new XMLDoc(xmldata, null); 
		var el = xd.docNode;
		var els = el.children;
		for(var e=0; e < els.length; e++) {
			var key = els[e];
			if( key.tagName == 'key' ) {
				var name = key.getAttribute('name');
				var value = trim(key.children[0].content);
				result[name] = value;
			}
		}
		
		return result;
	}
	
	function IsNumeric(strString) 
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789.-";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   }
   
  	function enableForm(form , state) {
	/*
			for( i = 0; i < form.elements.length; i++ ) {
				//alert(form.elements[i].disabled);
				form.elements[i].disabled = !state;
			} */
	}
	function getJScript(url) {
		var xmldata = getXMLData(url);
		var xd = new XMLDoc(xmldata, null); 
		var el = xd.docNode;
		return el.children[0].content;
	}

	

function FormatNumber(num,decimalNum,bolLeadingZero,bolParens,bolCommas)
/**********************************************************************
	IN:
		NUM - the number to format
		decimalNum - the number of decimal places to format the number to
		bolLeadingZero - true / false - display a leading zero for
										numbers between -1 and 1
		bolParens - true / false - use parenthesis around negative numbers
		bolCommas - put commas as number separators.
 
	RETVAL:
		The formatted number!
 **********************************************************************/
{ 
        if (isNaN(parseInt(num))) return "NaN";

	var tmpNum = num;
	var iSign = num < 0 ? -1 : 1;		// Get sign of number
	
	// Adjust number so only the specified number of numbers after
	// the decimal point are shown.
	tmpNum *= Math.pow(10,decimalNum);
	tmpNum = Math.round(Math.abs(tmpNum))
	tmpNum /= Math.pow(10,decimalNum);
	tmpNum *= iSign;					// Readjust for sign
	
	
	// Create a string object to do our formatting on
	var tmpNumStr = new String(tmpNum);

	// See if we need to strip out the leading zero or not.
	if (!bolLeadingZero && num < 1 && num > -1 && num != 0)
		if (num > 0)
			tmpNumStr = tmpNumStr.substring(1,tmpNumStr.length);
		else
			tmpNumStr = "-" + tmpNumStr.substring(2,tmpNumStr.length);
		
	// See if we need to put in the commas
	if (bolCommas && (num >= 1000 || num <= -1000)) {
		var iStart = tmpNumStr.indexOf(".");
		if (iStart < 0)
			iStart = tmpNumStr.length;

		iStart -= 3;
		while (iStart >= 1) {
			tmpNumStr = tmpNumStr.substring(0,iStart) + "," + tmpNumStr.substring(iStart,tmpNumStr.length)
			iStart -= 3;
		}		
	}

	// See if we need to use parenthesis
	if (bolParens && num < 0)
		tmpNumStr = "(" + tmpNumStr.substring(1,tmpNumStr.length) + ")";

	return tmpNumStr;		// Return our formatted string!
}

function Trim(TRIM_VALUE){
	if(TRIM_VALUE.length < 1){
		return"";
	}
	TRIM_VALUE = RTrim(TRIM_VALUE);
	TRIM_VALUE = LTrim(TRIM_VALUE);
	if(TRIM_VALUE==""){
		return "";
	}
	else{
return TRIM_VALUE;
}
} //End Function

function RTrim(VALUE){
var w_space = ' ';
var v_length = VALUE.length;
var strTemp = "";
if(v_length < 0){
return"";
}
var iTemp = v_length -1;

while(iTemp > -1){
if(VALUE.charAt(iTemp) == '-'){
}
else{
strTemp = VALUE.substring(0,iTemp +1);
break;
}
iTemp = iTemp-1;

} //End While
return strTemp;

} //End Function

function LTrim(VALUE){
var w_space = ' ';
if(v_length < 1){
return"";
}
var v_length = VALUE.length;
var strTemp = "";

var iTemp = 0;

while(iTemp < v_length){
if(VALUE.charAt(iTemp) == w_space){
}
else{
strTemp = VALUE.substring(iTemp,v_length);
break;
}
iTemp = iTemp + 1;
} //End While
return strTemp;
} //End Function

	function calculateSum(form) {
			var prices = new Array();
			var length = form.elements.length;
			var Sum = 0;
			var totalSum = 0;
			for( i=0; i < length; i++ ) {
				var elementName = form.elements[i].name;
				if( elementName.match(/price\[\d+\]/) ) {
					var num = elementName.replace(/price\[(\d+)\]/ , "$1");
					var price = 0;
					var quant = 0;
					var discount = 0;
					var discount_type = 0;
					if( typeof(form.elements['price['+num+']']) == "object"  && form.elements['price['+num+']'].value) price = parseFloat(form.elements['price['+num+']'].value);
					if( typeof(form.elements['quant['+num+']']) == "object" && form.elements['quant['+num+']'].value ) quant = parseFloat(form.elements['quant['+num+']'].value);
					if( typeof(form.elements['discount['+num+']']) == "object" && form.elements['discount['+num+']'].value ) discount = parseFloat(form.elements['discount['+num+']'].value);
					if( typeof(form.elements['discount_type['+num+']']) == "object" && form.elements['discount_type['+num+']'].value ) discount_type = parseInt(form.elements['discount_type['+num+']'].value);
					currentSum = price * quant;
					Sum = Sum + currentSum;
					if( discount_type == 1 )	currentSum = currentSum - (currentSum*(discount/100));
					if( discount_type == 2)  currentSum = currentSum - discount;
					totalSum = totalSum + currentSum;
				}
			}
				
				var invoice_discount_type = 0;
				var invoice_discount = 0;
				if( typeof(form.invoice_discount_type) == "object" ) invoice_discount_type = parseInt( form.invoice_discount_type.value );
				if( typeof(form.invoice_discount) == "object" ) invoice_discount = parseInt( form.invoice_discount.value );
				if( invoice_discount_type == 1 ) 		totalSum = totalSum - (totalSum*(invoice_discount/100));
				if( invoice_discount_type == 2 ) 		totalSum = totalSum - invoice_discount;				
				var result = new Array()
				result['sum'] = FormatNumber(Sum, 4, true, true, false);
				result['totalSum'] = FormatNumber(totalSum, 4, true, true, false);
				result['discountSum'] = FormatNumber(Sum - totalSum, 4, true, true, false);;
				return result;
	}
