    // динамическая загрузка css
    function load_css(css){
        var st = document.createElement("link");
        st.setAttribute("rel","stylesheet");
        st.setAttribute("href", css);
        st.setAttribute("type", "text/css");
        document.getElementsByTagName("head")[0].appendChild(st)    
    }
    
    // показываем всплывающую подсказку
    function showTip(id, obj){                         
		$.ajax({
            type: "POST",
            url: tipUrl, 
            data: "id="+id,
            success: function(msg)
            {
                if( $("#tip_outer").html() != null ) { $("#tip_outer").remove(); };
                
                var html = '<div id="tip_outer"><div id="tip_inner"><div id="tip_close" class="close" style="display:block"></div><div id="tip_content">'+msg+'</div></div></div>';                
                
                var curPosition = getElementPosition(obj);
                
                var clientWidth = getClientWidth(); 
     
                $(html).appendTo("body");
                
                var TIP = $("#tip_outer");
                TIP.fadeIn("slow");
                
                TIP.css({"left":(curPosition.left+Math.round(curPosition.width / 3)), "top":(curPosition.top)});
                var position = TIP.position();

                if (parseInt(position.left) +  parseInt(TIP.css("width")) >= clientWidth)
                {
                    TIP.css({"left":(clientWidth - 110) - parseInt(TIP.css("width"))});
                }

                
                $(".close").each(function(){            
                    $(this).click(function(){
                        $(this).parent().parent().fadeOut("slow", function(){$(this).remove()});
                    });           
                });       		
            }    
        });	
	}	
    
	
	function getClientWidth()
	{
  			return document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientWidth:document.body.clientWidth;
	}

    function getElementPosition(elem)
	{
	    //var elem = document.getElementById(elemId);
		
	    var w = elem.offsetWidth;
	    var h = elem.offsetHeight;
		
	    var l = 0;
	    var t = 0;
		
	    while (elem)
	    {
	        l += elem.offsetLeft;
	        t += elem.offsetTop;
	        elem = elem.offsetParent;
	    }
	
	    return {"left":l, "top":t, "width": w, "height":h};
	}


    
	function submitEmailForm()
	{
		var final="";
			
		if (document.emailForm.firstname.value.length < 2)
        {        
            final = "\n * First name should containt at least 2 characters";
        }
		if (document.emailForm.lastname.value.length < 2)
        { 
            final += "\n * Last name should containt at least 2 characters";
		}
		if (document.emailForm.email.value == '')
        { 
            final += "\n * Email is empty";
        }
        else
        {
            var reg = new RegExp("[0-9a-z_]+@[\-0-9a-z_^.]+\\.[a-z]{2,3}", 'i');
            if ( !reg.test(document.emailForm.email.value) ) 
            {
                final += "\n * Email is invalid";
            }    
        } 
        if (document.emailForm.email1.value == '')
        { 
            final += "\n * Re-enter Email is empty";
        }
        else
        {
            var reg = new RegExp("[0-9a-z_]+@[\-0-9a-z_^.]+\\.[a-z]{2,3}", 'i');
            if ( !reg.test(document.emailForm.email1.value) ) 
            {               
                final += "\n * Re-enter Email is invalid";
            }    
        } 
        if( document.emailForm.email.value != document.emailForm.email1.value )  
        {
            final += "\n * Emails are not equal";
        }     
		if (final.length <1 )
        {
            return true;
        } 
		else
        {
			alert(" Please check possible errors: " +final);
			return false;
		}
	}

    
    var month = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];    

    function showSpecial(obj){
        var end_date = new Date(parseInt(obj.end_date)*1000);
        var html = '';
        html += '<div id="resTopBlockRightRow1">'+obj.special_title_1+'</div>';
        html += '<div id="resTopBlockRightRow2">'+obj.special_title_2+'</div>';
        html += '<div id="resTopBlockRightRow3">'+obj.sub_title+'</div>';
        html += '<div id="resTopBlockRightRow4"><a onclick="showTip(\'specials_id#'+obj.specials_id+'\', this)">Learn more...</a></div>';
        html += '<div id="resTopBlockRightRow5">Valid through '+ month[end_date.getMonth()] +"&nbsp;"+  end_date.getDate() +",&nbsp;"+ end_date.getFullYear() +'</div>';  //December 04, 2010
        html += '<div id="resTopBlockRightRowSeparator"></div>';         
        html += '<div id="resTopBlockRightRow6"><form onsubmit="return checkQuoteForm(this)" action="'+siteUrl+'/reservation.htm" method="post"><input type="submit" class="reserve" value=""><input type="hidden" name="student" value="1"><input type="hidden" name="offer" value="1"><input type="hidden" name="zipcode1" value="'+obj.zipcode1+'" class="zipcode1"><input type="hidden" value="Reserve Now"></form></div>';
        $("#resTopBlockRight").html(html);
        var html = '';
        html += '<div>'+obj.special_title_1+'</div>';
        html += '<div>'+obj.special_title_2+'</div>';        
        $("#specialBox").html(html);
        
        $(".specialBoxTip").html('<a class="details" onclick="showTip(\'specials_id#'+obj.specials_id+'\', this)">details...</a>');
        $("#specialBoxZip").val(obj.zipcode1);
        
    }



    function openWindow(url,w,h) {
        e=window.open(url,'_blank', 'resizable=yes,width='+w +', height=' +h +',scrollbars=yes,toolbar=no,menubar=no,status=yes,location=no');
    }     
    
    // переводим страницу вверх
    function onTop(){
        window.scrollTo(0,0);
    }
    
    function popupWindow(url) {
        window.open(url,'popupWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=yes,copyhistory=no,width=600,height=800,screenX=150,screenY=150,top=150,left=150')
    }
    
    function OnLoad() {
        new CSEOverlay("005152992814805417409:fdfktbppxmg",
                 document.getElementById("searchbox_005152992814805417409:fdfktbppxmg"),
                 document.getElementById("results_005152992814805417409:fdfktbppxmg"));
    }
    
    // проверяем зип. Возвращает объект JSON
    function checkZip(zip)
    {    
        var response = $.ajax({
              url: siteUrl+"/prices/do_zip.htm", 
              type: "POST",
              data: "zip="+zip,
              async: false
           }
        ).responseText; 
        var json = eval('(' + response + ')');
        return json;            
    }    
    
    // prices.htm#storageQuote Если клиент не ввел зип, просим ввести
    function checkQuoteForm(form)
    {        
        var zipVal = form.zipcode1.value;
        if( zipVal == 0 )
        {
            alert( "Please enter Zip Code" );
            $("#anotherZip").focus().css("border", "1px solid red");
            return false;
        }
        else
        {
            return true;
        }        
    }
    
    
    function anotherZip(form)
    {         
            $("#anotherZip").css("border", "1px solid #AAAAAA");
            var zip = $("#anotherZip").val();            
            var respZip = checkZip(zip);
            if( respZip.status == "error" )
            {
                alert( respZip.message );
                $("#anotherZip").val('').focus();
            }
            else
            {
                var deliverySumm = parseInt(respZip.settings.base_delivery.value) + parseInt(respZip.zip.delivery_factor);
                if( deliverySumm == 0 )
                {
                    deliverySumm = "FREE";
                }
                else
                {
                    deliverySumm = "$"+deliverySumm;
                }
              
                $("#zd2, #zd, #zo, #zo2, .anotherZipYellow, #origin_zip_1, #origin_zip, #destination_zip_1").val(zip);
                showSpecial(respZip.special);

                $("#zipInfoArea").html(respZip.zip.city);
                $("#zipInfoZip").html(respZip.zip.zipcode);
                $("#RegularDeliveryFee").html(deliverySumm);               
                $(".zipcode1").each(function(){
                    $(this).val(respZip.zip.zipcode);
                });
            }     
    }    
    
    
    
    
    $(document).ready(function(){                           
        $("#mailForm_1").submit(function(){
            var cust_name = $("#cust_name_1").val();
            var one = $("#one_1").val();
            var two = $("#two_1").val();
            var three = $("#three_1").val();
            var cust_email = $("#cust_email_1").val();
            var origin_zip = $("#origin_zip_1").val();
            var destination_zip = $("#destination_zip_1").val();
            if( cust_name.length < 3 )
            {
                alert( "The Full Name field must be at least 6 characters in length" );
                return false;
            }
            if( one.length != 3 && two.length != 3 && three.length != 4 )
            {
                alert( "The Phone field must contain a valid phone number" );
                return false;                                
            }
            
            var reg = new RegExp("[0-9a-z_]+@[\-0-9a-z_^.]+\\.[a-z]{2,3}", 'i');
            if ( !reg.test(cust_email) ) 
            {
                alert("The E-mail field must contain a valid email address");
                return false;
            }    
            if( origin_zip.length != 5 )
            {
                alert( "Please fill in Origin Zip Code" );
                $("#origin_zip_1").val('').focus();
                return false;
            } 
            if( destination_zip.length != 5 )
            {
                alert( "Please fill in Destination Zip Code" );
                $("#destination_zip_1").val('').focus();
                return false;
            }                                   
            return true;
        });        
        
        
        $(".detailsButton").hover(function(e){
            //$(".detailsDiv").css("left", e.pageX - 150);
            //$(".detailsDiv").css("top", e.pageY + 15);             
            $(".detailsDiv").css("display", "block");
        },function(e){
            setTimeout(function(){$(".detailsDiv").css("display", "none")}, 500);                              
        });        
        
        
        // prices.htm#localMoving Проверяем поля формы и сабмитим на дальнейший заказ
        $("#reserveButton").click(function(){
            var zo = $("#zo").val();
            var zd = $("#zd").val();
            var respZo = checkZip(zo);
            var respZd = checkZip(zd);
            if( respZo.status == "OK" && respZd.status == "OK" )
            {
                $("#localMovingForm").submit();
            }
            else
            {
                if( respZo.status == "error" )
                {
                    alert( "Start Location " + respZo.message );
                }
                if( respZd.status == "error" )
                {
                    alert( "Destination Location " + respZd.message );
                }            
            }        
        });
        
        $("#reserveButton2").click(function(){
            var zo = $("#zo2").val();
            var zd = $("#zd2").val();
            var respZo = checkZip(zo);
            var respZd = checkZip(zd);
            if( respZo.status == "OK" && respZd.status == "OK" )
            {
                $("#localMovingForm2").submit();
            }
            else
            {
                if( respZo.status == "error" )
                {
                    alert( "Start Location " + respZo.message );
                }
                if( respZd.status == "error" )
                {
                    alert( "Destination Location " + respZd.message );
                }            
            }        
        });        

        
        $("#mailForm").submit(function(){
            var cust_name = $("#cust_name").val();
            var one = $("#one").val();
            var two = $("#two").val();
            var three = $("#three").val();
            var cust_email = $("#cust_email").val();
            var origin_zip = $("#origin_zip").val();
            if( cust_name.length < 3 )
            {
                alert( "The Full Name field must be at least 6 characters in length" );
                return false;
            }
            if( one.length != 3 && two.length != 3 && three.length != 4 )
            {
                alert( "The Phone field must contain a valid phone number" );
                return false;                                
            }
            
            var reg = new RegExp("[0-9a-z_]+@[0-9a-z_^.]+\\.[a-z]{2,3}", 'i');
            if ( !reg.test(cust_email) ) 
            {
                alert("The E-mail field must contain a valid email address");
                return false;
            }    
            if( origin_zip.length != 5 )
            {
                alert( "Please fill in Origin Zip Code" );
                $("#origin_zip").val('').focus();
                return false;
            }                        
            return true;
        });
        
        
        
        // prices.htm#localMoving Кнопка подсчета цены за переезд
        $("#localMovingPrice").click(function(){
            var data = new Object();
            data.zo = $("#zo").val();
            data.zd = $("#zd").val();
            data.containers = $("#containers").val();
            
            
            var respZo = checkZip(data.zo);
            var respZd = checkZip(data.zd);
            if( respZo.status == "OK" && respZd.status == "OK" )
            {
                $.ajax({
                    type: "POST",
                    url: siteUrl+"/prices/local_moving_price.htm", 
                    data: data,
                    success: function(json)
                    {
          		        var response = eval('(' + json + ')');
          		        if( response.status == "OK" )
                        {
                            $("#localMovingPriceTotal").html("$"+response.tp);
                            var zoZipcode = response.zo.zipcode;
                            var zoCity = response.zo.city;
                            var zdZipcode = response.zd.zipcode;
                            var zdCity = response.zd.city; 
                            $("#zoInfo").html('<div>'+zoZipcode+'</div><div>'+zoCity+'</div>');
                            $("#zdInfo").html('<div>'+zdZipcode+'</div><div>'+zdCity+'</div>');                          
                        }
                        else
                        {
                            alert(response.message);	                    
                        }
                    }    
                });                
            }
            else
            {
                if( respZo.status == "error" )
                {
                    alert( "Start Location " + respZo.message );
                }
                if( respZd.status == "error" )
                {
                    alert( "Destination Location " + respZd.message );
                }            
            }                             
        });
        
        $("#localMovingPrice2").click(function(){
            var data = new Object();
            data.zo = $("#zo2").val();
            data.zd = $("#zd2").val();
            data.zd_days = $("#zd_days").val();
            data.zo_days = $("#zo_days").val();
            data.containers = $("#containers2").val();
            
            
            var respZo = checkZip(data.zo);
            var respZd = checkZip(data.zd);
            if( respZo.status == "OK" && respZd.status == "OK" )
            {
                $.ajax({
                    type: "POST",
                    url: siteUrl+"/prices/local_moving_price_day.htm", 
                    data: data,
                    success: function(json)
                    {
          		        var response = eval('(' + json + ')');
          		        if( response.status == "OK" )
                        {
                            $("#localMovingPriceTotalWs").html("$"+response.tp);
                            var zoZipcode = response.zo.zipcode;
                            var zoCity = response.zo.city;
                            var zdZipcode = response.zd.zipcode;
                            var zdCity = response.zd.city; 
                            $("#zoInfoWs").html('<div>'+zoZipcode+'</div><div>'+zoCity+'</div>');
                            $("#zdInfoWs").html('<div>'+zdZipcode+'</div><div>'+zdCity+'</div>');                          
                        }
                        else
                        {
                            alert(response.message);	                    
                        }
                    }    
                });                
            }
            else
            {
                if( respZo.status == "error" )
                {
                    alert( "Start Location " + respZo.message );
                }
                if( respZd.status == "error" )
                {
                    alert( "Destination Location " + respZd.message );
                }            
            }                             
        });        
        
        // prices.htm Storage форма
        $("#topFormButton").click(function(){
            var zip = $("#resZipVal").val();
            var respZip = checkZip(zip);
            if( respZip.status == "error" )
            {
                alert( respZip.message );
                $("#resZipVal").val('').focus();
            }
            else
            {
                $("#topForm").submit();
            }            
        });
        
                
        // prices.htm Storage форма
        $("#storageEnter").click(function(){
            var zip = $("#resZip").val();
            var respZip = checkZip(zip);
            if( respZip.status == "error" )
            {
                alert( respZip.message );
                $("#resZip").val('').focus();
            }
            else
            {
                $("#resZipForm").submit();
            }            
        });
        
        // prices.htm#storageQuote кнопка "другой зип"
        $("#anotherZipSubmit").click(function(){
             anotherZip();              
        });


        $("#anotherZip").keyup(function(e){      
             if( $("#anotherZip").val().length == 5 && e.keyCode == 13 )
             {                
                anotherZip(); 
             }                            
        });        
        
        $("#zipButton").click(function(){
            var zip = $("#zip").val(); 
            
            if( zip == '' || zip == 'Enter Zip' || zip == null )
            {
                alert( 'Enter Zip!' );
                $("#zip").focus();
                return false;
            } 
            else 
            {
                $.ajax({
                    type: "POST",
                    url: '/zip.htm', 
                    data: "ip="+ip+"&referer="+referer+"&http_user_agent="+http_user_agent+"&zip="+zip,
                    success: function(msg)
                    {
                        $("#zipForm").submit();
                    }    
                });                 
            }                     
        });
        
        
        $("#readFull").click(function(){
            $("#thirdParagraph").slideToggle("slow", function(){
                if( $("#readFull").attr("src") == "-fimages/readFull.gif" )
                {
                    $("#readFull").attr("src", "-fimages/articleUp.gif");     
                }
                else
                {
                    $("#readFull").attr("src", "-fimages/readFull.gif");  
                }
            });
                         
        });                       

        
        $("#zip").focus( function(){
            $(this).val('');        
        });         
    });