function dirty_date_check(MM,DD,YY){
	//date check
	if( MM == "" || YY == "" || DD == ""){
		result_set = false;
	}else if( checkDate( DD, MM, YY ) === false ){
		result_set = false;
	}
	else{
	    var InputDate = new Date();
	    var CurrentDate = new Date();
	
	    InputDate.setDate(DD);
	    InputDate.setMonth(MM-1);
	    InputDate.setFullYear(YY);
	
	    if(InputDate < CurrentDate){
			result_set = false;
	    }
	    else{
	    var SixMonthsAheadDateTime = new Date();
	    SixMonthsAheadDateTime.setDate(SixMonthsAheadDateTime.getDate()+180);
	
	    if(SixMonthsAheadDateTime < InputDate){
			result_set = false;
	    }else{
			result_set = true;
	    }
		}
	}
	
	return result_set;
	//end of date check
}

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 == 'fname' || x == 'lname'){
		fname = document.form.fname.value;
		lname = document.form.lname.value;
		y = fname + ' ' + lname;
		
		if((ck_name.test(fname) && (fname != '' && fname.toLowerCase() != 'first')) && (ck_name.test(lname) && (lname != '' && lname.toLowerCase() != 'last'))){
			document.getElementById('check_name').style.background = "url('img/icon_complete.gif') no-repeat bottom left";
			document.form.name.value = y;
		}else{
			document.getElementById('check_name').style.background = "url('img/icon_null.gif') no-repeat bottom left";
			document.form.name.value = '';
		}
	}else if(x == 'rooms'){
		y = document.form.rooms.value;
		
		if(y != ''){
			document.getElementById('check_' + x).style.background = "url('img/icon_complete.gif') no-repeat bottom left";
		}else{
			document.getElementById('check_' + x).style.background = "url('img/icon_null.gif') no-repeat bottom left";
		}
	}else if(x == 'email'){
		y = document.form.email.value;
		
		if(ck_email.test(y)){
			document.getElementById('check_' + x).style.background = "url('img/icon_complete.gif') no-repeat bottom left";
		}else{
			document.getElementById('check_' + x).style.background = "url('img/icon_null.gif') no-repeat bottom left";
		}
	}else if(x == 'area_code' || x == 'first_3' || x == 'last_4'){
		area_code = new String(document.form.area_code.value);
		xxx = new String(document.form.first_3.value);
		xxxx = new String(document.form.last_4.value);

		y = area_code + xxx + xxxx;

		if(ck_phone.test(y)){
			document.getElementById('check_phone').style.background = "url('img/icon_complete.gif') no-repeat bottom left";
		}else{
			document.getElementById('check_phone').style.background = "url('img/icon_null.gif') no-repeat bottom left";
		}
	}else if(x == 'area_code_2' || x == 'xxx_2' || x == 'xxxx_2'){
		area_code = new String(document.form.area_code_2.value);
		xxx = new String(document.form.xxx_2.value);
		xxxx = new String(document.form.xxxx_2.value);

		y = area_code + xxx + xxxx;

		if(ck_phone.test(y)){
			document.getElementById('check_phone2').style.background = "url('img/icon_complete.gif') no-repeat bottom left";
		}else{
			document.getElementById('check_phone2').style.background = "url('img/icon_null.gif') no-repeat bottom left";
		}
	}else if(x == 'date'){
		month = document.form.month.value;
		day = document.form.day.value;
		year = document.form.year.value;
	
		if(dirty_date_check(month,day,year) === true){
			document.getElementById('check_date').style.background = "url('img/icon_complete.gif') no-repeat bottom left";
		}else{
			document.getElementById('check_date').style.background = "url('img/icon_null.gif') no-repeat bottom left";
		}
	}
}

function know_thy_width(){
	//alert('HEY');
	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 gradient(id, level)
{
	var box = document.getElementById(id);
	box.style.opacity = level;
	box.style.MozOpacity = level;
	box.style.KhtmlOpacity = level;
	box.style.filter = "alpha(opacity=" + level * 100 + ")";
	box.style.display="block";
	return;
}


function fadein(id) 
{
	var level = 0;
	while(level <= 1)
	{
		setTimeout( "gradient('" + id + "'," + level + ")", (level* 1000) + 10);
		level += 0.01;
	}
}


// Open the lightbox

function step_1_done(){

	pickup = document.zipform.pickup.value;
	delivery = document.zipform.delivery.value;
	//month = document.zipform.month.value;
	//day = document.zipform.day.value;
	//year = document.zipform.year.value;

	//document.getElementById('moving_date').innerHTML='Move Date: ' + month + '/' + day + '/' + year + '<input type="hidden" name="MM" value="' + month + '"> <input type="hidden" name="DD" value="' + day + '"> <input type="hidden" name="YY" value="' + year + '">';
	document.getElementById('moving_from').innerHTML='Moving From: ' + pickup;
	document.getElementById('moving_to').innerHTML='Moving To: ' + delivery;
	
	document.form.pzip.value = pickup;
	document.form.dzip.value = delivery;

}

function openbox(zipform, form, fadin){
	var ck_zipcode = /^[0-9-\s]{5,11}$/;
	
	pickup = document.zipform.pickup.value;
	delivery = document.zipform.delivery.value;
	//month = document.zipform.month.value;
	//day = document.zipform.day.value;
	//year = document.zipform.year.value;

	errors = [];

	if (!ck_zipcode.test(pickup)) {
		errors[errors.length] = "Please enter valid Pickup zipcode";
	}

	if (!ck_zipcode.test(delivery)) {
		errors[errors.length] = "Please enter 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);
	
		//alert(InputDate);

    	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"
    		}else{
        		//errors[errors.length] = InputDate;
    		}
		}
	}
	*/

	if (errors.length > 0) {
		reportErrors(errors);
		/*
		alert('Please verify that both ZIP code fields are correct to continue');
		
		if(pickup == ''){
			alert_this+= 'Pickup Zipcode';
		}
		
		if(delivery == ''){
			alert_this+= 'Delivery Zipcode';
		}
		
		if(month == ''){
			alert_this+= 'Month';
		}
		
		if(day == ''){
			alert_this+= 'Month';
		}
		*/
	}else{
		step_1_done();
	
		document.documentElement.style.overflow='hidden';
		document.body.scroll='no';
		window.scrollTo(0,0);
	
		var box = document.getElementById('box'); 
		document.getElementById('filter').style.display='block';

		var btitle = document.getElementById('boxtitle');
		btitle.innerHTML = 'Free Household Moving Quote';
  
		//form.from_zip.value = zipform.SZIP.value;
		//form.to_zip.value = zipform.RZIP.value;

		document.getElementById('zip_container_from').style.display='none';
		document.getElementById('zip_container_to').style.display='none';

		if(fadin){
			gradient("box", 0);
			fadein("box");
		}else{ 	
			box.style.display='block';
		}
	}
}


// Close the lightbox

function closebox(){
	document.getElementById('box').style.display='none';
	document.getElementById('filter').style.display='none';
	document.documentElement.style.overflow='auto';
	document.body.scroll='yes';
}

///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.form.ext.value;
	var phone2 = document.form.phonewrite2.value;

	if(pon.length == 10){
		if(ctrl.id == 'phonewrite'){
			document.form.area_code.value = pon[0] + '' + pon[1] + '' + pon[2];
			document.form.first_3.value = pon[3] + '' + pon[4] + '' + pon[5];
			document.form.last_4.value = pon[6] + '' + pon[7] + '' + pon[8] + '' + pon[9];
			
			if(ext == ''){
				document.form.ext.value = '';
			}
			
			if(phone2 == ''){
				cancel_add_phone();
			}

			check_this_one('first_3');
		}else if(ctrl.id == 'phonewrite2'){
			document.form.area_code_2.value = pon[0] + '' + pon[1] + '' + pon[2];
			document.form.xxx_2.value = pon[3] + '' + pon[4] + '' + pon[5];
			document.form.xxxx_2.value = pon[6] + '' + pon[7] + '' + pon[8] + '' + pon[9];

			check_this_one('xxx_2');
		}
	}else if(pon.length < 10 || pon.length > 10){
		if(ctrl.id == 'phonewrite'){
			document.form.area_code.value = '';
			document.form.first_3.value = '';
			document.form.last_4.value = '';

			if(ext == ''){
				document.form.ext.value = '';
			}
			
			if(phone2 == ''){
				cancel_add_phone();
			}

			check_this_one('first_3');
		}else if(ctrl.id == 'phonewrite2'){
			document.form.area_code_2.value = '';
			document.form.xxx_2.value = '';
			document.form.xxxx_2.value = '';

			check_this_one('xxx_2');
		}
	}

	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