function know_thy_width(){
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		winW = window.innerWidth;
		winH = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		winW = document.documentElement.clientWidth;
		winH = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		winW = document.body.clientWidth;
		winH = document.body.clientHeight;
	}
	
	document.getElementById('filter').style.width = winW + 'px';
	document.getElementById('filter').style.height = winH + 'px';
}

function get_scroll_position(){
	/*
	var vs = (document.all ? document.scrollTop : window.pageYOffset);
	//alert(vscroll);
	
	document.getElementById('filter').style.marginTop = vs + 'px';
	document.getElementById('second_step').style.marginTop = 50 + vs + 'px';
	*/
	
	var x1 = x2 = x3 = 0;
	var y1 = y2 = y3 = 0;

	if (document.documentElement) {
		x1 = document.documentElement.scrollLeft || 0;
		y1 = document.documentElement.scrollTop || 0;
	}

	if (document.body) {
		x2 = document.body.scrollLeft || 0;
		y2 = document.body.scrollTop || 0;
	}

	x3 = window.scrollX || 0;
	y3 = window.scrollY || 0;

	var x = Math.max(x1, Math.max(x2, x3));
	var y = Math.max(y1, Math.max(y2, y3));

	window.scrollTo(Math.floor(x / 2), Math.floor(y / 2));

	if (x > 0 || y > 0) {
		window.setTimeout("get_scroll_position()", 25);
	}
}

function second_step(){
	get_scroll_position();

	document.getElementById('disable_scroll_for_now').style.overflow='hidden';
	
	know_thy_width();

	month = document.z_start_form.month.value;

	document.theForm.MM.value = month;
	document.theForm.DD.value = document.z_start_form.day.value;
	document.theForm.YY.value = document.z_start_form.year.value;

	day = document.z_start_form.day.value;
	year = document.z_start_form.year.value;

	zf = document.z_start_form.zip_from.value;
	zt = document.z_start_form.zip_to.value;

	document.getElementById('date_moving').innerHTML = month + '/' + day + '/' + year;
	document.getElementById('zip_moving_from').innerHTML = zf;
	document.getElementById('zip_moving_to').innerHTML = zt;

	document.theForm.SZIP.value = zf;
	document.theForm.RZIP.value = zt;

	document.getElementById('filter').style.display = 'block';
	document.getElementById('second_step').style.display = 'block';
}

function back_to_first_step(){
	document.getElementById('disable_scroll_for_now').style.overflow='auto';
	document.getElementById('filter').style.display = 'none';
	document.getElementById('second_step').style.display = 'none';
}

function first_step(){
	var ck_zipcode = /^[0-9-\s]{5,11}$/;
	month = document.z_start_form.month.value;
	day = document.z_start_form.day.value;
	year = document.z_start_form.year.value;
	zip_from = document.z_start_form.zip_from.value;
	zip_to = document.z_start_form.zip_to.value;

	errors = [];

	if (!ck_zipcode.test(zip_from)) {
		errors[errors.length] = "Valid pickup zipcode";
	}

	if (!ck_zipcode.test(zip_to)) {
		errors[errors.length] = "Valid delivery zipcode";
	}
	
	if( month == "" || year == "" || day == ""){
		errors[errors.length] = "Please enter valid date";
	}else if( checkDate( day, month, year ) === false ){
		errors[errors.length] = "That is not a valid date";
	}else{
		var InputDate = new Date();
		var CurrentDate = new Date();

		InputDate.setDate(day);
		InputDate.setMonth(month-1);
		InputDate.setFullYear(year);
	
		if(InputDate < CurrentDate){
			errors[errors.length] = "Date entered has already passed"
		}else{
			var SixMonthsAheadDateTime = new Date();
			SixMonthsAheadDateTime.setDate(SixMonthsAheadDateTime.getDate()+180);

			if(SixMonthsAheadDateTime < InputDate){
				errors[errors.length] = "We only accept dates that are within 6 months";
			}
		}
	}

	if (errors.length > 0) {
		reportErrors(errors);
	}else{
		second_step();
	}
}

function reportErrors(errors){
	var msg = "Please Enter Valid Data for:\n";
	for (var i = 0; i<errors.length; i++) {
		var numError = i + 1;
		msg += "\n" + numError + ". " + errors[i];
	}
	alert(msg);
}

function checkDate(day,month,year){
	days_in_month = new Date(year, month, 0).getDate();
	
	if((day > 0 && day <= days_in_month) && (month > 0 && month < 13)){
		return true;
	}else{
		return false;
	}
}

function moveOnMax(field,nextFieldID){
	if(field.value.length >= field.maxLength){
		document.getElementById(nextFieldID).focus();
	}
}


function reformat_number(x){
	if(x == 'STELH'){
		areacode = new String(document.theForm.area_code.value);
		first3 = new String(document.theForm.xxx.value);
		last4 = new String(document.theForm.xxxx.value);
		document.theForm.STELH.value = areacode + first3 + last4;
	}else if(x == 'RTELH'){
		areacode = new String(document.theForm.area_code2.value);
		first3 = new String(document.theForm.xxx2.value);
		last4 = new String(document.theForm.xxxx2.value);
		document.theForm.RTELH.value = areacode + first3 + last4;
	}

	check_this_one(x);
}

/*
function reformat_number(x){
	var ck_area_code = /^[0-9]{3,3}$/;
	var ck_area_code_xxx = /^([\(])+([0-9]{3,3})+([\)])+([\s.])+([0-9]{3,3})$/;
	var ck_area_code_xxx_xxxx = /^([\(])+([0-9]{3,3})+([\)])+([\s.])+([0-9]{3,3})+([\-])+([0-9]{4,4})$/;
	
	if(x == 'phone_1'){
		x_string = document.theForm.phone_1.value;
		//x_length = document.theForm.phone_1.maxLength;
		//y_string = document.theForm.phone.value;
		
		if( ck_area_code.test(x_string) && (x_string.substring(0,1) != '(' && x_string.substring(5,5) != ')' )){
			document.theForm.phone_1.value = '(' + x_string.substring(0,3) + ') ' + x_string.substring(4);
		}else if(x_string.substring(0,1) == '(' || x_string.substring(5,5) == ')'){
			document.theForm.phone_1.value = document.theForm.phone_1.value;
		}
	
		if( ck_area_code_xxx.test(x_string) ){
			document.theForm.phone_1.value = document.theForm.phone_1.value + '-';
		}else{
			document.theForm.phone_1.value = document.theForm.phone_1.value;
		}

		if( ck_area_code_xxx_xxxx.test(x_string) ){
			document.theForm.phone_1.value = document.theForm.phone_1.value;
		}else{
			document.theForm.phone_1.value = document.theForm.phone_1.value;
		}
	
		if(document.theForm.phone_1.maxLength == 14){
			document.theForm.phone.value = x_string.replace('(','');
			x_string_1 = document.theForm.phone.value;
			document.theForm.phone.value = x_string_1.replace(')','');
			x_string_2 = document.theForm.phone.value;
			document.theForm.phone.value = x_string_2.replace(' ','');
			x_string_3 = document.theForm.phone.value;
			document.theForm.phone.value = x_string_3.replace('-','');
		
			check_this_one('phone');
		}
	}else if(x == 'phone_2'){
		x_string = document.theForm.phone_2.value;
		//x_length = document.theForm.phone_2.maxLength;
		//y_string = document.theForm.phone2.value;
		
		if( ck_area_code.test(x_string) && (x_string.substring(0,1) != '(' && x_string.substring(5,5) != ')' )){
			document.theForm.phone_2.value = '(' + x_string.substring(0,3) + ') ' + x_string.substring(4);
		}else if(x_string.substring(0,1) == '(' || x_string.substring(5,5) == ')'){
			document.theForm.phone_2.value = document.theForm.phone_2.value;
		}
	
		if( ck_area_code_xxx.test(x_string) ){
			document.theForm.phone_2.value = document.theForm.phone_2.value + '-';
		}else{
			document.theForm.phone_2.value = document.theForm.phone_2.value;
		}

		if( ck_area_code_xxx_xxxx.test(x_string) ){
			document.theForm.phone_2.value = document.theForm.phone_2.value;
		}else{
			document.theForm.phone_2.value = document.theForm.phone_2.value;
		}
	
		if(document.theForm.phone_2.maxLength == 14){
			document.theForm.phone2.value = x_string.replace('(','');
			x_string_1 = document.theForm.phone2.value;
			document.theForm.phone2.value = x_string_1.replace(')','');
			x_string_2 = document.theForm.phone2.value;
			document.theForm.phone2.value = x_string_2.replace(' ','');
			x_string_3 = document.theForm.phone2.value;
			document.theForm.phone2.value = x_string_3.replace('-','');
		
			check_this_one('phone2');
		}
	}
}
*/

function upon_load(){
	Defaults();
	
	document.theForm.SNAME.value = '';
	document.theForm.ROOMS.value = '';
	document.theForm.EMAIL.value = '';
	document.theForm.STELH.value = '';
	//document.theForm.ext.value = '';
	document.theForm.RTELH.value = '';
	document.theForm.area_code.value = '';
	document.theForm.xxx.value = '';
	document.theForm.xxxx.value = '';
	document.theForm.area_code2.value = '';
	document.theForm.xxx2.value = '';
	document.theForm.xxxx2.value = '';
	document.theForm.MM.value = '';
	document.theForm.DD.value = '';
	document.theForm.YY.value = '';
	document.theForm.SZIP.value = '';
	document.theForm.RZIP.value = '';

	document.z_start_form.month.value = '';
	document.z_start_form.day.value = '';
	document.z_start_form.year.value = '';
	document.z_start_form.zip_from.value = '';
	document.z_start_form.zip_to.value = '';

	//check_this_one('SNAME');
	check_this_one('ROOMS');
	check_this_one('EMAIL');
	check_this_one('STELH');
	check_this_one('RTELH');

	var two_weeks_from_now = new Date();
	two_weeks_from_now.setDate(two_weeks_from_now.getDate()+14);
	
	//alert(two_weeks_from_now.getDate());
	
	day = new String(two_weeks_from_now.getDate());
	month = new String(two_weeks_from_now.getMonth()+1);
	year = new String(two_weeks_from_now.getFullYear());

	if(day.length < 2){
		day = '0' + day;
	}
	
	if(month.length < 2){
		month = '0' + month;
	}

	document.z_start_form.month.value = month;
	document.z_start_form.day.value = day;
	document.z_start_form.year.value = year;
}

function check_this_one(x){
	var ck_name = /^[A-Za-z\s.]{3,50}$/;
	var ck_texts = /^[A-Za-z\s.]{3,100}$/;
	var ck_email = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	var ck_phone = /^[0-9]{10,10}$/;
	
	if(x == 'SNAME'){
		fname = document.theForm.fname.value;
		lname = document.theForm.lname.value;
		
		if((fname != '' && lname != '') && (fname != 'First' && lname != 'Last')){
			document.theForm.SNAME.value = fname + ' ' + lname;
			y = document.theForm.SNAME.value;

			if(ck_name.test(y)){
				document.getElementById('check_' + x).src = 'http://www.movers-moving-companies.com/img/tik.gif';
			}
		}else{
			document.getElementById('check_' + x).src = 'http://www.movers-moving-companies.com/img/x.gif';
		}
	}else if(x == 'remarks'){
		y = document.theForm.remarks.value;
		
		if(ck_texts.test(y)){
			document.getElementById('check_' + x).src = 'http://www.movers-moving-companies.com/img/tik.gif';
		}else{
			document.getElementById('check_' + x).src = 'http://www.movers-moving-companies.com/img/x.gif';
		}
	}else if(x == 'ROOMS'){
		y = document.theForm.ROOMS.value;
		
		if(y != ''){
			document.getElementById('check_' + x).src = 'http://www.movers-moving-companies.com/img/tik.gif';
		}else{
			document.getElementById('check_' + x).src = 'http://www.movers-moving-companies.com/img/x.gif';
		}
	}else if(x == 'EMAIL'){
		y = document.theForm.EMAIL.value;
		
		if(ck_email.test(y)){
			document.getElementById('check_' + x).src = 'http://www.movers-moving-companies.com/img/tik.gif';
		}else{
			document.getElementById('check_' + x).src = 'http://www.movers-moving-companies.com/img/x.gif';
		}
	}else if(x == 'STELH'){
		y = document.theForm.STELH.value;
		
		if(ck_phone.test(y)){
			document.getElementById('check_' + x).src = 'http://www.movers-moving-companies.com/img/tik.gif';
		}else{
			document.getElementById('check_' + x).src = 'http://www.movers-moving-companies.com/img/x.gif';
		}
	}else if(x == 'RTELH'){
		y = document.theForm.RTELH.value;
		
		if(ck_phone.test(y)){
			document.getElementById('check_' + x).src = 'http://www.movers-moving-companies.com/img/tik.gif';
		}else{
			document.getElementById('check_' + x).src = 'http://www.movers-moving-companies.com/img/x.gif';
		}
	}
}

function add_extension(){
	document.getElementById('show_ext').style.display = 'block';
	document.getElementById('show_ext_link').style.display = 'none';
}

function cancel_extension(){
	document.getElementById('show_ext').style.display = 'none';
	document.getElementById('show_ext_link').style.display = 'block';
	document.theForm.ext.value = '';
}

function add_additional(){
	document.getElementById('show_additional').style.display = 'block';
	document.getElementById('show_additional_link').style.display = 'none';
	document.getElementById('show_additional_input').style.display = 'block';
}

function cancel_additional(){
	document.getElementById('show_additional').style.display = 'none';
	document.getElementById('show_additional_link').style.display = 'block';
	document.getElementById('show_additional_input').style.display = 'none';
	document.theForm.RTELH.value = '';
	document.theForm.area_code2.value = '';
	document.theForm.xxx2.value = '';
	document.theForm.xxxx2.value = '';
	check_this_one('RTELH');
}

/////////////////////////////
function onfocus_first(){
	fname = document.theForm.fname.value;
	if(fname == 'First'){
		document.theForm.fname.value = '';
		document.getElementById('fname').style.color = '#333';
		document.getElementById('fname').style.fontStyle = 'normal';
	}
}

function onblur_first(){
	fname = document.theForm.fname.value;
	if(fname == ''){
		document.theForm.fname.value = 'First';
		document.getElementById('fname').style.color = '#ccc';
		document.getElementById('fname').style.fontStyle = 'italic';
	}
}

function onfocus_last(){
	//lname_color = document.getElementById('lname').style.color;
	//lname_style = document.getElementById('lname').style.fontStyle;
	lname = document.theForm.lname.value;
	if(lname == 'Last'){
		document.theForm.lname.value = '';
		document.getElementById('lname').style.color = '#333';
		document.getElementById('lname').style.fontStyle = 'normal';
	}
}

function onblur_last(){
	//lname_color = document.getElementById('lname').style.color;
	//lname_style = document.getElementById('lname').style.fontStyle;
	lname = document.theForm.lname.value;
	if(lname == ''){
		document.theForm.lname.value = 'Last';
		document.getElementById('lname').style.color = '#ccc';
		document.getElementById('lname').style.fontStyle = 'italic';
	}
}
/////////////////////////////
