/*   Updated  ARBIS Internet Solutions, inc.  03/17/2009 DenverTransportation.org  functions.js  -*/

function setCursor_login()  {
	loginform.u.focus();	
}

function setCursor_register()  {
	RegisterForm.login_email.focus();	
}

function setCursor_forgot_password()  {
	ForgotPasswordForm.user_email.focus();	
}

function setCursor_change_password()  {
	ChangePasswordForm.u.focus();	
}

function setCursor_member_update()  {
	MemberUpdateForm.first_name.focus();	
}

function setMyCursor(myField, ErrorCheck)  { 
	if (ErrorCheck) {
		myField.focus();	
	} 
}

/* FIELD HINT - BEGIN */
/* This function will display helpful text next to an input field.
DEFINE THE INPUT AS:
<input type="text" id="email" onFocus="javascript:toggleMsg('msg-1')" onBlur="javascript:toggleMsg('msg-1')" maxlength="20">
<span id="msg-1" class="msg" style="visibility:hidden;">Required to log-in</span></td>

*/
function toggleMsg(idElement){
  element = document.getElementById(idElement);
  if(element.style.visibility!='hidden'){
    element.style.visibility='hidden';
  } else {
    element.style.visibility='visible';
  }
}
/* FIELD HINT - END */

function check_new_password(form) {
	var display_error_text	= '';	
	No_Errors 				= true;	
	if (isEmpty(form.pw)		) {setMyCursor(ChangePasswordForm.pw,No_Errors);			No_Errors = false; display_error_text += '  Current Password is required\n';}
	if (isEmpty(form.npw)		) {setMyCursor(ChangePasswordForm.npw,No_Errors);			No_Errors = false; display_error_text += '  New Password is required\n';}
	if (isEmpty(form.cpw)		) {setMyCursor(ChangePasswordForm.cpw,No_Errors);			No_Errors = false; display_error_text += '  Confirm Password is required\n';}
	if (form.npw.value == form.pw.value)  {setMyCursor(ChangePasswordForm.npw,No_Errors);	No_Errors = false; display_error_text += '  New Password cannot match Current Password\n';}
	if (form.npw.value != form.cpw.value) {setMyCursor(ChangePasswordForm.cpw,No_Errors);	No_Errors = false; display_error_text += '  Confirm Password must match New Password\n';}

}
function validateForm(form) {
	var display_error_text	= '';
	
	No_Errors 				= true;	
	check_email 			= '';	

	if (isEmpty(form.business_name)		) {setMyCursor(addbizform.business_name,No_Errors);		No_Errors = false; display_error_text += '  Business is required\n';}
	if (isEmpty(form.buyer_address)		) {setMyCursor(addbizform.buyer_address,No_Errors);		No_Errors = false; display_error_text += '  Billing Address is required\n';}
	if (isEmpty(form.buyer_city)		) {setMyCursor(addbizform.buyer_city,No_Errors);		No_Errors = false; display_error_text += '  Billing City is required\n';}
	if (isEmpty(form.buyer_state)		) {setMyCursor(addbizform.buyer_state,No_Errors);		No_Errors = false; display_error_text += '  Billing State is required\n';}
	if (isEmpty(form.buyer_zip) 		) {setMyCursor(addbizform.buyer_zip,No_Errors);			No_Errors = false; display_error_text += '  Billing Zip is required\n';}
	if (isEmpty(form.buyer_country) 	) {setMyCursor(addbizform.buyer_country,No_Errors);		No_Errors = false; display_error_text += '  Billing Country is required\n';}
		
	check_email = form.buyer_email.value;
	if (typeof(form.buyer_email)  != 'undefined') {
		if (form.buyer_email.value.length > 0) {	//line 42
			if (!isValidEmail(check_email)) {
				setMyCursor(addbizform.buyer_email,No_Errors);
				No_Errors = false;
				display_error_text += '  Email is not valid format\n';
			}
		}
		else {
			setMyCursor(addbizform.buyer_email,No_Errors);
			No_Errors 			= false;
			display_error_text += '  Email is required\n';
		}
	}
	
	//alert ("1No Errors. \nReady to copy and submit to Hiddenform*"+document.hiddenForm+"*");
	if (No_Errors == false) {	
		alert ("Please correct the following error(s)\n" + display_error_text);
		return false;
	}
}

function isEmpty(aTextField) {
   if ((aTextField.value.length==0) || (aTextField.value==null)) {
      return true;
   }
   else { return false; }
}

function isValidEmail(strEmail){	// returns  false  if an error found

	var at="@";
	var dot=".";
	var lat=strEmail.indexOf(at);
	var lstr=strEmail.length;
	var ldot=strEmail.indexOf(dot);

	if (strEmail.indexOf(" ")!=-1)																{ return false;}	
	if (strEmail.indexOf(at)==-1)																{ return false;}
	if (strEmail.indexOf(at)==-1  || strEmail.indexOf(at)==0 || strEmail.indexOf(at)==lstr)		{ return false;}
	if (strEmail.indexOf(at,(lat+1))!=-1)														{ return false;}
	if (strEmail.indexOf(dot)==-1 || strEmail.indexOf(dot)==0 || strEmail.indexOf(dot)==lstr)	{ return false;}
	if (strEmail.substring(lat-1,lat)==dot || strEmail.substring(lat+1,lat+2)==dot)				{ return false;}
	if (strEmail.indexOf(dot,(lat+2))==-1)														{ return false;}	
 
	validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;
	// search email text for regular exp matches
	if (strEmail.search(validRegExp) == -1) {		// -1 means RegExp characters not found
		return false;
	} 
	else {
		return true;
	}
}

/******************* Pop-Up Window - BEGIN *********************************/
var newWindow;
var newPageURL;
var newPageWidth;
var newPageHeight;
var newWindowName;
// Create the new window
function makeNewWindow(newPageURL,newPageWidth,newPageHeight) {
	newWindowName 		= 'EventWindow';
	newWindowOptions	= "width="+newPageWidth+",height="+newPageHeight+",resizable=yes,status=no,location=no,menubar=no,copyhistory=no,toolbar=no";
	// check if the window is open already
	if (!newWindow || newWindow.closed) {
		newWindow = window.open(newPageURL,newWindowName,newWindowOptions);
		// check if window not opened, such as NS Navigator 2, which has no opener property
		if (!newWindow.opener) {
			newWindow.opener = window;
		}
	} else
	if (newWindow.focus) {
		// window is already open and focusable, so bring it to focus\front
		newWindow.focus();
	}
}

/******************* Pop-Up Window - END *********************************/


/****************************************************/
/*   This small script will launch the browser 		*/
/*   Favorites window to bookmark the current page.	*/
/*   Works in Firefox and Internet Explorer.		*/
/****************************************************/
function bookmark(address,sitename) {
  if (window.sidebar) {
    window.sidebar.addPanel(sitename, address,"");
  } else if( document.all ) {
    window.external.AddFavorite(address, sitename);
  } else if( window.opera && window.print ) {
    return true;
  }
}



/******************* DATE SELECT LIST.... Month / Day  - BEGIN *********************************/

var today 			= new Date();
var days_of_month 	= new Array("31","28","31","30","31","30","31","31","30","31","30","31");
var month_names 	= new Array("January","Febuary","March","April","May","June","July","August","September","October","November","December");

/******* Performs a check on a given year to see if its a leap year	*************/
function isLeapYear(year) { 
	return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) ? true : false;
}

/*******Re-populate the DAYS select list when month is changed	*************/
function check_days() { 
  var selected_month 	= document.getElementById('month_sel').value;
  var selected_year 	= 2008;						// FORCE year to 2008.
  var this_year 		= today.getYear();
  var max_days			= 0;
  if((isLeapYear(selected_year) == true) && (selected_month == 2)) {
	max_days 		= 29;
  } else {
	max_days = days_of_month[selected_month -1];
  }
  start_day = 1;
  document.getElementById("day_sel").options.length = 0;
  var int_dd	 = 0;
  // i tracks the occurrences of the dropdown list(i.e.  0 - 30 for 31 days).... 
  // x is used for the values of the days, so x is always 1 greater than i.
  for(i=0,x=1 ; i<max_days ; i++) {
		if (x < 10) { 
			int_dd 	= '0'+ x;
		} else {
			int_dd	= x;
		}
		document.RegisterForm.day_sel[i] = new Option(int_dd, int_dd);  
		x++;
  }
  document.getElementById("day_sel").selected = 0;
}

/******************* DATE SELECT LIST.... Month / Day / Year  - END   *********************************/
