//
// Alta v1.00 - Copyright (c) 2005 Altanet S.A.
// Global Functions
//

var Browser = new Object();
Browser.isMozilla = (typeof document.implementation != 'undefined') && (typeof document.implementation.createDocument != 'undefined') && (typeof HTMLDocument!='undefined');
Browser.isIE = window.ActiveXObject ? true : false;
Browser.isFirefox = (navigator.userAgent.toLowerCase().indexOf("firefox")!=-1);
Browser.isOpera = (navigator.userAgent.toLowerCase().indexOf("opera")!=-1);

var obj_event = null;

function GetObject(id){
	if (document.getElementById)
		return document.getElementById (id);
	return document.all[id];
}


stopEvent = function(ev) {
	if (Browser.isIE) {
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	} else {
		ev.preventDefault();
		ev.stopPropagation();
	}
}

addEvent = function(el, evname, func) {
	if (Browser.isIE) {
		el.attachEvent("on" + evname, func);
	} else {
		el.addEventListener(evname, func, true);
	}
}


function SetProcessEvent(e) { 
	var targ;
	if (!e) var e = window.event;
	if (e.target){
		targ = e.target;
	} else if (e.srcElement){
		targ = e.srcElement;
	}
	if (targ.nodeType == 3) // defeat Safari bug
		targ = targ.parentNode;
	if (targ.tagName == "INPUT"){
		obj_event = targ;
	}
	else{
		obj_event = null;
	}	
}

addEvent(document, "click", SetProcessEvent);

var StopValidation = false;
function ValidateCustomRequire()
{
	if (!StopValidation && obj_event != null && obj_event.type == "submit" && obj_event.onclick != null){
		if(typeof(CustomValidation) == "object")
		{
			for(i=0;i<this.CustomValidation.length;i++){
				V_obj = GetObject(CustomValidation[i][0]);
				if(V_obj)
				{
					if (trim(V_obj.value) == "" || Validator(trim(V_obj.value),CustomValidation[i][1]) == null){
						MakeStar(V_obj);
						return false;
					}else{
						RemoveStar(V_obj);
					}
				}
			}
		}
		if(typeof(ValidatorOnSubmit)=="function")
			if (!ValidatorOnSubmit()) return false;
	}
	
	return true;
}

MakeStar = function(control){
	
	var _er = "";
	_er = "&nbsp;<span class='n' id='" + control.id + "sp' name='" + control.id + "sp'>*</span>";
	if (GetObject(control.id + 'sp')){
		GetObject(control.id + 'sp').style.visibility = 'visible';
	}else{
		if (document.all) {
			control.insertAdjacentHTML("AfterEnd",_er);
		}
		else if (document.getElementById) {
			var r = control.ownerDocument.createRange();
			r.setStartAfter(control);
			var df = r.createContextualFragment( _er );
			control.parentNode.insertBefore(df, control.nextSibling);

		}
	}
	
	try
	{
		//control.focus();
	}
	catch(e){}
}
	
RemoveStar = function RemoveStar(control){
	var Controlsp = GetObject(control.id + 'sp');
	if (Controlsp){
		Controlsp.style.visibility = 'hidden';
	}	
}

function Validator(op, dataType) {
	var val = new Properties();

	function GetFullYear(year) {
		return (year + parseInt(val.century)) - ((year < val.cutoffyear) ? 0 : 100);
	}
	var num, cleanInput, m, exp;
	if (dataType == "Integer") {
		exp = /^\s*[-\+]?\d+\s*$/;
		if (op.match(exp) == null) 
			return null;
		num = parseInt(op, 10);
		return (isNaN(num) ? null : num);
	}
	else if (dataType == "Double") {
		exp = new RegExp("^\\s*([-\\+])?(\\d+)?(\\" + val.decimalchar + "(\\d+))?\\s*$");
		m = op.match(exp);
		if (m == null)
			return null;
		cleanInput = m[1] + (m[2].length>0 ? m[2] : "0") + "." + m[4];
		num = parseFloat(cleanInput);
		return (isNaN(num) ? null : num);            
	} 
	else if (dataType == "Currency") {
		exp = new RegExp("^\\s*([-\\+])?(((\\d+)\\" + val.groupchar + ")*)(\\d+)"
						+ ((val.digits > 0) ? "(\\" + val.decimalchar + "(\\d{1," + val.digits + "}))?" : "")
						+ "\\s*$");
		m = op.match(exp);
		if (m == null)
			return null;
		var intermed = m[2] + m[5] ;
		cleanInput = m[1] + intermed.replace(new RegExp("(\\" + val.groupchar + ")", "g"), "") + ((val.digits > 0) ? "." + m[7] : 0);
		num = parseFloat(cleanInput);
		return (isNaN(num) ? null : num);            
	}
	else if (dataType == "Date") {
		var temp_op = op.replace(/_/g, "").replace(/\//g, "");
		if(temp_op != "")
		{
			var yearFirstExp = new RegExp("^\\s*((\\d{4})|(\\d{2}))([-/]|\\. ?)(\\d{1,2})\\4(\\d{1,2})\\s*$");
			m = op.match(yearFirstExp);
			var day, month, year;
			if (m != null && (m[2].length == 4 || val.dateorder == "ymd")) {
				day = m[6];
				month = m[5];
				year = (m[2].length == 4) ? m[2] : GetFullYear(parseInt(m[3], 10))
			}
			else {
				if (val.dateorder == "ymd"){
					return null;		
				}						
				var yearLastExp = new RegExp("^\\s*(\\d{1,2})([-/]|\\. ?)(\\d{1,2})\\2((\\d{4})|(\\d{2}))\\s*$");
				m = op.match(yearLastExp);
				if (m == null) {
					return null;
				}
				if (val.dateorder == "mdy") {
					day = m[3];
					month = m[1];
				}
				else {
					day = m[1];
					month = m[3];
				}
				year = (m[5].length == 4) ? m[5] : GetFullYear(parseInt(m[6], 10))
			}
			month -= 1;
			var date = new Date(year, month, day);
		    
			return (typeof(date) == "object" && year == date.getFullYear() && month == date.getMonth() && day == date.getDate()) ? date.valueOf() : null;
		}
		else
		{
			return op.toString();
		}
	}
	else if (dataType == "DateTime") 
	{	
		var temp_op = op.replace(/_/g, "").replace(/\//g, "").replace(/:/g, "").replace(/ /g, "");;
		if(temp_op.length > 0)
		{
			if(temp_op.length == 14) return op.toString();
			else return null;
		}
		else
		{
			return op.toString();
		}
	}
	else {
		return op.toString();
	}
}

function Properties(){
	this.decimalchar = ","; // For Greece
	this.groupchar = "."; // For Greece
	this.digits = "2";
	this.dateorder = "ymd";//dmy
	this.century = "2000";
	this.cutoffyear = "2029";
}

function ShowContent( id )
{
	var location = window.location.toString();
	for (i = 0; i < document.all.tags("SPAN").length; ++i)
	{
		var obj = document.all.tags("DIV")[i];
		if (obj && obj.className == "at")
		{
			obj.className = "";
		}
	}
	
	GetObject(id).className = "at";
	if ( location.indexOf( "#" ) != -1 )
		location = location.substring( location, 0, location.indexOf( "#" ) );
	window.location = location + "#" + id;
}

function ValidateLetters(obj, err)
{
	if (obj.value.length < 2)
	{
		alert(err);
		return false;
	}
	return true;
}


function OpenPrint(PrintObj,LeaveLinks)
{
	var PrintWindow = window.open("PagePrint","PagePrint","status=yes,toolbar=no,menubar=yes,location=no,resizable=1,scrollbars=1"); 
	var PrintWindowHtml = "";
	var __Styles = GetPageStyleSheet();
	
	PrintWindowHtml += "<html><head>";
	PrintWindowHtml += "<title>" + window.document.title + " :: Print</title>\n";
	for(var i = 0; i < __Styles.length ; i++) 
	{
		PrintWindowHtml += "<link rel='stylesheet' type='text/css' href='" + __Styles[i].href + "'/>\n";
	}
	PrintWindowHtml += "</head><body>";
	PrintWindowHtml += GetObject(PrintObj).innerHTML;
	PrintWindowHtml += "<body></html>";
	
	if(!LeaveLinks)
	{
		PrintWindowHtml = PrintWindowHtml.replace(/\s*href="[^"]*"/gi, "" ) ;
	}
	PrintWindow.document.write(PrintWindowHtml);
	PrintWindow.focus();
	
}

function GetPageStyleSheet() {
	var i, a;
	var StyleArray = new Array();
	for(i = 0 ; (a = document.getElementsByTagName("link")[i]) ; i++) 
	{
		if(a.getAttribute("rel").indexOf("style") != -1) 
		{
			StyleArray.push(a);//a.disabled = true,if(a.getAttribute("title") == title) a.disabled = false;
		}
	}
	return StyleArray;
}

function PopupWindowArguments(width,height)
{
	var argument = "channelmode=no,";
	argument += "directories=no,";
	argument += "fullscreen=no,";
	argument += "location=no,";
	argument += "menubar=no,";
	argument += "resizable=yes,";
	argument += "scrollbars=yes,";
	argument += "status=no,";
	argument += "titlebar=no,";
	argument += "toolbar=no,";
	argument += "width=" + (width ? width : 500) + ",";
	argument += "height=" + (height ? height : 500) + ",";
	argument += "top=" + "0" + ",";
	argument += "left=" + (screen.width - (width ? (width+10) : 550)) + "";
	return argument;
}

var CurrentReturnValObjectID;
function GetBridgePage(bridge,module,custom,concat,ReturnValObjectID,width,height)
{
	CurrentReturnValObjectID = ReturnValObjectID;
	var argument = PopupWindowArguments((width ? width : 600),(height ? height : 600));
	var cWindow = window.open("default.aspx?bridge=" + bridge + (custom ? "&c=true" : "") + (module ? "&m=" + module : "") + (concat ? concat : ""),'window',argument,true);
	cWindow.focus();
}

function redir(url)
{
	if(url != "")
		window.location.href = url;
}

function AddScript(path)
{
	document.write('<s'+'cript language="JavaScript" src="' + path + '"></s'+'cript>'); 
}

function ValidateSelected()
{
	if(arguments)
	{
		for(var i=0;i<arguments.length;i++){
			var V_obj = GetObject(arguments[i]);
			if(V_obj)
			{
				if (Trim(V_obj.value) == ""){
					MakeStar(V_obj);
					return false;
				}else{
					RemoveStar(V_obj);
				}
			}
		}
	}
	return true;
}

function GoSecure()
{
	var __form = document.forms[0];
	if(__form)
	{
		var __action = "https://" + RealUrl + __form.action;
		__form.action = __action;
	}
}

function Trim(str)
{
	return str.replace(/(^\s*)|(\s*$)/g, "");
}


/*  Cookies Area */

function getCookie(CookieName) {
	
	var cookieValue = '';
	if(CookieName != "")
	{
		var posName = document.cookie.indexOf(escape(CookieName) + '=');
		if (posName != -1) {
			var posValue = posName + (escape(CookieName) + '=').length;
			var endPos = document.cookie.indexOf(';', posValue);
			if (endPos != -1) 
				cookieValue = unescape(document.cookie.substring(posValue, endPos));
			else 
				cookieValue = unescape(document.cookie.substring(posValue));
		}
	}

	return (cookieValue);

};

function updateCookie(CookieName,Value) {
	if(CookieName != "") setCookie(CookieName, Value);
};

function setCookie(cookieName, cookieValue, expires, path, domain, secure) {
	document.cookie =
		escape(cookieName) + '=' + escape(cookieValue)
		+ (expires ? '; expires=' + expires.toGMTString() : '')
		+ (path ? '; path=' + path : '')
		+ (domain ? '; domain=' + domain : '')
		+ (secure ? '; secure' : '');
};

/*  End Of Cookies Area */
