﻿/*
*******************************************************************************
Program:				JavaScript Functions Library
Version:				1.0
Date:					January 2002
Author:					Lucas Georgiou
Description:			Functions Library for common tasks
*******************************************************************************

COMMON FUNCTIONS

LIST:

Browser Auto Detection
writePageSign				Write Footer Sign of Pages
checkEmail					CHECK EMAIL Function
goTo						FORM OPTION Navigation Function
URLEncodeSpace				Replace 'empty spaces' of URLs
URLEncodeAmp				Replace '&' character of URLs
popUp						Pop-Up Window Function
popUpWindow					Pop-Up Window Advanced Function
swapImage					Swap Image Function
showHideLayer				Show - Hide Layers Function
printDate					Prints the current date and time in the following format: 'Friday 10.12.2002 12:30:23'.
getParameter				Analyzes the full document url, extracts the parameters part of the string and then searches for the value of the param
addZero						Adds a "0" prefix
addToFavorites				Add To Favorites Function
setToHome					Set current page to Home Page
setToHomeFunction			Set current page to Home Page Function

*/



//********************************************************* Browser Detecting

var browser;
var newWin;
var myURL;

browser = 'NS4'; 

if (document.layers) {
	browser = 'NS4';	
}
else if (document.getElementById && !document.all) {
	browser = 'NS6';	
}
else if (document.all && navigator.userAgent.indexOf("Opera")>-1){
	browser = 'Opera';	
}
else if (document.all) {
	browser = 'IE';	
}
else {
	browser = 'Other';	
}

function _detectBrowser() {
    var detect = navigator.userAgent.toLowerCase();
    var OS, browser2, version, total, thestring;

    if (checkIt('konqueror')) {
        browser2 = "Konqueror";
        OS = "Linux";
    }
    else if (checkIt('safari')) browser2 = "Safari"
    else if (checkIt('omniweb')) browser2 = "OmniWeb"
    else if (checkIt('opera')) browser2 = "Opera"
    else if (checkIt('webtv')) browser2 = "WebTV";
    else if (checkIt('icab')) browser2 = "iCab"
    else if (checkIt('msie')) browser2 = "Internet Explorer"
    else if (!checkIt('compatible')) {
        browser2 = "Netscape Navigator"
        version = detect.charAt(8);
        document.write('<br /><br /><br /><br /><br /><br /><br /><br /><br />')
    }
    else browser2 = "An unknown browser";

    if (!version) version = detect.charAt(place + thestring.length);

    if (!OS) {
        if (checkIt('linux')) OS = "Linux";
        else if (checkIt('x11')) OS = "Unix";
        else if (checkIt('mac')) OS = "Mac"
        else if (checkIt('win')) OS = "Windows"
        else OS = "an unknown operating system";
    }
}

function checkIt(string) {
    place = detect.indexOf(string) + 1;
    thestring = string;
    return place;
}

//********************************************************* Write Footer Sign of Pages
function writePageSign() {

	var sign;
		
	sign = '&nbsp;<br>' +
		   '<div align="center">' +
		   '<hr size="1" width="400">' +
		   '<span class="FontDarkBold"><a href="http://www.syscom.gr" target="_blank">Created and Powered By Syscom S.A.</a></span><br>' +
		   '<span class="FontFooter">Copyright © 1997-2003 Syscom S.A., All Rights Reserved</span>' +
		   '</div><br>';
		   
    document.write(sign);
    
}    
    


//********************************************************* CHECK EMAIL Function
function checkEmail(thisEmail,msg1,msg2){
	var strEmail = thisEmail;
	
	if(strEmail<=1){
		alert(msg1);
		return false;
	}
	
	if(strEmail.indexOf("@")<1 || strEmail.indexOf(".")<0){
		alert(msg2);
		return false;
	}
	else {
		return true;
	}
}



//********************************************************* FORM OPTION Navigation Function

function goTo(item, page){
    var url;
    url = page + item[item.selectedIndex].value;
    if (url.length > 0 ) window.location.href = url;	
}



//********************************************************* Replace 'empty spaces' of URLs

function URLEncodeSpace(strExpression,newChar){
	var output,rExp,replacement;
	output = strExpression;
	
	if(strExpression.length>0){
		strExpression = new String(strExpression);
		rExp = / /gi;
		replacement = new String (newChar);
		output = strExpression.replace(rExp,replacement);	
	}
	
	return output;
}



//********************************************************* Replace '&' character of URLs

function URLEncodeAmp(strExpression,newChar){
	var output,rExp,replacement;
	output = strExpression;
	
	if(strExpression.length>0){
		strExpression = new String(strExpression);
		rExp = /&/gi;
		replacement = new String (newChar);
		output = strExpression.replace(rExp,replacement);	
	}
	
	return output;
}



//********************************************************* Pop-Up Window Function

function popUp(page,w,h,s,content){
   var thisWin;
   wWidth = window.screen.width;
   wHeight = window.screen.height;
   pWidth = w;
   pHeight = h;
   pScroll = s;
   xPos =  parseInt((wWidth-pWidth)/2);
   yPos =  parseInt((wHeight-pHeight)/2);
   winProp = "top="+yPos+",left="+xPos+",width="+pWidth+",height="+pHeight+",scrollbars="+pScroll+",resizable=yes"; 
         
   if(page.length > 0){	
		thisWin = window.open(page,"newWin",winProp);
		thisWin.focus();
   }	
   else if (content.length > 0) {
		thisWin = window.open("","newWin",winProp);
		thisWin.document.open();		
		thisWin.document.write(content);
		thisWin.document.close();	
		thisWin.focus();
   }
}



//********************************************************* Pop-Up Window Advanced Function
/* 
Arguments:
	page: Page's relative path of popup Window
	x: The x position of popup Window
	y: The y position of popup Window
	w: The width of popup Window
	h: The height of popup Window
	scroll: The popup Window will be has scrollbars. Values: 'yes', 'no'.
	resize: The popup Window will be resizable. Values: 'yes', 'no'.
	content: HTML code of popup Window instead of loading another page	
Usage examples:
	<a href="javascript:popUpWindow('page.htm',20,20,100,200,'yes','yes','')">Link</a>
	<a href="javascript:popUpWindow('',20,20,100,200,'no','no','<p>Hello World</p>')">Link</a>	
*/

function popUpWindow(page,x,y,w,h,scroll,resize,content){
   
   var pWidth,pHeight,xPos,yPos,scrollThis,resizeThis;
  
   pWidth = w;
   pHeight = h;
   xPos =  x;
   yPos =  y;
   scrollThis = scroll;
   resizeThis = resize;
   winProp = "";
   
   if (xPos > 0 && yPos > 0) winProp = "top="+yPos+",left="+xPos+",";
   
   winProp = winProp + "width="+pWidth+",height="+pHeight+",scrollbars="+scrollThis+",resizable="+resizeThis; 
   
	if(typeof(newWin)=='object') {
		if(!newWin.closed && browser != 'Opera'){
			newWin.close();				
		}
	}
         
	if(page.length > 0){	
		newWin = window.open(page,"newWindow",winProp);
		newWin.focus();
	}	
	else if (content.length > 0) {
		newWin = window.open("","newWindow",winProp);
		newWin.document.open();		
		newWin.document.write(content);
		newWin.document.close();	
		newWin.focus();
	}
}



//********************************************************* Swap Image Function
/* 
Arguments
	imgName: value of "name" and "id" attribute in "img" tag
	imgPath: relative path of new image file
	imgLayer: if the target image is part of a layer, the layers's ID 
Note:
	"name" and "id" attributes for each "img" tag must have the same value 
Usage examples:
	onMouseOver="swapImage('imageName','newImageRelativePath','');"
	onMouseOver="swapImage('imageName','newImageReletivePath','LayerID');"
*/  

function swapImage(imgName, imgPath, imgLayer) {
	if (imgLayer.length <= 0) {						// Without Layers
		document.images[imgName].src = imgPath;	
	}
	else {											// With Layers
		if (browser == 'NS4') {
			document.layers[imgLayer].document.images[imgName].src = imgPath;
		}
		else if (browser == 'NS6') {
			document.getElementById(imgName).src = imgPath;
		}
		else {
			document.images[imgName].src = imgPath;
		}	
	}  
}  
 


//********************************************************* Show - Hide Layers Function
/* 
Arguments:
	layerID: value of "id" attribute in "div" tag
	action: Show or Hide a layer. Values: '', 'show', 'hide'.
Usage examples:
	onMouseOver="showHideLayer('LayerID','show');"
	onMouseOver="showHideLayer('LayerID','hide');"	
	onClick="showHideLayer('LayerID','');"	
*/
 
function showHideLayer(layerID,action) {
	
	var thisLayer;
	var thisAction;
	var visible;
	var hidden;
	
	if (browser == 'NS4') {
		thisLayer = document.layers[layerID];
		visible = 'show';
		hidden = 'hide';
	}
	else if (browser == 'NS6'){
		thisLayer = document.getElementById(layerID).style;
		visible = 'visible';
		hidden = 'hidden';
	}
	else if (browser == 'Opera') {
		thisLayer = document.all(layerID).style;
		visible = 'VISIBLE';
		hidden = 'HIDDEN';
	}
	else {
		thisLayer = document.all(layerID).style;
		visible = 'visible';
		hidden = 'hidden';				
	}
	
	if (action == '') {
		if (thisLayer.visibility == visible) {
			thisLayer.visibility = hidden;
		}
		else {
			thisLayer.visibility = visible;
		}
	}
	
	if (action == "show") {
		thisLayer.visibility = visible;		
	}
	else if (action == "hide") {
		thisLayer.visibility = hidden;
	}
	
} 



function printDate() {
	//EFFECTS:		Prints the current date and time in the following format:
	//				'Friday 10.12.2002 12:30:23'.
		
	var day = new Array('Κυριακή', 'Δευτέρα', 'Τρίτη', 'Τετάρτη', 'Πέμπτη', 'Παρασκευή', 'Σάββατο');
	//var month = new Array('Ιανουάριος', 'Φεβρουάριος', 'Μάρτιος', 'Απρίλιος', 'Μάϊος', 'Ιούνιος', 'Ιούλιος', 'Αύγουστος', 'Σεπτέμβριος', 'Οκτώβριος', 'Νοέμβριος', 'Δεκέμβριος');
	var today = new Date();
	var theDate = "";
			
			
	theDate = day[today.getDay()] + ' ' + 
			  addZero(today.getDate()) + '.' +
			  addZero((today.getMonth()) + 1) + '.' +
			  today.getFullYear() + '&nbsp;&nbsp;' + 
			  addZero(today.getHours()) + ':' +
			  addZero(today.getMinutes()); 
			  //today.getSeconds();			
								
	document.write(theDate);

}



function getParameter(param) {
	//REQUIRES:		All the name - value pairs have the valid parameters format
	//EFFECTS:		Analyzes the full document url, extracts the parameters
	//				part of the string and then searches for the value of 
	//				the param
	
	var i = 0, x = 0;
	var url = document.URL;
	var qPos = url.indexOf('?');
	var aPos = 0;
	var query = '';	
	var chr = '';
	var pair = '';
	var parameter = '';
	var value = '';

	
	if (qPos > -1) {
		query = url.substring(qPos + 1, url.length);
		//document.write(query + '<br>');
		for (i = 0; i < query.length; i++) {
			chr = query.charAt(i);
			if (chr == '&' || i == query.length - 1) {
				if (i == query.length - 1)
					pair += query.charAt(i);				
				if (pair.length > 0) {
					//document.write(pair + '<br>');
					aPos = pair.indexOf('=');
					parameter = pair.substring(0, aPos);
					//document.write(parameter + '<br>');
					value = pair.substring(aPos + 1, pair.length);	
					//document.write(value + '<br>');	
					if (parameter == param)
						return value;	
					pair = '';
					parameter = '';
					value = '';			
				}				
			}
			else {
				pair += chr;		
			}	
		}
	}
	
	return value;

}



function addZero(num) {

	out = parseInt(num);
	
	if (out <= 9) {
		out = '0' + out;
	}

	return out;

}



//********************************************************* Add To Favorites Function
function addToFavorites(lang) {
	
	if (browser == "IE") {		
				
		var myIcon1, myIcon2, myIcon3;
		var fmsg;
		
		myURL = location.href;
		myIcon1 = new Image(); myIcon1.src = "favicon.ico";
		myIcon2 = new Image(); myIcon2.src = "/favicon.ico";
		myIcon3 = new Image(); myIcon3.src = "../favicon.ico";
		
		if (lang == "gr") fmsg = 'Προσθήκη στα Favorites';
		else fmsg = 'Add to Favorites';
		
		if(myURL.indexOf("\"") > -1 || myURL.indexOf("\'") > -1) {
			myURL = myURL.substring(0, myURL.indexOf("?"));
		}
	
		document.write('<a href="javascript:window.external.AddFavorite(myURL, document.title);" title="' + fmsg + '">');
		document.write('Favorites:');
		document.write('<img src="../apsrv_images/common/favorites.gif" border="0" width="16" height="14" alt="' + fmsg + '">');
		document.write('</a>');
	}
	
}



//********************************************************* Set current page to Home Page
function setToHome(lang) {
	
	if (browser == "IE") {		
				
		var hmsg;
		
		myURL = location.href;
		
		if (lang == "gr") hmsg = 'Θέλω να την κάνω \'Αρχική Σελίδα\' όταν ανοίγω τον browser';
		else hmsg = 'Set this pages as \'Home Page\'';
		
		if(myURL.indexOf("\"") > -1 || myURL.indexOf("\'") > -1) {
			myURL = myURL.substring(0, myURL.indexOf("?"));
		}
							
		document.write('<a href="javascript:" style="cursor:hand;" onClick="setToHomeFunction(this, myURL);" title="' + hmsg + '">');
		document.write('Home&nbsp;Page:');
		document.write('<img src="../apsrv_images/common/home.gif" border="0" width="14" height="14" alt="' + hmsg + '">');
		document.write('</a>');
	}
	
}



//********************************************************* Set current page to Home Page Function
function setToHomeFunction(obj, theURL) {
	
	obj.style.behavior='url(#default#homepage)';
	obj.setHomePage(theURL);
	
}


