/********************************************************/
/* Iradesso Javascript Functions                        */
/* Developed and standarized by:                        */
/* The Iradesso Web Team                                */
/*  - Marco Maranao                                     */
/*  - Ryan Quigley                                      */
/*  - Richard Hammond                                   */
/*                                                      */
/* Copyright 2003-2006. All rights reserved.            */
/********************************************************/

// image functions

function i_goToURL() { //v3.0
  var i, args=i_goToURL.arguments; document.i_returnValue = false;
  for (i=0; i<(args.length-1); i+=2) eval(args[i]+".location='"+args[i+1]+"'");
}

function i_swapImgRestore() { //v3.0
  var i,x,a=document.i_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function i_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.i_p) d.i_p=new Array();
    var i,j=d.i_p.length,a=i_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.i_p[j]=new Image; d.i_p[j++].src=a[i];}}
}

function i_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=i_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function i_swapImage() { //v3.0
  var i,j=0,x,a=i_swapImage.arguments; document.i_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=i_findObj(a[i]))!=null){document.i_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function i_swapImage2() { // marco maranao hack
	var i,j=0,x,a=i_swapImage2.arguments; document.i_sr2=new Array; for(i=0;i<(a.length-2);i+=3)
	 if ((x=i_findObj(a[i]))!=null){ document.i_sr2[j++]=x; x.src = a[i+2].replace('over', 'out'); if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

function i_swapImgRestore2() { // marco maranao hack
  var i,x,a=document.i_sr2; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

// number functions

function i_getRandomNumber(number) {
	day = new Date();
	seed = day.getTime();
	return parseInt(((seed - (parseInt(seed/1000,10) * 1000)) /10) /100 * number + 1,10);
}

// date functions

function i_chkY2k(number) {
	return (number < 1000) ? number + 1900 : number; 
}

function i_chkDate(month, day, year) {
	var unvaliddate = day + '/' + month + '/' + year;
	var date = new Date(year-0,month-1,day-0);
	var validdate = date.getDate() + '/' + (date.getMonth()+1) + '/' + i_chkY2k(date.getYear());
	
	if (unvaliddate != validdate) {
		return false;
	}
	return true;
}

function i_getValiddateUTC(month, day, year, hour, minute) {
	if (i_chkDate(month, day, year)) {
		var date = new Date(year-0,month-1,day-0,hour-0,minute-0,0);
		return Date.UTC(i_chkY2k(date.getYear()),date.getMonth(),date.getDate(),hour,minute,0);
	} else {
		return null;
	}
}

// string functions

function i_chkName(name) {
	if(!(name.match(/^[a-zA-Z0-9_-]+$/))) return false;
	return true;
}

function i_chkPassword(password, strong) {
	var space = " ";
	
	// no spaces allowed
	if (password.indexOf(space) != -1) {
		return false;
	}
	
	if(strong == true) {
		// it must start with at least one letter     
		if (!(password.match(/^[a-zA-Z]+/))) return false;
		// it must contain at least one number
		if (!(password.match(/\d/))) return false;
		// it must contain at least one uppercase letter
		if (!(password.match(/[A-Z]/))) return false;
		// it password contain at least one lowercase letter
		if (!(password.match(/[a-z]/))) return false;
		// it must contain at least one special character #,@,%,!
		if (!(password.match(/\W+/))) return false;
	}
	
	return true;
}

function i_chkHexidecimal(hexidecimal) {
	if(!(hexidecimal.match(/^([a-fA-F0-9]{6})$/))) return false;
	return true;
}

function i_chkEmail(email) {
	if(!(email.match(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})$/))) return false;
	return true;
}

function i_chkPhone(phone) {
	if(!(phone.match(/^([0-9_\-]{7,})+$/))) return false;
	return true;
}

function i_chkUrl(url) {
	if(!(url.match(/^https?:\/\/([\w\.]+)+(:\d+)?(\/(\w\/_\.]*(\?\S+)?)?)?$/))) return false;
	return true;
}

function i_chkNumber(number) {
	if(!(number.match(/^[\d]+$/))) return false;
	return true;
}

// window functions

function i_setStatusText(text) {
	window.status = text;
}

function i_returnStatusText() {
	window.status = '';	
}

function i_popUpWin(url, win, width, height, options, position) {
	var defaultPos = 20;
		
	if(position == "cs") {
		leftPos = (screen.width) ? (screen.width-width)/2 : defaultPos;
		topPos = (screen.height) ? (screen.height-height)/2 : defaultPos;
	} else if(position == "cw") {
		leftPos = (document.body.clientWidth) ? (document.body.clientWidth-width)/2 : defaultPos;
		topPos = (document.body.clientHeight) ? (document.body.clientHeight-height)/2 : defaultPos;
		leftPos += (window.screenLeft) ? window.screenLeft : window.screenX;
		if(leftPos.toString() == "NaN") {
			leftPos = (screen.width) ? (screen.width-width)/2 : defaultPos;			
		}
		topPos = (topPos < 100) ? 100 : topPos;
	} else {
		leftPos = defaultPos;
		topPos = defaultPos;
	}
	
	options += ',width=' + width + ',height=' + height + ',left=' + leftPos + ',top=' + topPos;
	newWin = window.open(url, win, options);
	newWin.focus();
	
	return newWin;
}

function i_openDialog(url, name, width, height, options, position) {
	name = (name != null) ? name : 'Dialog';
	width = (width != null) ? width : 550;
	height = (height != null) ? height : 350;
	options = (options != null) ? options : 'resizable=yes,status=yes';
	position = (position != null) ? position : 'cw';
	
	Dialog = i_popUpWin(url, name, width, height, options, position);
}

// message functions

function i_display_reqired(required) {
	var count = 0;
	var message = lang.getLang('i_functions_fieldsrequired') + "\n";
	for(var i=0; i<required.length; i++) {
		if(required[i] != "" || required[i] != null) {
			message = message + " - " + required[i] + "\n";
			count += 1;
		}
	}
	
	if(count > 0) {
		alert(message);
		return false;
	} else {
		return true;
	}
}


function i_display_error(errors) {
	var count = 0;
	var message = lang.getLang('i_functions_errorsoccured') + "\n";
	for(var i=0; i<errors.length; i++) {
		if(errors[i] != "" || errors[i] != null) {
			message = message + " - " + errors[i] + "\n";
			count += 1;
		}
	}
	
	if(count > 0) {
		alert(message);
		return false;
	} else {
		return true;
	}
}

// menu functions
iMenu.Registry = [];
iMenu.aniLen = 0;
iMenu.hideDelay = 500;
iMenu.minCPUResolution = 10;

// constructor
function iMenu(id, dir, left, top, width, height) {
	var agt = navigator.userAgent.toLowerCase();
	var is_major = parseInt(navigator.appVersion);
    var is_minor = parseFloat(navigator.appVersion);
	
	this.is_ie = ((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1));
	this.is_nav = ((agt.indexOf('mozilla')!=-1) && (agt.indexOf('spoofer')==-1) && (agt.indexOf('compatible') == -1) && (agt.indexOf('opera')==-1) && (agt.indexOf('webtv')==-1) && (agt.indexOf('hotjava')==-1));
	this.is_nav4 = (this.is_nav && (is_major == 4));

	this.is_dom = document.getElementById ? 1 : 0;
	
	if (this.is_ie || this.is_nav4 || this.is_dom) {
		this.id = id;
		this.dir = dir;
		this.orientation = dir == "left" || dir == "right" ? "h" : "v";
		this.dirType = dir == "right" || dir == "down" ? "-" : "+";
		this.dim = this.orientation == "h" ? width : height;
		this.hideTimer = false;
		this.aniTimer = false;
		this.open = false;
		this.over = false;
		this.startTime = 0;
		this.gRef = "iMenu_"+id;
		
		eval(this.gRef+"=this");
		
		iMenu.Registry[id] = this;
		var d = document;
		var strCSS = '<style type="text/css">';
			strCSS += '#' + this.id + 'Container { visibility:hidden; ';
			strCSS += 'left:' + left + 'px; ';
			strCSS += 'top:' + top + 'px; ';
			strCSS += 'overflow:hidden; z-index:10000; }';
			strCSS += '#' + this.id + 'Container, #' + this.id + 'Content { position:absolute; ';
			strCSS += 'width:' + width + 'px; ';
			strCSS += 'height:' + height + 'px; ';
			strCSS += 'clip:rect(0 ' + width + ' ' + height + ' 0); ';
			strCSS += '}';
			strCSS += '</style>';
		d.write(strCSS);
		this.load();
	}
}

iMenu.prototype.load = function() {
	var d = document;
	var layerId1 = this.id + "Container";
	var layerId2 = this.id + "Content";
	
	var obj1 = this.is_dom ? d.getElementById(layerId1) : this.is_ie ? d.all[layerId1] : d.layers[layerId1];
	if (obj1) var obj2 = this.is_nav4 ? obj1.layers[layerId2] : this.is_ie ? d.all[layerId2] : d.getElementById(layerId2);
	
	var temp;
	
	if (!obj1 || !obj2) {
		window.setTimeout(this.gRef + ".load()", 100);
	} else {
		this.container = obj1;
		this.menu = obj2;
		this.style = this.is_nav4 ? this.menu : this.menu.style;
		this.homePos = eval("0" + this.dirType + this.dim);
		this.outPos = 0;
		this.accelConst = (this.outPos - this.homePos) / iMenu.aniLen / iMenu.aniLen ;
		// set event handlers.
		if (this.is_nav4) this.menu.captureEvents(Event.MOUSEOVER | Event.MOUSEOUT);
		this.menu.onmouseover = new Function("iMenu.showMenu('" + this.id + "')");
		this.menu.onmouseout = new Function("iMenu.hideMenu('" + this.id + "')");
		//set initial state
		this.endSlide();
	}
}

iMenu.showMenu = function(id) {
	var reg = iMenu.Registry;
	var obj = iMenu.Registry[id];
	if (obj.container) {
		obj.over = true;
		for (menu in reg) if (id != menu) iMenu.hide(menu);
		if (obj.hideTimer) {
			reg[id].hideTimer = window.clearTimeout(reg[id].hideTimer);
		}
		if (!obj.open && !obj.aniTimer) reg[id].startSlide(true);
	}
}

iMenu.hideMenu = function(id) {
	var obj = iMenu.Registry[id]
	if (obj.container) {
		if (obj.hideTimer) window.clearTimeout(obj.hideTimer)
		obj.hideTimer = window.setTimeout("iMenu.hide('" + id + "')", iMenu.hideDelay);
	}
}

iMenu.hideAll = function() {
	var reg = iMenu.Registry;
	for (menu in reg) {
		iMenu.hide(menu);
		if (menu.hideTimer) window.clearTimeout(menu.hideTimer);
	}
}

iMenu.hide = function(id) {
	var obj = iMenu.Registry[id];
	obj.over = false;
	if (obj.hideTimer) window.clearTimeout(obj.hideTimer);
	obj.hideTimer = 0;
	if (obj.open && !obj.aniTimer) obj.startSlide(false);
}

iMenu.prototype.startSlide = function(open) {
	this[open ? "onactivate" : "ondeactivate"]();
	this.open = open;
	if (open) this.setVisibility(true);
	this.startTime = (new Date()).getTime() ;
	this.aniTimer = window.setInterval(this.gRef + ".slide()", iMenu.minCPUResolution);
}

iMenu.prototype.slide = function() {
	var elapsed = (new Date()).getTime() - this.startTime;
	if (elapsed > iMenu.aniLen) {
		this.endSlide();
	} else {
		var d = Math.round(Math.pow(iMenu.aniLen-elapsed, 2) * this.accelConst);
		if (this.open && this.dirType == "-") d = -d;
		else if (this.open && this.dirType == "+") d = -d;
		else if (!this.open && this.dirType == "-") d = -this.dim + d;
		else d = this.dim + d;
		this.moveTo(d);
	}
}

iMenu.prototype.endSlide = function() {
	this.aniTimer = window.clearTimeout(this.aniTimer);
	this.moveTo(this.open ? this.outPos : this.homePos);
	if (!this.open) this.setVisibility(false);
	if ((this.open && !this.over) || (!this.open && this.over)) {
		this.startSlide(this.over);
	}
}

iMenu.prototype.setVisibility = function(bShow) { 
	var s = this.is_nav4 ? this.container : this.container.style;
	s.visibility = bShow ? "visible" : "hidden";
}

iMenu.prototype.moveTo = function(p) { 
	this.style[this.orientation == "h" ? "left" : "top"] = this.is_nav4 ? p : p + "px";
}

iMenu.prototype.getPos = function(c) {
	return parseInt(this.style[c])
}

iMenu.prototype.onactivate = function() { }
iMenu.prototype.ondeactivate = function() { }

// this function repositions a menu to the speicified offset from center
function i_repositioniMenu(menu, offset) {
	// the new left position should be the center of the window + the offset
	var newLeft = i_getWindowWidth() / 2 + offset;
	menu.container.style ? menu.container.style.left = newLeft + "px" : menu.container.left = newLeft;
}

// this function calculates the window's width - different for IE and netscape
function i_getWindowWidth() {
	var width = document.body.clientWidth != null ? document.body.clientWidth : window.innerWidth;
	return (width < 780 ) ? 780 : width;
}

var Menu1 = new iMenu("operations", "down", -1000, 267, 155, 200);
Menu1.onactivate = function() { i_repositioniMenu(Menu1, -160); }