// mouse over image source
function imageOver(imName,path) {
	if (document.images) {
    	document.images[imName].src = path + imName + "_on.gif";
    }
}

// mouse out image source
function imageOut(imName,path) {
	if (document.images) {
    	document.images[imName].src = path + imName + "_off.gif";
    }
}

// mouse click image source
function imageClick(imName,path) {
	if (document.images) {
    	document.images[imName].src = path + imName + "_off.gif"; 
    }
}



function imageStateController() {
	
	this.images = new Array();
	
	this.onSuffix = '_on.gif';
	
	this.offSuffix = '_off.gif';
	
	this.clickSuffix = '_on.gif';
	
	this.setOnSuffix = setOnSuffix;
	
	this.setOffSuffix = setOffSuffix;
	
	this.setClickSuffix = setClickSuffix;
	
	this.imageOver = attachedImageOver;
	
	this.imageOut = attachedImageOut;
	
	this.imageClick = attachedImageClick;
	
	this.addImage = addImage;
	
	this.getImageSource = getImageSource;
	
	this.switchState = switchState;
}

function switchState(eventId,eventType) {
	
	if (eventType == 'mouseover') {
	
		this.imageOver(eventId);
		
	} else if (eventType == 'mouseout') {
	
		this.imageOut(eventId);
		
	} else if (eventType == 'click') {
	
		this.imageClick(eventId);
	}
}


function attachedImageOver(eventId) {

	var eventObject = getDocumentObject(eventId);
	
	//alert(this.onSuffix);
	
	eventObject.src = this.getImageSource(eventId) + "_on.gif";
}

function attachedImageOut(eventId) {

	var eventObject = getDocumentObject(eventId);
	
	eventObject.src = this.getImageSource(eventId) + "_off.gif";
}

function attachedImageClick(eventId) {

	var eventObject = getDocumentObject(eventId);
	
	eventObject.src = this.getImageSource(eventId) + "_on.gif";
}

function addImage(imageId,imageSource) {

	this.images[imageId] = imageSource;
}

function getImageSource(imageId) {
	
	var src = this.images[imageId];
	
	return src;
}

function setOnSuffix(onSuffix) {

	this.onSuffix = onSuffix;
}

function setOffSuffix(offSuffix) {
	
	this.offSuffix = offSuffix;
}

function setClickSuffix(clickSuffix) {

	this.clickSuffix = clickSuffix;
}


function bgController() {
	
	this.overBGColor = '#dddddd';
	
	this.outBGColor = "#669933";
	
	this.clickBGColor = "#dddddd";
	
	this.mouseover = objectMouseover;
	
	this.mouseout = objectMouseout;
	
	this.mouseclick = objectMouseclick;
	
	this.switchBGState = switchBGState;
	
	this.setOverBGColor;
	
	this.setOutBGColor;
	
	this.setClickBGColor;
}

function objectMouseover(eventId) {

	var eventObject = getDocumentObject(eventId);
	
	eventObject.style.backgroundColor = this.overBGColor;
}

function objectMouseout(eventId) {

	var eventObject = getDocumentObject(eventId);
	
	eventObject.style.backgroundColor = this.outBGColor;
}

function objectMouseclick(eventId) {

	var eventObject = getDocumentObject(eventId);
	
	eventObject.style.backgroundColor = this.clickBGColor;
}

function switchBGState(eventId,eventType) {

	if (eventType == 'mouseover') {
	
		this.mouseover(eventId);
		
	} else if (eventType == 'mouseout') {
	
		this.mouseout(eventId);
		
	} else if (eventType == 'click') {
	
		this.mouseclick(eventId);
	}
}


function changeFormColour(name,colour) {
	document.forms[0].elements[name].style.backgroundColor = colour;
}


// universal element grabber - IE4+/NN6/Moz
function getDocumentObject(id) {
	if (document.getElementById) {
		var object = document.getElementById(id);
		return object;
	}
	else if (document.all) {
		var object = document.all[id];
		return object;
	}
}


function getTestAlert(id) {

	alert('Testing: ' + id);
}



function getEnclosedDocumentObject(enclosedDocument,id) {
	
	if (enclosedDocument.document.getElementById) {
		var object = enclosedDocument.document.getElementById(id);
		return object;
	}
	else if (enclosedDocument.document.all) {
		var object = enclosedDocument.document.all[id];
		return object;
	}
	
	//alert(id);
}

function domCompatible() {

	if (document.getElementById || document.all) {
		return true;
	} else {
		return false;
	}
}


function validate_email(elementName) {

	var warning = '';
	var emailFormElement = document.forms[0].elements[elementName];
	
    if (-1 == emailFormElement.value.indexOf("@")) { 
       warning = "Your email must have a '@'."; 
       return warning; 
    }
    
    if (-1 != emailFormElement.value.indexOf(",")) { 
       warning = "Your email must not have a ',' in it"; 
       return warning; 
    }
    
    if (-1 != emailFormElement.value.indexOf("#")) { 
       warning = "Your email must not have an '#' in it."; 
       return warning; 
    }
    
    if (-1 != emailFormElement.value.indexOf("!")) { 
       warning = "Your email must not have a '!' in it."; 
       return warning; 
    }
    
    if(-1 != emailFormElement.value.indexOf(" ")) { 
       warning = "Your email must not have a space in it."; 
       return warning; 
    }
    
    if(emailFormElement.value.length == (emailFormElement.value.indexOf("@")+1) ) { 
      warning = "Your email must have a domain name after the '@'."; 
      return warning; 
    }
    
    return warning;
}


function closeWindow() {
	window.close();
}


function openPhotoWindow(photoName) {
						
	var photoWindow = window.open('/rinpoche/photo_gallery/view_picture/' + photoName + '/',
									'rinpochePhoto',"width=800,height=600,status=yes,resizable=yes,scrollbars=yes");

	if (window.focus) {
	
		photoWindow.focus();
	}


	return false;
}


// event listener
function addEvent(object, eventType, func, useCapture) {

	if (object.addEventListener) {

		object.addEventListener(eventType, func, useCapture);

		return true;

	} else if (object.attachEvent) {

		var r = object.attachEvent("on" + eventType, func);

		return r;

	} else { // mac ie 5 event handler
		
		// filters out older browsers to prevent errors
		
		if (window.event) {
			//alert('adding: ' + eventType);
			eval('object.on' + eventType + ' = ' + func);
		}
		
		/*
		eval('var oldEventHandler = object.on' + eventType);
		
		if (typeof oldEventHandler != 'function') {
			//alert('no func');
			eval('object.on' + eventType + ' = ' + func)
			//objectEventHandler = func;
			
		} else {
			//alert('found func');
			eval('object.on' + eventType + ' = function() {oldEventHandler();func();  }');
			//window.onload = function() {oldEventHandler();func();}
		}
		*/
	}

}


function removeSubmit(id) {
	
	if (document.getElementById) {
	
		var submitCell = getDocumentObject(id);
		
		if (submitCell.childNodes.length != 0) {
		
			var submitButton = submitCell.lastChild;
			
			submitCell.removeChild(submitButton);
		}
		
		var message = document.createElement('p');
		
		var bold = document.createElement('b');
		
		var text = document.createTextNode('Processing Application...');
		
		bold.appendChild(text);
		
		message.appendChild(bold);
		
		submitCell.appendChild(message);
	
	} else if (document.all) {
		
		var submitCell = document.all[id];
		
		submitCell.innerHTML = '<p><b>Processing Application...</b></p>';
	}
}




function setFocus(elementId) {

	var element = getDocumentObject(elementId);
	
	element.focus();
}




function showWarning(id,warning) {

	var warningElement = getDocumentObject(id);
	
	if (warningElement.childNodes.length != 0) {
		
		var lastChild = warningElement.lastChild;
		
		// loops through children, deleting them all
		// this removes any warnings placed here previously
		for (i=0;i < warningElement.childNodes.length;i++) {
		
			warningElement.removeChild(warningElement.childNodes[i]);
		}
		
		var para = document.createElement('p');
		
		para.className = 'warning';
		
		var text = document.createTextNode(warning);
		
		para.appendChild(text);
		
		warningElement.appendChild(para);
		
		if (arguments[2] == 'append') {

			warningElement.appendChild(lastChild);
		}
	}
}




// returns the id of the source element of an event
function getEventId(evt) {
	
	// assign the event if using IE
	if ((evt == null) && window.event) {
	
		var evt = window.event
	}
	
	// get the element's id that triggered the event
	if (evt.target) {
	
		var tagId = evt.target.id;		// netscape
	
	} else if (evt.srcElement) {
	
		var tagId = evt.srcElement.id;	// ie4+
	}
	
	return tagId;
}


function showStatus(message) {

	window.status = message;
	
	return true;
}


function isArray(object) {

	if (!window.Array) { // js1.0 has no Arrays

		return false;

	} else {

		return true;
	}
}


function getUrlVars()
{
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
 
    for(var i = 0; i < hashes.length; i++)
    {
        hash = hashes[i].split('=');
        vars.push(hash[0]);
        vars[hash[0]] = hash[1];
    }
 
    return vars;
}