function SetFocus() {
  if (document.forms.length > 0) {
    var field = document.forms[0];
    for (i=0; i<field.length; i++) {
      if ( (field.elements[i].type != "image") && 
           (field.elements[i].type != "hidden") && 
           (field.elements[i].type != "reset") && 
           (field.elements[i].type != "submit") ) {

        document.forms[0].elements[i].focus();

        if ( (field.elements[i].type == "text") || 
             (field.elements[i].type == "password") )
          document.forms[0].elements[i].select();
        
        break;
      }
    }
  }
}

function confirmLink(theLink, confirmMsg)
{
    var is_confirmed = confirm(confirmMsg);

    return is_confirmed;
}

function CheckAll(id, checked) {
	count = document.all.length;
	for (i=0; i<count;i++) {
		if (document.all[i].id == id) 
			document.all[i].checked = checked;
	}
}

function CheckAllSub(id, checked) {
	var str = "";	
	var count = document.all.length;
	var len = id.length;

	for (i=0; i<count;i++) {
		str = document.all[i].id;

		if (document.all[i].id)
		if (document.all[i].id != 'undefined')
		if (str.substring(0,len) == id)
			document.all[i].checked = checked;
	}
}

function ShowAll(id, bShow) {
	if (navigator.appName=='Netscape') {
		document.getElementById(id).style.left = 0; 
		document.getElementById(id).style.visibility=(bShow?"visible":"hidden");

	} else {
	count = document.all.length;
	for (i=0; i<count;i++) {
		if (document.all[i].id == id) 
			document.all[i].style.display = (bShow ? 'block' : 'none');
	}
}
}

function object_show(name, id)
{	
	count = document.all.length;
	for (i=0; i<count;i++) {
		if (document.all[name+i]) {
			document.all[name+i].style.display='none';
		}
	}
	if (document.all[name+id]) {
		document.all[name+id].style.display='block';
	}
}

function checkFields(formobj, $req, $desc){
	// name of mandatory fields
	var fieldRequired = $req;
	// field description to appear in the dialog box
	var fieldDescription = $desc;
	// dialog message
	var alertMsg = "Пожалуйста заполните следующие поля:\n";

	var l_Msg = alertMsg.length;

	for (var i = 0; i < fieldRequired.length; i++){
		var obj = formobj.elements[fieldRequired[i]];
		if (obj){
			switch(obj.type){
			case "select-one":
				if (obj.selectedIndex == -1 || obj.options[obj.selectedIndex].text == ""){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "select-multiple":
				if (obj.selectedIndex == -1){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			case "text":
			case "textarea":
				if (obj.value == "" || obj.value == null){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
				break;
			default:
				if (obj.value == "" || obj.value == null){
					alertMsg += " - " + fieldDescription[i] + "\n";
				}
			}
		}
	}
	
	if (alertMsg.length == l_Msg){
		return true;
	}else{
		alert(alertMsg);
		return false;
	}
}

var WndPopup;

function popup_win(url, topleft, w, h)
{
	var title="";
	if(!w) w=420;
	if(!h) h=520;

    if (screen && !topleft) {
		if (h<(screen.height - 60)) w = w + 20;
   		var h = (h < screen.height) ? h : screen.height - 60;
   		var w = (w < screen.width) ? w : screen.width;
   		WndPopup = window.open(url,"",'width='+w+',height='+h+',left='+((screen.width-w-10)/2)+',top='+((screen.height-h-10)/2)+',scrollbars=yes,resizable=yes');

	}else {
		WndPopup = window.open(url,"",'width='+w+',height='+h+',left=0,top=0,scrollbars=yes,resizable=yes');
	}
   
	WndPopup.focus();

	
} 

/* Открывает окно для визуального редактирования текста */
var currentTextArea = null;
function openEditor(textarea) {

	// location of edit.php file:
	var editFile = '/spaw2/scripts/edit.php';
	
	currentTextArea = textarea;

	var edit = window.open(editFile, 'editorWindow', 'width=720, height=470, status=yes, scrollbars=yes, resizable=yes');
	edit.focus();
} 

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

/* Открывает окно для визуального редактирования текста 
var currentTextArea = null;
function openEditor(textarea) {

	// location of edit.php file:
	var editFile = '/spaw/scripts/edit.php';
	
	currentTextArea = textarea;

	var edit = window.open(editFile, 'editorWindow', 'width=720, height=470, status=yes, scrollbars=yes, resizable=yes');
	edit.focus();
} 
*/
function add_compare(ident)
{
	document.cookie = setCookie('cmp['+ident+']', 1, null, '/');
}

// name - имя cookie
// value - значение cookie
// [expires] - дата окончания действия cookie (по умолчанию - до конца сессии)
// [path] - путь, для которого cookie действительно (по умолчанию - документ, в котором значение было установлено)
// [domain] - домен, для которого cookie действительно (по умолчанию - домен, в котором значение было установлено)
// [secure] - логическое значение, показывающее требуется ли защищенная передача значения cookie
function setCookie(name, value, exp, path, domain, secure) {
	if (!exp) var exp=1;
	var expires = new Date ();
	expires.setTime(expires.getTime() + (24 * 3600 * 1000 * exp));
	var path="/";
	var curCookie = name + "=" + escape(value) +
	((expires) ? "; expires=" + expires.toGMTString() : "") +
	((path) ? "; path=" + path : "") +
	((domain) ? "; domain=" + domain : "") +
	((secure) ? "; secure" : "")
	if ((name + "=" + escape(value)).length <= 4000)
	document.cookie = curCookie;
}

function parse_url (str, component) {
    var  o   = {
        strictMode: false,
        key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
        q:   {
            name:   "queryKey",
            parser: /(?:^|&)([^&=]*)=?([^&]*)/g
        },
        parser: {
            strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
            loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/\/?)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/ // Added one optional slash to post-protocol to catch file:/// (should restrict this)
        }
    };
    
    var m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str);
    uri = [];
    i   = 14;
    while (i--) {uri[o.key[i]] = m[i] || "";}
    switch (component) {
        case 'PHP_URL_SCHEME':
            return uri.protocol;
        case 'PHP_URL_HOST':
            return uri.host;
        case 'PHP_URL_PORT':
            return uri.port;
        case 'PHP_URL_USER':
            return uri.user;
        case 'PHP_URL_PASS':
            return uri.password;
        case 'PHP_URL_PATH':
            return uri.path;
        case 'PHP_URL_QUERY':
            return uri.query;
        case 'PHP_URL_FRAGMENT':
            return uri.anchor;
        default:
            var retArr = [];
            if (uri.protocol !== '') {retArr.scheme=uri.protocol;}
            if (uri.host !== '') {retArr.host=uri.host;}
            if (uri.port !== '') {retArr.port=uri.port;}
            if (uri.user !== '') {retArr.user=uri.user;}
            if (uri.password !== '') {retArr.pass=uri.password;}
            if (uri.path !== '') {retArr.path=uri.path;}
            if (uri.query !== '') {retArr.query=uri.query;}
            if (uri.anchor !== '') {retArr.fragment=uri.anchor;}
            return retArr;
    }
}