// IE, Firefox & Opera



function GetAttr(Elem, attr) {

	var value = Elem.getAttribute(attr); value = value == null ? '' : value;

	return value;

}



function CheckValues(Form) {

	var error = false;

	var first = false;

	var Elem = null;

	var type = null;

	var rule = null;

	var name = null;

	var value = null;

	var mask_code = 0;



    for (var j = 0; j < Form.elements.length; j++){



		Elem = Form.elements[j];

		

//		type = Elem.getAttribute('type'); type = type == null ? 'hidden' : type;

		rule = Elem.getAttribute('rule'); rule = rule == null ? '' : rule;

		name = Elem.name; name = name == null ? '' : name;

		value = Elem.value; 



		Err = $('err_'+name);



		if (Err) {

			Err.style.display = 'none';

    

			if (rule != '') {



				filter = Tests[rule][0];

			

				if (!filter.test(value)) {

					Err.innerHTML = Tests[rule][1]; 

					Err.style.display = 'inline'; 

					if (!first) { Elem.focus(); first = true; }

					error = true;

				}

			}

	 

			if (Elem.maskId !== undefined && value != "") {

				mask_code = MaskAPI.objects[Elem.maskId].errorCodes.length > 0 ? MaskAPI.objects[Elem.maskId].errorCodes[0] : 0;

				if (mask_code > 0) {

					Err.innerHTML = sprintf(MaskErrors[mask_code], MaskAPI.objects[Elem.maskId].mask); 

					Err.style.display = 'inline'; 

					if (!first) { Elem.focus(); first = true; }

					error = true;

				}

    		}

		}

	}



	return !error;

}



function ChangeValue(Form) {

//	var Form = elem.form;

	var res = 1;

	var Calc = null;

	var Elem = null;

	var Opt = null;

	var k;

    for (var j = 0; j < Form.elements.length; j++){



		Elem = Form.elements[j];



		rule = Elem.getAttribute('rule'); rule = rule == null ? '' : rule.toLowerCase();

		calc = Elem.getAttribute('calc'); calc = calc == null ? '' : calc;



		if (rule == 'calc') {

			Calc = Elem;

			Calc.value = '';

		}



		k = 0;

	 	if (Elem.tagName.toUpperCase()=='INPUT' && !isNaN(calc) && Elem.checked) {

			k = calc;

 		} else if (Elem.tagName.toUpperCase()=='SELECT') {

			if (Elem.selectedIndex >= 0) {

				Opt = Elem.options[Elem.selectedIndex]

				k = Opt.getAttribute('calc');			

				k = ((k == null) || isNaN(calc)) ? 0 : k;

			}

		}

		res = res * (k != 0 ? k : 1);

	}



	if (Calc && res > 0) {

		Calc.value = Math.round(res);

	}

}





function AttachMasks(Form) {

	var M = null;

	var type = "";

    for (var j = 0; j < Form.elements.length; j++){



		mask = Form.elements[j].getAttribute('mask'); 

	

		if (mask) {

			mask = mask.toLowerCase();

            if (mask.indexOf('d') > -1 || mask.indexOf('m') > -1 || mask.indexOf('y') > -1) 

            	  type = "date";

			else 

				  type = "string"; 

			

			M = new Mask(mask, type);

			M.attach(Form.elements[j]);

			Form.elements[j].maskId = M.id;		

		}

	}

}





function getElementPosition(elemID) {

	var offsetTrail = typeof elemID == 'string' ? document.getElementById(elemID) : elemID;

    var offsetLeft = 0;

    var offsetTop = 0;

	var offsetWidth = offsetTrail.offsetWidth;

	var offsetHeight = offsetTrail.offsetHeight;





    while (offsetTrail) {

        offsetLeft += offsetTrail.offsetLeft;

        offsetTop += offsetTrail.offsetTop;

        offsetTrail = offsetTrail.offsetParent;

    }

    if (navigator.userAgent.indexOf("Mac") != -1 && 

        typeof document.body.leftMargin != "undefined") {

        offsetLeft += document.body.leftMargin;

        offsetTop += document.body.topMargin;

    }

    return {left:offsetLeft, top:offsetTop, width: offsetWidth, height: offsetHeight};

}



// from prototype.js



function $() {

  var elements = new Array();



  for (var i = 0; i < arguments.length; i++) {

    var element = arguments[i];

    if (typeof element == 'string')

      element = document.getElementById(element);



    if (arguments.length == 1) 

      return element;



    elements.push(element);

  }



  return elements;

}





function sprintf()

{

	if (!arguments || arguments.length < 1 || !RegExp)

	{

		return;

	}

	var str = arguments[0];

	var re = /([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X)(.*)/;

	var a = b = [], numSubstitutions = 0, numMatches = 0;

	while (a = re.exec(str))

	{

		var leftpart = a[1], pPad = a[2], pJustify = a[3], pMinLength = a[4];

		var pPrecision = a[5], pType = a[6], rightPart = a[7];

			

		//alert(a + '\n' + [a[0], leftpart, pPad, pJustify, pMinLength, pPrecision);



		numMatches++;

		if (pType == '%')

		{

			subst = '%';

		}

		else

		{

			numSubstitutions++;

			if (numSubstitutions >= arguments.length)

			{

				alert('Error! Not enough function arguments (' + (arguments.length - 1) + ', excluding the string)\nfor the number of substitution parameters in string (' + numSubstitutions + ' so far).');

			}

			var param = arguments[numSubstitutions];

			var pad = '';



	       	if (pPad && pPad.substr(0,1) == "'") pad = leftpart.substr(1,1);

			else if (pPad) pad = pPad;



			var justifyRight = true;

	       	if (pJustify && pJustify === "-") justifyRight = false;

			var minLength = -1;

	       	if (pMinLength) minLength = parseInt(pMinLength);

			var precision = -1;

			if (pPrecision && pType == 'f') precision = parseInt(pPrecision.substring(1));

			var subst = param;



			if (pType == 'b') subst = parseInt(param).toString(2);

			else if (pType == 'c') subst = String.fromCharCode(parseInt(param));

		  	else if (pType == 'd') subst = parseInt(param) ? parseInt(param) : 0;

			else if (pType == 'u') subst = Math.abs(param);

			else if (pType == 'f') subst = (precision > -1) ? Math.round(parseFloat(param) * Math.pow(10, precision)) / Math.pow(10, precision): parseFloat(param);

			else if (pType == 'o') subst = parseInt(param).toString(8);

			else if (pType == 's') subst = param;

			else if (pType == 'x') subst = ('' + parseInt(param).toString(16)).toLowerCase();

			else if (pType == 'X') subst = ('' + parseInt(param).toString(16)).toUpperCase();

		}

		str = leftpart + subst + rightPart;

	}

	return str;

}



function dump(d,l) {

    if (l == null) l = 1;

    var s = '';

    if (typeof(d) == "object") {

        s += typeof(d) + " {\n";

        for (var k in d) {

            for (var i=0; i<l; i++) s += "  ";

            s += k+": " + dump(d[k],l+1);

        }

        for (var i=0; i<l-1; i++) s += "  ";

        s += "}\n"

    } else {

        s += "" + d + "\n";

    }

    return s;

}

