function act(mod, act, params)
{
    self.location="?mod="+mod+"&act="+act+"&"+params;
    return false;
}

function sure()
{
    return confirm('Are you sure ?');
}

function openWin(url, name, width, height)
{
  width  = width  || screen.width*0.9;
  height = height || screen.height*0.75;

  var win = window.open(url, name, 'left=' + Math.ceil((screen.width - width)/2) + ',top=' + Math.ceil((0+screen.height - height)/2) + ',width=' + width + ',height=' + height + ',location=0,toolbar=0,directories=0,status=0,menubar=0,scrollbars=1,resizable=0,channelmode=0,fullscreen=0');
  win.focus();

  return false;
}


function getSelectedId(name)
{
    var cb = document.getElementsByName(name);
    for (var i=0; i < cb.length; i++)
        if(cb[i].checked)
            return cb[i].value;
            
    return false;
}

function insertToTextArea(textArea, txt)
{
    //for IE
    if (document.selection) {
            textArea.focus();
            sel = document.selection.createRange();
            sel.text = txt;
    }
    //for MOZILLA/NETSCAPE
    else if (textArea.selectionStart || textArea.selectionStart == "0") {
            var startPos  = textArea.selectionStart;
            var endPos    = textArea.selectionEnd;
            var str       = textArea.value;

            textArea.value = str.substring(0, startPos) + txt + str.substring(endPos, str.length);
    } else {
            textArea.value += txt;
    }

    return true;
}

function selectCategory(prefix, show_all, first_only)
{
    return openWin("/index.php?mod=catalog&act=show_cat&show_all="+show_all+"&prefix="+prefix+(first_only? "&show_level=1" : ""), "selectCat", 500);
}

function htmlFormOther(selMain, inpOther)
{
    if (!document.getElementById(inpOther))
        return;
    var s = (selMain.value == -1 ? 'block': 'none');
    document.getElementById(inpOther).style.display = s;
    if ('none' == s)
        document.getElementById(inpOther).value = "";
}

function htmlCopyValues(_contactCountrySelect, _contactCityInput, _contactAddrInput, _contactZipInput)
{
    var countrySelect = document.getElementById ? document.getElementById("countrySelect") : document.all.countrySelect;
    var contactCountrySelect = document.getElementById ? document.getElementById("contactCountrySelect") : document.all.contactCountrySelect;

    var cityInput = document.getElementById ? document.getElementById("cityInput") : document.all.cityInput;
    var contactCityInput = document.getElementById ? document.getElementById("contactCityInput") : document.all.contactCityInput;

    var addrInput = document.getElementById ? document.getElementById("addrInput") : document.all.addrInput;
    var contactAddrInput = document.getElementById ? document.getElementById("contactAddrInput") : document.all.contactAddrInput;

    var zipInput = document.getElementById ? document.getElementById("zipInput") : document.all.zipInput;
    var contactZipInput = document.getElementById ? document.getElementById("contactZipInput") : document.all.contactZipInput;

    var contactCopyCheckbox = document.getElementById ? document.getElementById("contactCopyCheckbox") : document.all.contactCopyCheckbox;

    if (!contactCopyCheckbox.checked) {
        contactCountrySelect.value = _contactCountrySelect;
        contactCityInput.value = _contactCityInput;
        contactAddrInput.value = _contactAddrInput;
        contactZipInput.value = _contactZipInput;
    } else {
        contactCountrySelect.value = countrySelect.value;
        contactCityInput.value = cityInput.value;
        contactAddrInput.value = addrInput.value;
        contactZipInput.value = zipInput.value;
    }
}

function submitForm(frmName, type, requiredChecked)
{
    if (requiredChecked == null)
        requiredChecked = false;
    
    if (requiredChecked && 
        getSelectedId("p_id[]") == false &&
        getSelectedId("l_id[]") == false &&
        getSelectedId("c_id[]") == false )
    {
         
        alert("Check items first");
        return;
    }
    var url = '';
    switch (type)
    {
        case 1 : // compare
            url="/compare.html";
            break;
        case 2 ://send bulk sms
            url="/index.php?alias=sendsms&t=sms";
            break;
        case 3 ://send messages
            url="/index.php?alias=sendsms&t=message";
            break;
        case 4 ://add to basket
            url="/index.php?mod=account&act=add_to_basket";
            break;
        case 5 ://delete from basket
            url="/index.php?mod=account&act=delete_from_basket";
            break;
    }
    document.getElementById(frmName).action = ('http://' + document.location.href.match(/([a-z]+\.[a-z]+)\/[a-z]/i)[1] + url);  
    document.getElementById(frmName).submit();
}

/*
* using in /mod/object/tpl/visitor_list_header.php
*/
function listHeaderMenuMOver(obj)
{
    obj.className = "listheader-active";
}

/*
* using in /mod/object/tpl/visitor_list_header.php
*/
function listHeaderMenuMOut(obj)
{
    obj.className = "listheader-inactive";
}

function selectAll(name, checked, formName)
{
    var names = [];
    if (name.length == 0)
        names = ["p_id[]","c_id[]"];
    else 
        names = [name];
    for(var j =0; j < names.length; j++){
        name=names[j];
    var cb = document.getElementsByName(name);
    for (var i=0; i < cb.length; i++)
    {
        if (formName == null || cb[i].form.name == formName)
            cb[i].checked = checked;
    }
    }
}

function listSelectClick(value)
{
    selectAll('c_id[]',value);
    selectAll('l_id[]',value);
    selectAll('p_id[]',value);
    selectAll('lh_select[]',value);    
}

function _phone(baseName)
{
    var v1 = document.getElementsByName(baseName + '_1')[0].value;
    var v2 = document.getElementsByName(baseName + '_2')[0].value;
    var v3 = document.getElementsByName(baseName + '_3')[0].value;
    if (v1 && v2 && v3)
        document.getElementsByName(baseName)[0].value = v1 + '-' + v2 + '-' + v3;
}

function setImg(url, w, h)
{
    var i = document.getElementById('big_img');
    if (i){
        i.src = url;
        //i.width = w;
        //i.height = h;
    }
    return false;
}


function sendsmskeyUp()
{   
    var i = document.getElementById("msg_body");
    if (i)
    {
        if (i.value.length >= 150)
        {
            i.value = i.value.substring(0,150);
            return false;
        } else 
            document.getElementById("countChars").value = i.value.length;
    }
    return true;
}

function setInfoVisibility(isMember)
{
    document.getElementById("contactinfo").style.display = isMember? "none": "block";    
    document.getElementById("logintbl").style.borderBottom = isMember? "1px solid #ADD7FC": "0px";
}


function sms_frm_close()
{
        window.send_container.setStyle("display","none");
}
function sms_frm_show_res(event)
{
//    var res = window.frames[window.send_sms_target.name].document.body.innerHTML;
    //var res = window.frames["sms_target"].document.getElementById("jsErr");
    var res = $("sms_target").contentWindow.document.getElementById("jsErr");
	if (!res) return;
    $("jsErr").innerHTML = res.innerHTML;
    $("jsErr").setStyle("display","block");
    if (!(res.innerHTML.indexOf("err.gif") > -1 ))
        sms_frm_close();
    window.scrollTo(0,210);
    window.send_container.setStyles({
                top:"370px"
            });        
}



function sendMessage(id, type, data, sbj)
{

    if (!type)
        type == "sms";

    if (window.send_container && window.send_container.getStyle("display") == "block"){
        window.scrollTo(0, parseInt(window.send_container.getStyle("top")));
        return;
    }
    var recipients = [];
    if (!data){
	    if (!id){
    	    var checkboxes = [];
        	checkboxes.combine(document.getElementsByName("c_id[]"));
	        checkboxes.combine(document.getElementsByName("p_id[]"));
    	    checkboxes.combine(document.getElementsByName("l_id[]"));
        	for(var i =0; i <checkboxes.length; i++)
            	if ( checkboxes[i].checked)
                	recipients.include({ 
                    	    id: checkboxes[i].getAttribute("member_id")? checkboxes[i].getAttribute("member_id") :  checkboxes[i].value
	                    });
    	} else {
        	    recipients.include({ 
                    id: id
                });
    	}


	    for(var i =0; i <recipients.length; i++){
    	    var a = $$("a[member_id="+recipients[i].id+"]")[0];
        	recipients[i].url = a.href;
	        recipients[i].title = a.innerHTML;
    	}

    } else {
    	for(var i=0 ; i < data.length; i++){
			recipients.include({ 
            	        id: data[i].id,
                	    title : data[i].title,
                    	url : data[i].url
	        });    	
        }
    }

    if (recipients.length == 0)
        {
            alert("Please choose recipients.");
            return;
        }

    if (!window.send_container){
        window.send_container = document.createElement("div");
        $(window.send_container).setStyle("position","absolute");
        window.send_container.setStyle("display","none");
        window.send_container.setStyle("left","50%");
        window.send_container.setStyle("background-color","white");
        document.body.appendChild(window.send_container);
    }
    // download form
    if (type == "sms"){
        if (!window.send_sms_frm){
            var frmReq = new Request({method:"get",async : false, url: '/index.php?mod=message&act=send_sms&t=sms&c_id=1&ajax=1'}).send("");
            window.send_sms_frm = frmReq.response.text;            
        }
        var str = window.send_sms_frm;
    } else {
        if (!window.send_msg_frm){
            var frmReq = new Request({method:"get",async : false, url: '/index.php?mod=message&act=send_sms&t=message&c_id=1&ajax=1'}).send("");
            window.send_msg_frm = frmReq.response.text;            
        }
        var str = window.send_msg_frm;
    }
    
/*    if (!window.send_sms_target){
        window.send_sms_target = document.createElement("iframe");
        window.send_sms_target.name="sms_target";
        $(window.send_sms_target).setStyle("display","none");
        document.body.appendChild(window.send_sms_target);
        window.send_sms_target.addEvent("load", sms_frm_show_res);
    }*/


    // fill form
    var rep = "";
    var repId = [];
    for(var i =0; i <recipients.length; i++){
        rep += '<div class="recipient_link"><span>'+recipients[i].title+'</span></div>';
        repId.include(recipients[i].id);
    }
	
    str = str.replace("{type}",type);
//    str = str.replace("{target}",window.send_sms_target.name);
    str = str.replace("{recipients_block}",rep);
    str = str.replace("{recipient_ids}",repId.join(','));
    $(window.send_container).innerHTML = str;
	if (sbj){
		var lstt = document.getElementsByName("data[subject]");
		lstt[0].value = sbj;
	}
    $("sms_target").addEvent("load", sms_frm_show_res);
    window.send_container.setStyle("display", "block");
    window.send_container.setStyle("top",window.getScrollTop()+"px");
    window.send_container.setStyle("margin-left", "-"+parseInt((parseInt($(window.send_container.childNodes[0]).getStyle("width")) /2 )) + "px");
    window.send_container.setStyle("width",$(window.send_container.childNodes[0]).getStyle("width"));

  $$("#send_frm_close").addEvent("click",sms_frm_close);
}



function sendTestSms(mobile, box)
{
    var myRequest = new Request({
        url: '/index.php?mod=account&act=send_test_sms', 
        method: 'get', 
        onSuccess: function(responseText, responseXML) {
            $(box).innerHTML = responseText;
        }
    });
    myRequest.send("mobile="+mobile);
    return false;
}
