
// Change models based on make selection
// Ajax function. This is a re-write from the one
// before. This one works in all browsers.
var xmlHttp

function showModels(str,stryear) {
	if (str.length==0) { 
  		document.getElementById("txtModels").innerHTML="";
  		return;
  	}
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null) {
		alert ("Your browser does not support AJAX!");
  		return;
  	} 
	var url="model.php";
	url=url+"?make="+str+"&year="+stryear;
	xmlHttp.onreadystatechange=modelsChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
} 

function modelsChanged() { 
	if (xmlHttp.readyState==4) { 
		document.getElementById("txtModels").innerHTML=xmlHttp.responseText;
	}
}

function showMakes(str) {
	if (str.length==0) { 
  		document.getElementById("txtMakes").innerHTML="";
  		return;
  	}
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null) {
		alert ("Your browser does not support AJAX!");
  		return;
  	} 
	var url="make.php";
	url=url+"?year="+str;
	xmlHttp.onreadystatechange=makesChanged;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(null);
} 

function makesChanged() { 
	if (xmlHttp.readyState==4) { 
		document.getElementById("txtMakes").innerHTML=xmlHttp.responseText;
	}
}

function GetXmlHttpObject() {
	var xmlHttp=null;
	try {
  		// Firefox, Opera 8.0+, Safari
  		xmlHttp=new XMLHttpRequest();
  	}catch (e){
 	 	// Internet Explorer
  		try {
    		xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    	}catch (e){
    		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    	}
  	}
	return xmlHttp;
}

// A little less java and ajax to
// show images from thumbnail view
function showImage(url, imgstr) { 
	var newImg = new Image();
	newImg.src = url+'/'+imgstr;
	var height = newImg.height;
	var width = newImg.width;
	var newwidth = 'width="300"';
	var newheight = 'height="250"';
	if(width >= 300) {
		var newwidth = 'width="300"';
	}
	document.getElementById("loadarea2").innerHTML = '<img '+newwidth+' '+newheight+' src="'+url+'" class="showimg">';
}
// A little less java and ajax to
// show images from thumbnail view
function showImage2(url, imgstr) { 
	var newImg = new Image();
	newImg.src = url+'/'+imgstr;
	var height = newImg.height;
	var width = newImg.width;
	var newwidth = 'width="300"';
	var newheight = 'height="250"';
	if(width >= 300) {
		var newwidth = 'width="300"';
	}
	document.getElementById("loadarea2").innerHTML = '<img '+newwidth+' '+newheight+' src="'+url+'/'+imgstr+'" class="showimg">';
}
// Little function to show and hide
// divs as needed
function toggleDiv(divid){
   if(document.getElementById(divid).style.display == 'none'){
     document.getElementById(divid).style.display = 'block';
   }else{
     document.getElementById(divid).style.display = 'none';
   }
}

// Monthly payment calculator
function CalculatePayments(principal, down_payment, interest, years) {
	var x = ((principal - down_payment) * interest / (12 * (1 - Math.pow(1 + (interest / 12), (-years * 12)))));
	return Math.floor(x * 100) / 100
}
		
function ShowPayments() {
	var x = CalculatePayments(document.jsForm.jsPrincipal.value, document.jsForm.jsDownPayment.value, document.jsForm.jsInterest.value / 100, document.jsForm.jsYears.value);
	if (isNaN(x)) {
		document.jsForm.jsResult.value = 'Could not compute';
	}else{
		document.jsForm.jsAmtFin.value = document.jsForm.jsPrincipal.value - document.jsForm.jsDownPayment.value;
		document.jsForm.jsResult.value = x;
	}
}

function moveOnMax(field,nextFieldID){
  if(field.value.length >= field.maxLength){
    document.getElementById(nextFieldID).focus();
  }
}
function numbersonly(myfield, e, dec){
var key;
var keychar;

if (window.event)
   key = window.event.keyCode;
else if (e)
   key = e.which;
else
   return true;
keychar = String.fromCharCode(key);

// control keys
if ((key==null) || (key==0) || (key==8) || 
    (key==9) || (key==13) || (key==27) )
   return true;

// numbers
else if ((("0123456789").indexOf(keychar) > -1))
   return true;

// decimal point jump
else if (dec && (keychar == "."))
   {
   myfield.form.elements[dec].focus();
   return false;
   }
else
   return false;
}
function data_copy() {
	if(document.form1.copy[0].checked){
		document.form1.coxstreetnumber.value=document.form1.streetnumber.value;
		document.form1.coxstreetname.value=document.form1.streetname.value;
		document.form1.coxstreettype.value=document.form1.streettype.value;
		document.form1.coxaptsuite.value=document.form1.aptsuite.value;
		document.form1.coxcity.value=document.form1.city.value;
		document.form1.coxstate.value=document.form1.state.value;
		document.form1.coxpostal.value=document.form1.postal.value;
		document.form1.coxhmphone1.value=document.form1.hmphone1.value;
		document.form1.coxhmphone2.value=document.form1.hmphone2.value;
		document.form1.coxhmphone3.value=document.form1.hmphone3.value;
		document.form1.coxaddressyears.value=document.form1.addressyears.value;
		document.form1.coxaddressmonths.value=document.form1.addressmonths.value;
		document.form1.coxhousestatus.value=document.form1.housestatus.value;
	}else{
		document.form1.coxstreetnumber.value="";
		document.form1.coxstreetname.value="";
		document.form1.coxstreettype.value="";
		document.form1.coxaptsuite.value="";
		document.form1.coxcity.value="";
		document.form1.coxstate.value="";
		document.form1.coxpostal.value="";
		document.form1.coxhmphone1.value="";
		document.form1.coxhmphone2.value="";
		document.form1.coxhmphone3.value="";
		document.form1.coxaddressyears.value="";
		document.form1.coxaddressmonths.value="";
		document.form1.coxhousestatus.value="";
	
	}

}
<!--

/***********************************************
* Required field(s) validation v1.10- By NavSurf
* Visit Nav Surf at http://navsurf.com
* Visit http://www.dynamicdrive.com/ for full source code
***********************************************/

function formCheck(formobj){
	// dialog message
	var alertMsg = "Please complete the following fields:\n";
	
	if(document.getElementById('cox').checked) {
		// Enter name of mandatory fields
		var fieldRequired = Array("firstname", "lastname", "emailaddress", "ssn1", "ssn2", "ssn3", "birthdate1", "birthdate2", "birthdate3"
		, "streetnumber", "streetname", "city", "state", "postal", "hmphone1", "hmphone2", "hmphone3", "addressyears", "housestatus", "housepay"
		, "employedstatus", "employername", "workphone1", "workphone2", "workphone3", "employertimeyears", "income", "coxrelationship"		
		,"coxfirstname", "coxlastname", "coxssn1", "coxssn2", "coxssn3", "coxbirthdate1", "coxbirthdate2", "coxbirthdate3"
		, "coxstreetnumber", "coxstreetname", "coxcity", "coxstate", "coxpostal", "coxhmphone1", "coxhmphone2", "coxhmphone3", "coxaddressyears", "coxaddressmonths", "coxhousestatus", "coxhousepay"
		, "coxemployedstatus", "coxemployername", "coxworkphone1", "coxworkphone2", "coxworkphone3", "coxemployertimeyears", "coxincome", "methodid");
		// Enter field description to appear in the dialog box
		var fieldDescription = Array("First Name", "Last Name", "Email Address", "Social Security 1", "Social Security 2", "Social Security 3"
		, "Birth Date Month", "Birth Date Day", "Birth Date Year", "Street Number", "Street Name", "City", "State", "Postal/Zip", "Home Phone Area Code", "Home Phone Prefix", "Home Phone Suffix"
		, "Years at address", "Housing Status", "Mtg Payment or Rent", "Employment Status", "Employers Name", "Work Phone Area Code", "Work Phone Prefix", "Work Phone Suffix"
		, "Years employed", "Income", "Co-Applicants Relationship"		
		,"Co-Applicants First Name", "Co-Applicants Last Name", "Co-Applicants Social Security 1", "Co-Applicants Social Security 2", "Co-Applicants Social Security 3"
		, "Co-Applicants Birth Date Month", "Co-Applicants Birth Date Day", "Co-Applicants Birth Date Year", "Co-Applicants Street Number", "Co-Applicants Street Name", "Co-Applicants City", "Co-Applicants State", "Co-Applicants Postal/Zip", "Co-Applicants Home Phone Area Code", "Co-Applicants Home Phone Prefix", "Co-Applicants Home Phone Suffix"
		, "Co-Applicants Years at address", "Co-Applicants Months atr address", "Co-Applicants Housing Status", "Co-Applicants Mtg Payment or Rent", "Co-Applicants Employment Status", "Co-Applicants Employers Name", "Co-Applicants Work Phone Area Code", "Co-Applicants Work Phone Prefix", "Co-Applicants Work Phone Suffix"
		, "Co-Applicants Years employed", "Co-Applicants Income", "Contact Method");
	}else{
		// Enter name of mandatory fields
		var fieldRequired = Array("firstname", "lastname", "emailaddress", "ssn1", "ssn2", "ssn3", "birthdate1", "birthdate2", "birthdate3"
		, "streetnumber", "streetname", "city", "state", "postal", "hmphone1", "hmphone2", "hmphone3", "addressyears", "housestatus", "housepay"
		, "employedstatus", "employername", "workphone1", "workphone2", "workphone3", "employertimeyears", "income", "methodid");
		// Enter field description to appear in the dialog box
		var fieldDescription = Array("First Name", "Last Name", "Email Address", "Social Security 1", "Social Security 2", "Social Security 3"
		, "Birth Date Month", "Birth Date Day", "Birth Date Year", "Street Number", "Street Name", "City", "State", "Postal/Zip", "Home Phone Area Code", "Home Phone Prefix", "Home Phone Suffix"
		, "Years at address", "Housing Status", "Mtg Payment or Rent", "Employment Status", "Employers Name", "Work Phone Area Code", "Work Phone Prefix", "Work Phone Suffix"
		, "Years employed", "Income", "Contact Method");
		
	}
	
	var l_Msg = alertMsg.length;
	
	for (var i = 0; i < fieldRequired.length; i++){
		var obj = formobj.elements[fieldRequired[i]];
		if (obj){
			switch(obj.type){
			case "select-one":
				if (obj.selectedIndex == 0){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "select-multiple":
				if (obj.selectedIndex == -1){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "text":
			case "textarea":
				if (obj.value == "" || obj.value == null){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			default:
			}
			if (obj.type == undefined){
				var blnchecked = false;
				for (var j = 0; j < obj.length; j++){
					if (obj[j].checked){
						blnchecked = true;
					}
				}
				if (!blnchecked){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
			}
		}
	}

	if (alertMsg.length == l_Msg){
		return true;
	}else{
		alert(alertMsg);
		return false;
	}
}
// -->

// Function to open new window
var win=null;
function NewWindow(mypage,myname,w,h,scroll,pos) {
	if(pos=="random"){
		LeftPosition=(screen.width)?Math.floor(Math.random()*(screen.width-w)):100;
		TopPosition=(screen.height)?Math.floor(Math.random()*((screen.height-h)-75)):100;
	}
	if(pos=="center"){
		LeftPosition=(screen.width)?(screen.width-w)/2:100;
		TopPosition=(screen.height)?(screen.height-h)/2:100;
	}else if((pos!="center" && pos!="random") || pos==null){
		LeftPosition=0;TopPosition=20;
	}
	settings='width='+w+',height='+h+',top='+TopPosition+',left='+LeftPosition+',scrollbars='+scroll+',location=no,directories=no,status=no,menubar=no,toolbar=no,resizable=no';
	win=window.open(mypage,myname,settings);
}
function newImage(arg) {
	if (document.images) {
		rslt = new Image();
		rslt.src = arg;
		return rslt;
	}
}

function changeImages() {
	if (document.images && (preloadFlag == true)) {
		for (var i=0; i<changeImages.arguments.length; i+=2) {
			document[changeImages.arguments[i]].src = changeImages.arguments[i+1];
		}
	}
}

var preloadFlag = false;
function preloadImages() {
	if (document.images) {
		nav_welcome_over = newImage("{$templateurl}/images/nav_welcome_over.gif");
		nav_listinventory_over = newImage("{$templateurl}/images/nav_listinventory_over.gif");
		nav_searchinventory_over = newImage("{$templateurl}/images/nav_searchinventory_over.gif");
		nav_contactus_over = newImage("{$templateurl}/images/nav_contactus_over.gif");
		findus_over = newImage("{$templateurl}/images/findus_over.gif");
		nav_specials_over = newImage("{$templateurl}/images/nav_specials_over.gif");
		moreinfo_over = newImage("{$templateurl}/images/moreinfo_over.gif");
		searchbutton_over = newImage("{$templateurl}/images/searchbutton_over.gif");
		submit_on = newImage("{$templateurl}/images/submit_on.gif");
		reset_on = newImage("{$templateurl}/images/reset_on.gif");
		getdirections_over = newImage("{$templateurl}/images/getdirections_over.gif");
		preloadFlag = true;
	}
}
var zxcZIndex=0;         // the base Z-Index for the images
var zxcDelay=10;         //  the global zoom speed may be specified in addition to the call
var zxcAddCursor=true;   // true to add a 'hand'/'pointer' cursor to the Zoom Image, false for no cursor

// Zoom In/Zoom Out zxcPart2 (15-04-2006)
// by Vic Phillips http://www.vicsjavascripts.org.uk
//

// Functional Code - N0 NEED to Change

var zxcOOPCnt=0;
var zxcCursor=document.all?'hand':'pointer';
zxcZIndex=zxcZIndex||1;
var zxcZIndx=zxcZIndex;
zxcDelay=zxcDelay||10;

function zxcZoom(zxcobj,zxcph,zxcmw,zxcmh,zxcspd,zxcopt){
 if (typeof(zxcobj)=='string'){ zxcobj=document.getElementById(zxcobj); }
 var zxcphoto;
 if (zxcobj.tagName.toUpperCase()=='IMG'){
  zxcphoto=zxcph||zxcobj.src;
  if (zxcphoto.length<5){ zxcphoto=zxcobj.src; }
 }
 var zxcspd=zxcspd||1;
 var zxcopt=zxcopt||null;
 if (!zxcobj.zxcoop){ zxcobj.zxcoop=new zxcOOPZoom(zxcobj,zxcphoto,zxcmw,zxcmh,zxcspd,zxcopt,zxcopt); }
 clearTimeout(zxcobj.zxcoop.to);
 zxcobj.zxcoop.inc*=-1
 if (zxcobj.zxcoop.large.load){ zxcobj.src=zxcobj.zxcoop.large.src; }
 zxcZIndx++;
 zxcStyle(zxcobj,{zIndex:(zxcZIndx+'')});
 zxcobj.zxcoop.zoom();
}

function zxcOOPZoom(zxcobj,zxcph,zxcmw,zxcmh,zxcspd,zxcopt){
 this.obj=zxcobj;
 this.objS=zxcobj.style;
 this.clone=zxcobj.cloneNode(true);
 this.zxcspd=zxcspd;
 this.zxct=zxcPos(zxcobj)[1];
 this.zxcl=zxcPos(zxcobj)[0];
 zxcStyle(this.obj,{position:'absolute',zIndex:(zxcZIndex*1+1+''),width:zxcobj.offsetWidth+'px',height:zxcobj.offsetHeight+'px',left:this.zxcl+'px',top:this.zxct+'px'});
 if (zxcAddCursor){ zxcStyle(this.obj,{cursor:zxcCursor}); }
 this.minw=zxcobj.offsetWidth;
 this.minh=zxcobj.offsetHeight;
 this.center=zxcopt;
 this.maxw=zxcmw;
 this.maxh=zxcmh||zxcmw*this.minh/this.minw;
 this.thumb=zxcobj.src;
 this.large=new Image();
 this.large.obj=this.obj;
 if (zxcph){ this.large.onload=function(){ this.load=true; this.obj.src=this.src; }; this.large.src=zxcph; }
 zxcobj.parentNode.insertBefore(this.clone,zxcobj);
 this.inc=((this.maxw-this.minw)/100);
 this.inc=-this.inc*this.zxcspd;
 this.ratio=(this.maxh/this.maxw);
 this.ref='zxc'+zxcOOPCnt;
 window[this.ref]=this;
 this.to=null;
 zxcOOPCnt++;
}

zxcOOPZoom.prototype.setTimeOut=function(zxcf,zxcd){
 this.to=setTimeout("window."+this.ref+"."+zxcf,zxcd);
}

zxcOOPZoom.prototype.zoom=function(){
 this.w=parseInt(this.objS.width)+this.inc; this.h=parseInt(this.objS.width)*this.ratio;
 zxcStyle(this.obj,{width:(this.w)+'px',height:(this.h)+'px'});
 this.w=parseInt(this.objS.width); this.h=parseInt(this.objS.height);
 if (this.center){ zxcStyle(this.obj,{top:(this.zxct-(this.h-this.minh)/2)+'px',left:(this.zxcl-(this.w-this.minw)/2)+'px'}); }
 if ((this.inc>0&&this.w<this.maxw)||(this.inc<0&&this.w>this.minw)){ this.setTimeOut('zoom();',zxcDelay); }
 else {
  if (this.inc>0){ zxcStyle(this.obj,{width:this.maxw+'px',height:this.maxh+'px'}); }
  else {
   zxcStyle(this.obj,{zIndex:zxcZIndex,width:this.minw+'px',height:this.minh+'px',top:(this.zxct)+'px',left:(this.zxcl)+'px'});
   zxcZIndx--;
   this.obj.src=this.thumb;
  }
 }
}

function zxcStyle(zxcele,zxcstyle){
 for (key in zxcstyle){ zxcele.style[key]=zxcstyle[key]; }
}

function zxcPos(zxc){
 zxcObjLeft=zxc.offsetLeft;
 zxcObjTop=zxc.offsetTop;
 while(zxc.offsetParent!=null){
  zxcObjParent=zxc.offsetParent;
  zxcObjLeft+=zxcObjParent.offsetLeft;
  zxcObjTop+=zxcObjParent.offsetTop;
  zxc=zxcObjParent;
 }
 return [zxcObjLeft,zxcObjTop];
}

function commitFlashObject(_obj, _container){
	var _output=_paramoutput=_src=_ver="";
	for(var _cO in _obj){
		_output+=_cO+"=\""+_obj[_cO]+"\" "
		_paramoutput+="<param name="+_cO+" value=\""+_obj[_cO]+"\">";
		if(_cO=="movie")_src="src=\""+_obj[_cO]+"\"";
		if(_cO=="version")_ver=_obj[_cO];
	}
	if(_ver=="")_ver="8,0,0,0"
	var ihtm="<object classid=clsid:D27CDB6E-AE6D-11cf-96B8-444553540000 codebase="+location.protocol+"//download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version="+_ver+" "+_output+">\n"
	ihtm+=_paramoutput+"\n"
	ihtm+="<embed "+_src+" pluginspage="+location.protocol+"//www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash type=application/x-shockwave-flash "+_output+">\n";
	ihtm+="</embed>\n";
	ihtm+="</object>\n";
	document.getElementById(_container).innerHTML=ihtm
}

function setItems(checkObj) {

var checkCount = 0;
var maxChecks = 4;

var itemNum = 1;

//Determine the number of checked boxes
while (document.getElementById('item'+itemNum))
{
fieldObj = document.getElementById('item'+itemNum);
if (fieldObj.checked)
{
checkCount++;
}
itemNum++;
}

var itemNum = 1;
//Enable/disable unchecked boxes
while (document.getElementById('item'+itemNum))
{
fieldObj = document.getElementById('item'+itemNum);
if (!fieldObj.checked)
{
fieldObj.disabled = (checkCount==maxChecks);
}
itemNum++;
}
}
// Project: Dynamic Date Selector (DtTvB) - 2006-03-16
// Script featured on JavaScript Kit- http://www.javascriptkit.com
// Code begin...
// Set the initial date.
var ds_i_date = new Date();
ds_c_month = ds_i_date.getMonth() + 1;
ds_c_year = ds_i_date.getFullYear();

// Get Element By Id
function ds_getel(id) {
	return document.getElementById(id);
}

// Get the left and the top of the element.
function ds_getleft(el) {
	var tmp = el.offsetLeft;
	el = el.offsetParent
	while(el) {
		tmp += el.offsetLeft;
		el = el.offsetParent;
	}
	return tmp;
}
function ds_gettop(el) {
	var tmp = el.offsetTop;
	el = el.offsetParent
	while(el) {
		tmp += el.offsetTop;
		el = el.offsetParent;
	}
	return tmp;
}

// Output Element
var ds_oe = ds_getel('ds_calclass');
// Container
var ds_ce = ds_getel('ds_conclass');

// Output Buffering
var ds_ob = ''; 
function ds_ob_clean() {
	ds_ob = '';
}
function ds_ob_flush() {
	ds_oe.innerHTML = ds_ob;
	ds_ob_clean();
}
function ds_echo(t) {
	ds_ob += t;
}

var ds_element; // Text Element...

var ds_monthnames = [
'January', 'February', 'March', 'April', 'May', 'June',
'July', 'August', 'September', 'October', 'November', 'December'
]; // You can translate it for your language.

var ds_daynames = [
'Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'
]; // You can translate it for your language.

// Calendar template
function ds_template_main_above(t) {
	return '<table cellpadding="3" cellspacing="1" class="ds_tbl">'
	     + '<tr>'
		 + '<td class="ds_head" style="cursor: pointer" onclick="ds_py();">&lt;&lt;</td>'
		 + '<td class="ds_head" style="cursor: pointer" onclick="ds_pm();">&lt;</td>'
		 + '<td class="ds_head" style="cursor: pointer" onclick="ds_hi();" colspan="3">[Close]</td>'
		 + '<td class="ds_head" style="cursor: pointer" onclick="ds_nm();">&gt;</td>'
		 + '<td class="ds_head" style="cursor: pointer" onclick="ds_ny();">&gt;&gt;</td>'
		 + '</tr>'
	     + '<tr>'
		 + '<td colspan="7" class="ds_head">' + t + '</td>'
		 + '</tr>'
		 + '<tr>';
}

function ds_template_day_row(t) {
	return '<td class="ds_subhead">' + t + '</td>';
	// Define width in CSS, XHTML 1.0 Strict doesn't have width property for it.
}

function ds_template_new_week() {
	return '</tr><tr>';
}

function ds_template_blank_cell(colspan) {
	return '<td colspan="' + colspan + '"></td>'
}

function ds_template_day(d, m, y) {
	return '<td class="ds_cell" onclick="ds_onclick(' + d + ',' + m + ',' + y + ')">' + d + '</td>';
	// Define width the day row.
}

function ds_template_main_below() {
	return '</tr>'
	     + '</table>';
}

// This one draws calendar...
function ds_draw_calendar(m, y) {
	// First clean the output buffer.
	ds_ob_clean();
	// Here we go, do the header
	ds_echo (ds_template_main_above(ds_monthnames[m - 1] + ' ' + y));
	for (i = 0; i < 7; i ++) {
		ds_echo (ds_template_day_row(ds_daynames[i]));
	}
	// Make a date object.
	var ds_dc_date = new Date();
	ds_dc_date.setMonth(m - 1);
	ds_dc_date.setFullYear(y);
	ds_dc_date.setDate(1);
	if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) {
		days = 31;
	} else if (m == 4 || m == 6 || m == 9 || m == 11) {
		days = 30;
	} else {
		days = (y % 4 == 0) ? 29 : 28;
	}
	var first_day = ds_dc_date.getDay();
	var first_loop = 1;
	// Start the first week
	ds_echo (ds_template_new_week());
	// If sunday is not the first day of the month, make a blank cell...
	if (first_day != 0) {
		ds_echo (ds_template_blank_cell(first_day));
	}
	var j = first_day;
	for (i = 0; i < days; i ++) {
		// Today is sunday, make a new week.
		// If this sunday is the first day of the month,
		// we've made a new row for you already.
		if (j == 0 && !first_loop) {
			// New week!!
			ds_echo (ds_template_new_week());
		}
		// Make a row of that day!
		ds_echo (ds_template_day(i + 1, m, y));
		// This is not first loop anymore...
		first_loop = 0;
		// What is the next day?
		j ++;
		j %= 7;
	}
	// Do the footer
	ds_echo (ds_template_main_below());
	// And let's display..
	ds_ob_flush();
	// Scroll it into view.
	ds_ce.scrollIntoView();
}

// A function to show the calendar.
// When user click on the date, it will set the content of t.
function ds_sh(t) {
	// Set the element to set...
	ds_element = t;
	// Make a new date, and set the current month and year.
	var ds_sh_date = new Date();
	ds_c_month = ds_sh_date.getMonth() + 1;
	ds_c_year = ds_sh_date.getFullYear();
	// Draw the calendar
	ds_draw_calendar(ds_c_month, ds_c_year);
	// To change the position properly, we must show it first.
	ds_ce.style.display = '';
	// Move the calendar container!
	the_left = ds_getleft(t);
	the_top = ds_gettop(t) + t.offsetHeight;
	ds_ce.style.left = the_left + 'px';
	ds_ce.style.top = the_top + 'px';
	// Scroll it into view.
	ds_ce.scrollIntoView();
}

// Hide the calendar.
function ds_hi() {
	ds_ce.style.display = 'none';
}

// Moves to the next month...
function ds_nm() {
	// Increase the current month.
	ds_c_month ++;
	// We have passed December, let's go to the next year.
	// Increase the current year, and set the current month to January.
	if (ds_c_month > 12) {
		ds_c_month = 1; 
		ds_c_year++;
	}
	// Redraw the calendar.
	ds_draw_calendar(ds_c_month, ds_c_year);
}

// Moves to the previous month...
function ds_pm() {
	ds_c_month = ds_c_month - 1; // Can't use dash-dash here, it will make the page invalid.
	// We have passed January, let's go back to the previous year.
	// Decrease the current year, and set the current month to December.
	if (ds_c_month < 1) {
		ds_c_month = 12; 
		ds_c_year = ds_c_year - 1; // Can't use dash-dash here, it will make the page invalid.
	}
	// Redraw the calendar.
	ds_draw_calendar(ds_c_month, ds_c_year);
}

// Moves to the next year...
function ds_ny() {
	// Increase the current year.
	ds_c_year++;
	// Redraw the calendar.
	ds_draw_calendar(ds_c_month, ds_c_year);
}

// Moves to the previous year...
function ds_py() {
	// Decrease the current year.
	ds_c_year = ds_c_year - 1; // Can't use dash-dash here, it will make the page invalid.
	// Redraw the calendar.
	ds_draw_calendar(ds_c_month, ds_c_year);
}

// Format the date to output.
function ds_format_date(d, m, y) {
	// 2 digits month.
	m2 = '00' + m;
	m2 = m2.substr(m2.length - 2);
	// 2 digits day.
	d2 = '00' + d;
	d2 = d2.substr(d2.length - 2);
	// YYYY-MM-DD
	return m2 + '-' + d2 + '-' + y;
}

// When the user clicks the day.
function ds_onclick(d, m, y) {
	// Hide the calendar.
	ds_hi();
	// Set the value of it, if we can.
	if (typeof(ds_element.value) != 'undefined') {
		ds_element.value = ds_format_date(d, m, y);
	// Maybe we want to set the HTML in it.
	} else if (typeof(ds_element.innerHTML) != 'undefined') {
		ds_element.innerHTML = ds_format_date(d, m, y);
	// I don't know how should we display it, just alert it to user.
	} else {
		alert (ds_format_date(d, m, y));
	}
}
var TIMPICKER_GLOBAL_DEFAULT_INCR = 15; //number of minutes incremented for the time picker hour
var TIMEPICKER_GLOBAL_DEFAULT_START_TIME = 7; //in case we only want to offer times between 9am and 5pm or something
var TIMEPICKER_GLOBAL_DEFAULT_END_TIME = 17; //ditto here. We can limit the start end times. 24 hours is default right now.
var TIMEPICKER_MILITARY_TIME = false;

var initTimePickers = function(){
	var clocks = getElementsByClassName(document,'img','timePickerTrigger');
	for(var i=0;i<clocks.length;i++){
		var targ = clocks[i].className.split(' ')[1];
		clocks[i].setAttribute('targ',targ);
		clocks[i].onclick = function(){TimePicker(this.getAttribute('targ'));};
	}
}
if(window.attachEvent){window.attachEvent('onload',initTimePickers);}/*Dynamic onload IE*/
if(window.addEventListener){window.addEventListener('load',initTimePickers,false);}/*Dynamic onload MOZILLA*/

function setTimeFromPicker(val,objId){
	document.getElementById(objId).value = val;
	document.getElementById(objId).focus();
	document.body.removeChild(document.getElementById('MyOnlyTimePicker'));
}
var TimePicker = function(tObjID){
	//optional argments [,incr,sTm,eTm]
	var tObj = document.getElementById(tObjID);
	this.militaryTime = TIMEPICKER_MILITARY_TIME;
	this.incr = (arguments[1]) ? arguments[1] : TIMPICKER_GLOBAL_DEFAULT_INCR;
	this.sTm = (arguments[2]) ? arguments[2] : TIMEPICKER_GLOBAL_DEFAULT_START_TIME;
	this.eTm = (arguments[3]) ? arguments[3] : TIMEPICKER_GLOBAL_DEFAULT_END_TIME;
	this.cellsPer = 60 / this.incr;
	var pos = findPos(tObj);
	this.xPos = pos[0];
	this.yPos = pos[1]+tObj.offsetHeight;
	this.getTimeLink = function(hr,mn){
		var myTm = this.sTm+hr;
		var myMn = this.incr*mn;
		var hrDsp = myTm;
		if(myTm>12 && this.militaryTime==false)hrDsp = myTm - 12;
		if(myTm==0 && this.militaryTime==false)hrDsp = 12;
		if(myMn==0)myMn = '00';
		var tmDsp = hrDsp+':'+myMn;
		if(this.militaryTime==false){
			tmDsp += (myTm>11)?' pm':' am';
		}
		var myA = document.createElement('a');
		myA.innerHTML = tmDsp;
		myA.href = "javascript:setTimeFromPicker('"+tmDsp+"','"+tObj.id+"');";
		return myA;
	};
	
	if(document.createElement){
		if(document.getElementById('MyOnlyTimePicker')){
			document.body.removeChild(document.getElementById('MyOnlyTimePicker'));
		}
		var tp = document.createElement('div');
		tp.className = 'timePicker';
		tp.id = 'MyOnlyTimePicker';
		tp.style.top = this.yPos+'px';
		tp.style.left = this.xPos+'px';
		this.tbl = document.createElement('table');
		this.tbl.width = '100%';
		this.tbl.cellSpacing='1';
		this.tbl.cellPadding='0';
		for(var i=0;i<this.eTm-this.sTm;i++){ 
			var nr = document.createElement('tr'); 
			for(var j=0;j<this.cellsPer;j++){ 
				var nc = document.createElement('td'); 
				nc.appendChild(this.getTimeLink(i,j)); 
				nr.appendChild(nc); 
			} 
			this.tbl.appendChild(nr); 
		}
		tp.appendChild(this.tbl);
		tp.innerHTML= tp.innerHTML+'<div style="clear:both;"></div>';
		document.body.appendChild(tp);
	}
}

function getElementsByClassName(oElm, strTagName, strClassName){ 
	var arrElements = (strTagName == "*" && oElm.all)? oElm.all : oElm.getElementsByTagName(strTagName);
	var arrReturnElements = new Array();
	strClassName = strClassName.replace(/\-/g, "\\-");
	var oRegExp = new RegExp("(^|\\s)" + strClassName + "(\\s|$)");
	var oElement;
	for(var i=0; i<arrElements.length; i++){
		oElement = arrElements[i];      
		if(oRegExp.test(oElement.className)){arrReturnElements.push(oElement);}   
	}
	return (arrReturnElements);
}


function findPos(obj) {
	var curleft = curtop = 0;
	if (obj.offsetParent) {
		curleft = obj.offsetLeft
		curtop = obj.offsetTop
		while (obj = obj.offsetParent) {
			curleft += obj.offsetLeft
			curtop += obj.offsetTop
		}
	}
	return [curleft,curtop];
}
