/*
	Functions and variables for post_job.php

	**********************************************************
	Table of Contents

	$2B - Step 2B functions
	$3 - Step 3 Functions

*/

// Global Vars
var max_budget, open_rate_type;
var usedStepButtons = false;

// Global Functions
var validateBid, updateJobCost, updateDefaultAddMover;

/* Prevent user from accidentally navigating away and losing data */

Event.observe(window,'load',function(){
	if($('post_job_form')){
		Event.observe($('post_job_form'),'submit',function(){
			usedStepButtons = true;
		});

		window.onbeforeunload = function(){
			var skip = false;
			if($("calendar_div") && $("calendar_div2")){
				if($("calendar_div").style.visibility == "visible" ||
				   $("calendar_div2").style.visibility == "visible"
				){
					skip = true;
				}
			}

			if(skip){
				// Skip check
			} else if(!usedStepButtons && $('next_step').value != "finished"){
				return "You might lose your work unless you use the PREVIOUS STEP and NEXT STEP buttons" +
						" at the bottom of the page to go between steps. (Recommended: Cancel)";
			}
		};
	}
});

// Hide tag and put a place holder, using title attribute
function hideSection(tag,useEffects){
	var parent2 = $(tag);
	if(useEffects==undefined){
		useEffects = true;
	}

	if(!parent2.visible()){
		return false;
	}

	var dur = 0.5;
	if(useEffects){
		Effect.Fade(parent2,{ duration: dur });
	}else{
		parent2.hide();
	}

	var content =
		'<a href="#" onclick="return showSection(this);"/>(+) Change This</a>' +
		parent2.readAttribute('title');

	var newNode = document.createElement('div');
	newNode = $(newNode);
	newNode.addClassName("boxHidden");
	newNode.update(content);
	newNode.hide();

	parent2.insert( { before: newNode });

	if(useEffects)
		Effect.Appear(newNode,{ duration: dur });
	else
		newNode.show();

	return false;
}

// show or hide, depending on "hidden" attribute
function hideSiblings(thisE,callerTag){
	if(thisE == null) return false;
	var callerTag = $(callerTag);
	var showIt = callerTag.readAttribute("hidden");

	var tempNode = $(thisE).next();

	if(showIt=="0"){
		callerTag.update("(+) Show This");
		callerTag.writeAttribute("hidden","1");
		while(tempNode != null){
			tempNode.hide();
			tempNode = tempNode.next();
		}
	} else {
		callerTag.update("(-) Hide This");
		callerTag.writeAttribute("hidden","0");
		while(tempNode != null){
			tempNode.show();
			tempNode = tempNode.next();
		}
	}

	return false;
}

// for the job summary page
function hideThisTag(thisE,callerTag){
	if(thisE == null) return false;
	var callerTag = $(callerTag);
	var thisE = $(thisE);
	var showIt = callerTag.readAttribute("hidden");

	if(showIt!="1"){ // hide it
		callerTag.update("[ + SHOW ]");
		callerTag.writeAttribute("hidden","1");
		thisE.hide();
	} else { // show it
		callerTag.update("[ - HIDE ]");
		callerTag.writeAttribute("hidden","0");
		thisE.show();
	}

	return false;
}

// Show the section hidden by hideSection()
function showSection(tag){
	var e = $(tag).up().next();
	var e2 = $(tag).up();
	var dur = 0.5;

	e2.remove();
	Effect.Appear(e,{ duration: dur });
	// Effect.Fade( e2, { duration: dur } );

	return false;
}

function colorRows(table_id){
	// Color Rows alternately,
	//   also hide the table if there are no rows

	var trows = $$('table#'+table_id + ' tbody > tr');
	if(trows.length < 1){
		$(table_id).hide();
	}else{
		$(table_id).show();
	}

	trows.each(function(s) {
	    s.removeClassName('odd');
	    s.removeClassName('even');
	});
	$$('table#' + table_id + ' tbody > tr:nth-child(odd)').each(function(s) {
	    s.addClassName('odd');
	});
	$$('table#' + table_id + ' tbody > tr:nth-child(even)').each(function(s) {
	    s.addClassName('even');
	});

}

// go to step specified by stepNum
function goPrevStep(stepNum){
	usedStepButtons = true;
	$('next_step').value = stepNum;

	$('post_job_form').submit();

	return false;
}

// changes the next step value and POSTS the form
function saveThisStep(form_id,response_tag,redirect){
	usedStepButtons = true;
	if(redirect==undefined || !redirect){
		var currentStep = $(form_id).prev_step.value;
		$(response_tag).update("<img src='imgs/_icons/loading.gif' alt=''/> Saving...");

		$('next_step').value = currentStep;

		$(form_id).submit();
	} else if(redirect){
		$(form_id).request({
			method:'post',
			onSuccess: function(transport){
				$(response_tag).update("Information Saved. (" + new Date().toLocaleTimeString() + ")");
				window.location = redirect;
			},
			onFailure: function(transport){
				$(response_tag).update("Error occured while saving. (" + new Date().toLocaleTimeString() + ")");
			}
		});
	}

	return false;
}

function zebraAllTables(){
	$$('table tbody > tr:nth-child(odd)').each(function(s) {
	    s.addClassName('odd');
	});
	$$('table tbody > tr:nth-child(even)').each(function(s) {
	    s.addClassName('even');
	});
}

function goFinalStep(){
	try{
		$('next_step').value = 'final';
	}catch(e){ ; }

	return true;
}

function setAltTime(){
	var alt_t = $('alt_time'); // select box

	if(alt_t.selectedIndex < 1){
		selectIndex(alt_t,1);
	}

	//return false;
}

// Cross-browser function to select an index from a select box (multiple or not)
function selectIndex(selectElement,index){
	var el = $(selectElement);

	for(var x=0; x < el.options.length; x++){
		if(index == x){
			try{
			$(el.options[x]).selected = true;
			} catch( e ) { ; }
		} else {
			try{
			$(el.options[x]).selected = false;
			} catch( e ) { ; }
		}
	}

	return false;
}


/* **********************************************************************
/* *********** STEP 2B ************************************************** $2B
/* ********************************************************************** */

function showAll(visibleState,async){

 	if(async==undefined){
// 		setAjaxGifs(true);
 		if(visibleState){
 			var t = setTimeout('showAll(true,1)',500);
 		} else {
 			var t = setTimeout('showAll(false,1)',500);
 		}
 		return false;
 	}

 	$$('#item_location_form input').each(function(s){
 		s.checked=visibleState;
 		selectLocation(s);
 	});

 	if(!visibleState){
 		// $('post_job_form').hide();
 	}

	$$('div.boxHidden').each(function(s){
		s.remove();
	});

//	setAjaxGifs(false);
 	return false;
}

 // handles checking the location box
 function selectLocation(cbox){

	cbox = $(cbox);

	var loc_id = cbox.readAttribute('location');

	if(cbox.checked){
		$$('#post_job_form div[location="'+loc_id+'"]').each(function(s){
			s.show();
		});
	} else {
		$$('#post_job_form div[location="'+loc_id+'"]').each(function(s){
			s.hide();
		});
	}

	$('post_job_form').show();

	return false;
 }


 function loadItemsAsync(){
//		$('item_location_form').request({
//			onComplete: function(transport){
//				$('general_list').update(transport.responseText);
//				var glist = $('general_list');
//				glist.selectedIndex = Math.min(glist.options.length - 1,0);
//			}
//		});
 }

 // add multiple selected items
 function addMultipleJobItem(location_id){
 		if(!location_id || location_id < 1 || location_id == null || location_id == undefined){ return false; }

		var arSelected = new Array();
		var glist = $('general_list_'+location_id);

		if(glist.selectedIndex < 0){
			return false;
		}

		while (glist.selectedIndex > -1) {
			arSelected.push(glist.options[glist.selectedIndex]); // push the element
			addJobItem(location_id); // add the selected item
			glist.options[glist.selectedIndex].selected = false; // unselect this option
		}

		// reselect the elements
 		for (var z=0; z < arSelected.length; z++){
			arSelected[z].selected = true;
 		};

 		return false;
 }

 function addJobItem(location_id,uid,type_id,NAME,L,W,H,LB,A,D,G){
 	if(!location_id || location_id < 1 || location_id == null || location_id == undefined){ return false; }

	var glist = $('general_list_'+location_id);
	var ilist = $('item_list_'+location_id);

	var theTypeID = "";
	var newUniqueID = "";

	if(uid === undefined || uid == ''){ /* Add Row via User Interface */
		if(glist.selectedIndex < 0){
			return false;
		}

		newUniqueID = unique_row_id++;

 		/* varray:
 			0 id
 			1 name
 			2 L
 			3 W
 			4 H
 			5 Wgt
 			6 Location ID
 		*/
		var varray = getValueArray(glist);
		var cloned = $(glist.options[glist.selectedIndex]).cloneNode(true);

		// var cloned = optionTag.cloneNode(true);
		// alert("unique_attribute = newUniqueID, " + unique_attribute + ", " + newUniqueID);
		Element.writeAttribute(cloned,unique_attribute,newUniqueID);
		//Element.writeAttribute(cloned,"selected","true");

		Element.insert(ilist,{'bottom':cloned}); // add the new row

		//alert("Flag");

		// alert(ilist.options.length + " -- " + ilist.selectedIndex);
		theTypeID = varray[0];
		NAME = varray[1];
		L = varray[2];
		W = varray[3];
		H = varray[4];
		LB = varray[5];
		A = "";
		D = "";
		G = "";

	} /* Add Row Programmatically */
	else {
		newUniqueID = uid;
		theTypeID = type_id;
		var optionTag = "<option selected='true' value='|" + NAME + "|' unique_id='" + newUniqueID + "'>"+NAME+"</option>";
		ilist.insert(optionTag);
	}

	var content  = '<tr '+unique_attribute+'="' + newUniqueID + '" >';

	content += '<td class="item-1">' + NAME;
	content += '<input type="hidden" name="job_item_type_id[]" value="' + theTypeID + '" /> ';
	content += '<input type="hidden" name="job_item_unique_id[]" value="' + newUniqueID + '" /> ' + '</td>';
	content += '<td class="item-2">length <input class="int16" onkeyup="validateFieldByClass(this)" name="job_item_length[]" value="' + L + '" />  ' +
			   'width <input class="int16" onkeyup="validateFieldByClass(this)" name="job_item_width[]" value="' + W + '" /> ' +
			   'Height <input class="int16" onkeyup="validateFieldByClass(this)" name="job_item_height[]" value="' + H + '" /></td>';
	content += '<td class="item-3"><input class="int16" onkeyup="validateFieldByClass(this)" name="job_item_weight[]" value="' + LB + '" /></td>';
	content += '<td class="item-4">';

	content += '<input id="assembly_' + newUniqueID + '" onclick="toggleHiddenValue(' + location_id + ',this)" type="checkbox" location="' + location_id + '"/><input name="job_item_assembly[]" type="hidden" value="0" /> <label for="assembly_' + newUniqueID + '">assembly</label>';
	content += '<input id="disassembly_' + newUniqueID + '" onclick="toggleHiddenValue(' + location_id + ',this)" type="checkbox" location="' + location_id + '"/><input name="job_item_disassembly[]" type="hidden" value="0" /> <label for="disassembly_' + newUniqueID + '">Disassembly</label>';
	content += '<input id="glass_' + newUniqueID + '" onclick="toggleHiddenValue(' + location_id + ',this)" type="checkbox" location="' + location_id + '"/><input name="job_item_glass[]" type="hidden" value="0" /> <label for="glass_' + newUniqueID + '">glass</label>';

	content += '</td></tr>';

	$('item_details_'+location_id).insert(content);

	colorRows('item_details_table_'+location_id);

	/* Dynamically click checkboxes */
	if(A=="1" || A==1){
		$('assembly_'+newUniqueID).checked=true;
		toggleHiddenValue(location_id,$('assembly_'+newUniqueID));
	}
	if(D=="1" || D==1){
		$('disassembly_'+newUniqueID).checked=true;
		toggleHiddenValue(location_id,$('disassembly_'+newUniqueID));
	}
	if(G=="1" || G==1){
		$('glass_'+newUniqueID).checked=true;
		toggleHiddenValue(location_id,$('glass_'+newUniqueID));
	}

	selectIndex(ilist,ilist.options.length - 1); // select the last item

	return false;
 }

function removeMultipleJobItems(location_id,id){
	if(!location_id || location_id < 1 || location_id == null || location_id == undefined){ return false; }

	var arSelected = new Array();
	var index = 0;
	var ilist = $('item_list_'+location_id);

	if(ilist.selectedIndex < 0){
		return false;
	}

	while (ilist.selectedIndex > -1) {
		// arSelected.push(ilist.options[ilist.selectedIndex]); // push the element
		index = ilist.selectedIndex;
		// console.log("si: " + index);
		removeJobItem(location_id,0); // REMOVE the ITEM
		// ilist.options[ilist.selectedIndex].selected = false; // unselect this option
	}

	selectIndex(ilist,Math.min(ilist.options.length - 1, index));

	colorRows('item_details_table_'+location_id);
	analyzeWork(location_id);

	return false;
}

 function removeJobItem(location_id,id){
 	var ilist = $('item_list_'+location_id);
	if(ilist.selectedIndex < 0){
		return false;
	}
	var index = ilist.selectedIndex;
	var optionTag = ilist.options[index];
	optionTag.selected = false;

	var uniqueID = optionTag.readAttribute(unique_attribute);
	$$('['+unique_attribute+'="'+uniqueID+'"]').each(function(s){
		s.remove();
	}); // remove row
	// optionTag.remove();

	// selectIndex(ilist,Math.min(ilist.options.length - 1, index));
	// ilist.selectedIndex = Math.min(ilist.options.length - 1,index);

 	return false;
 }

//     function addSpecialItem(){
//     	return false;
//
//     }
//
//     function removeSpecialItem(){
//     	return false;
//
//     }

 function getValueArray(elem){
	var val = "" + $(elem).getValue();
	return val.split("|");
 }

 function toggleHiddenValue(location_id,cboxNode){
 	// toggles the value of the next hidden field.
 	cboxNode = $(cboxNode);

 	if(cboxNode.checked){
 		cboxNode.next().value = "1";
 	} else {
 		cboxNode.next().value = "0";
 	}

 	analyzeWork(location_id);

 }

 // fill in the extra work required select box
 function analyzeWork(location_id){
 	var work_items = new Array();
 	var work_ids = new Array();
//     	var locations = new Array();
 	var elist = $('extra_list_'+location_id);
 	var item_cbox = $$('#item_details_' + location_id + ' [type=checkbox]');
 	var custom_cbox = $$('#additional_items_tbody_' + location_id + ' [type=checkbox]');
	elist.update(); // clear list

	var row;
	var cell;
	item_cbox.each(function(e){
		if(e.checked){
			row = e.up().up();
			cell = e.up().previous('.item-1');
			work_items.push(cell.innerHTML);
			work_ids.push(row.readAttribute(unique_attribute));
//				locations.push(row.readAttribute('location'));
		}
	});
	custom_cbox.each(function(e){
		if(e.checked){
			row = e.up().up();
			cell = e.up().previous('.item-1');
			work_items.push(cell.down().value);
			work_ids.push(row.readAttribute(unique_attribute));
//				locations.push(row.readAttribute('location'));
		}
	});

	var txt = "";
	var options = new Array();
	work_items.each(function(e){
		txt = e;
		txt = '<option value="' + work_ids.shift() + '" >'+txt+'</option>';
		if(	options.indexOf(txt) < 0){ // don't allow duplicates
			options.push(txt);
			elist.insert(txt);
		}

	});

	elist.selectedIndex = -1; // select the last item
 }

 function addCustomRow(location_id,item_type,NAME,L,W,H,LB,Q,A,D,G){
 	if(!location_id || location_id < 1 || location_id == null || location_id === undefined){
 		return false;
 	}
 	var newUniqueID = unique_row_id++;
 	var content = "";
 	var Ac;
 	var Dc;
 	var Gc;

 	if(item_type == undefined){ // add blank row
 		NAME = "Custom Item";
 		item_type = 98; // id of custom large item
 		L = W = H = LB = Ac = Dc = Gc = "";
 		A = D = G = 0;
 		Q = 1;
 	}
 	else{ // add programmatically
		if(A==1 || A=="1"){
			Ac = " checked ";
		}
		if(D==1 || D=="1"){
			Dc = " checked ";
		}
		if(G==1 || G=="1"){
			Gc = " checked ";
		}
 	}
	var z=0;

	content  = '<tr '+unique_attribute+'="' + newUniqueID + '" location="' + location_id + '">';
	content += '<td class="item-1"><input type="text" name="custom_item_name[]" value="' + NAME + '"/>';
	content += '<input type="hidden" name="custom_item_type_id[]" value="' + item_type + '" /> ';
	content += '<input type="hidden" name="custom_item_location[]" value="' + location_id + '" /> </td>';
	content += '<td class="item-2">L <input class="int16" onkeyup="validateFieldByClass(this)" name="custom_item_length[]" value="' + L + '" />  ' +
			   'W <input class="int16" onkeyup="validateFieldByClass(this)" name="custom_item_width[]" value="' + W + '" /> ' +
			   'H <input class="int16" onkeyup="validateFieldByClass(this)" name="custom_item_height[]" value="' + H + '" /></td>';
	content += '<td class="item-3"><input class="int16" onkeyup="validateFieldByClass(this)" name="custom_item_weight[]" value="' + LB + '" /></td>';
	content += '<td class="item-4"><input class="int16 grzero" onkeyup="validateFieldByClass(this)" name="custom_item_quantity[]" value="' + Q + '" /></td>';
	content += '<td class="item-5">';
	content += '<input id="checkbox_' + newUniqueID + '_' + (++z) + '" type="checkbox" ' + Ac + ' onclick="toggleHiddenValue(' + location_id + ',this)" /><input name="custom_item_assembly[]" type="hidden" value="' + A + '"/> <label for="checkbox_' + newUniqueID + '_' + (z) +'">assembly</label>';
	content += '<input id="checkbox_' + newUniqueID + '_' + (++z) + '" type="checkbox" ' + Dc + ' onclick="toggleHiddenValue(' + location_id + ',this)" /><input name="custom_item_disassembly[]" type="hidden" value="' + D + '"/> <label for="checkbox_' + newUniqueID + '_' + (z) +'">Disassembly</label>';
	content += '<input id="checkbox_' + newUniqueID + '_' + (++z) + '" type="checkbox" ' + Gc + ' onclick="toggleHiddenValue(' + location_id + ',this)" /><input name="custom_item_glass[]" type="hidden" value="' + G + '"/> <label for="checkbox_' + newUniqueID + '_' + (z) + '">glass</label> </td> ';
    content += '<td class="item-6"><a onclick="return addCustomRow(' + location_id + ')" href="#"><img src="imgs/_icons/add_box.gif" alt="" /></a></td>';
    content += '<td class="item-7"><a onclick="return removeCustomRow(' + location_id + ',this.parentNode.parentNode)" href="#">';
    content += '<img src="imgs/_icons/remove_box.gif" alt="" /></a></td>';
	content += '</tr>';

	$('additional_items_tbody_' + location_id).insert(content);
	analyzeWork(location_id);
	colorRows('additional_items_table_' + location_id);

	return false;
 }

 function validateFieldByClass(ifield_){
 	var ifield = $(ifield_);
 	var result = true;
 	var grzero = false;

 	if(ifield.hasClassName('grzero')){
 		// Field must be > 0 and not blank
 		grzero = true;
 	}

 	if(ifield.hasClassName('int16')){
 		// validate 16 bit Int

		if(ifield.value == '' && !grzero){
			result = true;
		}
		else if(isNaN(ifield.value)){
			result = false;
		}else if(ifield.value.indexOf(".") >= 0){
			result = false;
		}else if(grzero && ifield.value <= 0){
			result = false;
		}
 	}

 	if(!result){
 		ifield.writeAttribute("style","background-color:#FF5F5F;");
 	} else {
 		ifield.writeAttribute("style","");
 	}

 	return result;
 }

 function removeCustomRow(l,trNode){

 	$(trNode).remove();

 	analyzeWork(l);

 	return false;
 }

 function hideOtherSections(divtag){
 	/* Function disabled for aesthetic reasons */
//		var e = $(divtag);
//
//		if(!e.previous().hasClassName("content-box") &&
//			!e.previous().hasClassName("boxHidden")
//		){
//			// hide item_location_form
//
//			return false;
//		}
//
//		hideOtherSections(e.previous()); // recursion
//
//		if(e.previous().hasClassName("content-box")){
//			hideSection(e.previous(),false);
//			// e.scrollTo();
//		}

 }

/* **********************************************************************
/* *********** STEP 3  ************************************************** $3
/* ********************************************************************** */

function showPart(idstr,atag){
	if(!validatePart(currentID)){
		return false;
	}
	$('Awhere').hide();
	$('Bwhen').hide();
	$('Cbudget').hide();
	$('Dcontact').hide();
	$('Awhere_link').removeClassName("active");
	$('Bwhen_link').removeClassName("active");
	$('Cbudget_link').removeClassName("active");
	$('Dcontact_link').removeClassName("active");

	$(idstr).show();
	currentID = idstr;
	$(idstr + "_link").addClassName("active");

	return false;
}

function goNext(){

	switch(currentID){
		case "Awhere":
			showPart('Bwhen');
			break;
		case "Bwhen":
			showPart('Cbudget');
			break;
		case "Cbudget":
			showPart("Dcontact");
			break;
		case "Dcontact":
			return validateStep3(); // post form
			break;
		default:
			break;
	}

	return false;
}

function goPrev(){
	switch(currentID){
		case "Awhere":
			return goPrevStep('2c');
			break;
		case "Bwhen":
			showPart('Awhere');
			break;
		case "Cbudget":
			showPart("Bwhen");
			break;
		case "Dcontact":
			showPart("Cbudget");
			break;
		default:
			break;
	}

	return false;
}

// Step 3 A B C D validation
function validatePart(curID){
	var is_logged_in = true;

	var errors = "";
	$$('#post_job_form span.error').each(function(s){s.remove();}); // remove errors

	try{
		var t1 = $('email1').value;
	}catch(e){ is_logged_in = false; }

	switch(curID){
		case "Dcontact":
			if( is_logged_in ){
				if($('email1').value != $('email2').value){
					errors += "The email addresses do not match. ";
				}
				if($('password1').value != $('password2').value){
					errors += "The passwords do not match. ";
				}
				if($('email1').value == ""){
					errors += "The email address cannot be blank. ";
				}
				if($('password1').value == ""){
					errors += "The password cannot be blank. ";
				}
			}
			if($('firstname').value == ""){
				errors += "First name cannot be blank. ";
			}
			if($('lastname').value == ""){
				errors += "Last name cannot be blank. ";
			}
			var pho = $('phone-number').value;
			pho = pho.replace(/\D/g, ""); // strip non-digits

			if(pho.length == 11){
				if(pho.startsWith('1')){
					pho = pho.substring(1); // strip leading 1
					$('phone-number').value = pho;
				}
			}
			if(pho.length != 10){
				errors += "Phone number must contain 10 digits. ";
			}

			if(pho == ""){
				errors += "Phone number cannot be blank. ";
			}

			var selBox = $("foundout");
			if(selBox){
				if(selBox.selectedIndex == 0){
					errors += "Please choose how you found out about CityMove.com.";
				} else if(selBox.selectedIndex == (selBox.options.length - 1) && selBox.next().getValue() == ""){
					errors += "Please specify how you found out about CityMove.com.";
				}

				if($("companyagree").checked != true){
					errors += "You must agree to the terms of service to continue.";
				}
			}

			break;
		case 'Cbudget':
			var bmin = $('min_budget').value;
			var bmax = $('max_budget').value;
			if(bmin != 'No min'){
				if(isNaN(parseInt( bmin ) ) ||
				    parseInt( bmin ) != bmin ){
					errors += "Minimum is not a valid whole number. ";
				}
			}
			if(bmax != 'No max'){
				if(isNaN(parseInt( bmax ) )  ||
				    parseInt( bmax ) != bmax ){
					errors += "Maximum is not a valid whole number. ";
				}
			}
			break;
		case 'Awhere':
			if($('address1').value == ""){
				errors += "'From' address cannot be blank. ";
				$('address1').insert({'before':'<span class="error">!!!</span>'});
			}
			if($('city1').value == ""){
				errors += "'From' city cannot be blank. ";
				$('city1').insert({'before':'<span class="error">!!!</span>'});
			}
			if($('cross_street1').value == ""){
				errors += "'From' cross street cannot be blank. ";
				$('cross_street1').insert({'before':'<span class="error">!!!</span>'});
			}
			if($('zip1').value == ""){
				// errors += "'From' zip code cannot be blank. ";
			}
			if($('address2').value == ""){
				errors += "'To' address cannot be blank. ";
				$('address2').insert({'before':'<span class="error">!!!</span>'});
			}
			if($('city2').value == ""){
				errors += "'To' city cannot be blank. ";
				$('city2').insert({'before':'<span class="error">!!!</span>'});
			}
			if($('cross_street2').value == ""){
				errors += "'To' cross street cannot be blank. ";
				$('cross_street2').insert({'before':'<span class="error">!!!</span>'});
			}
			if($('zip2').value == ""){
				// errors += "'To' zip code cannot be blank. ";
			}

			if($('sqft1').value != '' && isNaN( $('sqft1').value )){
				errors += "Location 1 Square Footage must be a whole number. ";
			}
			if($('sqft2').value != '' && isNaN( $('sqft2').value )){
				errors += "Location 2 Square Footage must be a whole number. ";
			}

			var select1 = $('location1_type');
			var select2 = $('location2_type');
			if( select1.options[select1.selectedIndex].value < 1 ){
				errors += "''From' location type option must be selected. ";
				select1.insert({'before':'<span class="error">!!!</span>'});
			}
			if( select2.options[select2.selectedIndex].value < 1 ){
				errors += "''To' location type option must be selected. ";
				select2.insert({'before':'<span class="error">!!!</span>'});
			}

			break;
		case 'Bwhen':

			break;
		default:
			break;
	}

	if(errors != ""){
		errors = "Errors: " + errors;
		$('A_errors').update(errors);
		Effect.Pulsate('A_errors',{ pulses: 3, duration: 1.5});
		return false;
	}

	$('A_errors').update();
	return true;
}

function validateStep3(){

	if(!validatePart(currentID)){
		return false;
	}

	return true;
}

function validateEmail(){
	var email1 = $('email1');
	var email2 = $('email2');

	var e = false;

	if(email1.value != email2.value ){
		e = true;
	}

	if(email1.value.search( /^[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,4}$/i ) < 0){
		e = true;
	}

	if(e){
		email1.style.borderLeft = "3px solid red";
		email2.style.borderLeft = "3px solid red";
		return false;
	} else {
		email1.style.borderLeft = "0px solid white";
		email2.style.borderLeft = "0px solid white";
		return true;
	}
}

function validatePassword(){
	var p1 = $('password1');
	var p2 = $('password2');

	var e = false;

	if(p1.value != p2.value){
		e = true;
	}

	if(p1.value == ""){
		e = true;
	}

//		if(p1.value.search( /^[A-Z0-9_]$/i ) < 0){
//			e = true;
//		}

	if(e){
		p1.style.borderLeft = "3px solid red";
		p2.style.borderLeft = "3px solid red";
		return false;
	} else {
		p1.style.borderLeft = "1px solid gray";
		p2.style.borderLeft = "1px solid gray";
		return false;
	}
}

function fixPhoneNumber(itag){
	itag = $(itag);
	var str = itag.value;
	var newstr = str.replace(/\D/g, ""); // strip non-digits
	if(newstr.length == 11 && newstr.startsWith('1')){
		newstr = newstr.substring(1);
		//itag.value = newstr;
	}
	if(newstr.length != 10){
		itag.style.border = "2px solid red";
	}else{
		itag.style.border = "";
	}
	newstr = newstr.replace(/^([0-9]{3})([0-9]{3})([0-9]{4})/, "$1-$2-$3");
	itag.value = newstr;

}

function clearAltDate(){
	selectIndex('month2',0);
	selectIndex('day2',0);
	selectIndex('year2',0);
	selectIndex('alt_time',0);

	return false;
}