var tabsel='';


function findAll(link)
{
            return (/^#/).exec((Prototype.Browser.WebKit ? decodeURIComponent(link.href) : link.href).replace(window.location.href.split('#')[0],''));
}
function initTab(sElementName)
{
    var clean_href = window.location.href.replace(window.location.href.split('#')[0],'');
    var opened=clean_href.substring(2);

    var tabs='#'+sElementName+' .tabs';
    var href;
    var first;
    $('#load').show();
    $('#content').hide();
$('.linkktab').click(function ()
       {
            $('#load').show();
            $('#content').hide();

         window.location.href=$(this).attr('href');
         openFKAjax($(this).attr('type'));
         return false;
        });


    $('.linkktab').each(function(){

    if (!first)
    {
        first=$(this).attr('id');
    }
    varid=$(this).attr('id');
    objvar='#'+varid;
    href=$(this).attr('href');
    $(this).attr('type',href);
    $(this).attr('href','#'+href);
    });

if (opened)
{
    opened="/"+opened;
    openFKAjax(opened);
}
else
{
    objvar='#'+first;
    openFKAjax($(objvar).attr('type'));

}
    
}





function openFKAjax(url)
{
//alert(url);
var rand = Math.random(9999);
$.ajax({
   type: "GET",
   cache: false,
   async: false,
   ifModified: true,
   url: url,
   data: "rand="+rand,
   dataType: "html",
   beforeSend: function()
   {

        $('.linkktab').each(function(){
            $(this).parent().parent().removeClass('active');
            if ($(this).attr('type') == url)
                {
            $(this).parent().parent().addClass('active');

                }

        });
   },
   success: function(html)
   {
		$('#load').hide();
		$("#content").html(html);
		$('#content').show();
   },
    error:function(x,e){
			if(x.status==0){
			alert('You are offline!!\n Please Check Your Network.');
			}else if(x.status==404){
			alert('Requested URL not found.');
			}else if(x.status==500){
			alert('Internel Server Error.');
			}else if(e=='parsererror'){
			alert('Error.\nParsing JSON Request failed.');
			}else if(e=='timeout'){
			alert('Request Time out.');
			}else {
if (x.responseText)
    {
		$('#load').hide();
		$("#content").html(x.responseText);
		$('#content').show();
    }
    else
        {
			alert('Unknow Error.\n'+x.responseText);

        }

			}
		}
 });
         return false;

}

function ordersubmit_myFormShot()
{
	$("#myFormShot").validate({
		invalidHandler: function(e, validator)
		{
			var errors = validator.numberOfInvalids();
			if (errors) {
				var message = errors == 1
					? 'Вы пропустили 1 поле. Оно будет подсвечено'
					: 'Вы пропустили ' + errors + ' полей.  Они будут подсвечены';
                                        message='Заполните пропущенные поля';
				$("div.error span").html(message);
				$("div.error").show();
			} else {
				$("div.error").hide();
			}
		},
		onkeyup: false,
		submitHandler: function()
		{
			$("div.error").hide();
//			alert("submit! use link below to go to the other step");

			var str = $("#myFormShot").serialize();
//                        $("#myForm").submit();
                        //alert(str);
						order_send(str);
			//openFKAjaxShot(str,url);
			

		},
		rules: {
			name: "required",
			email: {
				required: true,
				email: true
			},
			comment: "required"

		},
		messages: {
			name: {
				required: " "
			},
			email: {
				required: " ",
				email: "укажите правильный e-mail, например: you@yourdomain.com",
				remote: jQuery.validator.format("{0} is already taken, please enter a different address.")
			},
			comment: {
				required: " "
			}
		},
		debug:true
	});

$("#myFormShot").submit();

}	
function order_send(str)
{
		var url = '/process.php';
		var rand = Math.random(9999);
		var pars = str + '&act=orderins&rnd='+rand;

        
		$('#orderform').html('Loading<img src="/js/loader.gif">');
        orderLoadAjax(url,pars);
		
		return false;

}

function ordersubmit1111()
{
	if ($('#email').value == '' || $('#comment').value == '' )
	{
		alert('Вы ввели не все данные, необходимые для оформления заказа!');
		return false;
	}
	else
	{
		var url = '/process.php';
		//var rand = Math.random(9999);
		var pars = 'phone=' + $('#phone').val() + '&tovid=' + $('#tovid').val() + '&comment=' + $('#comment').val() + '&email=' + $('#email').val() + '&name=' + $('#name').val() + '&act=orderins';
		//pars = pars + '&rand=' + rand;

//		$('cartcontent').innerHTML = '';
                $('#orderform').html('Loading<img src="/js/loader.gif">');

                orderLoadAjax(url,pars);
//alert(url);
//		var myAjax = new Ajax.Request( url, {method: 'get', parameters: pars, onLoading: orderLoad, onComplete: orderResponse} );
		return false;
	}
}

function ordersubmit1()
{
/*	if ($('email').value == '' || $('name').value == '' )*/
	if ($('email').value == '' || $('comment').value == '' )
	{
		alert('Вы ввели не все данные, необходимые для оформления заказа!');
		return false;
	}
	else
	{
		var url = '/process.php';
		var rand = Math.random(9999);
		var pars = 'phone=' + $('phone').value + '&tovid=' + $('tovid').value + '&comment=' + $('comment').value + '&email=' + $('email').value + '&name=' + $('name').value + '&act=orderins';
		pars = pars + '&rand=' + rand;
//		$('cartcontent').innerHTML = '';
		
		var myAjax = new Ajax.Request( url, {method: 'get', parameters: pars, onLoading: orderLoad, onComplete: orderResponse} );
		return false;
	}	
}



function orderLoadAjax(url,datasend)
{
//alert(url);
var rand = Math.random(9999);
$.ajax({
   type: "GET",
   cache: false,
   async: false,
   ifModified: true,
   url: url,
   data: datasend+"&rand="+rand,
   dataType: "html",
   beforeSend: function()
   {

   },
   success: function(html)
   {
//		$('#load').hide();
		$("#orderform").html(html);
//		$('#content').show();
   },
    error:function(x,e){
			if(x.status==0){
			alert('You are offline!!\n Please Check Your Network.');
			}else if(x.status==404){
			alert('Requested URL not found.');
			}else if(x.status==500){
			alert('Internel Server Error.');
			}else if(e=='parsererror'){
			alert('Error.\nParsing JSON Request failed.');
			}else if(e=='timeout'){
			alert('Request Time out.');
			}else {
if (x.responseText)
    {
		$("#orderform").html(x.responseText);
//		$('#load').hide();
//		$("#content").html(x.responseText);
//		$('#content').show();
    }
    else
        {
			alert('Unknow Error.\n'+x.responseText);

        }

			}
		}
 });
         return false;

}



function orderLoad () {

	$('orderform').innerHTML = 'Loading<img src="/js/loader.gif">';
}
function orderResponse (originalRequest) {
	var newData = originalRequest.responseText;
	$('orderform').innerHTML = newData;
}

