function chgImg(imgField,newImg)
{
	if (document.images)
	{
		document[imgField].src= eval(newImg + ".src")
	}
}

function openWin(lnk, nm, wid, hi, scrll)
{
	var win = window.open(lnk, nm, "width=" + wid + ",height=" + hi + ",left=10,top=10,toolbar=no,location=no,menubar=no,directories=no,status=no,scrollbars=" + scrll + ",resizable=yes");
}

// this function opens a new window, and submits the form on the page to a jsp processor. In use on BuildMyRide.jsp

function openWin_SubmitForm(lnk, nm, wid, hi, scrll, frm)
{
	//var frmName, frmAddress, frmCity, frmState, frmZip;
	//var frmName2, frmAddress2, frmCity2, frmState2, frmZip2;

	var frmNameVal = document.forms[frm].elements['name'].value;
	var frmAddressVal = document.forms[frm].elements['address'].value;
	var frmCityVal = document.forms[frm].elements['city'].value;
	var frmStateVal = document.forms[frm].elements['state'].value;
	var frmZipVal = document.forms[frm].elements['zip'].value;

	var frmName2Val = document.forms[frm].elements['name2'].value;
	var frmAddress2Val = document.forms[frm].elements['address2'].value;
	var frmCity2Val = document.forms[frm].elements['city2'].value;
	var frmState2Val = document.forms[frm].elements['state2'].value;
	var frmZip2Val = document.forms[frm].elements['zip2'].value;

	var actionStr = "ChangeSessionVars.jsp?name=" + frmNameVal + "&address=" + frmAddressVal + "&city=" + frmCityVal + "&state=" + frmStateVal + "&zip=" + frmZipVal + "name2=" + frmName2Val + "&address2=" + frmAddress2Val + "&city2=" + frmCity2Val + "&state2=" + frmState2Val + "&zip2=" + frmZip2Val;

	document.forms[frm].action = actionStr;
	document.forms[frm].submit();
	openWin(lnk, nm, wid, hi, scrll);

	actionStr = "";
}



/**
 * DHTML date validation script. 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 daysInMonth(i)
{
var result = 31;

    if (i==4 || i==6 || i==9 || i==11)
    {
        result = 30
    }
    if (i==2)
    {
        result = 29
    }
    return result
}

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)
{
	
	if (document.HDForm.CalForm.value == "de")
		{
		alerttxt = "Bitte tragen Sie ein gültiges Datum ein."
		}
	if (document.HDForm.CalForm.value == "nl")
		{
		alerttxt = "Voer een geldige datum in."
		}
	if (document.HDForm.CalForm.value == "fr")
		{
		alerttxt = "Veuillez entrer une date valide."
		}
	if (document.HDForm.CalForm.value == "ca")
		{
		alerttxt = "Veuillez entrer une date valide."
		}
	if (document.HDForm.CalForm.value == "be")
		{
		alerttxt = "Veuillez entrer une date valide."
		}
	if (document.HDForm.CalForm.value == "it")
		{
		alerttxt = "Digitare una data valida."
		}
	if (document.HDForm.CalForm.value == "es")
		{
		alerttxt = "Por favor, introduzca una fecha válida."
		}
	if (document.HDForm.CalForm.value == "en")
		{
		alerttxt = "Please enter a valid date."
		}
	
	dtStr = dtStr.replace (/\s/gi, "");	// remove all spaces from the string
	
	if (dtStr.length == 0)
		return true		// allow an empty string

	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	if (pos1==-1 || pos2==-1)
	{
		
			alert(alerttxt);
			return false;
	}

	//var daysInMonth = DaysArray(12)
	
	if(document.HDForm.CalIntel.value == 1)
		{
		strMonth=dtStr.substring(0,pos1)
		strDay=dtStr.substring(pos1+1,pos2)
		strYear=dtStr.substring(pos2+1)
		}
	else
		{
		strDay=dtStr.substring(0,pos1)
		strMonth=dtStr.substring(pos1+1,pos2)
		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 (strMonth.length < 1 || month < 1 || month > 12)
	{
		alert(alerttxt)
		return false
	}
	if ((strDay.length < 1) || (day < 1) || (day > 31) || ((month==2) && (day > daysInFebruary(year))) || (day > daysInMonth(month)))
	{
		alert(alerttxt)
		return false
	}

	if (strYear.length != 4 || year == 0 || year < minYear || year > maxYear)
	{
		alert(alerttxt)
		return false
	}

	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false)
	{
		alert(alerttxt)
		return false
	}

	return true
}

function isDateAccepted(dtToStr, dtFromStr)
{
	if ((dtToStr.length < 1) || (dtFromStr.length < 1))
	{
		return true;
	}
	
	//alert("inside isDateAccepted");
	var pos1To=dtToStr.indexOf(dtCh)
	//alert(pos1To);
	var pos2To=dtToStr.indexOf(dtCh,pos1To+1)
	//alert(pos2To);
	var strMonthTo=dtToStr.substring(0,pos1To)
	//alert (strMonthTo);
	var strDayTo=dtToStr.substring(pos1To+1,pos2To)
	//alert(strDayTo);
	var strYearTo=dtToStr.substring(pos2To+1)
	//alert(strYearTo);

	if (strDayTo.charAt(0)=="0" && strDayTo.length>1)
		strDayTo=strDayTo.substring(1)

	if (strMonthTo.charAt(0)=="0" && strMonthTo.length>1)
		strMonthTo=strMonthTo.substring(1)

	var monthTo=parseInt(strMonthTo)
	var dayTo=parseInt(strDayTo)
	var yearTo=parseInt(strYearTo)

	//alert(monthTo);
	//alert(dayTo);
	//alert(yearTo);

	// the date FROM section
	var pos1From=dtFromStr.indexOf(dtCh)
	//alert(pos1From);
	var pos2From=dtFromStr.indexOf(dtCh,pos1From+1)
	//alert(pos2From);
	var strMonthFrom=dtFromStr.substring(0,pos1From)
	//alert(strMonthFrom);
	var strDayFrom=dtFromStr.substring(pos1From+1,pos2From)
	//alert(strDayFrom);
	var strYearFrom=dtFromStr.substring(pos2From+1)
	//alert(strYearFrom);

	if (strDayFrom.charAt(0)=="0" && strDayFrom.length>1)
		strDayFrom=strDayFrom.substring(1)

	if (strMonthFrom.charAt(0)=="0" && strMonthFrom.length>1)
		strMonthFrom=strMonthFrom.substring(1)

	var monthFrom=parseInt(strMonthFrom)
	var dayFrom=parseInt(strDayFrom)
	var yearFrom=parseInt(strYearFrom)

	//alert(monthFrom);
	//alert(dayFrom);
	//alert(yearFrom);

	// end of the date FROM section

	if (yearTo > yearFrom)
	{
		// done & happy
	}
	else if (yearTo < yearFrom)
	{
		alert("Your Beginning Date cannot be after your End Date");
		return false;
	}
	else	// same year, need to test month
	{
		if (monthTo > monthFrom)
		{
			// done & happy
		}
		else if (monthTo < monthFrom)
		{
			alert("Your Beginning Date cannot be after your End Date");
			return false;
		}
		else
		{
			if (dayTo >= dayFrom)
			{
				// done & happy
			}
			else
			{
				alert("Your Beginning Date cannot be after your End Date");
				return false;
			}
		}
	}
	
	return true;
}


/****************************************************************
pop-up window functionality
*****************************************************************/

window.onerror=null;
var win = null;

function NewWindow(mypage, myname, w, h, scroll) {
closePopWin();

var winl = (screen.width - w) / 2;
var wint = (screen.height - h) / 2;
winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',noresize';
if( mypage.indexOf("bmLocale") < 0 ) {
    if( mypage.indexOf("?") < 0 ) {
      mypage = mypage + "?";
    }
    else {
      mypage = mypage + "&";
    }
    mypage = mypage + "bmLocale=en_US";
}

win = window.open(mypage, myname, winprops);
//if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

function NewIntLoginWindow(mypage, myname, w, h, scroll) {
closePopWin();

var winl = (screen.width - w) / 2;
var wint = (screen.height - h) / 2;
winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',noresize';

win = window.open(mypage, myname, winprops);
//if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

function closePopWin() {
	if(win != null) {
		if(!win.closed) {
			win.close();
			win = null;
		} else {
			win = null;
		}
	}
}

window.onerror=null;
var win = null;

function PrivacyWindow(mypage, myname, w, h, scroll) {
closePopWin();

var winl = (screen.width - w) / 2;
var wint = (screen.height - h) / 2;
winprops = 'height='+h+',width='+w+',top='+wint+',left='+winl+',scrollbars='+scroll+',noresize';

win = window.open(mypage, myname, winprops);
//if (parseInt(navigator.appVersion) >= 4) { win.window.focus(); }
}

function closePopWin() {
	if(win != null) { 
		if(!win.closed) { 
			win.close();
			win = null;
		} else {
			win = null;
		}
	}
}