/* FORM VALIDATION ////////////////////////////////////////////////////////////////////////// */

	//filter das foan
	function filterPhone( oSender, nMax, sWhereTo ){

		var sChar = oSender.value.charAt( oSender.length-1 );
		switch( parseInt(sChar) ){
			case 0:
			case 1:
			case 2:
			case 3:
			case 4:
			case 5:
			case 6:
			case 7:
			case 8:
			case 9: if( oSender.value.length == nMax && sWhereTo != "" ){
						document.getElementById( sWhereTo ).focus();	
					}
				break;
			default: oSender.value = oSender.value.substring( 0, oSender.value.length -2 );
				break;
		}
		
	}
	
	//validate ZIP
	function checkZip( sValue ){
		if( isNaN( sValue ) || sValue.length < 5 ){
			return true;
		}else{
			return false;	
		}
		
	}
	
	//validate PHONE 
	function checkPhone( sValue ){
		if( isNaN( sValue ) || sValue.length < 10 ){
			return true;
		}else{
			return false;
		}	
	}
	
	
	//validate das email
	function checkEmail( sValue ){
		var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
		if( filter.test( sValue ) ){ return true; } //alert('YES! Correct email address');
		else { return false; } //alert('NO! Incorrect email address');
	}
	
	////////////////////////////////////////////////////////////////////////////////////////
	// validation calls
	
	var steps = new Array();
		steps[1] = "fname, lname, pf1, pf2, pf3, email";
		steps[2] = "pu_city, pu_state, d_city, d_state";
		steps[3] = "";
		steps[4] = "fname, lname, pf1, pf2, pf3, email, order_reg_num, ld_address1, ld_city, ld_state, ld_zip, claim01";
		
	var errorHead = "The following fields were entered incorrectly:\n\n";
	var errorFoot = "\nPlease correct the field(s) before moving on.";

	function doValidate( oForm, nSection ){
	var errorMsg = "";		
		//alert( "steps[" + nSection + "].indexOf( " + oForm[0].name + ")" );
		for( i = 0; i < oForm.length; i++ ){
			
			if( steps[nSection].indexOf( oForm[i].name ) != -1 && oForm[i].value == "" && oForm[i].type == "text" ){
				
				switch( oForm[i].name ){
					
					case 'fname': errorMsg += "   'First Name'\n";
						break;
					case 'lname': errorMsg += "   'Last Name'\n";
						break;
					case 'email': errorMsg += "   'Email'\n";
						break;
						
					case 'pu_city': errorMsg += "   'Pick Up City'\n";
						break;
					case 'pu_state': errorMsg += "   'Pick Up State'\n";
						break;
					case 'd_city':
					case "ld_city": errorMsg += "   'Delivery City'\n";
						break;
					case 'd_state':
					case "ld_state": errorMsg += "   'Delivery State'\n";
						break;
					
					case "ld_address1": errorMsg += "   'Delivery Address'\n";
						break;
					case "ld_zip": errorMsg += "   'Delivery State'\n";
						break;
					case 'order_reg_num': errorMsg += "   'Order/Registration Number'\n";
						break;
					case 'claim01': errorMsg += "   'Claim Invoice'\n";
						break;
					
				}
				
			}
			
		}
		
		
		//special considerations
		switch( nSection ){
			case 1: //check PHONE for NaN or wrong length // also validate email
			case 4:
					var phone = oForm['pf1'].value + oForm['pf2'].value + oForm['pf3'].value + oForm['pfext'].value;
					if( checkPhone( phone ) ){
						errorMsg += ( errorMsg.indexOf("   'Primary Phone'\n") == -1 )? "   'Primary Phone'\n" : "" ;
					}
					if( !checkEmail( oForm['email'].value ) ){
						errorMsg += ( errorMsg.indexOf("   'Email'\n") == -1 )? "   'Email'\n" : "" ;
					}
				break;
			case 2: //check ZIP for NaN or wrong length
//					if( checkZip( oForm['pu_zip'].value ) ){
//						errorMsg += ( errorMsg.indexOf("   'Pick Up Zip Code'\n") == -1 )? "   'Pick Up Zip Code'\n" : "" ;
//					}
//					if( checkZip( oForm['d_zip'].value ) ){
//						errorMsg += ( errorMsg.indexOf("   'Delivery Zip Code'\n") == -1 )? "   'Delivery Zip Code'\n" : "" ;
//					}
				break;
		}
		
		return errorMsg;
		//alert( nSection + '\n\n' + errorHead + errorMsg + errorFoot );	
		
	}
	
	
	/////////////////////////////
	// claim form
	function validateClaim( oForm, nSection ){
		
		var errorMsg = doValidate( oForm, nSection );
		if( errorMsg != "" ){ 
			alert( errorHead + errorMsg + errorFoot );
			return false;	
		}
		else{ return true;}
		
	}
	
	
	/////////////////////////////
	// pro/regress estimate form
	
	var step = 1;
	function advanceStep( oForm, nSection, direction ){

		var errorMsg = ( direction == 'next' )? doValidate( oForm, nSection ) : "" ;
		//var errorMsg = ""; // :: DEBUG ::
		if( errorMsg == "" ){
		
			var curStep = "";
			
			step = ( direction == 'next' )? step+1 : step-1 ;
			if( step == 4 ){ oForm.submit(); return; } //submit form when moving to the proverbial "step 4"
			
			for( i=1; i<4; i++){
				document.getElementById( 'step' + i ).style.display = "none";
				curStep += "<div class='" + ( ( i == step )? "curStep" : "nonStep" ) + "'>Step " +i+ "</div>";
			}
						
			document.getElementById( 'step' + step ).style.display = "block";
			document.getElementById( 'vbEstReqStep' ).innerHTML = curStep;
		
		}else{
			alert( errorHead + errorMsg + errorFoot );
		}
				
	}
	
/*
Hides/displays number field for rooms in MOVING DETAILS section. Also focuses/selects field's value
*/

	function getNumRooms( oSender, oVictim ){

		if( oSender.checked ){
			document.getElementById( oVictim ).style.display = 'inline';
			document.getElementById( oVictim ).select();
		}else{
			document.getElementById( oVictim ).style.display = 'none';
			document.getElementById( oVictim ).value=0;
		}

	}
	
	
////////////////////////////////////////////////
// Contact Us form

	var contactList = " fname, lname, email";
	function validateMe( oForm ){
		
		var errorMsg = "";
		
		for( i = 0; i < oForm.length; i++ ){
			
			if( contactList.indexOf( oForm[i].name ) != -1 && oForm[i].value == "" && oForm[i].type == "text" ){
				
				switch( oForm[i].name ){
					
					case 'fname': errorMsg += "   'First Name'\n";
						break;
					case 'lname': errorMsg += "   'Last Name'\n";
						break;
					case 'email': errorMsg += "   'Email'\n";
						break;
			
				}
				
			}
			
		}
		
		if( !checkEmail( oForm['email'].value ) ){
			errorMsg += ( errorMsg.indexOf("   'Email'\n") == -1 )? "   'Email'\n" : "" ;
		}
		
		if( errorMsg == "" ){ return true; }
		else{
			alert( errorHead + errorMsg + errorFoot );
			return false;
		}
		
	}
	
	
/* ///////////////////////////// CALENDAR
Gets all the month, day, year information
*/

	var Today = new Date;
	var Year = Today.getFullYear();	
		var LeapYear = ( ( Year - 2004 ) % 4 == 0 )? true : false;
	var Month = Today.getMonth();
		var NameOfMonth = new Array();
			NameOfMonth[0] = "January";
			NameOfMonth[1] = "February";
			NameOfMonth[2] = "March";
			NameOfMonth[3] = "April";
			NameOfMonth[4] = "May";
			NameOfMonth[5] = "June";
			NameOfMonth[6] = "July";
			NameOfMonth[7] = "August";
			NameOfMonth[8] = "September";
			NameOfMonth[9] = "October";
			NameOfMonth[10] = "November";
			NameOfMonth[11] = "December";
	var Day = Today.getDate();
		var DaysInMonth = new Array();
			DaysInMonth[0] = 31;
			DaysInMonth[1] = ( LeapYear )? 29 : 28 ;
			DaysInMonth[2] = 31;
			DaysInMonth[3] = 30;
			DaysInMonth[4] = 31;
			DaysInMonth[5] = 30;
			DaysInMonth[6] = 31;
			DaysInMonth[7] = 31;
			DaysInMonth[8] = 30;
			DaysInMonth[9] = 31;
			DaysInMonth[10] = 30;
			DaysInMonth[11] = 31;
		

	function getMoveMonth(){
		for( i = 0; i < NameOfMonth.length; i++ ){
			var CurrentMonth = ( i == Month )? 'selected="selected"' : "" ;
			document.write( '<option value="' + (i+1) + '" ' + CurrentMonth + '> ' + NameOfMonth[i] + '</option>' );
		}
	}
	
	function getMoveDay(){
		for( i = 0; i < DaysInMonth[Month]; i++ ){
			var CurrentDay = ( (i+1) == Day )? 'selected="selected"' : "" ;
			document.write( '<option value="' + (i+1) + '" ' + CurrentDay + '> ' + (i+1) + '</option>' );
		}
	}
	
	function getMoveYear(){
		document.write( '<option value="' + (Year) + '" selected="selected"> ' + (Year) + '</option>' );
		document.write( '<option value="' + (Year+1) + '"> ' + (Year+1) + '</option>' );
	}
		
	
	//change className of oSender
	var isClicked = "";
	function changeClass( oSender, sClass ){
	
		if( oSender.id == isClicked ){ return; }
		document.getElementById( oSender.id ).className = sClass;
		
	}
	
	//send date information for calendar stuff.
	function setMD( Filter, nMonth, nDay, nYear, oSender, sClass, destination ){
		//oForm = document.vbEstimateForm;
		
		//alert( nMonth+'/'+nDay+'/'+nYear );
		
		var nDays = DaysInMonth[nMonth-1];
			
			if( nMonth == (Month+1) && nYear == Year ){
				if( Filter == "past" ){
					for( i = 1; i > Day; i++ ){
						document.getElementById( 'calNum'+i ).className = 'calNumGrey';
					}
				}else if( Filter == "future" ){
					for( i = 1; i < Day; i++ ){
						document.getElementById( 'calNum'+i ).className = 'calNumGrey';
					}
				}
			}
		
		//document.getElementById( 'calNum'+Day ).className = ( nMonth == (Month+1) && nYear == Year )? 'calNumToday' : "calNum" ;
		//document.getElementById( oSender.id ).className = sClass;
		//isClicked = oSender.id;
		
		formatDateSet( nMonth, nDay, nYear );
		
		sPrefix = getPrefix( destination );
		loadHTML( '/includes/datePulldown.php?Filter=' +Filter+ '&Month=' +nMonth+ '&Day=' +nDay+ '&Year=' +nYear+ '&Prefix=' +sPrefix, destination );
		//changeDate( nMonth, nDay, nYear, destination );
		
	}
	
	//progress or regress calendar
	function changeMonth( sDir, nMonth, nYear, Filter ){
	
		var Direction;	
		switch( sDir ){
			case "prev": nMonth--;
				break;
			case "next": nMonth++;
				break;
		}
		
		if( nMonth > 12 ){
			nMonth = 1;
			nYear++;
		}else if( nMonth < 1 ){
			nMonth = 12;
			nYear--;
		}
		
		loadHTML( '/includes/calendar.php?Filter=' +Filter+ '&Month=' +nMonth+ '&Day=1&Year=' +nYear+ '&Prefix=' +sPrefix, sPrefix+ '_holdCalendar' );
		
	}
	
	
	//show/hide calendar; position calendar using X,Y of mouse
	var canseeArray = new Array();
		canseeArray['md'] = false;
		canseeArray['ld'] = false;
		canseeArray['d'] = false;
	function showCalendar( URL, destination ){
		
		sPrefix = getPrefix( destination );
		
		if( !canseeArray[sPrefix] ){
		
			document.getElementById( destination ).style.left = ( tempX + 10 );
			document.getElementById( destination ).style.top = ( tempY - 10 );
			
			var pointer = ( URL.indexOf('?') == -1 )? '?' : '&' ;
			
			URL += pointer +'Prefix=' +sPrefix;
			//alert(URL +' ------- ' +destination);
			
			loadHTML( URL, destination );
			canseeArray[sPrefix] = true;
		
		}else{
			
			document.getElementById( destination ).innerHTML = "";
			canseeArray[sPrefix] = false;
			
		}
		
	}
	
	var sPrefix = "";
	function changeDate( oMonth, oDay, oYear, destination, Filter ){
		
		var nMonth = oMonth.value;
		var nDay = oDay.value;
		var nYear = oYear.value;
		
		//get sPrefix from oSender.name; NOTE: respect destination naming conventions!!!
		sPrefix = getPrefix( oMonth.name );
		
		var URL = '/includes/datePulldown.php?Month=' +nMonth+ '&Day=' +nDay+ '&Year=' +nYear+ '&Prefix=' +sPrefix+ '&Filter=' +Filter;
		
		if( URL.indexOf( 'Prefix' ) > -1 ){
			
			sPrefix = URL.substring( URL.indexOf( 'Prefix' ), URL.length );
			
			if( sPrefix.indexOf('&') != -1){
				sPrefix = sPrefix.substring( 7, sPrefix.indexOf( '&' ) );
				
			}else{
				sPrefix = sPrefix.substring( 7, sPrefix.length  );
			}
			
			
		}
		
		//alert(nMonth+ '/' +nDay+ '/' +nYear);
		formatDateSet( nMonth, nDay, nYear );
		loadHTML( URL, destination );
		
	}
	
	function getPrefix( sValue ){
	
		if( sValue.indexOf('_') != -1 ){
			sValue = sValue.split('_');
			return sValue[0];
		}else{
			alert( "getPrefix( sValue ): argument: sValue="+sValue+" not formatted properly!" )
		}
	}
	

	//formats date from pulldowns into a text field. This field is then referenced for the correct date...
	function formatDateSet( nMonth, nDay, nYear ){
		
		//alert(sPrefix);
		document.forms[0][sPrefix+'_fulldate'].value = nMonth+ '/' +nDay+ '/' +nYear;
	
	}
	
	
		
	//////////////////////////////////////////////////////////////////////////////////////////
	//AJAX crap
		var request;
		var dest;
		var panes = new Array( 0,1,2 );
		
	//process state change check
		function processStateChange(){
			try{
				if (request.readyState == 4){
					contentDiv = document.getElementById(dest);
					if (request.status == 200){
						response = request.responseText;
						contentDiv.innerHTML = response;
					} else {
						contentDiv.innerHTML = "Error: Status "+request.status;
					}
					
				}
			}catch(e){ alert( "Error: Status "+request.status ); return; }
		}		
			
	//creates the XMLHttpRequest object 
		function loadHTML( URL, destination ){
			
			var pointer = ( URL.indexOf('?') == -1 )? '?' : '&' ;
			URL += ( URL.indexOf('Destination') == -1 )? pointer + 'Destination=' + destination : "" ;
			dest = destination;
			
			try{
				if (window.XMLHttpRequest){
					request = new XMLHttpRequest();
					request.onreadystatechange = processStateChange;
					request.open("GET", URL, true);
					request.send(null);
				} else if (window.ActiveXObject) {
					request = new ActiveXObject("Microsoft.XMLHTTP");
					if (request) {
						request.onreadystatechange = processStateChange;
						request.open("GET", URL, true);
						request.send();
					}
				}
			}catch(e){ alert( "Error: Status "+request.status ); return; }
			
		}	


	//code for grabbing mouse position
	// Detect if the browser is IE or not.
	// If it is not IE, we assume that the browser is NS.
		var IE = document.all?true:false
	
	// If NS -- that is, !IE -- then set up for mouse capture
		if (!IE) document.captureEvents(Event.MOUSEMOVE)
	
	// Set-up to use getMouseXY function onMouseMove
		document.onmousemove = getMouseXY;
	
	// Temporary variables to hold mouse x-y pos.s
		var tempX = 0
		var tempY = 0
	
	// Main function to retrieve mouse x-y pos.s
	
	function getMouseXY(e) {
		if (IE) { // grab the x-y pos.s if browser is IE
			tempX = event.clientX + document.body.scrollLeft
			tempY = event.clientY + document.body.scrollTop
		} else {  // grab the x-y pos.s if browser is NS
			tempX = e.pageX
			tempY = e.pageY
		}  
		// catch possible negative values in NS4
		if (tempX < 0){tempX = 0}
		if (tempY < 0){tempY = 0}  
		// show the position values in the form named Show
		// in the text fields named MouseX and MouseY
		//window.status = 'X: ' + tempX + ', Y: ' + tempY;
		return true
	}
	
	
	
	
	
