//************************************************************************/
// Web Application Maker Library
// Copyright (c) Declarativa 2000-2007. All Rights Reserved.
// Author: Miguel Calejo; Development by: Mário Araújo, Sónia Mota Araújo;
// See http://www.declarativa.com/wam for licensing terms.
//************************************************************************/
function _Date_toHTMLFormat(domain){
	var r, tYear, tMonth, tDate, tHours, tMinutes, tSeconds;

	switch (domain){
		case WAMDomain.wamdate:
			// --- Get Date
			tYear = this.getFullYear();
			tMonth = this.getMonth() + 1;
			tDate = this.getDate();
			tMonth = tMonth < 10 ? "0"+tMonth : tMonth;
			tDate = tDate < 10 ? "0"+tDate : tDate;
			r = _Date_toHTMLFormat____format(tYear, tMonth, tDate);
			break;
		case WAMDomain.wamtime:
			// --- Get Time
			tHours = this.getHours(); tHours = tHours < 10 ? "0"+tHours : tHours;
			tMinutes = this.getMinutes(); tMinutes = tMinutes < 10 ? "0"+tMinutes : tMinutes;
			tSeconds = this.getSeconds(); tSeconds = tSeconds < 10 ? "0"+tSeconds : tSeconds;
			r = tHours+":"+tMinutes+":"+tSeconds;
			break;
		default:
			// --- Get DateTime
			tYear = this.getFullYear();
			tMonth = this.getMonth() + 1;
			tDate = this.getDate();
			tMonth = tMonth < 10 ? "0"+tMonth : tMonth;
			tDate = tDate < 10 ? "0"+tDate : tDate;
			tHours = this.getHours(); tHours = tHours < 10 ? "0"+tHours : tHours;
			tMinutes = this.getMinutes(); tMinutes = tMinutes < 10 ? "0"+tMinutes : tMinutes;
			tSeconds = this.getSeconds(); tSeconds = tSeconds < 10 ? "0"+tSeconds : tSeconds;
			r = _Date_toHTMLFormat____format(tYear, tMonth, tDate)+" "+tHours+":"+tMinutes+":"+tSeconds;
			break;
	}
	return r;

	function _Date_toHTMLFormat____format(sYear, sMonth, sDate){
		var sR = Date.prototype.format;
		sR = sR.charAt(0)+Date.prototype.separator+sR.charAt(1)+Date.prototype.separator+sR.charAt(2);
		sR = sR.replace(/y/, sYear).replace(/m/, sMonth).replace(/d/, sDate);
		return sR;
	}
}

function _String_formatNum(format){
	var newStr = "";

	for (i = 0; i < format.length-this.length; i++) newStr += format.charAt(i);
	return newStr+this.valueOf();
}

function _String_toNumber(){
	var val;
	if (!isEmpty(Number.prototype.groupingSymbol)) val = this.replace(new RegExp("\\"+Number.prototype.groupingSymbol, "g"), "");
	else val = this;
	val = val.replace(new RegExp("\\"+Number.prototype.decimalSymbol, "g"), ".");
	if (isNaN(val)) return invalidValue();
	return new Number(val);
}

function _String_toDate(){
	var sDateHour = this.split(" ");
	if (sDateHour[0].length == 10){
		var sDate = sDateHour[0].split(Date.prototype.separator);
		var sDay = sDate[Date.prototype.format.indexOf("d")];
		var sMonth = sDate[Date.prototype.format.indexOf("m")];
		var sYear = sDate[Date.prototype.format.indexOf("y")];
		var sToDate = sMonth+"/"+sDay+"/"+sYear;
		if (sDateHour.length > 1) sToDate += " "+sDateHour[1];
	} else {
		var sToDate = "1/1/1970 "+sDateHour[0];
	}
	
	return new Date(sToDate);
}

function _String_replaceSubstr(iFrom, strWith){
	var newStr = "";
	iFrom--;
	return this.substr(0,iFrom)+this.substr(iFrom).replace(new RegExp("#{"+strWith.length+"}"), strWith);
}

function _String_splitWords(sep){
	var r; var i; var s = "";
	
	r = this.split(sep);
	for (i in r){
		if (!isEmpty(r[i])){
			s += r[i]+" ";
		}
	}
	s = s.substr(0, s.length-1);
	this._words = s.split(sep);
}

function _String_getWord(i){
	var r;

	this.splitWords(" ");
	if (isNaN(i)){
		r = "";
	} else {
		if (isUndefined(this._words[i-1])){
			r = "";
		} else {
			r = this._words[i-1];
		}
	}
	delete this._words;
	return r;
}

function _String_deleteWord(iBegin, iEnd){
	var i; var s;

	this.splitWords(" ");
	if (isUndefined(iEnd)){
		iEnd = iBegin;
	} else {
		if (iEnd > this._words.length){
			iEnd = this._words.length;
		}
	}
	for (i = iBegin; i <= iEnd; i++){
		delete this._words[i-1];
	}
	s = new String(this._words.join(" "));
	s.splitWords(" ");
	delete this._words;
	return new String(s._words.join(" "));
}

function _String_toScriptFormat(){
	return this.replace(/\\/g, "\\\\").replace(/\n/g, "\\n").replace(/\r/g, "\\r").replace(/"/g, '\\"');
}

function _String_convertToBase(radix){
	return (parseInt(this)).toString(radix);
}

function _String_toLatLng(){
	var t = this.replace(" ", "").split(",");
	return { latitude:new Number(t[0]), longitude:(t.length > 1) ? new Number(t[1]) : 0 };
}

function _String_trim(){
	return this.replace(/^ */, "").replace(/ *$/, "");
}

function _String_toHTMLName(){
	return this.replace(/,|\.| |-/g, "_");
}

function _String_insertLeadingZeros(maxLength){
    var temp = this;
    while (temp.length < maxLength) temp = "0"+temp;
    return temp;    
}

function _Number_toMoney(numDecimalPlaces, sDecSymbol, sGroupSymbol){
	if (isUndefined(numDecimalPlaces)) numDecimalPlaces = Number.prototype.moneyDigitsAfterDecimal;
	if (isUndefined(sDecSymbol)) sDecSymbol = Number.prototype.moneyDecimalSymbol;
	if (isUndefined(sGroupSymbol)) sGroupSymbol = Number.prototype.moneyGroupingSymbol;
	return formatNumber(this.round(numDecimalPlaces), numDecimalPlaces, sDecSymbol, sGroupSymbol);
}

function _Number_round(numDecimalPlaces){
	if (isUndefined(numDecimalPlaces)) numDecimalPlaces = 0;
	return Math.round(this*Math.pow(10, numDecimalPlaces))/Math.pow(10, numDecimalPlaces)
}

function _Number_toNumber(){
	return this;
}

function _Number_convertToBase(radix){
	return this.toString(radix);
}

function _Number_toRGB(invert, BGR){
	if (invert == true) colorNumber = new Number("0xFFFFFF".convertToBase(10)) - this;
	else var colorNumber = this
	var sRGB = colorNumber.convertToBase(16);
	sRGB = ("000000"+sRGB).substr(sRGB.length);
	if (BGR) sRGB = sRGB.substr(4)+sRGB.substr(2, 2)+sRGB.substr(0, 2);
	return "#"+sRGB.toUpperCase();
}

function _Number_toDMS(type){
	var dms = new DMS();
	if (type == DMS.TYPE.LONGITUDE) {	
		if (parseFloat(deg) < 0) { dms.direction = DMS.DIRECTION.WEST; } else { dms.direction = DMS.DIRECTION.EAST; }
	} else {
		if (parseFloat(deg) < 0) { dms.direction = DMS.DIRECTION.SOUTH; } else { dms.direction = DMS.DIRECTION.NORTH; }
	}
	dms.degrees = Math.floor(Math.abs(parseFloat(deg)));
	var mmm = 60 * (Math.abs(parseFloat(deg)) - parseFloat(dms.degrees))
	mmm = Math.round(1000000 * mmm) / 1000000;
	dms.minutes = Math.floor(parseFloat(mmm));
	dms.seconds = 60 * (parseFloat(mmm) - parseFloat(dms.minutes))
	dms.seconds = Math.round(1000 * dms.seconds) / 1000;
	return dms;
}

function DMS(dir, d, m, s){ this.direction = dir; this.degrees = d; this.minutes = m; this.seconds = s; this.toString = function(){ return this.direction+" "+this.degrees+"° "+this.minutes+"&#39; "+this.seconds.toFixed(2)+"&quot;" }; }

function _Function_getName(){
	var s = this.toString().replace(/^function */, "");
	return s.substr(0, s.indexOf("("));
}

function formatNumber(val, dec, decSymbol, groupSymbol){
	var iIntPart, iDot, lDecPart, i;
	if (isUndefined(groupSymbol)) groupSymbol = Number.prototype.groupingSymbol;
	if (isUndefined(decSymbol)) decSymbol = Number.prototype.decimalSymbol;

	val = (val+"").replace(/\./, decSymbol);
	iDot = val.indexOf(decSymbol, 0);
	iIntPart = (iDot == -1 ? val.length : iDot);
	lDecPart = val.substr(iIntPart+1).length;
	
	for (i = iIntPart-4; i > 0; i-=3){
		val = val.substr(0, i+1) + groupSymbol + val.substr(i+1);
	}
	if (i == 0) val = val.substr(0, i+1) + groupSymbol + val.substr(i+1);
	if (isNaN(dec) || dec <= 0) return val;
	if (iDot == -1 && dec > 0) val += decSymbol;
	for (i=0; i<(dec-lDecPart); i++) val += "0";
	return val;
}

function GetScriptEngineInfo(){
    var s;
    s = ""; // Build string with necessary info.
    s += ScriptEngine() + " Version ";
    s += ScriptEngineMajorVersion() + ".";
    s += ScriptEngineMinorVersion() + ".";
    s += ScriptEngineBuildVersion();
    return s;
}

function encodeURL(strToEncode){
	// --- * @ - _ + . /
	return escape(strToEncode).replace(/\+/g, "%2B");
}

function validMask(varValue, strMask){
	var re = new RegExp(strMask);

	if (re.test(varValue)){
		return true;
	} else {
		return false;
	}
}

function isUndefined(varIn){
	return varIn+"" == "undefined";
}

function isNull(varIn){
	return varIn == null;
}

function isEmpty(varIn){
	return varIn+"" == "";
}

function isDigit(val){
	var digits = "^\\d$";
	return validMask(val, digits);
}

function searchArray(array, val){
	for (var i = 0; i < array.length; i++) if (array[i] == val) break;
	if (i < array.length && array[i] == val) return i;
	return -1;
}

function numberSort(n1, n2){
	if (n1 < n2) retVal = -1;
	else if (n1 > n2) retVal = 1;
	else retVal = 0;
	return retVal;
}

function previewImage(formEl, strIMGName, OLDsource){
	var strFileTemp;
	strFileTemp = formEl.value;
	if (typeof __strFileUpLoadPath == "undefined") __strFileUpLoadPath = null;
	if (strFileTemp != __strFileUpLoadPath){
		if (strFileTemp == ""){
			window.document.getElementById(strIMGName).src = OLDsource;
		} else {
			window.document.getElementById(strIMGName).src = (strFileTemp.search(/^http/i) == -1 ? "file://" : "") + strFileTemp;
		}
		__strFileUpLoadPath = strFileTemp;
	}
}

function getElementsByClassName(node, classname){
	var a = [];
	var re = new RegExp('\\b' + classname + '\\b');
	var els = node.getElementsByTagName("*");
	for (var i = 0, j = els.length; i < j; i++) if(re.test(els[i].className))a.push(els[i]);
	return a;
}

function selectGroup(container, el){
	if (selectGroup.CURRENT == null) selectGroup.CURRENT = getElementsByClassName(window.document.getElementById(container), "selected")[0].id;
	var menu = window.document.getElementById(selectGroup.CURRENT);
	menu.className = menu.className.replace(/selected/, "unselected");
	var body = window.document.getElementById(selectGroup.CURRENT.replace(/__wgMenu_/, "__wgBody_"));
	body.className = body.className.replace(/visible/, "hidden");
	el.className = el.className.replace(/unselected/, "selected");
	var body = window.document.getElementById(el.id.replace(/__wgMenu_/, "__wgBody_"));
	body.className = body.className.replace(/hidden/, "visible")
	selectGroup.CURRENT = el.id;
}
selectGroup.CURRENT = null;

function resizeImageToFit(image, size){
	var wh = null;
	if (image.width > image.height) wh = "width"; else wh = "height";
	if (image[wh] > size) image[wh] = size;
}

Date.prototype.toHTMLFormat = _Date_toHTMLFormat;
String.prototype.formatNum = _String_formatNum;
String.prototype.toNumber = _String_toNumber;
String.prototype.toDate = _String_toDate;
String.prototype.replaceSubstr = _String_replaceSubstr;
String.prototype.splitWords = _String_splitWords;
String.prototype.getWord = _String_getWord;
String.prototype.deleteWord = _String_deleteWord;
String.prototype.toScriptFormat = _String_toScriptFormat;
String.prototype.convertToBase = _String_convertToBase;
String.prototype.toLatLng = _String_toLatLng;
String.prototype.trim = _String_trim;
String.prototype.toHTMLName = _String_toHTMLName;
String.prototype.insertLeadingZeros = _String_insertLeadingZeros;

Number.prototype.toMoney = _Number_toMoney;
Number.prototype.round = _Number_round;
Number.prototype.toNumber = _Number_toNumber;
Number.prototype.convertToBase = _Number_convertToBase;
Number.prototype.toRGB = _Number_toRGB;
Number.prototype.toDMS = _Number_toDMS;

DMS.DIRECTION = { NORTH:"N", SOUTH:"S", EAST:"E", WEST:"W" };
DMS.TYPE = { LATITUDE:"LAT", LONGITUDE:"LNG" };

Function.prototype.getName = _Function_getName;