
function openWind(url,width,height) {
        number = Math.round(Math.random() * 100000000);
        auxWnd = window.open(url, 'newWindow_' + number,'width=' + width + ',height=' + height + ',resizable=yes,scrollprint=yes,toolbar=no,menubar=no,location=no,status=no,directories=no,copyhistory=no,left=0,top=0');
}

function SwapImage(target, fname) {
	document[target].src = fname;
}

function DisplayFlash(path, width, height, wmode){

    text = "<object type=\"application/x-shockwave-flash\" data=\""+path+"\" width=\""+width+"\" height=\""+height+"\" wmode=\""+wmode+"\" quality=\"high\">\r\n";
    text += "<param name=\"movie\" value=\""+path+"\" />\r\n";
    text += "<param name=\"wmode\" value=\""+wmode+"\" />\r\n";
    text += "<param name=\"quality\" value=\"high\" />\r\n";
    text += "<param name=\"width\" value=\""+width+"\" />\r\n";
    text += "<param name=\"height\" value=\""+height+"\" />\r\n";
    text += "</object>";
    document.write(text);
}



function DisplayDiv(ID) {


	atts = document.getElementsByName('attributes');
	for (i = 0; i < atts.length; i++){
		item = atts[i];
		item.style.display = "none";
	}


	if(ID)
		document.getElementById(ID).style.display = (document.getElementById(ID).style.display == "none" ) ?  "" :   "none";
}

function DisplayAnimatedDiv(ID){
        var vis = document.getElementById(ID).style.display;
	  var div_id = "#"+ID;



	  if(vis=="none")
        	$(div_id).animate({height: "show"}, 500);
        else
        	document.getElementById(ID).style.display = "none";
}



function onAuthSuccess(data) {

    if(data=="") {
		$("#result-text").html("Ошибка авторизации! Неправильное имя пользователя или пароль!");
		$("#result").show();
    }
    else{
		var ss = data.split("+++");
		$("#auth").html(ss[0]);
		$("#commentform").html(ss[1]);
		$("#result-text").html("Добро пожаловать, Вы успешно авторизовались.");
		$("#result").show();
    }
}


function Auth() {
	var login = document.getElementById("login").value;
	var pwd = document.getElementById("password").value;
	var comtype ="";
	var item ="";
		
	if(document.getElementById("comtype"))
		comtype = document.getElementById("comtype").value;
	if(document.getElementById("comitem"))
		item = document.getElementById("comitem").value;
	

	$.post('/js/auth.php', {login: login, pwd: pwd, comtype: comtype, item: item}, onAuthSuccess);
}



function onAddCommentSuccess(data){

      var ss = data.split("+++");


	$("#comments").html(ss[1]);
	$("#commentform").html(ss[2]);
	$("#commenttext").attr("value", "");

	$("#result-text").html("Отзыв успешно добавлен!");
	$("#result").show();
}



function AddComment(item, type) {

	var text = $("#commenttext").attr("value");
	var rtype = $("#resptype").attr("value");
    	var add_type = $("#addtype").attr("value");

    	if(!text){
    		$("#result-text").html("Укажите текст комментария!");
			$("#result").show();

			return;
    	}


    	//добавление комментария зарегистрированным
    	if(add_type==0){
    		$.post('/js/addcomment.php', {item: item, type: type, restype: rtype, text: text}, onAddCommentSuccess);
    	}

    	//авторизация и добавление комментария
    	else if(add_type==1){

    	var login = $("#auth-login").attr("value");
    	var pwd = $("#auth-pwd").attr("value");

    	if(!login||!pwd){
    		$("#result-text").html("Укажите имя пользователя и пароль!");
			$("#result").show();
			return;
    	}

		$.post('/js/auth.php', {login: login, pwd: pwd},
		function(data){
			if(data=="") {
				$("#result-text").html("Ошибка авторизации! Неправильное имя пользователя или пароль!");
				$("#result").show();
    		}
    		else{
				$("#auth").html(data);
				$.post('/js/addcomment.php', {item: item, type: type, restype: rtype, text: text}, onAddCommentSuccess);
    		}
		});

        return;
    }

    //регистрация и добавление комментария
    else if(add_type==2){
    	var fio = $("#reg-fio").attr("value");

    	var login = $("#reg-login").attr("value");
        var pwd = $("#reg-pwd").attr("value");
        var captcha = $("#reg-captcha").attr("value");


		if(fio==""||login==""||pwd==""||captcha==""){
    		$("#result-text").html("Укажите имя, логин, пароль и код с картинки!");
			$("#result").show();
			return;
    	}

		if(login.length<4){
			$("#result-text").html("Длина логина: не менее 4-х символов!");
			$("#result").show();
			return;
		}

		if(pwd.length<4){
			$("#result-text").html("Длина пароля: не менее 4-х символов!");
			$("#result").show();
			return;
		}

    	$.post('/js/register.php', {fio: fio, login: login, pwd: pwd, captcha: captcha},
		function(data){
			var ss = data.split("+++");
			if(ss[0]!=1) {
				$("#result-text").html(ss[1]);
				$("#result").show();
    		}
    		else{
				$("#auth").html(ss[1]);
				$.post('/js/addcomment.php', {item: item, type: type, restype: rtype, text: text}, onAddCommentSuccess);
    		}
		});

    }


	//var restype = $(".resptypes").find("input:radio:checked");
    //var rtype = restype[0].value;
	//$.post('/js/addcomment.php', {item: item, type: type, restype: rtype, text: text}, onAddCommentSuccess);

}



function DeleteComment(item) {
	$.post('/js/delcomment.php', {item: item}, onAddCommentSuccess);
}

function CheckFields(action){

	//document.company.submit();
	//return;

	if(action=="companyadd")
		var required = new Array("title", "fio", "phone", "login", "pwd");
	else
		var required = new Array("title", "fio", "phone", "login");

	var required_show = new Array("торговое название организации", "Ваше имя и фамилию", "телефон", "имя пользователя", "Пароль");
	var i, j;

	for(j=0; j<required.length; j++) {
    		for (i=0; i<document.company.length; i++) {
        		if (document.company.elements[i].name == required[j] && document.company.elements[i].value == "" ) {
            				alert('Пожалуйста, введите ' + required_show[j]);
            				document.company.elements[i].focus();
            				return false;
        		}
    		}
	}

	if(action=="add"&&!document.company.elements["agree"].checked){
		alert('Вы забыли согласиться с условиями размещения информации на сайте системы Калуга-Старт');
		document.company.elements["agree"].focus();
		return false;

	}

	document.company.submit();
}


function onSendMessageSuccess(data){

	$("#result").html(data);
	$("#message").attr("value", "");

	DisplayDiv('errorform');
}


function SendErrorMessage(url) {

	var message= $("#message").attr("value");

	if(!message)
		alert('Напишите: в чем ошибка!');
	else
		$.post('/js/jsend.php', {url: url, message: message}, onSendMessageSuccess);
}

function CheckOrderFields(){

	var required = new Array("ordertext", "contacts");

	var required_show = new Array("Описание заявки", "Контакты");
	var i, j;

	for(j=0; j<required.length; j++) {
    		for (i=0; i<document.order.length; i++) {
        		if (document.order.elements[i].name == required[j] && document.order.elements[i].value == "" ) {
            				alert('Пожалуйста, введите ' + required_show[j]);
            				document.order.elements[i].focus();
            				return false;
        		}
    		}
	}

	if(!document.order.elements["agree"].checked){
		alert('Вы забыли согласиться с условиями размещения заявки на сайте системы Калуга-Старт');
		document.order.elements["agree"].focus();
		return false;

	}

	document.order.submit();
}



function onSwitchSuccess(data) {

	var ss = data.split("+++");


	if(ss[0]=="") {
		$("#result-text").html("Ошибка авторизации!");
		$("#result").animate({height: "show"}, 500);
     	}
	else{
		$("#auth").html(ss[0]);
		$("#result-text").html("Вы успешно переключились на другой филиал: "+ss[1]);
		$("#result").animate({height: "show"}, 500);
    	}
}


function SwitchFilial(id) {

	$.post('/js/switchFilial.php', {id: id}, onSwitchSuccess);
}



function SetRespType(type){
	var current_value = $("#resptype").attr("value");

    $("#commenttext").removeClass("type1");
  	$("#commenttext").removeClass("type2");

	if(type==current_value){
		$(".comment-menu a").removeClass("selected");
		$("#resptype").attr("value", 0);

	}

	else {
		$(".comment-menu a").removeClass("selected");
		$("#amenu"+type).addClass("selected");
		$("#resptype").attr("value", type);
		$("#commenttext").addClass("type"+type);
	}

	$("#commenttext").focus();

	//alert($("#resptype").attr("value"));
}



function GetCaptcha(){
	//$("#captcha").html("");
    var rand_num = Math.random();

	$("#captcha-img").attr('src', '/images/captcha.php?rand='+rand_num);
}
