//----------------------------------------------------------------------//
// common_lib:-  a library of commonly used javascript functions        //
//                                                                      //
// Written by :  Michael Stephens                                       //
//               Copyright (c) 2003, iDezign Technology                 //
//----------------------------------------------------------------------//

// javascript to insert todays date formatted
function todays_date() {
	var dayName = new Array ("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday")
	var monName = new Array ("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "Decmember")
	var now = new Date
	var day = dayName[now.getDay()]
	var date = new String(now.getDate())
	var month = monName[now.getMonth()]
	var year = now.getYear()
	if ( year < 1900 ) {
		year += 1900
	}
	if ( date.substring(date.length - 1) == "1" && date != "11" ) {
		date += "st"
	}
	else if ( date.substring(date.length - 1) == "2" && date != "12" ) {
		date += "nd"
	}
	else if ( date.substring(date.length - 1) == "3" && date != "13" ) {
		date += "rd"
	}
	else {
		date += "th"
	}
	return(day+", "+date+" "+month+", "+year)
}

// function to jump to a new URL
function jumpPage(newLoc) {
	newPage = newLoc.options[newLoc.selectedIndex].value
	if (newPage != "") {
		window.location.href = newPage
	}
}

// validate forms
function rm_trail_space(str_val) {
	var str_len = str_val.length;
	var i;
	
	for ( i=str_len; i>=0; i-- ) {
		if ( str_val.substr(i,1) == " " ) {
			str_val = str_val.substr(0,str_len - i)
		}
	}
	return(str_val)
}
function shortname(str_val) {
	var str_len = str_val.length;
	var i;
	
	for ( i=str_len; i>=0; i-- ) {
		if ( str_val.substr(i,1) == "_" ) {
			str_val = str_val.substr(0,i) + " " + str_val.substr(i+1)
		}
		if ( str_val.substr(i,1) == "-" ) {
			str_val = str_val.substr(i+1)
		}
	}
	str_val = str_val.substr(0,1).toUpperCase() + str_val.substr(1)
	return(str_val)
}
function validForm(form) {
	var ret_val=true;
	var i;
	var radio_name;
	var rad_check = false;
	
	for ( i=0; i<form.length; i++ ) {
		var tempobj=form.elements[i];	

//alert("Type = '"+tempobj.type+"'  Name = '"+tempobj.name+"'")

		if ( tempobj.name && (tempobj.name.indexOf("required-") >= 0 || tempobj.id.indexOf("required-") >= 0) && rm_trail_space(tempobj.value) == "" ) {
			alert("Please enter the \'"+shortname(tempobj.name)+"\' field before submitting!")
			ret_val = false
			break
		}
		if ( tempobj.type == "radio" ) {
			rad_check = false;
			radio_name = tempobj.name
			for ( i; i<=form.length; i++ ) {
				tempobj=form.elements[i];
				if ( tempobj.name && tempobj.name == radio_name ) {
					if ( tempobj.checked ) {
						rad_check = true
					}
				}
				else {
					--i
					break
				}
			}
			if ( !rad_check ) {
				alert("Please enter the \'"+shortname(radio_name)+"\' field before submitting!")
				ret_val = false
				break
			}
		}
	}
	return(ret_val)
}
function openWindow(file, windowWidth, windowHeight, winOptions, returnref)
{
	leftPos      = 0
	topPos       = 0

	if ( screen ) {
		leftPos = ((screen.width/2)-(windowWidth/2));
		topPos  = ((screen.height/2)-(windowHeight/2));
	}

	noticewin = window.open(file, 'newWin', 'width='+windowWidth+',height='+windowHeight+',left='+leftPos+',top='+topPos+','+winOptions)
	
	if ( returnref ) {
		return(noticewin);
	}
}

// image fading functions follow (useful for slideshows, etc)
function opacity(id, opacStart, opacEnd, millisec) {
	//speed for each frame
	var speed = Math.round(millisec / 100);
	var timer = 0;

	//determine the direction for the blending, if start and end are the same nothing happens
	if(opacStart > opacEnd) {
		for(i = opacStart; i >= opacEnd; i--) {
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	} else if(opacStart < opacEnd) {
		for(i = opacStart; i <= opacEnd; i++)
			{
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	}
}

//change the opacity for different browsers
function changeOpac(opacity, id) {
	var object = document.getElementById(id).style; 
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
}

function shiftOpacity(id, millisec) {
	//if an element is invisible, make it visible, else make it ivisible
	if(document.getElementById(id).style.opacity == 0) {
		opacity(id, 0, 100, millisec);
	} else {
		opacity(id, 100, 0, millisec);
	}
}

function blendimage(divid, imageid, imagefile, millisec) {
	var speed = Math.round(millisec / 100);
	var timer = 0;
	
	//set the current image as background
	document.getElementById(divid).style.backgroundImage = "url(" + document.getElementById(imageid).src + ")";
	
	//make image transparent
	changeOpac(0, imageid);
	
	//make new image
	document.getElementById(imageid).src = imagefile;

	//fade in image
	for(i = 0; i <= 100; i++) {
		setTimeout("changeOpac(" + i + ",'" + imageid + "')",(timer * speed));
		timer++;
	}
}

function currentOpac(id, opacEnd, millisec) {
	//standard opacity is 100
	var currentOpac = 100;
	
	//if the element has an opacity set, get it
	if(document.getElementById(id).style.opacity < 100) {
		currentOpac = document.getElementById(id).style.opacity * 100;
	}

	//call for the function that changes the opacity
	opacity(id, currentOpac, opacEnd, millisec)
}
