function toggleChbxAll(formName) {
	var jq = "form[@name=" + formName +"] input[@type=checkbox]";
	//jq = "#" + formName + " :checkbox"; alert();
	$(jq).each(function() {
		this.checked = !this.checked;
	});
	return true;
}

function EncodeAndLinkEmail(linkId, mail)
{
    var strMail = mail.substr(1).replace(new RegExp("(.)(.)", "g"), "$2$1");
    var objLink = document.getElementById(linkId);
    objLink.href = 'mailto:' + strMail;
    objLink.innerHTML = strMail;
}

function ConfirmMess(mess) {
    return window.confirm(mess);
}

function NewSizedWin(url, name, w, h) {
    return window.open(url, name, "height=" + h + ",width=" + w + ",status=0,scrollbars=1,resizable=1,toolbar=0,menubar=0,location=0");
}

function UpdateTaskSelect(values, names) {
    var sel = document.getElementById('dinamictasks');
    
    // Очищаем все кроме первой, у которой очищаем описание
    while (sel.options.length > 1) {
        //sel.options.remove(sel.options.length - 1);
        sel.options[sel.options.length - 1] = null;
    }
    sel.options[0].text = '            ';
    
    for (var i = 0; i < values.length; i++) {
        sel.options[sel.options.length] = new Option(names[i], values[i]);
    }
    return true;
}

function LoadTasksInIFrame(select, ref) {
    var part = select.options[select.selectedIndex].value;
    window.uploadfr.document.location = ref + part;    
    return true;
}


function ShowDiv(id, span) {
    var el = (id && id.style) ? id : $id(id);
    el.style.display = span ? '' : 'block';
    return el;
}

function HideDiv(id) {
    var el = (id && id.style) ? id : $id(id);
    el.style.display = 'none';
    return el;
}

function CodeWin() {
    var codewin = window.open('about:blank', 'codewin', 'top=200, left=200, width=400, height=400');
    return false;
}

function showInTextarea(link, code_id, pseudo) {
    var p = '';
    if (pseudo) p = pseudo;
    
    var tarea      = document.getElementById('tarea_' + p + 'code' + code_id);
    var selectlink = document.getElementById('tarea_' + p + 'code' + code_id + '_slink');
    
    if (!tarea) {
        return false;
    }
    if (tarea.style.display == 'none') {
        tarea.style.display = '';
        link.innerHTML = 'убрать textarea';
        selectlink.style.display = '';
    } else {
        tarea.style.display = 'none';
        selectlink.style.display = 'none';
        link.innerHTML = 'показать в textarea';
    }
    return false;
}

function ShowCode(code_id) {
    var codediv = document.getElementById('code' + code_id);
    
    if (!codediv) {
        return false;
    }
    if (codediv.style.display == 'none') {
        codediv.style.display = '';
    } else {
        codediv.style.display = 'none';
    }
    return false;
}

function SelectTextarea(link, code_id, pseudo) {
    var p = '';
    if (pseudo) p = pseudo;
    
    var tarea      = document.getElementById('tarea_' + p + 'code' + code_id);
    
    if (!tarea) {
        return false;
    }
    
    tarea.focus();
    tarea.select();
    
    return false;
}

function HideShowLayer(id, clicked, new_text) {
    if (!document.tmpvar1) document.tmpvar1 = clicked.innerHTML;
    
    var el = document.getElementById(id);
    if (!el) {
        alert('Layer ' + id + ' not found!');
        return false;
    }
    if (el.style.display == 'none') {
        el.style.display = '';
        clicked.innerHTML = document.tmpvar1;
    } else {
        el.style.display = 'none';
        clicked.innerHTML = new_text;
    }
    return false;
}

function HideShowDiv(id) {
    var el = document.getElementById(id);
    
    if (!el) {
        alert('Div ' + id + ' not found!');
        return false;
    }
    
    if (el.style.display == 'none') {
        el.style.display = '';
    } else {
        el.style.display = 'none';
    }
    return false;
}

function ClickOnElement(id) {
    var el = document.getElementById(id);
    if (!el.click) {
        //alert('element ' + el + ' is not clickable!');
        return false;
    }
    el.click();
    return true;
}

function $id(id) {
    return document.getElementById(id);
}

function resizeHight(id, delta, minH) {
    var el = document.getElementById(id);
    var currHeight = parseInt(el.style.height);
    
    var newHeight = (currHeight + parseInt(delta));
    if (newHeight < minH) newHeight = minH;
    
    el.style.height = newHeight + 'px';
    return true;
}


// *******************************************************************
// *********************************************************** LIBS >>

var Utils = {
    InArray: function(el, arr) {
        for (var i = 0; i < arr.length; i++) {
            if (el == arr[i]) return true;
        }
        return false;
    },
	trimInt: function(str) {
		var res = /\d+/.exec(str);
		if (res) return res[0];
		return -1; 
	}
}

var Forms = {
    addOptions: function(sel, options) {
    	$sel = $(sel).empty();
        for (var i = 0; i < options.length; i++) {
            $sel.append("<option value='" + options[i].value + "'>" + options[i].name + "</option>");
        }
        return true;
    },
    addParams: function(form, params) {
    	for (var i in params) {
    		$(form).append('<input type="hidden" name="' + i + '" value="' + params[i] + '"/>');
    	}
    	return true;
    }
}

var Text = {
	hasText: function(str) {
		return (str && this.trim(str).length > 0);
	},
	trim: function(str) {
		return str.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
	}
}

var rate = {
    ConfirmRate : function(mark, markName, lnk) {
        HideDiv("rateWarning");
        var mess = 'Оценить как "' + markName + '"?';
        if (Utils.InArray(mark, checkedMarks)) {
            var div = ShowDiv("rateWarning");
            $("#markName").text(markName);
            rateReasonForm = div.getElementsByTagName('FORM')[0];
            clickedLnk = lnk;
            return false;
        }
        
        return window.confirm(mess);
    },
    
    RateReason : function(button) {
        var txt = rateReasonForm.elements['reason'];
        if (/[^\s\d]{3,}/.exec(txt.value)) {
            rateReasonForm.action = clickedLnk.href;
            rateReasonForm.submit();
        } else {
        	txt.value = '';
            txt.focus();
        }
    },
    
    ConfirmRateDelete : function(formName) {
		var n = $("form[@name=" + formName + "] input[@type=checkbox][@checked]").length;
		if (n > 0 && ConfirmMess("Да?")) {
			$("form[@name=" + formName + "]")[0].submit();
		}
    },
    
    clickedLnk : {},
    rateReasonForm : {}
}

// ******************************************************************************
// *********************************************************** JQUERY PLUGINGS >>
jQuery.fn.DefaultValue = function(text){
    return this.each(function(){
		//Make sure we're dealing with text-based form fields
		if(this.type != 'text' && this.type != 'password' && this.type != 'textarea')
			return;
		
		//Store field reference
		var fld_current=this;
		
		//Set value initially if none are specified
        if(this.value=='') {
			this.value=text;
		} else {
			//Other value exists - ignore
			return;
		}
		
		//Remove values on focus
		$(this).focus(function() {
			if(this.value==text || this.value=='')
				this.value='';
		});
		
		//Place values back on blur
		$(this).blur(function() {
			if(this.value==text || this.value=='')
				this.value=text;
		});
		
		//Capture parent form submission
		//Remove field values that are still default
		$(this).parents("form").each(function() {
			//Bind parent form submit
			$(this).submit(function() {
				if(fld_current.value==text) {
					fld_current.value='';
				}
			});
		});
    });
};

// *******************************************************************
// *********************************************************** CORE >>
String.prototype.trim = Text.trim;


// *******************************************************************