$(document).ready(function() {
	$('#filesurl').change(function() {
		$('#pseudfile').val($('#filesurl').val());
	});
});
function switchTab(id_block)
{
	if ( $('#'+id_block).css('display') == 'block' ) {
		$('#'+id_block).hide();
	} else {
		$('#'+id_block).show();
	}
}

function showLink(id_show, id_hide)
{
	$('#'+id_hide).hide();
	$('#'+id_show).show();
}

function showForm(id_show, id_hide, spec_param)
{
	$('#'+id_hide).hide('slow');
	$('#'+id_show).show('slow');
}

function createCookie(name,value,days) 
{
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function eraseCookie(name) 
{
	createCookie(name,"",-1);
}

function makeTabActive(id_on, id_off, id_off_2, redirect_path)
{
	eraseCookie(id_off);
	eraseCookie(id_off_2);
	eraseCookie(id_on);
	
	createCookie(id_on, 1, 1);
	window.location = redirect_path;
}

function getText(id_from, id_where)
{
	var text      = $('#'+id_from).html();
	var user_text = $('#'+id_where).html();
	
	text = jQuery.trim(text);
	
	$('#'+id_where).html(user_text+'[quote]'+text+'[/quote]');
}

function addChildNames()
{
	var ch_quantity = $('#ch_num').val();
	
	$('#child_names').html('');
	for(i = 1; i <= ch_quantity; i++)
	{
		var text = '<div class="form-field"><label for="ln" class="form-reg-text">Имя ребенка</label><input type="text" name="ch_name[]" id="ln" class="text-left" /></div>'
					+'<div class="form-field"><label for="ln" class="form-reg-text">Дата рождения (в виде 10.10.2005)</label><input type="text" name="ch_date[]" id="ln" class="text-left" /></div>';
		$('#child_names').html($('#child_names').html()+text);
	}
}

function strip_tags (str, allowed_tags) 
{
    var key = '', allowed = false;
    var matches = [];
    var allowed_array = [];
    var allowed_tag = '';
    var i = 0;
    var k = '';
    var html = '';

    var replacer = function (search, replace, str) {
        return str.split(search).join(replace);
    };

    // Build allowes tags associative array
    if (allowed_tags) {
        allowed_array = allowed_tags.match(/([a-zA-Z0-9]+)/gi);
    }

    str += '';

    // Match tags
    matches = str.match(/(<\/?[\S][^>]*>)/gi);

    // Go through all HTML tags
    for (key in matches) {
        if (isNaN(key)) {
            // IE7 Hack
            continue;
        }

        // Save HTML tag
        html = matches[key].toString();

        // Is tag not in allowed list? Remove from str!
        allowed = false;

        // Go through all allowed tags
        for (k in allowed_array) {
            // Init
            allowed_tag = allowed_array[k];
            i = -1;

            if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+'>');}
            if (i != 0) { i = html.toLowerCase().indexOf('<'+allowed_tag+' ');}
            if (i != 0) { i = html.toLowerCase().indexOf('</'+allowed_tag)   ;}

            // Determine
            if (i == 0) {
                allowed = true;
                break;
            }
        }

        if (!allowed) {
            str = replacer(html, "", str); // Custom replace. No regexing
        }
    }

    return trim(str);
}

function trim(string)
{
	return string.replace(/(^\s+)|(\s+$)/g, "");
}


function changeLink()
{
	if(this.id == 'img_1'){
		//$('#sl_1').html('<div class="sal-tb"><div class="arrow"></div></div><div class="sal-cb">'+$('#sl_1').html()+'</div><div class="sal-bb"></div>');
		$('#sl_1').addClass('active');
		$('#sl_2').html(strip_tags($('#sl_2').html()));
		$('#sl_3').html(strip_tags($('#sl_3').html()));
		$('#sl_4').html(strip_tags($('#sl_4').html()));
		$('#sl_5').html(strip_tags($('#sl_5').html()));
		$('#sl_2, #sl_3, #sl_4, #sl_5').removeClass();
	}else if(this.id == 'img_2'){
//		$('#sl_2').html('<div class="sal-tb"><div class="arrow"></div></div><div class="sal-cb">'+$('#sl_2').html()+'</div><div class="sal-bb"></div>');
		$('#sl_2').addClass('active');
		$('#sl_1').html(strip_tags($('#sl_1').html()));
		$('#sl_3').html(strip_tags($('#sl_3').html()));
		$('#sl_4').html(strip_tags($('#sl_4').html()));
		$('#sl_5').html(strip_tags($('#sl_5').html()));
		$('#sl_1, #sl_3, #sl_4, #sl_5').removeClass();
	}else if(this.id == 'img_3'){
//		$('#sl_3').html('<div class="sal-tb"><div class="arrow"></div></div><div class="sal-cb">'+$('#sl_3').html()+'</div><div class="sal-bb"></div>');
		$('#sl_3').addClass('active');
		$('#sl_2').html(strip_tags($('#sl_2').html()));
		$('#sl_1').html(strip_tags($('#sl_1').html()));
		$('#sl_4').html(strip_tags($('#sl_4').html()));
		$('#sl_5').html(strip_tags($('#sl_5').html()));
		$('#sl_2, #sl_1, #sl_4, #sl_5').removeClass();
	}else if(this.id == 'img_4'){
//		$('#sl_4').html('<div class="sal-tb"><div class="arrow"></div></div><div class="sal-cb">'+$('#sl_4').html()+'</div><div class="sal-bb"></div>');
		$('#sl_4').addClass('active');
		$('#sl_2').html(strip_tags($('#sl_2').html()));
		$('#sl_3').html(strip_tags($('#sl_3').html()));
		$('#sl_1').html(strip_tags($('#sl_1').html()));
		$('#sl_5').html(strip_tags($('#sl_5').html()));
		$('#sl_2, #sl_3, #sl_1, #sl_5').removeClass();
	}else if(this.id == 'img_5'){
//		$('#sl_5').html('<div class="sal-tb"><div class="arrow"></div></div><div class="sal-cb">'+$('#sl_5').html()+'</div><div class="sal-bb"></div>');
		$('#sl_5').addClass('active');
		$('#sl_2').html(strip_tags($('#sl_2').html()));
		$('#sl_3').html(strip_tags($('#sl_3').html()));
		$('#sl_4').html(strip_tags($('#sl_4').html()));
		$('#sl_1').html(strip_tags($('#sl_1').html()));
		$('#sl_2, #sl_3, #sl_4, #sl_1').removeClass();
	}
}

var modalWindow = {
	parent:   "body",
	windowId: null,
	content:  null,
	width:    null,
	height:   null,
	close:    function()
	{
		$("#modal-overlay").remove();
		$("#"+this.windowId).remove();
		
	},
	open:function()
	{
		var modal = "";
		modal += "<div id=\"modal-overlay\" class=\"modal-overlay\">&nbsp;</div>";
		modal += "<div id=\"" + this.windowId + "\" class=\"modal-window\" style=\"width:" + this.width + "px; height:" + this.height + "px; margin-top:-" + (this.height / 2) + "px; margin-left:-" + (this.width / 2) + "px;\">";
		modal += this.content;
		modal += "</div>";	

		$(this.parent).append(modal);

		$(".modal-window").append("<a class=\"close-window\"></a>");
		$(".close-window").click(function(){modalWindow.close();});
		$(".modal-overlay").click(function(){modalWindow.close();});
	}
};

var openMyModal = function(source)
{
	modalWindow.windowId = "myModal";
	modalWindow.width = 700;
	modalWindow.height = 700;
	modalWindow.content = "<iframe width='700' height='700' align='middle'  frameborder='0' scrolling='no' allowtransparency='true' src='" + source + "'></iframe>";
	modalWindow.open();
};

var closeMyModal = function(id)
{
	modalWindow.windowId = id;
	modalWindow.close();
};	

function editQuestion(id_answer, id_textfield)
{
	$('#'+id_textfield).removeClass();
	$('#'+id_textfield).addClass('va-textarea2_l');
	$('#'+id_textfield).attr("disabled", true);
	var new_text = $('#'+id_textfield).val();
	$.post(
			"/specialists/editanswer/id_answer/"+id_answer+"/", 
			{ answer_text : new_text },
			function(data){
				if(data){
					$('#edit-form-'+id_answer).hide('slow');
					$('#vci-text-'+id_answer).html(new_text);
					$('#edit-'+id_answer).show();
					$('#'+id_textfield).attr("disabled", false);
					$('#'+id_textfield).removeClass();
					$('#'+id_textfield).addClass('va-textarea2');
				}else{
				 alert('error');
				}
			});
}
