function GetObject(id){
	
	if (document.getElementById)
		return document.getElementById (id);
	else if(document.all)
		return document.all[id];
	else
		return null;
}

function trim(_handler){
	while(''+_handler.charAt(_handler.length-1)==' '){
		_handler=_handler.substring(0,_handler.length-1);
	}
	return _handler
}

function PageIsValid()
{
	if(typeof(MultiligualInstances) != 'undefined'){
		for(i=0 ; i < MultiligualInstances.length ; i++){
			if(!Validate(MultiligualInstances[i]))
				return false;
		}
	}
	
	if(typeof(CustomValidation) != 'undefined'){
		var SubmitForm = true;
		for(i=0 ; i < CustomValidation.length ; i++){
			
			var ControlsArray = CustomValidation[i];
			var Control = GetObject(ControlsArray[0]);
			
			if(!Control)
				return true;
			
			RemoveStar(Control);
			
			var Type = ControlsArray[2];
			var Require = ControlsArray[1];
			
			if(Control.tagName == "SELECT"){
				if(Require && Control.options[0].selected)	{
					SubmitForm = false;
					MakeStar(Control);
				}
			}
			else{
				if(Require && trim(Control.value) == ''){
					SubmitForm = false;
					MakeStar(Control);
				}
				ret = Validator(Control.value,Type);
				if( ret == null){
					SubmitForm = false;
					MakeStar(Control);
				}
			}
		}
		
		if(!SubmitForm){
			return false;
		}
	}
	return true;
}

function cm(command,validate,selector,confirmMsg)
{
	if(validate)
	{
		if(!PageIsValid())
			return;
	}
	
	if(selector)
	{
		if(!ValidateSelectorCheck())
			return;
	}
	
	if(confirmMsg != "" && !confirm(confirmMsg))
		return;
	
	GetObject("Command").value = command;
	GetObject("__PageForm").submit();
}

function __KeyDown(command,validate)
{
	if ( event.keyCode == 13 )
	{
		event.returnValue = false;
		event.cancel = true;
		cm(command,validate,0);
	}
}

MakeStar = function(control){
	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 ValidateSelectorCheck() {
	myOption = -1;
	if(__PageForm.__Record)
	{
		if(__PageForm.__Record.length)
		{
			for (i=0; i<__PageForm.__Record.length; i++) {
				if (__PageForm.__Record[i].checked) {
					myOption = i;
				}
			}
		}
		else if (__PageForm.__Record.checked) 
		{
			myOption = 1;
		}
	}
	if (myOption == -1) {
		alert(recordSelect);
		return false;
	}
	return true;
}


function Validator(op, dataType) {
	function Properties(){
		this.decimalchar = ","; // For Greece
		this.groupchar = "."; // For Greece
		this.digits = "2";
		this.dateorder = "ymd";//dmy
		this.century = "2000";
		this.cutoffyear = "2029";
	}

	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();
	}
}


/*Upload Control*/

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 += "height=300,width=500,";
	argument += "top=" + "0" + ",";
	argument += "left=" + (screen.width - 550) + "";
	
var CurrentFileObject;
function GetFile(obj,url)
{
	CurrentFileObject = GetObject(obj);
	window.open(url,'window',argument,true);
}

function PreviewFile(obj)
{
	CurrentFileObject = GetObject(obj);
	if(CurrentFileObject && CurrentFileObject.value != "")
	{
		window.open(CurrentFileObject.value,'_preview',argument,true);
	}
}


function SetUrl(filepath)
{
	CurrentFileObject.value = filepath;
	CurrentFileObject = null;
}


function high(which2){
	theobject=which2;
	highlighting=setInterval("highlightit(theobject)",50);
}
function low(which2){
	clearInterval(highlighting);
	if (which2.style.MozOpacity)
		which2.style.MozOpacity=0.3;
	else if (which2.filters)
		which2.filters.alpha.opacity=60;
}

function highlightit(cur2){
	if (cur2.style.MozOpacity<1)
		cur2.style.MozOpacity=parseFloat(cur2.style.MozOpacity)+0.1;
	else if (cur2.filters&&cur2.filters.alpha.opacity<100)
		cur2.filters.alpha.opacity+=10;
	else if (window.highlighting)
		clearInterval(highlighting);
}

function LoadImage(img)
{
	var Control = GetObject("Image_Load");
	if(Control)
		Control.src = img;
}

function OpenPrint(PrintObj,LeaveLinks)
{
	var PrintWindow = window.open("PagePrint","PagePrint","status=yes,toolbar=no,menubar=yes,location=no"); 
	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;
}

