/*
	This file contain an object message that User can see.
	The message about some literal restriction when hi's writing in a input text or something that isn't
	requeried the server side
	
	Simple create an object 
	var obj = new UIError();
	and if the function setErrorBase dosen't contain your type off error simpli add it
	obj.addError(param1,param2);
	param1 - the name of error, param2 - the text that will be present to user.
	
	when you want to present to user an error : obj.Alert(param1,param2);
	param1 - obj beyound that this error will appear, it could be a input text, or a button etc.
	param2 - the error name specified in setErrorBase, if this one isn't found it will appear 
	an default error message
	
*/
var vPattern=new Array();
vPattern["passwordM"]=/^((\d+[A-Za-z]+)|([A-Za-z]+\d+))(\w*)$/;
vPattern["noFolderSelected1"]= /^(.)+$/;
vPattern["name"]=/^[a-zA-Z0-9- \.\_]*$/;
vPattern["hexnumbers"]=/^[0-9a-fA-F]{6}$/;
vPattern["notEmpty"]=/./;
vPattern["standart_characters"]=/^[^\\\/:*?."<>|]+$/;
vPattern["email"]=/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]{1,4}$/;
vPattern["multiemail"]=/^\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]{1,4}((,|;)( )*\w+((-\w+)|(\.\w+))*\@[A-Za-z0-9]+((\.|-)[A-Za-z0-9]+)*\.[A-Za-z0-9]{1,4})*$/;
vPattern["nodigits"]=/^[^0-9]+$/;
vPattern["compareint"]=/^([0-9])*$/;
vPattern["digits"]=/^([0-9]){0,9}$/;
vPattern["numbers"]=/[0123456789]/g;
vPattern["iddomain"]=/^([0-9]){6}$/;
vPattern["fileIntranetXsl"]= /^(([a-zA-Z]:)|(\\{2}([\w|\.| |-]|)+)?)(\\(\w[\w ]*))+\.(xsl|xslt)$/i;
vPattern["filePdf"]= /^(([a-zA-Z]:)|(\\{2}[^\\/\|”:\*\?<>]+)?)((\\[^\\/\|”:\*\?<> ]([^\\/\|”:\*\?<>]*[^\\/\|”:\*\?<> ])?)*)(\.pdf)$/i;
//vPattern["filePdf"]= /^(([a-zA-Z]:)|(\\{2}([\w|\.| |-]|)+)?)(\\(\w[\w|\.| |-]*))+\.(pdf)$/i;
vPattern["freeTextRequired"]=/(.*\{\!freeText\!\}.*)/;
vPattern["phonefax"]=/^0044\d{10}$/;
vPattern["day"]=/^\d{1,2}$/;
vPattern["month"]=/^\d{1,2}$/;
vPattern["year"]=/^\d{1,4}$/;
vPattern["general"]=/^[^\<\>]*$/;
//vPattern["www"]= new RegExp("^((http|ftp)s?)\://([0-9a-zA-Z\-]+\.)+[0-9a-zA-Z]{2,6}(\:[0-9]+)?(/([0-9a-zA-Z_\-])+(\.)?([0-9a-zA-Z_\-])+)*/?([a]?\\?[0-9a-zA-Z_\&=\-]+)?$", "i");
vPattern["www"] = /http(s)?:\/\/[A-Za-z0-9\.-]{3,}\.[A-Za-z]{2,3}/;
//      /^((htt(?:p|ps)\:\/\/)+?|(www\.)+?)[\~\%\?\.\/\w]+\.[\w]{1,4}$/;
vPattern["domain"]=/^(\w)+[\~\%\?\.\/\-\w]+\.[\w]{2,4}$/;
vPattern["guid"]=/^\{?[a-fA-F\d]{8}-([a-fA-F\d]{4}-){3}[a-fA-F\d]{12}\}?$/ ;
//vPattern["url"]=/^http(?:s?)\:\/\/(\w+\:\w+@)?([^/\r\n\:]+)?(\:\d+)?((\/.*)*\/)?(.*?\.((jpg|jpeg|wav|wma|mp3|gif|png|tiff|tif|aiff|avi|mpg|mpeg|wmv|mov|asf|asx|doc|pdf|xls|pdb|txt|wpd|bmp)))?(\??(\w+\=[^\#]+)(\&?\w+\=\w+)*)*(\#.*)?$/i;
//vPattern["url"]=/^(?:(?<protocol>http(?:s?)|ftp)(?:\:\/\/))(?:(?<usrpwd>\w+\:\w+)(?:\@))?(?<domain>[^/\r\n\:]+)?(?<port>\:\d+)?(?<path>(?:\/.*)*\/)?(?<filename>.*?\.(?<ext>\w{2,4}))?(?<qrystr>\??(?:\w+\=[^\#]+)(?:\&?\w+\=\w+)*)*(?<bkmrk>\#.*)?$/;
//vPattern["fileType"]=/^(([a-zA-Z]:)|(\\{2}\w+)?)(\\(\w[\w |\.]*))+\.(gif|jpg|jpeg|tiff)$/i;
//vPattern["url"]=/^((http(s?)|ftp)(\:\/\/))((\w+\:\w+)(\@))?([^\/\r\n\:]+)?(\:\d+)?((\/.*)*\/)?(.*?\.(\w{2,4}))?(\??(\w+\=[^\#]+)(\&?\w+\=\w+)*)*(\#.*)?$/;
//vPattern["fileTypeClipping"]=/^(([a-zA-Z]:)|(\\{2}\w+)?)(\\(\w[\w |\.]*))+\.(gif|jpg|jpeg|tiff|wav|wma|mp3|aiff|avi|mpg|mpeg|wmv|mov|asf|asx|doc|pdf|xls|pdb|txt|wpd|bmp)$/i;
//vPattern["fileType"]=/^[a-zA-Z0-9\-\\\_\!\: \,\.]+(\.){0,1}(gif)|(jpg)|(tiff){1,1}$/i;
//vPattern["fileTypeClipping"]=/^[a-zA-Z0-9\-\\\_\!\: \,\.]+(\.){0,1}(jpg)|(jpeg)|(tiff)|(wav)|(wma)|(mp3)|(aiff)|(avi)|(mpg)|(mpeg)|(wmv)|(mov)|(asf)|(asx)|(doc)|(pdf)|(xls)|(pdb)|(txt)|(wpd)|(bmp){1,1}$/;

vPattern["time"]= /^(^([0-9]|[0-1][0-9]|[2][0-3])):(([0-5][0-9])$)$$/;
vPattern["timeISO"]= /^(([0-9]|[0-1][0-9]|[2][0-3])):(([0-5][0-9])):(([0-5][0-9]))$$/;

var _CREATED_ERROR = "";
var xmlTr;
var PathToResources = "../";
// ERRORS database. If you want to add an error just add it in this function.

	
function setErrorBase(){
	var theXML = xmlTr;
	var error = xmlTr.responseXML.childNodes[0].childNodes[0];
	var Item="";
	for(i=0;i<error.childNodes.length; i++)
	{	
		if (error.childNodes[i].nodeType == 1) 
		{
			Item = error.childNodes[i];
			this.addError(Item.nodeName,Item.getAttribute("value")); 
		}
	}
	_CREATED_ERROR = this;
}

/************************* UIError object ************************************/
UIError.prototype = new Error;
function UIError(){
	_tmpImageCashe.src = PathToResources+"Resources/img/ico_alert.gif";
	if( _CREATED_ERROR!="") return  _CREATED_ERROR;
	this.base = TR_CLIENT_MESSAGES;
	//this.base = new Array;
	this.drowed = false;
	this.getError = getError;
	this.getMessage = getError;
	this.addError = addError;
	this.Alert = Alert;
	this.Message = _errMessage;
	this.Close = _alertClose;
	this.Prompt = Prompt;
	this.validate = validateObjects;
	this.setErrorBase = setErrorBase;
	this.onScreen = false;
	this.promptOnScreen = false;
	this.ExistValidationRule = _ExistValidationRule;
	this.findPosY = findPosY;
	_CREATED_ERROR = this;
}

function _ExistValidationRule(obj)
{
	if (!obj.getAttribute("validator"))
	{
		return true;
	}
	if (vPattern[obj.getAttribute("validator")])
	{
		return true;
	}
	else return false;
}

function getError(id){
	if (id){
		if(this.base[id]!=undefined){return this.base[id];}
		else {return this.base["default"];}
	}
	else
	{
		return '';
	}
}

function addError(id,message){
	this.base[id] = message;
}


function drawDiv(){
	var errContainer=document.createElement("DIV");
	errContainer.id="alertMessage";
	errContainer.style.display="none";
	document.body.appendChild(errContainer);
	var newObject = document.getElementById("alertMessage");
	newObject.style.padding = "2px";
	newObject.style.zIndex = "5000";
	newObject.style.position = "absolute";
	newObject.style.backgroundColor = "white";
	newObject.style.border = "1px solid black";
	
	/*prompt object*/
	/*var promptContainer = document.createElement("DIV");
	promptContainer.style.width = "82px";
	promptContainer.id="promptMessage";
	promptContainer.style.display="none";
	document.body.appendChild(promptContainer);
	var newObject = document.getElementById("promptMessage");
	var innerObject = document.createElement("DIV");
	newObject.style.padding = "0px";
	newObject.style.zIndex = "1001";
	newObject.style.position = "absolute";
	newObject.style.backgroundColor = "white";
	innerObject.style.border = "1px solid black";
	
	var txtObject = document.createElement("INPUT");
	txtObject.style.border = "0px";
	txtObject.style.fontSize = "10px";
	txtObject.style.width = "80px";
	//txtObject.style.float = "left";
	innerObject.appendChild(txtObject);
	newObject.appendChild(innerObject);*/
	
	/*var cmdTbl = document.createElement("<table border='0' cellspacing='0' cellpadding='0' width='100%'>");
	var tr = document.createElement("TR");
	var td = document.createElement("<TD align='right'>");
	var cmd1 = document.createElement("<a href='#' class='smallnav'/>");
	cmd1.innerHTML = "ok";
	var cmd2 = document.createElement("<a href='#' class='smallnav'/>");
	cmd2.innerHTML = "cancel";
	td.appendChild(cmd1);
	td.appendChild(document.createElement("<span style='width:2px'>"));
	td.appendChild(cmd2);
	tr.appendChild(td);
	cmdTbl.appendChild(tr);
	newObject.innerHTML += cmdTbl.outerHTML;
	alert(newObject.innerHTML);*/
	
	
}
// user alert message
function validateObjects(obj){
	var stat = true;
	if(obj.getAttribute("validator")){
		if(!vPattern[obj.getAttribute("validator")])
		{
			 stat = true;
		}
		else
		{	
			if(vPattern[obj.getAttribute("validator")].exec(obj.value))
			{
				stat = true;
			}
			else 
			{
				stat = false;
			}
			if (obj.getAttribute("validator") == "general")
			{
				unicode = /^(.*&#)|(&#)+.*$/im;
				
				if (unicode.exec(obj.value))
				{
					stat = false;
				}
			}
		}
	} 
	
	if(!stat && obj.getAttribute("validator") && obj.getAttribute("validator") != "nothing"){
		this.Alert(obj, obj.getAttribute("validator"));
	}
	return stat;
}

var _tmpImageCashe = new Image();



function Alert(obj, id, externalMsg, isPrefix){
	
	if(!this.drowed){drawDiv();this.drowed = true;}
	var obj1 = document.getElementById("alertMessage");
	if(this.onScreen)return;
	var x = findPosX(obj);
	var y = this.findPosY(obj) - 20;
	obj1.style.left = x;
	obj1.style.top = y;
	obj1.innerHTML =  "<img src='"+PathToResources+"Resources/img/ico_alert.gif' style='float:left'>";

	if(externalMsg)	
	{
		if (isPrefix)
		{
			obj1.innerHTML += "<span class='blockedClass'>"+externalMsg.replace("_"," ")+" "+this.getError(id)+"</span>";
		}
		else
		{
			obj1.innerHTML += "<span class='blockedClass'>"+this.getError(id)+" "+externalMsg.replace("_"," ")+"</span>";
		}
	}
	else obj1.innerHTML += "<span class='blockedClass'>"+this.getError(id)+"</span>";
	obj1.style.display = "";
	try{obj1.focus();}
	catch(e){}
	this.onScreen = true;
	var str1 = "setTimeout('document.getElementById(\""+obj1.id+"\").style.display=\"none\";_CREATED_ERROR.onScreen=false;',4000)";
	eval(str1);
}


function _errMessage(obj, evt){
	if (!obj.getAttribute("message") || !obj.getAttribute("message").length > 0)
		return;
	if(!this.drowed){drawDiv();this.drowed = true;}
	var obj1 = document.getElementById("alertMessage");
	if(this.onScreen)return;
	var width = (obj.width) ? obj.width : 20;
	
	var x = (document.body.scrollLeft)? document.body.scrollLeft + evt.clientX : evt.clientX;
	//findPosX(obj, evt.clientX);
	//x= (x > evt.clientX) ? x + width : evt.clientX + width;
	var y = (document.body.scrollTop)? document.body.scrollTop + evt.clientY-20 : evt.clientY-20;//findPosY(obj, evt.offsetY);// - 20;
	obj1.style.left = x;// - window.document.body.scrollLeft;
	obj1.style.top = y;
	obj1.className = "CurrentClipping";
	obj1.innerHTML = obj.getAttribute("message");
	if (obj1.innerHTML.length > 50) 
	{
		obj1.style.overflow = "hidden";
		obj1.style.width = "250px";
	}
	else obj1.style.width = "";
	obj1.style.display = "";
	this.onScreen = true;
}

function _alertClose()
{
	var oDiv = document.getElementById("alertMessage");
	if (oDiv) oDiv.style.display = "none";
	this.onScreen=false;
}

_TMPPromptObject = null;
function Prompt(obj, outputFunction){
	if(!this.drowed){drawDiv();this.drowed = true;}
	if(this.promptOnScreen)
	{
		this.promptOnScreen = false;
		return ;
	}
	this.promptOnScreen = true;
	_TMPPromptObject = obj;
	var obj1 = document.getElementById("promptMessage");
	var x = findPosX(obj);
	var y = findPosY(obj);
	obj1.style.left = x;
	obj1.style.top = y;
	obj1.innerHTML +=  "<img src='"+PathToResources+"Resources/img/promptOk.jpg' style='float:left;cursor:pointer' onclick='_TMPPromptObject.innerHTML = document.getElementById(\"promptMessage\").childNodes[0].value;document.getElementById(\"promptMessage\").style.display=\"none\";"+outputFunction+"(_TMPPromptObject.innerHTML);_TMPPromptObject = null;'>";
	obj1.childNodes[0].value = obj.innerHTML;
	obj1.style.display = "";
	//obj1.childNodes[0].focus();
	return "";
}
function findPosX(obj, evtX)
{
	var curleft = 0;
	if (obj.offsetParent)
	{
		if (evtX)
		{
			obj = obj.offsetParent;
			curleft = evtX;
		}
		while (obj.offsetParent)
		{
			curleft += obj.offsetLeft;
			obj = obj.offsetParent;
		}
	}
	else{ if (obj.x) curleft += obj.x;}
	return curleft;
}

function findPosY(obj, evtY)
{
	var curtop = (evtY) ? evtY : 0;
	if (obj.offsetParent)
	{
		while (obj.offsetParent)
		{
			curtop += obj.offsetTop;
			obj = obj.offsetParent;
		}
	}
	else{ if (obj.y) curtop += obj.y;}
	return curtop;
}





