//
// Alta v1.00 - Copyright (c) 2005 Altanet S.A.
// Global Functions
//

var ie=document.all;
var nn6=document.getElementById&&!document.all;
var obj_event = null;

function GetObject(id){
	if (document.getElementById)
		return document.getElementById (id);
	return document.all[id];
}


stopEvent = function(ev) {
	if (ie) {
		window.event.cancelBubble = true;
		window.event.returnValue = false;
	} else {
		ev.preventDefault();
		ev.stopPropagation();
	}
}

addEvent = function(el, evname, func) {
	if (ie) {
		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);

function ValidateCustomRequire()
{
	if (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 (V_obj.value == "" || Validator(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){
	try
	{
		control.focus();
	}
	catch(e){}
	var _er = "";
	_er = "&nbsp;<span class='error' id='" + control.name + "sp' name='" + control.name + "sp'>*</span>";
	if (GetObject(control.id + 'sp')){
		GetObject(control.id + 'sp').style.visibility = 'visible';
		return;
	}
	
	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);

	}
}
	
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 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();
	}
}

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,SameWindow,LeaveLinks)
{
	var PrintWindow = window.open("PagePrint",(SameWindow?null:""),"status=yes,toolbar=no,menubar=yes,location=no,resizable=yes"); 
	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;
}

