
String.prototype.ltrim = function () 
{
	return this.replace(/^\s+/,'');
}

String.prototype.rtrim = function () 
{
	return this.replace(/\s+$/,'');
}

String.prototype.trim = function () 
{
	return this.replace(/^\s+/,'').replace(/\s+$/,'');
}

Date.prototype.formattedString = function ()
{
	var months = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December")
	var days = new Array("Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday");
	var day = this.getDate();
	var dayofweek = this.getDay();
	var month = this.getMonth();
	var year = this.getFullYear();		
	
	return days[dayofweek] + ", " + months[month] + " " + day + ", " + year;
	
}

Date.prototype.isoString = function ()
{
	var day = this.getDate();
	var month = this.getMonth();
	var year = this.getFullYear();		
	
	return year + "-" + (month+1) + "-" + day;
}

function calcNextDate(date)
{
	var numdays = new Array(31, ((new Date()).getFullYear() % 4 == 0 ? 29 : 28), 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);

	var day = date.getDate();
	var dayofweek = date.getDay();
	var month = date.getMonth();
	var year = date.getFullYear();
	
	var nextday = day + 1;
	var nextmonth = month;
	var nextyear = year;
	var skippingsun = false;
	var skippingsat = false;

	if ( dayofweek == 6 || dayofweek == 0 )
	{
		skippingsun = true;
		nextday++;
	}
	else if ( dayofweek == 5 )
	{
		skippingsat = true;
		nextday=nextday+2;
	}
	
	if ( nextday > numdays[month] )
	{
		if ( skippingsat )
			nextday = 3;
		else if ( skippingsun )
			nextday = 2;
		else
			nextday = 1;		

		if ( month < 12 )
			nextmonth++;
		else if ( month == 12 ) 
		{
			nextmonth = 1;
			nextyear++;
		}
	}
	
	return new Date(nextyear, nextmonth, nextday)
}

function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}

function MM_findObj(n, d) { //v4.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && document.getElementById) x=document.getElementById(n); return x;
}

function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}

var required = new Array("txtName", "txtPhone1", "txtPhone2", "txtPhone3", 
	"txtEmail", "txtAddress", "txtCity", "cboState", "txtZip", "cboMake", 
	"cboYear", "txtModel", "txtProblem", "cboDate", "cboTime", "cboLocation");
var requiredText = new Array("Your Name", "Your Area Code", "Your Phone Prefix", "Your Phone Extension", 
	"Your Email Address", "Your Address", "City", "State", "Zip Code", "Car's Make",
	"Car's Year", "Car's Model", "Please describe any problems you are having.", "Please select an appointment date.", "Please select an appointment time.", "Please select an appointment location.");

function validateEmail(src) {
     var emailReg = "^[\\w-_\.]*[\\w-_\.]\@[\\w]\.+[\\w]+[\\w]$";
     var regex = new RegExp(emailReg);
     return regex.test(src);
  }
		
function isRequired(name)
{
	for(var i = 0; i < required.length; i++)
	{
		if ( name == required[i] )
		{
			return i;
		}
	}
	return -1;
}

function checkRequired()
{
	var errors = "";
	
	for(var i = 0; i < document.forms.makeappt.elements.length; i++)
	{
		var j = isRequired(document.forms.makeappt.elements[i].name);
		if ( document.forms.makeappt.elements[i].value.trim() == "" && j != -1 )
		{
			errors += "\n" + requiredText[j] ;
		}
		else if ( document.forms.makeappt.elements[i].name == "txtEmail" &&  !validateEmail(document.forms.makeappt.elements[i].value.trim()) )
		{
			errors += "\n" + requiredText[j] ;
		}
	}

	if ( errors != "" )
	{
		alert("Required information is either missing or invalid.\n--------------------------------------------------" + errors);
		return false;
	}
	else
	{	
		return true;
	}
}
