/*
IMPORTANT - This script request the 'browser.js' script file.
*/

// Simple 'getElementById'
// ------------------------------------------------------------------------------------------------

function $(id) {
	var element = null;
	if (typeof id == 'string') {
		if (document.getElementById) { element = document.getElementById(id); }
		else if (document.all) { element = document.all[id]; }
	}
	return element;
}

// String prototypes
// ------------------------------------------------------------------------------------------------

String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}
String.prototype.isEmpty = function() {
	return (this.length == 0);
}
String.prototype.isInteger = function() {
	if (this.isEmpty()) { return false; }
	for (var i = 0; i < this.length; i++) {
		var c = this.charAt(i);
		if (c < "0" || c > "9") { return false; }
	}
	return true;
}

String.prototype.firstWhitechar = function(pos) {
	if (this.isEmpty) return -1;
	var whitespace = "\n\r\t ";
	while (pos < this.length) {
		if (whitespace.indexOf(this.charAt(pos)) != -1) { return pos; }
		else { pos++; }
	}
	return this.length;
}
String.prototype.capitalize = function() {
	if (this.isEmpty()) { return this; }
	var s = this.trim();
	if (s.length == 1) { return s.toUpperCase(); }
	return s.substr(0, 1).toUpperCase() + s.substr(1);
}

// Array prototypes
// ------------------------------------------------------------------------------------------------

if (!Array.copy) {
	Array.prototype.copy = function() {
		var result = [], i = this.length;
		while (i--) {
			result[i] = typeof this[i].copy !== 'undefined' ? this[i].copy() : this[i];
		}
		return result;
	};
}

if (!Array.forEach) {
	Array.prototype.forEach = function(f) {
		var j;
		for (var i = 0; i < this.length; i++ ) {
			if ((j = this[i])) { f(j); }
		}
	};
}

if (!Array.indexOf) {
	Array.prototype.indexOf = function(obj) {
		for (var i = 0; i < this.length; i++) {
			if (this[i] == obj) { return i; }
		}
		return -1;
	}
}

if (!Array.insert) {
	Array.prototype.insert = function(index, value) {
		if (index >= 0) {
			var a = this.slice(), b = a.splice(index);
			a[index] = value;
			return a.concat(b);
		}
	};
}

if (!Array.lastIndexOf) {
	Array.prototype.lastIndexOf = function(value, begin, str) {
		begin = +begin || 0;
		var i = this.length;
		while (i --> b) {
			if (this[i] === value || str && this[i] == value ) { return i; }
		}
		return -1;
	};
}

// Add event function
// ------------------------------------------------------------------------------------------------

function addEvent(obj, eventType, func) {
	if (obj.addEventListener){
		obj.addEventListener(eventType, func, false);
		return true;
	} else if (obj.attachEvent){
		return obj.attachEvent("on" + eventType, func);
	}
	return false;
}

// Point Object
// ------------------------------------------------------------------------------------------------

function Point(posX, posY) {
	this.x = posX;
	this.y = posY;
	this.setX = function(newPos) {
		this.x = newPos;
	}
	this.setY = function(newPos) {
		this.y = newPos;
	}
	this.xy = function() {
		return new Array(this.x, this.y);
	}
	this.toString = function() {
		return "x = " + this.x + "px, y = " + this.y + "px";
	}
}

// Rect Object
// ------------------------------------------------------------------------------------------------

function Rect(posX1, posY1, posX2, posY2) {
	this.left = (posX1 <= posX2) ? posX1 : posX2;
	this.top = (posY1 <= posY2) ? posY1 : posY2;
	this.right = (posX1 <= posX2) ? posX2 : posX1;
	this.bottom = (posY1 <= posY2) ? posY2 : posY1;

	this.updateSize = function() {
		this.width = this.right - this.left;
		this.height = this.bottom - this.top;
	}
	this.setLeft = function(newPos) {
		this.left = newPos;
		this.updateSize();
	}
	this.setTop = function(newPos) {
		this.top = newPos;
		this.updateSize();
	}
	this.setRight = function(newPos) {
		this.right = newPos;
		this.updateSize();
	}
	this.setBottom = function(newPos) {
		this.bottom = newPos;
		this.updateSize();
	}
	this.xy1 = function() {
		return new Array(this.left, this.top);
	}
	this.xy2 = function() {
		return new Array(this.right, this.bottom);
	}
	this.topLeft = function() {
		return new Point(this.left, this.top);
	}
	this.bottomRight = function() {
		return new Point(this.right, this.bottom);
	}
	this.isEmpty = function() {
		return (this.left == this.right || this.top == this.bottom);
	}
	this.toString = function() {
		return this.topLeft().toString() + " - " + this.bottomRight().toString();
	}
	this.updateSize();
}

// Point and Rect methods
// ------------------------------------------------------------------------------------------------

function pointInRect(p, r) {
	if (p == null || r == null) { return false; }
	return (p.x >= r.left && p.x <= r.right && p.y >= r.top && p.y <= r.bottom);
}

function rectIntersect(r1, r2) {
	// This function must be revised
	if (r1 == null || r2 == null) { return false; }
	return (r1.bottom >= r2.top && r1.top < r2.bottom && r1.right >= r2.left && r1.left < r2.right);
}

// Elements position and layout methods
// ------------------------------------------------------------------------------------------------

function findPos(obj) {
	if (obj == null) { return new Point(0, 0); }
	var objLeft = objTop = 0;
	if (obj.offsetParent) {
		do {
			objLeft += obj.offsetLeft;
			objTop += obj.offsetTop;
		} while (obj = obj.offsetParent);
	}
	return new Point(objLeft, objTop);
}

function getObjLayout(obj) {
	if (obj == null) { return new Rect(0, 0, 0, 0); }
	var objPos = findPos(obj);
	return new Rect(objPos.x, objPos.y, objPos.x + obj.offsetWidth, objPos.y + obj.offsetHeight);
}

function getObjLayoutStyle(obj) {
	alert('aici');
	if (obj == null) { return new Rect(0, 0, 0, 0); }
	alert(obj.style.left);
}

// Get page size
// ------------------------------------------------------------------------------------------------

function getPageSize() {
	var xScroll, yScroll, windowWidth, windowHeight, pageWidth, pageHeight;
	if (window.innerHeight && window.scrollMaxY) {
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else {
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	if (self.innerHeight) {
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth;
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) {
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) {
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}
	if (yScroll < windowHeight) { pageHeight = windowHeight; }
	else { pageHeight = yScroll; }
	if(xScroll < windowWidth){ pageWidth = xScroll; }
	else { pageWidth = windowWidth; }
	return new Rect(0, 0, pageWidth, pageHeight);
}

// Get page scroll position
// ------------------------------------------------------------------------------------------------

function getScrollPos() {
  var result = new Point(0, 0);
  if (typeof(window.pageYOffset) == 'number') {
    result.setY(window.pageYOffset);
    result.setX(window.pageXOffset);
  } else if (document.body &&
		(document.body.scrollLeft || document.body.scrollTop)) {
    result.setY(document.body.scrollTop);
    result.setX(document.body.scrollLeft);
  } else if( document.documentElement &&
		(document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
    result.setY(document.documentElement.scrollTop);
    result.setX(document.documentElement.scrollLeft);
  }
  return result;
}

// Extract number value from style "px", "pt", "em"... values
// ------------------------------------------------------------------------------------------------

function extractNumber(value) {
	var n = parseInt(value);
	return n == null || isNaN(n) ? 0 : n;
}

// Element Fade-In and Fade-Out methods
// ------------------------------------------------------------------------------------------------
var fadeTimerId = 0;
var fadeTime = 30;
var fadeDelta = 2;

function objFadeIn(objId) {
	if (fadeTimerId > 0) {
		clearTimeout(fadeTimerId);
		fadeTimerId = 0;
	}
	if (is_empty(objId)) { return; }
	var fadeObj = document.getElementById(objId);
	if (fadeObj == null) { return; }
	var fadeLevel = 0;
	if (fadeObj.getAttribute("fadeLevel")) { fadeLevel = parseInt(fadeObj.getAttribute("fadeLevel")); }
	fadeLevel = fadeLevel + fadeDelta;
	if (browser.isExplorer()) { fadeObj.style.filter = "alpha(opacity=" + (fadeLevel * 10) + ")"; }
	else { fadeObj.style.opacity = (fadeLevel / 10); }
	if (fadeLevel <= 10) { fadeTimerId = setTimeout("objFadeIn('" + objId + "')", fadeTime); }
	if (fadeLevel >= 10) { fadeLevel = 10; }
	fadeObj.setAttribute("fadeLevel", fadeLevel);

}

function objFadeOut(objId) {
	if (fadeTimerId > 0) {
		clearTimeout(fadeTimerId);
		fadeTimerId = 0;
	}
	if (is_empty(objId)) { return; }
	var fadeObj = document.getElementById(objId);
	if (fadeObj == null) { return; }
	var fadeLevel = 0;
	if (fadeObj.getAttribute("fadeLevel")) { fadeLevel = parseInt(fadeObj.getAttribute("fadeLevel")); }
	fadeLevel = fadeLevel - fadeDelta;
	if (browser.isExplorer()) { fadeObj.style.filter = "alpha(opacity=" + (fadeLevel * 10) + ")"; }
	else { fadeObj.style.opacity = (fadeLevel / 10); }
	if (fadeLevel >= 0) { fadeTimerId = setTimeout("objFadeOut('" + objId + "')", Math.round(fadeTime * 1.25)); }
	if (fadeLevel <= 1) {
		fadeLevel = 1;
		fadeObj.style.visibility = "hidden";
	}
	fadeObj.setAttribute("fadeLevel", fadeLevel);
}

// Fixed Popup Window

var sw = screen ? screen.width : 0;
var sh = screen ? screen.height : 0;

function showPopup(pwidth, pheight, surl) {
	var param = "toolbar=no,scrollbars=no,location=no,statusbar=no,menubar=no,resizable=no," +
		"width=" + pwidth + ",height=" + pheight+",left=" + ((sw - pwidth) / 2) + ",top=" + ((sh - pheight) / 2);
	var fixdlgwin = window.open(surl, null, param, true);
	fixdlgwin.opener = window;
	fixdlgwin.focus();
}

// Extended Popup Window

function showPopupExt(pwidth, pheight, surl, extparams) {
	var param = "width=" + pwidth + ",height=" + pheight + ",left=" + ((sw - pwidth) / 2) + ",top=" +
		((sh - pheight) / 2) + "," + extparams;
	var dlgwin = window.open(surl, null, param, true);
	dlgwin.opener = window;
	dlgwin.focus();
}

// Generate a unique identifier based on the current time in microseconds
// ------------------------------------------------------------------------------------------------

function uniqueId() {
	var d = new Date();
	var uid = d.getTime();
	return uid.toString(16);
}

// Date utilities + Check for leap year
// ------------------------------------------------------------------------------------------------

var month_days = [31,29,31,30,31,30,31,31,30,31,30,31];
var month_names = ['Ianuarie','Februarie','Martie','Aprilie','Mai','Iunie','Iulie',
	'August','Septembrie','Octombrie','Noiembrie','Decembrie'];

function isLeapYear(year) {
	year = parseInt(year);
	if (year % 4 == 0) {
		if (year % 100 != 0) { return true; }
		else {
			if (year % 400 == 0) { return true; }
			else { return false; }
		}
	}
	return false;
}

function getDaysInMonth(month, year) {
	var result = month_days[month];
	if (month == 1 && !isLeapYear(year)) { result = 28; }
	return result;
}

// RegExp functions
// ------------------------------------------------------------------------------------------------

var global_email_regexp  = /^((([a-zA-Z]|[0-9]|!|#|$|%|&|'|\*|\+|\-|\/|=|\?|\^|_|`|\{|\||\}|~)+(\.([a-zA-Z]|[0-9]|!|#|$|%|&|'|\*|\+|\-|\/|=|\?|\^|_|`|\{|\||\}|~)+)*)@((((([a-zA-Z]|[0-9])([a-zA-Z]|[0-9]|\-){0,61}([a-zA-Z]|[0-9])\.))*([a-zA-Z]|[0-9])([a-zA-Z]|[0-9]|\-){0,61}([a-zA-Z]|[0-9])\.)[\w]{2,4}|(((([0-9]){1,3}\.){3}([0-9]){1,3}))|(\[((([0-9]){1,3}\.){3}([0-9]){1,3})\])))$/;
var global_url_regexp    = /^((([fF][tT][pP]|[hH][tT][tT][pP]|[hH][tT][tT][pP][sS])+:)?\/\/)?(([\d\w]|%[a-fA-f\d]{2,2})+(:([\d\w]|%[a-fA-f\d]{2,2})+)?@)?([\d\w][-\d\w]{0,253}[\d\w]\.)+[\w]{2,4}(:[\d]+)?(\/([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)*(\?(&?([-+_~.\d\w]|%[a-fA-f\d]{2,2})=?)*)?(#([-+_~.\d\w]|%[a-fA-f\d]{2,2})*)?$/;
var global_number_regexp = /^-?[0-9]*\\.[0-9]*$/;
var global_phone_regexp  = /^([\(]+[0-9]+[\)])?([\ ]{0,1})+(((([0-9]){1,24}([\.\ ](([0-9]){1,8})){0,1})){1,8})$/;
var global_mobile_regexp = /^(([0-9]){4}([\ ]{0,1}))+(((([0-9]){1,24}([\.\ ](([0-9]){1,8})){0,1})){1,8})$/;

function reg_match(text, pattern) {
	if (text == "") { return false; }
	if (text.match(pattern)) { return true; }
	return false;
}

function check_email(value) {
	return reg_match(value, global_email_regexp);
}
