/*
 * ============================================================================
 * CORE
 * ============================================================================
 * 
 * --- GizmoManager -----------------------------------------------------------
 */

GizmoManager.prototype.mouseDownListeners = Array();
GizmoManager.prototype.mouseUpListeners = Array();
GizmoManager.prototype.mouseMoveListeners = Array();
GizmoManager.prototype.mouseOverListeners = Array();
GizmoManager.prototype.mouseOutListeners = Array();
GizmoManager.prototype.topLayer = 0;

GizmoManager.prototype.gizmos = Array();
GizmoManager.prototype.moveables = Array();

GizmoManager.prototype.loadEvents = new Array();

// Constructor
function GizmoManager(){
	
	var self = this;
	document.onmousedown = function(e){ self.mouseDown(e); }
	document.onmouseup = function(e){ self.mouseUp(e); }
	document.onmousemove = function(e){ self.mouseMove(e); }
	document.onmouseover = function(e){ self.mouseOver(e); }
	document.onmouseout = function(e){ self.mouseOut(e); }
	//this.addLoadEvent(function(){ self.init() });
	//var self = this;
	var self = this;
	//window.onload = function(){ self.init(); }
	var func = function(){ self.init() };
	
	if(window.addEventListener){ window.addEventListener("load", func, false); }
	else{
		if(window.attachEvent){ window.attachEvent("onload", func); }
		else{ 
			if(document.getElementById){
				var oldonload = window.onload; 
				if(typeof window.onload != 'function'){ window.onload = func; }
				else{
					window.onload = function(){ 
						if(oldonload){ oldonload(); }
						func();
					}
				}
			}
		}
	}

}

GizmoManager.prototype.init = function(){

	// Set up gizmos
	var sliderGizmo_elements = gizmoUtil.getElementsByClass("slidergizmo");
	for(var i = 0; i < sliderGizmo_elements.length; i++){
		var element = sliderGizmo_elements[i];
		this.gizmos.push(new SliderGizmo(element));
	}

	var tabpaneGizmo_elements = gizmoUtil.getElementsByClass("tabpanegizmo");
	for(var i = 0; i < tabpaneGizmo_elements.length; i++){
		var element = tabpaneGizmo_elements[i];
		this.gizmos.push(new TabPaneGizmo(element));
	}

	
	// Build fundementals
	var moveables = gizmoUtil.getElementsByClass("moveable");
	for(var i = 0; i < moveables.length; i++){
		var element = moveables[i];
		this.moveables.push(new Moveable(element));
	}
	
	this.topLayer = this.getTopLayer();
	
	for(var i = 0; i < this.loadEvents.length; i++){
		var func = this.loadEvents[i];
		func();
	}
}

GizmoManager.prototype.getGizmo = function(id){
	for(var i = 0; i < this.gizmos.length; i++){
		if(this.gizmos[i].id == id) return this.gizmos[i];
	}
	return null;
}

GizmoManager.prototype.mouseDown = function(e){ this.notifyListeners(this.mouseDownListeners, gizmoUtil.getEventTarget(e), e); }
GizmoManager.prototype.mouseUp = function(e){ this.notifyListeners(this.mouseUpListeners, gizmoUtil.getEventTarget(e), e); }
GizmoManager.prototype.mouseMove = function(e){
	if(this.currentEement){ if(this.currentElement != this.getEventTarget(e)) this.lastElement = this.currentElement; }
	else{ this.lastElement = document; }
	this.currentElement = gizmoUtil.getEventTarget(e);	
	this.notifyListeners(this.mouseMoveListeners, gizmoUtil.getEventTarget(e), e);
}
GizmoManager.prototype.mouseOver = function(e){ this.notifyListeners(this.mouseOverListeners, gizmoUtil.getEventTarget(e), e); }
GizmoManager.prototype.mouseOut = function(e){ this.notifyListeners(this.mouseOutListeners, gizmoUtil.getEventTarget(e), e); }

GizmoManager.prototype.notifyListeners = function(listeners, element, e){
	for(var i = 0; i < listeners.length; i++){
		var listener = listeners[i];
		listener(e)
	}
}

GizmoManager.prototype.registerMouseDownListener = function(listener){ this.mouseDownListeners.push(listener); }
GizmoManager.prototype.registerMouseUpListener = function(listener){ this.mouseUpListeners.push(listener); }
GizmoManager.prototype.registerMouseMoveListener = function(listener){ this.mouseMoveListeners.push(listener); }
GizmoManager.prototype.registerMouseOverListener = function(listener){ this.mouseOverListeners.push(listener); }
GizmoManager.prototype.registerMouseOutListener = function(listener){ this.mouseOutListeners.push(listener); }
GizmoManager.prototype.addLoadEvent = function(func){
	/*
	if(window.addEventListener){ window.addEventListener("load", func, false); }
	else{
		if(window.attachEvent){ window.attachEvent("onload", func); }
		else{ 
			if(document.getElementById){
				var oldonload = window.onload; 
				if(typeof window.onload != 'function'){ window.onload = func; }
				else{
					window.onload = function(){ 
						if(oldonload){ oldonload(); }
						func();
					}
				}
			}
		}
	}
	*/
	
	this.loadEvents.push(func);
}


GizmoManager.prototype.getTopLayer = function(){
	var all_elements = document.getElementsByTagName("*");
	var topZ = 0;
	for(var i = 0; i < all_elements.length; i++){
		element = all_elements[i];
		topZ = Math.max(element.style.zIndex, topZ);
	}
	return topZ++;
}

/*
 * --- GizmoEffects -----------------------------------------------------------
 */
function GizmoEffects(){}
GizmoEffects.prototype.setOpacity = function(opacity, elem) {
	var object = elem.style;
	if(object != undefined){
		object.opacity = (opacity / 100);
		object.MozOpacity = (opacity / 100);
		object.KhtmlOpacity = (opacity / 100);
		object.filter = "alpha(opacity=" + opacity + ")";
	}
}

GizmoEffects.prototype.toggleDisplay = function(elem_id, hidden_message, visible_message, hidden_initially){

	var elem = document.getElementById(elem_id);
	var ancs = gizmoUtil.getElementsByClass(elem_id + "_anchor");
	
	if(elem.style.display == ''){
		if(hidden_initially){
			elem.style.display = 'block'; 
			//if(anc != undefined){ anc.innerHTML = visible_message; }
			gizmoUtil.setInnerHTML(ancs, visible_message);
		}
		else{ elem.style.display = 'none'; 
			//if(anc != undefined){ anc.innerHTML = hidden_message; }
			gizmoUtil.setInnerHTML(ancs, hidden_message);
		}
	}
	else if(elem.style.display == 'none'){ 
		elem.style.display = 'block'; 
		//if(anc != undefined){ anc.innerHTML = visible_message; }
		gizmoUtil.setInnerHTML(ancs, visible_message);
	}
	else{ 
		elem.style.display = 'none'; 
		//if(anc != undefined){ anc.innerHTML = hidden_message; }
		gizmoUtil.setInnerHTML(ancs, hidden_message);
	}
}

GizmoEffects.prototype.highlight = function(element_id, 
											highlight_r, highlight_g, highlight_b, 
											background_r, background_g, background_b){

	// Get the element
	var elem = document.getElementById(element_id);

	// Setup background
	var status;
	if(elem.status != 1){ 
		elem.status = 1;
		elem.level = 100;
	}
	
	// Clear timeout if exists
	if(elem.anim){ clearTimeout(elem.anim); }
   
	// calculate levels
	var r = gizmoUtil.parseHex(background_r + (highlight_r - background_r) * (elem.level / 100));
	var g = gizmoUtil.parseHex(background_g + (highlight_g - background_g) * (elem.level / 100));
	var b = gizmoUtil.parseHex(background_b + (highlight_b - background_b) * (elem.level / 100));

	elem.style.background = "#" + r + g + b;
	
	if(elem.level <= 0){
		elem.status = 0;
	}
	else{
		// decrement level
		elem.level = elem.level - 10;
		
		// Set timeout
		var self = this;
		elem.anim = setTimeout(function(){ self.highlight(element_id, highlight_r, highlight_g, highlight_b, background_r, background_g, background_b); }, 10);
	}
}

GizmoEffects.prototype.reveal = function(id, final_height, message_a, message_b, closedinitially, speed){

   // Get the element
   var elem = document.getElementById(id);
   var anc = document.getElementById(id+"anchor");

   // Setup heights
   var status;
   if(closedinitially){ if(elem.status != 0 && elem.status != 1 && elem.status != -1){ elem.style.height = 0; } }
   else{ if(elem.status != 0 && elem.status != 1 && elem.status != -1){ elem.style.height = final_height + 'px'; } }
   
   // Clear timeout if exists
   if(elem.movement){ clearTimeout(elem.movement); }

   // Get current height
   var height = parseInt(elem.style.height);

   // Get status
   if(elem.status == 0 || elem.status == 1){ status = elem.status; }
   else{
      if(height == 0){
         status = 1;
         elem.status = status;
      }
      else{
         status = 0;
         elem.status = status;
      }
   }

   // Opening
   if(status == 1){

      // If the final height has been reached and we are not just starting
      if(height == final_height) {
      	if(anc != undefined){ anc.innerHTML = message_b; }
         elem.status = -1;
         return;
      }
      var dist = Math.ceil( (final_height - height) / speed);
      height = height + dist;
      elem.style.height = height + "px";
   }

   // Closing
   else{
      // If the final height has been reached and we are not just starting
      if (height == 0){
         if(anc != undefined){ anc.innerHTML = message_a; }
         elem.status = -1;
         return;
      }
      var dist = Math.ceil(height / speed);
      height = height - dist;
      elem.style.height = height + "px";
   }

   // Set timeout
   var self = this;
   elem.movement = setTimeout(function(){ self.reveal(id, final_height, message_a, message_b, closedinitially, speed); }, 1);
}

/*
 * --- GizmoUtil --------------------------------------------------------------
 */
function GizmoUtil(){}
GizmoUtil.prototype.getX = function(e){
	if(e){ return e.clientX; }
	return event.clientX;
}
GizmoUtil.prototype.getY = function(e){
	if(e){ return e.clientY; }
	return event.clientY;
}
GizmoUtil.prototype.getEventTarget = function(e){
	if(e){ return e.target; }
	else{ return event.srcElement; }
}
GizmoUtil.prototype.getElementsByClass = function(searchClass,node,tag) {
	var classElements = new Array();
	if(node == null) node = document;
    if(tag == null) tag = '*';
	var els = node.getElementsByTagName(tag);
	//var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
	for(i = 0, j = 0; i < els.length; i++){
	//	if(pattern.test(els[i].className)){
	if(this.stringContains(els[i].className, searchClass)){
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}
GizmoUtil.prototype.stringContains = function(str, search){
	var pattern = new RegExp('(^|\\s)'+search+'(\\s|$)');
	return (pattern.test(str));
}
GizmoUtil.prototype.elementHeight = function(id){ var elem = document.getElementById(id); return elem.offsetHeight; }
GizmoUtil.prototype.elementWidth = function(id){ var elem = document.getElementById(id); return elem.offsetWidth; }
GizmoUtil.prototype.windowHeight = function(){
	if(typeof window.innerWidth != 'undefined') { return window.innerHeight; }
	else{
		if(document.documentElement && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0){ return document.documentElement.clientHeight; }
		else{
			if(document.body && typeof document.body.clientWidth != 'undefined') { return document.body.clientHeight; }
		}
	}
}
GizmoUtil.prototype.contains = function(container, element){
	if(container == element){ return true; }
	if(container.childNodes){
	for(var i = 0; i < container.childNodes.length; i++){
		if(element == container.childNodes[i] || this.contains(container.childNodes[i], element)){ return true; }
	}
	}
	return false;
}
GizmoUtil.prototype.roundNumber = function(num, dec) {
	var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
	return result;
}

GizmoUtil.prototype.parseHex = function(num){
	var lookup = "0123456789ABCDEF"
	var one = Math.floor(num/16)
	var two = num % 16
	var hex = lookup.charAt(one) + lookup.charAt(two)
	return hex;
}

GizmoUtil.prototype.setInnerHTML = function(collection, value){
	for(var i = 0; i < collection.length; i++){
		var element = collection[i];
		element.innerHTML = value;
	}
}

/*
 * ============================================================================
 * GIZMOS
 * ============================================================================
 * 
 * --- SliderGizmo ------------------------------------------------------------
 */

function Observable(){}
Observable.prototype.registerListener = function(listener){ this.listeners.push(listener); }
Observable.prototype.notifyListeners = function(e){
	for(var i = 0; i < this.listeners.length; i++){
		var listener = this.listeners[i];
		listener.notify(e)
	}
}

function SliderGizmo(element){
	this.id = element.id;
	// set up marker
	var marker = document.getElementById(this.id + "marker");
	marker.className += " moveable x_axis confined";
	marker.style.position = "absolute";
	// TODO inital from attributes?
	marker.style.top = 0;
	//marker.style.width = "5px";
	marker.style.left = "0";
	
	var self = this;
	marker.onmove = function(e){
		self.relativePosition = this.style.left;
		self.position = parseInt(this.style.left) / (self.getWidth() - gizmoUtil.elementWidth(this.id)) * 100;
		self.notifyListeners(e);
	}
}
SliderGizmo.prototype = new Observable();
SliderGizmo.prototype.listeners = new Array();
SliderGizmo.prototype.getWidth = function(){
	return parseInt(gizmoUtil.elementWidth(this.id));
}
SliderGizmo.prototype.getPosition = function(){ return this.position; }
SliderGizmo.prototype.setPosition = function(position){ 
	var marker = document.getElementById(this.id + "marker");
	marker.style.left = ((this.getWidth() - gizmoUtil.elementWidth(marker.id)) * position) + "px";
}
SliderGizmo.prototype.getRelativePosition = function(){ return this.relativePosition; }
SliderGizmo.prototype.setRelativePosition = function(relativeposition){
	var marker = document.getElementById(this.id + "marker");
	marker.style.left = relativeposition + "px";
}

 /* 
 * --- TabPaneGizmo -----------------------------------------------------------
 */
function TabPaneGizmo(tabpane){
	var tab_initiators = gizmoUtil.getElementsByClass("tab_initiator", tabpane);
   
	for(var i = 0; i < tab_initiators.length; i++){
		var tab_initiator = tab_initiators[i];
		var self = this;
		tab_initiator.onmousedown = function(e){ 
			var id = this.id;
			if(!id){
				this.id = getIdFromClass(this.className);
			}
			self.show_tab(this.id, tabpane, e);
		}
	}
}

function getIdFromClass(className){
	var classNames = className.split(" ");
	for(var i = 0; i < classNames.length; i++){
		if(classNames[i].substring(0,3) == "id_") return classNames[i].substring(3,classNames[i].length);
	}
	return null;
}

TabPaneGizmo.prototype = new Observable;
TabPaneGizmo.prototype.listeners = new Array();
TabPaneGizmo.prototype.show_tab = function(tab_initiator_id, tabpane, e) {
	// Turn off all tabs
	var tabs = gizmoUtil.getElementsByClass("tab", tabpane);
	for(var i = 0; i < tabs.length; i++){
		var tab = tabs[i];
		tab.style.display = "none";
	}

	// Get the required tab and turn it on
	var tab = document.getElementById(tab_initiator_id + "-content");
	tab.style.display = "block";

	// set all tabs initiators to not current
	var tab_initiators = gizmoUtil.getElementsByClass("tab_initiator", tabpane);
	for(var i = 0; i < tab_initiators.length; i++){
		var tab_initiator = tab_initiators[i];
		tab_initiator.className = tab_initiator.className.replace(/ current/, "");
	}

	var tab_initiator = document.getElementById(tab_initiator_id);
	tab_initiator.className = tab_initiator.className + " current";
	
	this.notifyListeners(e);
}

/*
function FloatingWindowGimzo(element){
	
	this.divMoving = false;
	this.window = element;
	this.window.style.cursor = "move";
	this.background = document.createElement("div");
	this.background.id = this.window.id + "_background";
	this.background.style.position = "absolute";
	this.background.style.top = this.window.offsetTop + "px";
	this.background.style.left = this.window.offsetLeft + "px";
	this.background.style.width = this.window.offsetWidth + "px";
	this.background.style.height = + this.window.offsetHeight + "px";
	this.background.style.background = "#000000";
	this.background.style.cursor = "move";
	this.setOpacity(25, this.background);
	document.body.appendChild(this.background);

	this.setOpacity(25, this.window);
	
	this.window.style.zIndex = gizmoManager.topLayer + 1;
	this.background.style.zIndex = gizmoManager.topLayer;
		
	var self = this;
	// Down
	gizmoManager.registerMouseDownListener(function(e){ 
							var eventSrc = gizmoManager.getEventTarget(e);
							if(eventSrc == self.background || eventSrc == self.window){
								self.selectDiv(e);
							}
							var elements = self.window.childNodes;
							for(var i = 0; i < elements.length; i++){
								var element = elements[i];
								if(eventSrc == element) self.selectDiv(e);
							}
					       } );
	// Move
	gizmoManager.registerMouseMoveListener(function(e){ self.moveDiv(e); } );
	// Up
	gizmoManager.registerMouseUpListener(function(e){
							var eventSrc = gizmoManager.getEventTarget(e);
							if(eventSrc == self.background || eventSrc == self.window){
								self.placeDiv(e);
							}
							var elements = self.window.childNodes;
							for(var i = 0; i < elements.length; i++){
								var element = elements[i];
								if(eventSrc == element) self.placeDiv(e);
							}
					       } );
	// Out
	gizmoManager.registerMouseOutListener(function(e){ 
							var eventSrc = gizmoManager.getEventTarget(e);
							if(eventSrc == self.background || eventSrc == self.window){
								self.revertDiv(e);
							}
							var elements = self.window.childNodes;
							for(var i = 0; i < elements.length; i++){
								var element = elements[i];
								if(eventSrc == element) self.revertDiv(e);
							}
					       } );
	// Over
	gizmoManager.registerMouseOverListener(function(e){ 
							var eventSrc = gizmoManager.getEventTarget(e);
							if(eventSrc == self.background || eventSrc == self.window){
								self.highlightDiv(e);
							}
							var elements = self.window.childNodes;
							for(var i = 0; i < elements.length; i++){
								var element = elements[i];
								if(eventSrc == element) self.highlightDiv(e);
							}
					       } );
}

FloatingWindowGimzo.prototype.selectDiv = function selectDiv(e){
	this.startXmouse = this.getX(e);
	this.startYmouse = this.getY(e);
	this.divMoving = true;
	this.startX = parseInt(this.window.style.left);
	this.startY = parseInt(this.window.style.top);
	this.window.style.zIndex =  parseInt(this.window.style.zIndex) + 3;
	this.background.style.zIndex = parseInt(this.background.style.zIndex) + 3;
}

FloatingWindowGimzo.prototype.moveDiv = function(e){
	if(this.divMoving){
		this.background.style.left = this.startX + this.getX(e) - this.startXmouse + "px";
		this.background.style.top = this.startY + this.getY(e) - this.startYmouse + "px";
		this.window.style.left = this.startX + this.getX(e) - this.startXmouse + "px";
		this.window.style.top = this.startY + this.getY(e) - this.startYmouse + "px";
	}
}

FloatingWindowGimzo.prototype.placeDiv = function(e){
	if(this.divMoving){ 
		this.divMoving = false;
		this.window.style.zIndex = parseInt(this.window.style.zIndex) - 3;
		this.background.style.zIndex = parseInt(this.background.style.zIndex) - 3;
	}
gizmoManager.debug("Window: " + this.window.style.zIndex + ", Background: " + this.background.style.zIndex);
}

FloatingWindowGimzo.prototype.highlightDiv = function(e){
	this.setOpacity(50, this.background);
	this.setOpacity(100, this.window);
}
FloatingWindowGimzo.prototype.revertDiv = function(e){
	if(!this.mouseOver(e)){
		this.setOpacity(25, this.background);
		this.setOpacity(25, this.window);
	}
}

FloatingWindowGimzo.prototype.setOpacity = function(opacity, elem) {
	var object = elem.style;
	if(object != undefined){
		object.opacity = (opacity / 100);
		object.MozOpacity = (opacity / 100);
		object.KhtmlOpacity = (opacity / 100);
		object.filter = "alpha(opacity=" + opacity + ")";
	}
}

FloatingWindowGimzo.prototype.mouseOver = function(e){
	return (this.getX(e) >= parseInt(this.window.style.left) + 2 &&
	        this.getX(e) < (parseInt(this.window.style.left) + parseInt(this.window.style.width)) &&
	        this.getY(e) >= parseInt(this.window.style.top) + 2 &&
	        this.getY(e) < (parseInt(this.window.style.top) + parseInt(this.window.style.height)));
}
FloatingWindowGimzo.prototype.getX = function(e){
	if(e){ return e.clientX; }
	return event.clientX;
}

FloatingWindowGimzo.prototype.getY = function(e){
	if(e){ return e.clientY; }
	return event.clientY;
}

*/

/*
 * ============================================================================
 * FUNDAMENTALS
 * ============================================================================
 * 
 * --- Moveable ---------------------------------------------------------------
 * 
 * class: moveable
 * attributes: x_axis, the element is moveable along the x axis
 * 	           y_axis, the element is moveable along the y axis
 *             confined, the element is contained within it's parent container
 *             TODO initiator_X, X is the id of the initiating component
 */
function Moveable(element){
	
	this.element = element;
		
	// Detect parameters
	var className = element.className;
	this.x_axis = gizmoUtil.stringContains(className, "x_axis");
	this.y_axis = gizmoUtil.stringContains(className, "y_axis");
	
	if(gizmoUtil.stringContains(className, "confined")){
		var parent = element.parentNode;
		this.y_limit = gizmoUtil.elementHeight(parent.id) - gizmoUtil.elementHeight(this.element.id);
		this.x_limit = gizmoUtil.elementWidth(parent.id) - gizmoUtil.elementWidth(this.element.id);
	}
	
	this.moving = false;
		
	var self = this;
	// Down
	gizmoManager.registerMouseDownListener(function(e){
											   if(gizmoUtil.getEventTarget(e).id == self.element.id){
												   self.startXmouse = gizmoUtil.getX(e);
												   self.startYmouse = gizmoUtil.getY(e);
												   self.moving = true;
												   self.startX = parseInt(self.element.style.left);
												   self.startY = parseInt(self.element.style.top);										   
											   }
										   }
										  );
	// Move
	gizmoManager.registerMouseMoveListener(function(e){
										   	   if(self.moving){
												   if(self.x_axis){
												   	   var resultant_x = self.startX + gizmoUtil.getX(e) - self.startXmouse;
												   	   if(!self.x_limit){ self.element.style.left = resultant_x + "px"; }
												   	   else{
												   	       if(resultant_x < self.x_limit && resultant_x > 0){ self.element.style.left = resultant_x + "px"; }
												   	       else if(resultant_x <= 0){ self.element.style.left = "0"; }
												   	       else{ self.element.style.left = self.x_limit + "px"; }
												   	   }												   	   
												   }
												   if(self.y_axis){
												   	   var resultant_y = self.startY + gizmoUtil.getY(e) - self.startYmouse;
												   	   if(!self.y_limit){ self.element.style.top = resultant_y + "px"; }
												   	   else{
												   	       if(resultant_y < self.y_limit && resultant_y > 0){ self.element.style.top = resultant_y + "px"; }
												   	       else if(resultant_y <= 0){ self.element.style.top = "0"; }
												   	       else{ self.element.style.top = self.y_limit + "px"; }
												   	   }												   	   
												   }
												   //gizmoManager.debug(self.element.style.left);
												   if(self.element.onmove) self.element.onmove(e);
										   	   }
										   }
										  );
	// Up
	gizmoManager.registerMouseUpListener(function(e){
											 if(self.moving){ 
												 self.moving = false;
												 self.element.style.zIndex = parseInt(self.element.style.zIndex) - 3;
											 }
										 }
										);
}

Moveable.prototype.moving = null;
Moveable.prototype.element = null;
Moveable.prototype.startXmouse = null
Moveable.prototype.startYmouse = null
Moveable.prototype.startX = null
Moveable.prototype.startY = null

// Resizable
// Hide-able?

GizmoManager.prototype.debug = function(message){
	var debug = document.getElementById("debug");
	debug.innerHTML = debug.innerHTML + message + "<br />";
}

gizmoManager = new GizmoManager();
gizmoEffects = new GizmoEffects();
gizmoUtil = new GizmoUtil();



/**********************************************************
 * Validate contact forms                                 *
 **********************************************************/
function validateString(field, msg, min, max) {
	if (!min) { min = 1 }
	if (!max) { max = 65535 }
	if (!field.value || field.value.length < min || field.value.max > max) {
		alert(msg);
		field.focus();
		field.select();
		return false;
	}
	return true;
}

function validateEmail(email, msg, optional) {

	if(!email.value && optional) return true;

	var re_mail = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z])+$/;

	if(!re_mail.test(email.value)){
		alert(msg);
		email.focus();
		email.select();
		return false;
	}

	return true;
}

function validateEmailsSame(emaila, emailb, msg){
	if(emaila.value!=emailb.value){
		   alert(msg);
		   emailb.focus();
		   emailb.select();
		   return false;
	} 
	return true;
}

