var _xmlhttp = null;
var _highlight_border = '';

function get_xmlhttp () {
    if (window.XMLHttpRequest) {
	xmlhttp = new XMLHttpRequest();
	if (typeof xmlhttp.overrideMimeType != 'undefined') { 
	    xmlhttp.overrideMimeType('text/html'); 
	}
    } else if (window.ActiveXObject) {
	xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
    } else {
	alert('Perhaps your browser does not support xmlhttprequests?');
    }
    return xmlhttp;
}

function getObj (id) {
    return document.getElementById (id);
}

//var _counters = http_read_s ('?action=ajax&int=counters');

function scroll_vpos () {
    if (document.all) {
        if (!document.documentElement.scrollLeft)
            scrollX = document.body.scrollLeft;
        else
            scrollX = document.documentElement.scrollLeft;

        if (!document.documentElement.scrollTop)
            scrollY = document.body.scrollTop;
        else
            scrollY = document.documentElement.scrollTop;
    } else {
        scrollX = window.pageXOffset;
        scrollY = window.pageYOffset;
    }

    return scrollY;
}


function toggle_loading (vis) {
    _divdqdo = getObj ('divdqdo');
    // move the object based on scroller position
    _divdqdo.style.top = scroll_vpos () + 3 + 'px';
    
    if (vis) 
	_divdqdo.style.visibility = 'visible';	
    else
	_divdqdo.style.visibility = 'hidden';    
}

function http_read (url, holder) {
    toggle_loading (1);
    if (_xmlhttp && _xmlhttp.readyState != 0) {
	_xmlhttp.abort ();
    }

    _xmlhttp = get_xmlhttp ();
    _xmlhttp.open ("GET", url, true);
    _xmlhttp.send (null);
    _xmlhttp.onreadystatechange = function () {
	if (_xmlhttp.readyState == 4 && _xmlhttp.responseText) {
	    getObj (holder).innerHTML = _xmlhttp.responseText;
	    toggle_loading (0);
	}
    }
    
    return false;
}

function http_read_s (url, holder) {
    toggle_loading (1);
    if (_xmlhttp && _xmlhttp.readyState != 0) {
	_xmlhttp.abort ();
    }

    _xmlhttp = get_xmlhttp ();
    result = '';
    
    _xmlhttp.open ("GET", url, false);
    _xmlhttp.send (null);
    _xmlhttp.onreadystatechange = function () {
	if (_xmlhttp.readyState == 4 && _xmlhttp.responseText) {
	    getObj('holder').innerHTML = _xmlhttp.responseText;
	    toggle_loading (0);
	}
    }
    getObj('holder').innerHTML = _xmlhttp.responseText;
    toggle_loading (0);
    
    return false;
}

function toggle_div (divname) {
    if (typeof (_prev_div) != 'undefined' && getObj (_prev_div)) 
	getObj (_prev_div).style.display = 'none';
    
    _cur_div = getObj (divname)
    if (_cur_div) {
	_prev_div = divname;
	_cur_div.style.display = 'block';
    }
}

function check_reg () {
    _form = document.forms['reg_form'];
    _form.but_register.disabled = true;

    _params = Array ();

    _params['fullname'] = _form.fullname.value;
    _params['country'] = _form.country.value;
    _params['city'] = _form.city.value;
    _params['address'] = _form.address.value;
    _params['pocode'] = _form.pocode.value;
    _params['phone'] = _form.phone.value;
    _params['email'] = _form.email.value;

    if (_form.regtype[0].checked) {
	_params['regtype'] = 'private';

	_params['personalid'] = _form.personalid.value;
	_params['cardid'] = _form.cardid.value;
    }
    
    
    if (_form.regtype[1].checked) {
	_params['regtype'] = 'business';
	
	_params['company'] = _form.company.value;
	_params['bulstat'] = _form.bulstat.value;
	_params['companypaper'] = _form.companypaper.value;
	_params['mol'] = _form.mol.value;
    }
    
    _params['agree'] = _form.agree.checked;
    
    http_read_s ('?action=ajax&obj=reg_check&params=' + php_serialize (_params), 'holder');
    _form.but_register.disabled = false;
    
    _result = getObj ('holder').innerHTML;
    if (_result.length > 0) {
	alert (_result);
	return false;
    } else {
	return true;
    }
}


function SetCookie (cookieName, cookieValue, nDays) {
    var today = new Date ();
    var expire = new Date ();
    if (nDays == null || nDays == 0) nDays = 1;
    expire.setTime (today.getTime () + 3600000*24*nDays);
    document.cookie = cookieName + "=" + escape (cookieValue) + ";expires=" + expire.toGMTString ();
}

function ReadCookie (cookieName) {
    var theCookie = "" + document.cookie;
    var ind = theCookie.indexOf (cookieName);
    if (ind == -1 || cookieName == "") return "";
    var ind1 = theCookie.indexOf (';', ind);
    if (ind1 == -1) ind1 = theCookie.length;
    return unescape (theCookie.substring (ind + cookieName.length + 1, ind1));
}

function highlight_on (obj) {
    // record original border
    _highlight_border = obj.style.border;
    
    // set border
    obj.style.border = '1px solid #FF0000';
    
    return true;
}

function highlight_off (obj) {
    // restore border
    obj.style.border = _highlight_border;
    obj.style.border = '1px solid #969696';
    
    return true;
}

function property_debug (obj, all) {
    var _info = '';
    for (style in obj.style) {
	_info += style + ': '
	
	if (typeof (obj.style[style]) != 'function') 
	    _info += obj.style[style];
	else
	    _info += ' function';
	_info += "; "
    }
    
    alert (_info);
}

function highlight_inputs () {
    var _list = Array ();

    _list = document.getElementsByTagName ('input');
    for (var i = 0; i < _list.length; i++) {
	_list[i].onmouseover = highlight_on (_list[i]);
	_list[i].onmouseout = highlight_off (_list[i]);
    }
	
    _list = document.getElementsByTagName ('textarea');
    for (var i = 0; i < _list.length; i++) {
	_list[i].onmouseover = highlight_on (_list[i]);
	_list[i].onmouseout = highlight_off (_list[i]);
    }
	
    _list = document.getElementsByTagName ('select');
    for (var i = 0; i < _list.length; i++) {
	_list[i].onmouseover = highlight_on (_list[i]);
	_list[i].onmouseout = highlight_off (_list[i]);
    }
	
    _list = document.getElementsByTagName ('button');
    for (var i = 0; i < _list.length; i++) {
	_list[i].onmouseover = highlight_on (_list[i]);
	_list[i].onmouseout = highlight_off (_list[i]);
    }
}

function moveOptions (theSelFrom, theSelTo) {
    var selLength = theSelFrom.length;
    var selectedText = new Array();
    var selectedValues = new Array();
    var selectedCount = 0;
	
    var i;
	
    // Find the selected Options in reverse order
    // and delete them from the 'from' Select.
    for (i = selLength - 1; i >= 0; i--) {
	if (theSelFrom.options[i].selected) {
	    selectedText[selectedCount] = theSelFrom.options[i].text;
	    selectedValues[selectedCount] = theSelFrom.options[i].value;
	    deleteOption (theSelFrom, i);
	    selectedCount++;
	}
    }
	
    // Add the selected text/values in reverse order.
    // This will add the Options to the 'to' Select
    // in the same order as they were in the 'from' Select.
    for (i = selectedCount - 1; i >= 0; i--) {
	addOption (theSelTo, selectedText[i], selectedValues[i]);
    }
	
}

function addOption (theSel, theText, theValue) {
    var newOpt = new Option(theText, theValue);
    var selLength = theSel.length;
    theSel.options[selLength] = newOpt;
}

function deleteOption (theSel, theIndex) {	
    var selLength = theSel.length;
    if (selLength>0) {
	theSel.options[theIndex] = null;
    }
}

function selectAllOptions (selStr) {
    var selObj = getObj (selStr);
    for (var i = 0; i < selObj.options.length; i++) {
	selObj.options[i].selected = true;
    }
}


