var rd = 0;

$(function() 
{
	$('#gallery a').lightBox();
});

$(document).ready (function() {
	do_form();
	$("a").tooltip({showURL: false });

	});

function isNumberKey(evt)
{
 var charCode = (evt.which) ? evt.which : evt.keyCode
 //alert(charCode);
 if (charCode == 13)
 	do_form ();
 if (charCode == 44 || charCode == 46)
 	return true;
 if (charCode > 31 && (charCode < 48 || charCode > 57))
    return false;

 return true;
}

function str_replace ( search, replace, subject ) {    // Replace all occurrences of the search string with the replacement string
    // 
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Gabriel Paderni
 
    if(!(replace instanceof Array)){
        replace=new Array(replace);
        if(search instanceof Array){//If search    is an array and replace    is a string, then this replacement string is used for every value of search
            while(search.length>replace.length){
                replace[replace.length]=replace[0];
            }
        }
    }
 
    if(!(search instanceof Array))search=new Array(search);
    while(search.length>replace.length){//If replace    has fewer values than search , then an empty string is used for the rest of replacement values
        replace[replace.length]='';
    }
 
    if(subject instanceof Array){//If subject is an array, then the search and replace is performed with every entry of subject , and the return value is an array as well.
        for(k in subject){
            subject[k]=str_replace(search,replace,subject[k]);
        }
        return subject;
    }
 
    for(var k=0; k<search.length; k++){
        var i = subject.indexOf(search[k]);
        while(i>-1){
            subject = subject.replace(search[k], replace[k]);
            i = subject.indexOf(search[k],i);
        }
    }
 
    return subject;
 
}

function do_form ()
{
	if ($("#s").val ())
	{
		if ($("#step").val() == 1)
		{
			if ($("#s").val() < 0.65 || $("#s").val() > 20)
			{
				notify('Для расчёта площади менее 0.65м2 или более 20м2 свяжитесь с менеджером по телефону (495) 398-63-95', 5);
				$("#calculatorForm").ajaxForm(options);
				return false;
			}
			else
			{
				var options = 
				{
				  target: "#complectsDiv",
				  url: "/custom_calc/ajax.php?contr=calc&method=complects",
				  success: function() 
				  {
						$("#complectsDiv").show ();
						$("#step").val (2);
				  }
				}; 

				// ïåðåäàåì îïöèè â  ajaxSubmit
				$("#calculatorForm").ajaxForm(options);
			}
		}
		if ($("#step").val() == 2 && rd)
		{
			var options = 
			{
			  target: "#thermostatsDiv",
			  url: "/custom_calc/ajax.php?contr=calc&method=thermo",
			  success: function() 
			  {
					$("#thermostatsDiv").show ();
					$("#step").val (3);
			  }
			}; 
			 
			// ïåðåäàåì îïöèè â  ajaxSubmit
			$("#calculatorForm").ajaxForm(options);
		}
		if ($("#step").val() == 3)
		{
			var options = 
			{
			  target: "#additionalsDiv",
			  url: "/custom_calc/ajax.php?contr=calc&method=add",
			  success: function() 
			  {
					$("#additionalsDiv").show ();
					$("#step").val (4);
			  }
			}; 
			 
			// ïåðåäàåì îïöèè â  ajaxSubmit
			$("#calculatorForm").ajaxForm(options);
			$("#divTotalPrice").load ("/custom_calc/ajax.php?contr=calc&method=save&rd="+rd+"&regul="+$("#thermostat").val());
			$("#divTotalPrice").show ();
		}
		if ($("#step").val() == 4)
		{
			/*var options = 
			{
			  target: "#calculatorForm",
			  url: "/ajax.php?contr=calc&method=order",
			  success: function() 
			  {
					//$("#additionalsDiv").show ();
					//$("#step").val (4);
			  }
			}; */
		
			$("#all_page").load ("/custom_calc/ajax.php?contr=calc&method=order");
			 
			// ïåðåäàåì îïöèè â  ajaxSubmit
			/*$("#calculatorForm").ajaxForm(options);
			$("#divTotalPrice").load ("/ajax.php?contr=calc&method=save&rd="+rd+"&regul="+$("#thermostat").val());
			$("#divTotalPrice").show ();*/
		}
		return true;
	}
	else
	{
		return false;
	}
}

function do_thermo ()
{
	$("#thermostatsDiv").load ("/custom_calc/ajax.php?contr=calc&method=thermo");
	$("#thermostatsDiv").show ();
	$("#step").val (3);
	
	$("#divTotalPrice").load ("/custom_calc/ajax.php?contr=calc&method=save&rd="+rd+"&regul="+$("#thermostat").val());
	$("#divTotalPrice").show ();
}

function thermo (id)
{
	$("#thermostatImg").load ("/custom_calc/ajax.php?contr=calc&method=thermo&mode=img&id="+id);
	$("#thermDesc").load ("/custom_calc/ajax.php?contr=calc&method=thermo&mode=descr&id="+id);
	
	$("#divTotalPrice").load ("/custom_calc/ajax.php?contr=calc&method=save&rd="+rd+"&regul="+$("#thermostat").val());
	$("#divTotalPrice").show ();
}

function notify(message, timeOut) {
	// Set the message using text method and chain fadeIn with it
	// apply simple setTimeout to fadeOut the message
	$('#boxd').text(message).fadeIn();
	setTimeout(function(){
		$('#boxd').fadeOut();
	}, timeOut*1000);
	
	var left = 0;
	var top = 0;
	cart_div = document.getElementById('boxd');

	left = Math.round((screen.width - cart_div.clientWidth)/2);
	top = Math.round((screen.height - cart_div.clientHeight)/2);

	//top = 100+document.documentElement.scrollTop;
	//alert(document.documentElement.scrollTop);
	//alert(top);
	cart_div.style.left = left+'px';
	cart_div.style.top = top+'px';
}

function add_dop (id, value)
{
	if (value)
	{
		$.get("/custom_calc/ajax.php?contr=calc&method=add_dop&mode=add&id="+id);
	}
	else
	{
		$.get("/custom_calc/ajax.php?contr=calc&method=add_dop&mode=del&id="+id);
	}
}


