
/*
 * Adds the trim function to a string.
 */
String.prototype.trim = function()
{
	var val = this.replace(/^\s*([\s\S]*\S+)\s*$|^\s*$/, "$1")
	if ( val == "$1" ) val = ""; // fix for mozilla
	return( val ); 

}



/*
	returns false and displays a message if 
	there is nothing selected in the selection box
*/
function hasSelected(form, fieldName, message)
{
		el = form.elements[fieldName];
		
		for (i = 0; i < el.options.length; i++){
			if (el.options[i].selected) return true;
		}

		// got this far without finding a selected one 
		// so throw a barny
		alert(message);
		el.focus();
		return false;
}
	

/*
 * returns false and displays message if
 * field of name fieldName does not contain
 * a minimum of 'length' characters.
 * length defaults to 0 if you leave it out.
 *
*/
function isRequired(form, fieldName, message, length)
{

		if (typeof length == 'undefined') length = 0;

		el = form.elements[fieldName];
		var val = el.value.trim();
		if ( val.length > length ){
			return true;
		}
		else
		{
			alert(message);
			el.focus();
			return false;
		}
}

/*
* Compares two fields in a form
* displays message if they aren't the same
* and returns false.
*
*/
function areSame(form, fieldName1, fieldName2, message)
{
	el1 = form.elements[fieldName1];
	el2 = form.elements[fieldName2];
	if (el1.value == el2.value) return true;
	else
	{
			alert(message);
			el2.focus();
			el2.select();
			return false;
	}

}


function isEmailAddress(form, fieldName, message)
{

	var result = false;

	var el = form.elements[fieldName];
	var theStr = el.value;

	var index = theStr.indexOf("@");
	if (index > 0)
	{
		var pindex = theStr.indexOf(".",index);
		if ((pindex > index+1) && (theStr.length > pindex+1))
			result = true;
	}

	if (result){
		return true;
	}else{
		alert(message);
		el.focus();
		el.select();
		return false;

	}

}


function maxFieldLength(form, fieldName, maxLen, message)
{
	var element = form.elements[fieldName];
	if (element.value.length > maxLen )
	{
		alert(message);
		// element.value = element.value.substring(0, (maxLen -1) );
		element.focus();
		return false;
	}
}

function sendToPopUp(itemID){
	var url = "SendTo.aspx?item_id=" + itemID;
	myWindow = window.open( url , "myWindow", "height=400, width=400, resizable=yes, scrollbars=yes");
	myWindow.focus();
	myWindow.location = url;
}
function popUp(URL) {
day = new Date();
id = day.getTime();
eval("page" + id + " = window.open(URL, '" + id + "', 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=600,height=400,left = 0,top = 0');");
}
function helpPopUp(topic){
	var url = "admin/help/default.htm#" + topic;
	myWindow = window.open( url , "myWindow", "height=600, width=400, resizable=yes, scrollbars=yes");
	myWindow.focus();
	myWindow.location = url;
}

function setPictureSize(value){
	maxWidth = value.substring(0, value.indexOf(":") );
	maxHeight = value.substring(value.indexOf(":") + 1, value.length  );
	mainForm.file_pic1_max_width.value = maxWidth;
	mainForm.file_pic1_max_height.value = maxHeight;
}

/* --------------------------------------------------------------------- */

function checkDate(form, fieldName){
	el = form.elements[fieldName];
	if ( isDate(el.value) != true ){
 		el.focus();
 		el.select();
		return false;
	}
	return true;
}

/**
	* DHTML date validation script.
	* Originally Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
	*/
// Declaring valid date character, minimum year and maximum year
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger(s){
	var i;
	for (i = 0; i < s.length; i++){
		// Check that current character is number.
		var c = s.charAt(i);
		if (((c < "0") || (c > "9"))) return false;
	}
	// All characters are numbers.
	return true;
}

function stripCharsInBag(s, bag){
	var i;
	var returnString = "";
	// Search through string's characters one by one.
	// If character is not in bag, append to returnString.
	for (i = 0; i < s.length; i++){
		var c = s.charAt(i);
		if (bag.indexOf(c) == -1) returnString += c;
	}
	return returnString;
}

function daysInFebruary (year){
	// February has 29 days in any year evenly divisible by four,
	// EXCEPT for centurial years which are not also divisible by 400.
	return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}

function DaysArray(n) {
	for (var i = 1; i <= n; i++) {
		this[i] = 31
		if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
		if (i==2) {this[i] = 29}
	}
	return this
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	var strDay=dtStr.substring(0,pos1)
	var strMonth=dtStr.substring(pos1+1,pos2)
	var strYear=dtStr.substring(pos2+1)
	strYr=strYear
	if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
	if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
	for (var i = 1; i <= 3; i++) {
		if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
	}
	month=parseInt(strMonth)
	day=parseInt(strDay)
	year=parseInt(strYr)
	if (pos1==-1 || pos2==-1){
		alert("The date format should be : dd/mm/yyyy")
		return false
	}
	if (strMonth.length<1 || month<1 || month>12){
		alert("Please enter a valid month")
		return false
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		alert("Please enter a valid day")
		return false
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
		alert("Please enter a valid date")
		return false
	}
return true
}

function launchCalendar(formName, inputName) {

    mywindow=open('calendar.html?form='+ formName + '&input=' + inputName,'myname','resizable=no,width=350,height=270');
    mywindow.focus();
    if (mywindow.opener == null) mywindow.opener = self;
}


	 function clearBox(box) 
	 {
	 	 if(box.value==box.defaultValue) 
		 {
	 	 	 box.value = "";
		 }
	 	 
	 }

