function e(id) {
	return document.getElementById(id);
}
function toggleNavigation(obj) {
	if (obj) {
		if (obj.className == "off") {
			obj.className = "on";
		}
		else {
			obj.className = "off";
		}
	}
}

function checkNavigation () {
	if (e(str_active_page)) {
		onRollOver( e(str_active_page) );
	}
}


function onRollOver(img_obj){
	img_obj.src = img_obj.src.split("-off").join("-on");
	//alert(img_obj.src);
}

function onRollOut(img_obj){
	img_obj.src = img_obj.src.split("-on").join("-off");	
}  

function refreshParent() {
  //window.opener.location.href = window.opener.location.href;
  window.opener.location.reload();

  if (window.opener.progressWindow)
		
 {
    window.opener.progressWindow.close()
  }
  window.close();
}



function LaunchFileWindow(destination, action, page_uid, comp_uid, bln_is_common) {
	var str = destination + "?action=" + action;
	
	if (page_uid) {
		str = str + "&page_uid=" + page_uid;
	}
	if (comp_uid) {
		str = str + "&comp_uid=" + comp_uid;
	}
	
	if (bln_is_common) {
		str = str + "&is_common=" + bln_is_common;
	}
	
	window.open(str, "admin", "width=600, height=500");
}


//-------------- IMAGE FADER --------------//

function Fader(container,time,frames) {
	this.container = container;
	this.time = time;
	this.num_frames = frames;
	this.start_frame = 0;
	this.previous_frame = 0;
	
	this.animationTimeout = null;
	this.framesPerSec = 20;
	this.transparencyStep = 100 / ((this.time / 4000) * this.framesPerSec);
	this.fadeUp = false;
	this.animateForward = true;
	this.currTransparency = 100;
	
	
	this.setTransparency = function (percentage) 
	{	
		var Fader = document.getElementById(this.container + this.start_frame);
	
		if(!Fader){return false;}
		if (Fader.filters != null) {
			if (typeof Fader.filters == "[object]") { //if IE6+
				Fader.filters[0].opacity = percentage;
			} else { //else if IE5.5-
				Fader.style.filter = "progid:DXImageTransform.Microsoft.Alpha(opacity="+percentage+")";
			}
		} else if (Fader.style.MozOpacity) {
			Fader.style.MozOpacity = percentage / 101;
		} else if (Fader.style.KhtmlOpacity) {
			Fader.style.KhtmlOpacity = percentage / 100;
		}
	}
	
	
	this.animateFader = function ()
	{	
		clearTimeout(this.animationTimeout);
		
		if (this.fadeUp == false) {
			this.currTransparency -= this.transparencyStep;			
			if (this.currTransparency <= 0) {
				this.fadeUp = true;
				this.currTransparency = 0;
			}
		} else {
			this.currTransparency += this.transparencyStep
			if (this.currTransparency >= 100) {
			  this.fadeUp = false;
				this.currTransparency = 100;
			}
		}
		this.setTransparency(this.currTransparency);
		
		if (this.currTransparency == 0) {
			this.goNextFrame();
		} 
		
		if (this.currTransparency == 100) {	
			var _self = this;
			
			if (this.num_frames > 1) {
				this.animationTimeout = setTimeout(function(){ _self.animateFader(); }, this.time);
			}
			
			return;
			
		} else {
			var _self = this;
			this.animationTimeout = setTimeout(function(){ _self.animateFader(); }, 1000 / this.framesPerSec);
		}
	}
  
	
	this.goNextFrame = function() {		
		if (document.getElementById(this.container + this.start_frame)) {
				document.getElementById(this.container + this.start_frame).className="hide";	
			}
		
		if  (this.start_frame != this.num_frames) {
			this.start_frame++;
			this.setTransparency(0);
			if (document.getElementById(this.container + this.start_frame)) {
				document.getElementById(this.container + this.start_frame).className="show";
			}
			
			this.setTransparency(0);
		}
		else {
			this.start_frame = 1;
			if (document.getElementById(this.container + this.start_frame)) {
				document.getElementById(this.container + this.start_frame).className="show";
			}
		}	
	}
	this.animateFader();
}

/*------------------- FORM VALIDATION -----------------------*/

function validate_form(obj,field_array)
{
	//alert('validating form');
	var field_len = field_array.length;
	for(var i=0;i<field_len;i++)
	{
		var field_info = field_array[i];
		var field_id = field_info[0];
		var field_title = field_info[1];
		var field_datatype = field_info[2];
		
		if(field_datatype=='email')
		{
			if(!testEmail(obj,field_id))
			{
				showEmailError(field_title);
				fieldFocus(obj, field_id);
				return false;
			}
		}else if(field_datatype=='string')
		{		
			field_minlength = field_info[3];
			if(!testString(obj,field_id,field_minlength))
			{
				showStringError(field_title, field_minlength);
				fieldFocus(obj, field_id);
				return false;
			}			
		}else if(field_datatype=='combo')
		{
			if(!testCombo(obj,field_id))
			{
				showComboError(field_title);
				fieldFocus(obj, field_id);
				return false;
			}			
		}else if(field_datatype=='checkbox')
		{
			if(!testCheckbox(obj,field_id))
			{
				showCheckboxError(field_title);
				fieldFocus(obj, field_id);
				return false;
			}			
		}	
	}
	return true;
}

function fieldFocus(obj, field)
{
	obj[field].focus();
}

function testString(obj,field,minLen)
{
	var val = obj[field].value;
	if(val.length < minLen)
	{
		return false;
	}else{
		return true;
	}
}

function testCombo(obj,field)
{
	var val = obj[field];
	
	if(val.selectedIndex > 0)
	{
		return true;
	}else{
		return false;
	}
}
function testEmail(obj,field)
{

	var val = obj[field].value;
	var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	if (filter.test(val))
	{
		return true;
	}else{
		return false;
	}
}

function testCheckbox(obj,field)
{
	var val = obj[field].value;
	if(!obj[field].checked)
	{
		return false;
	}else{
		return true;
	}
}

function showEmailError(field)
{
	alert("Invalid email in '" + field + "' field, please fix and re-submit.");
	return false;
}
function showStringError(field,minLen)
{
	alert("Invalid entry in '" + field + "' field, must be at least "+ minLen+" characters in length, please fix and re-submit.");
	return false;
}
function showComboError(field)
{
	alert("Please select item in '" + field + "'.");
	return false;
}

function showCheckboxError(field)
{
	alert("You must accept the terms for '" + field + "'.");
	return false;
}

function showError(field)
{
	alert("Invalid entry in '" + field + "' field, please fix and re-submit.");
	return false;
}
