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.documentElement.style.overflow='hidden';
	document.body.scroll='no';
	
	know_thy_width();

	month = document.z_start_form.month.value;

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

	//document.Form1.token.value = document.z_start_form.tok.value;
	//document.Form1.tokennum.value = document.z_start_form.tnum.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.Form1.from_zip.value = zf;
	document.Form1.to_zip.value = zt;

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

function back_to_first_step(){
	document.documentElement.style.overflow='auto';
	document.body.scroll='yes';
	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 == 'phone'){
		area_code = new String(document.Form1.area_code.value);
		xxx = new String(document.Form1.xxx.value);
		xxxx = new String(document.Form1.xxxx.value);
		document.Form1.phone.value = area_code + xxx + xxxx;
	}else if(x == 'phone2'){
		area_code = new String(document.Form1.area_code2.value);
		xxx = new String(document.Form1.xxx2.value);
		xxxx = new String(document.Form1.xxxx2.value);
		document.Form1.phone2.value = area_code + xxx + xxxx;
	}

	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.Form1.phone_1.value;
		//x_length = document.Form1.phone_1.maxLength;
		//y_string = document.Form1.phone.value;
		
		if( ck_area_code.test(x_string) && (x_string.substring(0,1) != '(' && x_string.substring(5,5) != ')' )){
			document.Form1.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.Form1.phone_1.value = document.Form1.phone_1.value;
		}
	
		if( ck_area_code_xxx.test(x_string) ){
			document.Form1.phone_1.value = document.Form1.phone_1.value + '-';
		}else{
			document.Form1.phone_1.value = document.Form1.phone_1.value;
		}

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

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

function upon_load(){
	if(typeof document.Form1.fname != 'undefined'){
	
	Defaults();
	
	document.Form1.name.value = '';
	document.Form1.fname.value = 'First';
	document.Form1.lname.value = 'Last';
	document.Form1.aptsize.value = '';
	document.Form1.email.value = '';
	document.Form1.phone.value = '';
	document.Form1.ext.value = '';
	document.Form1.phone2.value = '';
	document.Form1.area_code.value = '';
	document.Form1.xxx.value = '';
	document.Form1.xxxx.value = '';
	document.Form1.area_code2.value = '';
	document.Form1.xxx2.value = '';
	document.Form1.xxxx2.value = '';
	document.Form1.MM.value = '';
	document.Form1.DD.value = '';
	document.Form1.YY.value = '';
	document.Form1.from_zip.value = '';
	document.Form1.to_zip.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('name');
	check_this_one('aptsize');
	check_this_one('email');
	check_this_one('phone');
	check_this_one('phone2');

	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;
	
	
	var identifier = getCookie('identifier');
	if(identifier == "") {
		identifier = genRandomString(15);
		setCookie("identifier", identifier, 2);
	}
	var sessionIdElement = document.getElementById("session_id");
	sessionIdElement.value = identifier;

	}
}

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 == 'name'){
		fname = document.Form1.fname.value;
		lname = document.Form1.lname.value;
		
		if((fname != '' && lname != '') && (fname != 'First' && lname != 'Last')){
			document.Form1.name.value = fname + ' ' + lname;
			y = document.Form1.name.value;
			
			if(ck_name.test(y)){
				document.getElementById('check_' + x).src = 'http://www.zmoving.com/img/checkmark.gif';
			}
		}else{
			document.getElementById('check_' + x).src = 'http://www.zmoving.com/img/checkmark_blank.gif';
		}
	}else if(x == 'remarks'){
		y = document.getElementById(x).value;
		
		if(ck_texts.test(y)){
			document.getElementById('check_' + x).src = 'http://www.zmoving.com/img/checkmark.gif';
		}else{
			document.getElementById('check_' + x).src = 'http://www.zmoving.com/img/checkmark_blank.gif';
		}
	}else if(x == 'aptsize'){
		y = document.getElementById(x).value;
		
		if(y != ''){
			document.getElementById('check_' + x).src = 'http://www.zmoving.com/img/checkmark.gif';
		}else{
			document.getElementById('check_' + x).src = 'http://www.zmoving.com/img/checkmark_blank.gif';
		}
	}else if(x == 'email'){
		y = document.getElementById(x).value;
		
		if(ck_email.test(y)){
			document.getElementById('check_' + x).src = 'http://www.zmoving.com/img/checkmark.gif';
		}else{
			document.getElementById('check_' + x).src = 'http://www.zmoving.com/img/checkmark_blank.gif';
		}
	}else if(x == 'phone'){
		y = document.Form1.area_code.value + '' + document.Form1.xxx.value + '' + document.Form1.xxxx.value;

		if(ck_phone.test(y)){
			document.getElementById('check_' + x).src = 'http://www.zmoving.com/img/checkmark.gif';
		}else{
			document.getElementById('check_' + x).src = 'http://www.zmoving.com/img/checkmark_blank.gif';
		}
	}else if(x == 'phone2'){
		y = document.Form1.area_code2.value + '' + document.Form1.xxx2.value + '' + document.Form1.xxxx2.value;

		if(ck_phone.test(y)){
			document.getElementById('check_' + x).src = 'http://www.zmoving.com/img/checkmark.gif';
		}else{
			document.getElementById('check_' + x).src = 'http://www.zmoving.com/img/checkmark_blank.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.Form1.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.Form1.phone2.value = '';
	document.Form1.area_code2.value = '';
	document.Form1.xxx2.value = '';
	document.Form1.xxxx2.value = '';
	check_this_one('phone2');
}

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

function onblur_first(){
	fname = document.Form1.fname.value;
	if(fname == ''){
		document.Form1.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.Form1.lname.value;
	if(lname == 'Last'){
		document.Form1.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.Form1.lname.value;
	if(lname == ''){
		document.Form1.lname.value = 'Last';
		document.getElementById('lname').style.color = '#ccc';
		document.getElementById('lname').style.fontStyle = 'italic';
	}
}
/////////////////////////////

///FOR PHONE
var ck_number = /[0-9]/;

function write_phone_2(ctrl){
	//alert(ctrl.id);
	
	//var phon = document.form.phonewrite.value;
	var phon = ctrl.value;

	phon = phon.replace(/ /g,'');

	p = phon.split('');

	pon = [];

	y = 0;
	for(i in p){
		if( ck_number.test(p[i]) ){
			pon[y] = p[i];
			y++;
		}
	}

	if(typeof pon[3] != 'undefined'){
		var str = '(';
	}else{
		var str = '';
	}

	for(x in pon){
		str+= pon[x];

		if(x == 2 && pon.length > 3){
			str+= ') ';
		}else if(x == 5 && pon.length > 6){
			str+= ' - ';
		}
	}

	var ext = document.Form1.ext.value;
	var phone2 = document.Form1.phone2.value;

	if(pon.length == 10){
		if(ctrl.id == 'phone'){
			document.Form1.area_code.value = pon[0] + '' + pon[1] + '' + pon[2];
			document.Form1.xxx.value = pon[3] + '' + pon[4] + '' + pon[5];
			document.Form1.xxxx.value = pon[6] + '' + pon[7] + '' + pon[8] + '' + pon[9];
			
			if(ext == ''){
				document.Form1.ext.value = '';
			}
			
			if(phone2 == ''){
				cancel_additional();
			}

			check_this_one('phone');
		}else if(ctrl.id == 'phone2'){
			document.Form1.area_code2.value = pon[0] + '' + pon[1] + '' + pon[2];
			document.Form1.xxx2.value = pon[3] + '' + pon[4] + '' + pon[5];
			document.Form1.xxxx2.value = pon[6] + '' + pon[7] + '' + pon[8] + '' + pon[9];

			check_this_one('phone2');
		}
	}else if(pon.length < 10 || pon.length > 10){
		if(ctrl.id == 'phone'){
			document.Form1.area_code.value = '';
			document.Form1.xxx.value = '';
			document.Form1.xxxx.value = '';

			if(ext == ''){
				document.Form1.ext.value = '';
			}
			
			if(phone2 == ''){
				cancel_additional();
			}

			check_this_one('phone');
		}else if(ctrl.id == 'phone2'){
			document.Form1.area_code2.value = '';
			document.Form1.xxx2.value = '';
			document.Form1.xxxx2.value = '';

			check_this_one('phone2');
		}
	}

	if(pon.length > 10){
		ctrl.value = '(' + pon[0] + '' + pon[1] + '' + pon[2] + ') ' + pon[3] + '' + pon[4] + '' + pon[5] + ' - ' + pon[6] + '' + pon[7] + '' + pon[8] + '' + pon[9];
	}else{
		ctrl.value = str;
	}
	setCaretPositionPhone(str.length,ctrl);
	
	/*
	if(ctrl.id == 'phonewrite'){
		check_this_one('xxx');
	}else if(ctrl.id == 'phonewrite2'){
		check_this_one('xxx_2');
	}
	*/
}

function doGetCaretPositionPhone(ctrl) {
	//var ctrl = document.form.phonewrite;
	
	var CaretPos = 0;	// IE Support
	if (document.selection) {
		ctrl.focus ();
		var Sel = document.selection.createRange ();
		Sel.moveStart ('character', -ctrl.value.length);
		CaretPos = Sel.text.length;
	}else if (ctrl.selectionStart || ctrl.selectionStart == '0'){
		CaretPos = ctrl.selectionStart;
	}
	
	var end = ctrl.selectionEnd;
	
	//alert(CaretPos);
	return CaretPos;
}

function onfocus_phone(ctrl){
	d = ctrl.value;

	p = d.split('');

	setCaretPositionPhone(p.length,ctrl);
}

function setCaretPositionPhone(pos,ctrl){
	//var ctrl = document.form.phonewrite;
	
	if(ctrl.setSelectionRange){
		ctrl.focus();
		ctrl.setSelectionRange(pos,pos);
	}else if (ctrl.createTextRange) {
		var range = ctrl.createTextRange();
		range.collapse(true);
		range.moveEnd('character', pos);
		range.moveStart('character', pos);
		range.select();
	}
}
///FOR PHONE

