﻿var ERROR_DISPLAY = 'ALERT';
var INITIAL_SERVICE = '1001';
	
var Type = '';
var Service = '';
var FromCity = '';
var FromState = '';
var FromPostal = '';
var FromCountry = '';
var FromCountryName = '';
var ToState = '';
var ToCountry = '';
var ToCountryName = '';
var Size = '';

var fromStateID = '';

function ClearFromZip() {
    fromStateID = '';
}

function GetStateIDFromZip(el) {
    el = document.getElementById(el); 
    if (el != null) {
        var s;
        $.ajax({
            async: false
            , type: "POST"
            , contentType: "application/json; charset=utf-8"
            , url: "/WebServices/AJAXHelper.asmx/GetStateIDOfZipCode"
            , data: "{ zip : '" + el.value + "' }"
            , dataType: "text"
            , dataFilter: function(data) {
            	var msg;

            	if (typeof (JSON) !== 'undefined' && typeof (JSON.parse) === 'function') {
            		msg = JSON.parse(data);
            	}
            	else {
            		msg = eval('(' + data + ')');
            	}

            	if (msg.hasOwnProperty('d')) {
            		return msg.d;
            	}
            	else {
            		return msg;
            	}

            }
            , success: function(msg) {
                s = msg;
            }
            , error: function(XMLHttpRequest, textStatus, errorThrown) {
                alert( 'error' );
            }
        });

        if (s != null) {
            return s;
        }
    }

    return '';
}


function Init()
{
    $('#ddlMovingType').val(INITIAL_SERVICE);
	MovingTypeChange($('#ddlMovingType').val());
	
	InitilizeDdlCountry('ddlMovingFromCountry')
}
function MovingTypeChange(val) 
{
	switch (val) {
	    case ('1000'):
//	        $('#trMovingZipCode').hide();
//		    $('#trAutoZipCode').hide();
//		    $('#trStorageZipCode').hide();
//		    $('#trMovingFromCountry').show();
//		    break;
		case ('1001'):
		case ('1013'):
		    $('#trMovingZipCode').show();
		    $('#trAutoZipCode').hide();
		    $('#trStorageZipCode').hide();
		    $('#trMovingFromCountry').hide();
			break;
		case ('1002'):
		    $('#trMovingZipCode').hide();
		    $('#trAutoZipCode').show();
		    $('#trStorageZipCode').hide();
		    $('#trMovingFromCountry').hide();
			break;
		case ('1010'):
		case ('1022'):
			$('#trMovingZipCode').hide();
		    $('#trAutoZipCode').hide();
		    $('#trStorageZipCode').show();
		    $('#trMovingFromCountry').hide();
			break;
	}
}

function ValidateFields(el, sp, val, err) {
    el = document.getElementById(el);
    sp = document.getElementById(sp);

    if (el != null && sp != null) {
        sp.className = 'validfields';
        if (el.value == val) {
            sp.className = 'invalidfields';
            return err;
        }
    }

    return '';
}

function ValidateZipCode(el, sp, req, inv) {
    el = document.getElementById(el);
    sp = document.getElementById(sp);

    if (el != null && sp != null) {
        sp.className = 'validfields';
        if (el.value == '') {
            sp.className = 'invalidfields';
            return req;
        }
        else if (!el.value.match(/^\d\d\d\d\d$/)) {
            sp.className = 'invalidfields';
            return inv;
        } else { 
            if (fromStateID == '') {
                fromStateID = GetStateIDFromZip(el.id);
            }
            
            if (fromStateID <= 0)  {
                sp.className = 'invalidfields';
                return inv;
            }
        }
    }

    return '';
}

function ValidateZipState(zip, to, err) {
    to = document.getElementById(to);

    if (to != null) {

        if (fromStateID == '') {
            fromStateID = GetStateIDFromZip(zip);
        }

        if (fromStateID == to.value) {
            return err;
        }
    }

    return '';
}

function Validate() {
    var Errors = '';


    switch ($('#ddlMovingType').val()) {
        case '1000':
//            Errors += ValidateFields('ddlMovingFromCountry', 'spnMovingFromCountry', '0', 'From Country - Required\n\r');
//            break;
        case '1001':
        case '1012':
        case '1013':        
            Errors += ValidateZipCode('txtMovingZipCode', 'spnMovingZipCode', 'From ZipCode - Required\n\r', 'From ZipCode - Invalid\n\r');
            break;
        case 'Storage':
            Errors += ValidateZipCode('txtStorageZipCode', 'spnStorageZipCode', 'Storage ZipCode - required\n\r', 'Storage ZipCode - Invalid\n\r');
            break;
        case '1002':
            Errors += ValidateZipCode('txtAutoZipCode', 'spnAutoZipCode', 'From ZipCode - Required\n\r', 'From ZipCode - Invalid\n\r');                       
            break;
    }
  

    if (Errors != '') {
        if (ERROR_DISPLAY == 'ALERT') {
            alert(Errors);
        }
        else {
            $('#spErrors').show();
        }
        return false;
    }
    else {
        $('#spErrors').hide();
        return true;
    }
}

function SubmitForm() 
{
    var destination = '/Results.aspx';
    var queryString = '';
    
    if( Validate() )
    {
        switch($('#ddlMovingType').val())
        {
	        case('1000'):
	        case('1001'):		        
	        case('1012'):
	        case('1013'):               
                queryString += "?Type=Moving";
                queryString += "&Service=" + $('#ddlMovingType').val();
                queryString += "&FromPostal=" + $('#txtMovingZipCode').val();
//                queryString += $('#ddlMovingType').val() == '1000' && $('#ddlMovingFromCountry').val() != '' ? "&FromCountry=" + $('#ddlMovingFromCountry').val() : '&FromCountry=1';
//                queryString += $('#ddlMovingType').val() == '1000' && $('#ddlMovingFromCountry option:selected').text() != '' ? "&FromCountryName=" + $('#ddlMovingFromCountry option:selected').text() : '';
                break;
            case('1002'):
                queryString += "?Type=Moving";
                queryString += "&Service=1002";
                queryString += "&FromPostal=" + $('#txtAutoZipCode').val();
                break;
	        case('1010'):
	        case('1022'):
		        queryString += "?Type=Storage";
                queryString += "&Service=" + $('#ddlMovingType').val();
                queryString += "&FromPostal=" + $('#txtStorageZipCode').val();
		        break;

        }
        
	    if (destination != '' && queryString != '') {
            window.location.href = destination + queryString;
        }            	    
	}
}

function CreateForm()
{
    var html = '';
    html += "<link rel='stylesheet' href='/css/ShortForm_v1001.css' type='text/css' />";
    html += "<div class='formdir_inner'>";
    html += "	<div class='formdir_field'>";
    html += "		<span class='validfields'>Service Type:</span>";
    html += "		<span class='value'>";
    html += "			<select id='ddlMovingType' onchange='MovingTypeChange(this.value)' onkeyup='MovingTypeChange(this.value)'>";
    html += "				<option value='1001'>Full Service Movers</option>";
    html += "				<option value='1002'>Auto Transport</option>";
    html += "				<option value='1000'>International Movers</option>";
    html += "				<option value='1013'>U-Load They'll Drive</option>";
    html += "				<option value='1010'>Self Storage</option>";
    html += "				<option value='1022'>Mobile Storage</option>";
    html += "			</select>";
    html += "		</span>";
    html += "	</div>";
    html += "	<div id='trAutoZipCode' style='display:none' class='formdir_field'>";
    html += "		<span id='spnAutoZipCode' class='validfields'>Zip Code: <b style='color: #ff0011'>*</b></span>";
    html += "		<span class='value'><input type='text' maxlength='5' class='zip' id='txtAutoZipCode' /><span id='spnAutoZipFinder' style='display : none' onclick='ShowZipFinder(this)'>Zip Code Finder</span></span>";
    html += "	</div>";
    html += "	<div id='trStorageZipCode' style='display:none' class='formdir_field'>";
    html += "		<span id='spnStorageZipCode' class='validfields'>Zip Code: <b style='color: #ff0011'>*</b></span>";
    html += "		<span class='value'><input type='text' maxlength='5' class='zip' id='txtStorageZipCode' /><span id='spnStorageZipFinder' style='display : none' onclick='ShowZipFinder(this)'>Zip Code Finder</span></span>";
    html += "	</div>";
    html += "	<div id='trMovingZipCode' style='display:block' class='formdir_field'>";
    html += "		<span id='spnMovingZipCode' class='validfields'>Zip Code: <b style='color: #ff0011'>*</b></span>";
    html += "		<span class='value'><input type='text' maxlength='5' class='zip' id='txtMovingZipCode' /><span id='spnMovingZipFinder' style='display : none' onclick='ShowZipFinder(this)'>Zip Code Finder</span></span>";
    html += "	</div>";
    html += "	<div id='trMovingFromCountry' style='display:none' class='formdir_field'>";
    html += "		<span id='spnMovingFromCountry' class='validfields'>Moving From Country: <b style='color: #ff0011'>*</b></span>";
    html += "		<span class='value'><select id='ddlMovingFromCountry'></select></span>";
    html += "	</div>";
    html += "	<div class='formdir_button'><input type='button' value='Next >>>' onclick='SubmitForm()'/></div>";
    html += "</div>";

    document.write(html);
    setTimeout("Init();", 1500);

}

function CreateSelectOption(val, txt, select)
{
    if( select != null && select == true )
    {
        return '<option value="' + val + '" selected="true">' + txt + '</option>';
    }
    
    return '<option value="' + val + '">' + txt + '</option>';
}

var countryIdArray = new Array(0,1,67746,67747,67748,67749,67750,67751,67752,67753,67754,67755,67756,67757,67758,67759,67760,67761,67762,67763,67764,67765,67766,67767,67768,67769,67770,67771,67772,67773,67774,67775,67776,67777,67778,67779,67780,67781,67782,67783,2,67784,67785,67786,67787,67788,67789,67790,67791,67792,67793,67794,67795,67796,67797,67798,67799,67800,67801,67802,67803,67804,67805,67806,67807,67808,67809,67810,67811,67812,67813,67814,67815,67816,67817,67818,67819,67820,67821,67822,67823,67824,67825,67826,67827,67828,67829,67830,67831,67832,67833,67834,67835,67836,67837,67838,67839,67840,67841,67842,67843,67844,67845,67846,67847,67848,67849,67850,67851,67852,67853,67854,67855,67856,67857,67858,67859,67860,67861,67862,67863,67864,67865,67866,67867,67868,67869,67870,67871,67872,67873,67874,67875,67876,67877,67878,67879,67880,67881,67882,67883,67884,67885,67886,67887,67888,67889,67890,67891,67892,67893,67894,67895,67896,67897,67898,67899,67900,67901,67902,67903,67904,67905,67906,67907,67908,67909,67910,67911,67912,67913,67914,67915,67916,67917,67918,67919,67920,67921,67922,67923,67924,67925,67926,67927,67928,67929,67930,67931,67932,67933,67934,67935,67936,67937,67938,67939,67940,67941,67942,67943,67944,67945,67946,67947,67948,67949,67950,67951,67952,67953,67954,67955,67956,67957,67958,67959,67960,67961,67962,67963,67964,67965,67966,67967,67968,67969,67970,67971,1,67972,67973,67974,67975,67976,67977,67978,67979,67980,67981,67982,67983,67984,67985,67986);
var countryNameArray = new Array("Select a country","United States of America","Afghanistan","Albania","Algeria","American Samoa","Andorra","Angola","Anguilla","Antarctica","Antigua and Barbuda","Argentina","Armenia","Aruba","Australia","Austria","Azerbaijan","Bahamas","Bahrain","Bangladesh","Barbados","Belarus","Belgium","Belize","Benin","Bermuda","Bhutan","Bolivia","Bosnia-Herzegovina","Botswana","Bouvet Island","Brazil","British Indian Ocean Territory","British Virgin Islands","Brunei Darussalam","Bulgaria","Burkina Faso","Burma","Burundi","Cameroon","Canada","Canton And Enderbury Is.","Cape Verde","Cayman Islands","Central African Rep.","Chad","Chile","China","Christmas Island","Cocos (keeling) Island","Colombia","Comoros","Congo","Cook Islands","Costa Rica","Cote D'ivoire","Croatia","Cuba","Cyprus","Czech Republic","Czechoslovakia","Democratic Kampuchea","Democratic Yemen","Denmark","Djibouti","Dominica","Dominican Republic","Dronning Maud Island","East Timor","Ecuador","Egypt","El Salvador","England","Equatorial Guinea","Estonia","Ethiopia","Faeroe Islands","Falkland Islands-Malvinas","Fiji","Finland","France","French Guiana","French Polynesia","French Southern Terr","Gabon","Gambia","Georgia","Germany","Ghana","Gibraltar","Greece","Greenland","Grenada","Guadaloupe","Guam","Guatemala","Guinea","Guinea-Bissau","Guyana","Haiti","Heard and Mcdonald Is.","Honduras","Hong Kong","Hungary","Iceland","India","Indonesia","Iran","Iraq","Ireland","Israel","Italy","Jamaica","Japan","Johnston Island","Jordan","Kazakhstan","Kenya","Kiribati","Kuwait","Kyrgyzstan","Laos","Latvia","Lebanon","Lesotho","Liberia","Libya","Liechtenstein","Lithuania","Luxembourg","Macau","Macedonia","Madagascar","Malawi","Malaysia","Maldives","Mali","Malta","Martinique","Mauritania","Mauritius","Mexico","Midway Islands","Moldova","Monaco","Mongolia","Montserrat","Morocco","Mozambique","Namibia","Nauru","Nepal","Netherlands","Netherlands Antilles","Neutral Zone","New Caledonia","New Zealand","Nicaragua","Niger","Nigeria","Niue","Norfolk Island","North Korea","Norway","Oman","Pacific Is. Trust Terr.","Pakistan","Panama","Papua New Guinea","Paraguay","Peru","Philippines","Pitcairn Islands","Poland","Portugal","Puerto Rico","Qatar","Reunion","Romania","Russia","Rwanda","Samoa","San Marino","Sao Tome and Principe","Saudi Arabia","Scotland","Senegal","Seychelles","Sierra Leone","Singapore","Slovakia","Slovenia","Solomon Islands","Somalia","South Africa","South Korea","Spain","Sri Lanka","St. Helena","St. Kitts-Nevis-Anguilla","St. Lucia","St. Pierre Et Miquelon","St. Vincent and The Grenadines","Sudan","Suriname","Svalbard and Jan Mayen Is.","Swaziland","Sweden","Switzerland","Syria","Taiwan","Tajikistan","Tanzania","Thailand","Togo","Tokelau","Tonga","Trinidad And Tobago","Tunisia","Turkey","Turkmenistan","Turks And Caicos Is.","Tuvalu","U.S. Pacific Islands","U.S. Virgin Islands","Uganda","Ukraine","United Arab Emirates","United Kingdom","United States of America","Uruguay","Uzbekistan","Vanuatu","Vatican City State","Venezuela","Vietnam","Wake Island","Wales","Wallis and Futuna Is.","Western Sahara","Yemen Arab Republic","Yugoslavia","Zaire","Zambia","Zimbabwe");
function InitilizeDdlCountry(objId)
{
    var options = '';
    for( i = 0; i < countryIdArray.length; i++ ) 
    {
        options += CreateSelectOption(countryIdArray[i], countryNameArray[i]);
    }
                    
	$('#' + objId).html(options);
}

var hdnState = '';
var hdnCity = '';
function SetZipFromZipFinder(zipcode, City, State)
{
    switch($('#ddlMovingType').val())
    {
	    case('1002'):		        
	        $('#txtAutoZipCode').val(zipcode);
		    break;
	    case('1001'):
	    case('1013'):
	        $('#txtMovingZipCode').val(zipcode);
		    break;
	    case('Storage'):
	        $('#txtStorageZipCode').val(zipcode);
		    break;
    }

	//hdnState = state;
	hdnCity = City;
	HideDhtml();
}

document.write('<div id="DhtmlDiv" class="js_form_zipfinder"><div class="jsf_close"><span onclick="HideDhtml()">Close</span></div><div class="jsf_iframe"><iframe src="/QuotesAtHome/MiscPages/ZipCodeFinder.aspx" id="iFrameZipFinder" frameborder="0" scrolling="no" width="250" height="250"></iframe></div></div>');
function ShowZipFinder(obj)
{
	findPosition(obj,"DhtmlDiv");
}
function findPosition( oElement, dhtmlDiv ) 
{
    oDiv = document.getElementById(dhtmlDiv);
    if( typeof( oElement.offsetParent ) != 'undefined' ) 
    {
        for( var posX = 0, posY = 0; oElement; oElement = oElement.offsetParent ) 
        {
            posX += oElement.offsetLeft;
            posY += oElement.offsetTop;
        }
        oDiv.style.top = posY;// - document.getElementById('divSmartLinking').scrollTop;
        if (dhtmlDiv == 'DhtmlDiv')
			oDiv.style.top = posY - 40 + 'px';// - document.getElementById('divTree').scrollTop;
        //alert(document.getElementById('divSmartLinking').scrollTop);
        oDiv.style.left = posX + 140 + 'px';
    } 
    else 
    {
        oDiv.style.top = oElement.y + 'px';
        oDiv.style.left = oElement.x + 'px';
    }
    oDiv.style.visibility = 'visible';
    oDiv.style.display = 'block';
}

function HideDhtml()
{
    document.getElementById('DhtmlDiv').style.visibility = 'hidden';
    document.getElementById('DhtmlDiv').style.display = 'block';
}
var appPath = '/';
