﻿function documentLeft()
{
    var iebody=(document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body ;
    return document.all?iebody.scrollLeft:window.pageXOffset;
}
function documentTop()
{
    var iebody=(document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body ;
    return document.all?iebody.scrollTop:window.pageYOffset;
}
function documentWidth()
{
    var iebody=(document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body ;
    return document.all?iebody.clientWidth:window.innerWidth; 
}
function documentHeight()
{
    var iebody=(document.compatMode && document.compatMode != "BackCompat")? document.documentElement : document.body ;
    return document.all?iebody.clientHeight:window.innerHeight;
}

function attachEvent(obj,evName,delegate)
{
    if(obj.addEventListener)
    {
         obj.addEventListener(evName,delegate,false);
    }  
    else if(obj.attachEvent)
    {
        obj.attachEvent('on'+evName, delegate);
    }   
}

function detachEvent(obj,evName,delegate)
{
    if(obj.removeEventListener)
    {
        obj.removeEventListener(evName,delegate,false); 
    }  
    else if(obj.detachEvent)
    {
        obj.detachEvent('on'+evName,delegate);
    }   
}

//Ranges from 0-1 for Gecko browsers
//Ranges from 0-100 for IE browsers
//Value should be given in 0-10 range
function setOpacity(obj,value)
{
    if(document.all)
    {
         obj.style.filter = 'alpha(opacity=' + value*10 + ')';   
     }
     else
     {
        obj.style.opacity = value/10;
     }
}

String.prototype.startsWith = function(s) 
{
    return this.indexOf(s)==0; 
}

String.prototype.toCamelCase = function()
{
    var ret = '';
    var upper = false; 
    for(var i = 0 ; i < this.length;i++)
        if(i==0)
            ret += this.charAt(i).toLowerCase();
        else if(upper)
        {
            ret += this.charAt(i).toUpperCase();
            upper = false;
        }
        else if(this.charAt(i) == '-')
            upper = true;
        else
            ret += this.charAt(i);
            
     return ret;
}

function isEmpty(_value)
{
    if(_value==null||_value.length==0)
        return true;
    else
        return false;
}


String.prototype.startsWith = function(s) 
{ 
    return this.indexOf(s)==0; 
}

Array.prototype.getByKey = function(name)
{
    for(var i = 0 ; i < this.length;i++)
    {
        try
        {if(this[i].key == name) return this[i].value;}
        catch(e)
        {return null;}
    }  
    return null; 
}

Array.prototype.merge = function(mediator)
{
    var ret = '';
    for(var i = 0; i < this.length;i++)
    {
        if(i>0) ret += (mediator?mediator:",");
        ret += this[i];
    }
   return ret;   
}
//A method shows waiting bar
function Waiting(state)
{
    var div = document.getElementById('waiting');    
    if(!state)
    {    
        div.style.visibility = 'hidden';
        document.body.style.cursor = 'default'; 
        window.onscroll = null; 
    }
    else
    {
        document.body.style.cursor = 'wait';
        if (!div)
            div = createWaitingDiv(); 
        
        div.style.top = documentTop();
        div.style.left = documentLeft();
        div.style.visibility = '';
        window.onscroll = function()
        {
            document.getElementById('waiting').style.top = documentTop();
            document.getElementById('waiting').style.left = documentLeft();
        }
    } 
}
function createWaitingDiv()
{
    var div = document.createElement('div');
    div.id = 'waiting'; 
    document.body.appendChild(div);
    setOpacity(div,7); 
    div.style.position='absolute';
    var img = document.createElement('img');
    img.src = GetRootDir() +'Icons/waiting.gif';
    div.appendChild(img);
    return div;
}

function ProductNewsGetPage(uniqueid,controlid,pageno)
{
    Modules.Categories.Products.GetPage(uniqueid,controlid,pageno,document.location.pathname,GetPageCallBack);
}

function CategoryProductGetPage(uniqueid,controlid,pageno)
{
    Modules.Categories.Products.GetPage(uniqueid,controlid,pageno,document.location.pathname,GetPageCallBack);
}

function BrandNewsGetPage(uniqueid,controlid,pageno)
{
    Modules.Brands.News.GetPage(uniqueid,controlid,pageno,document.location.pathname,GetPageCallBack);
}

function BrandCategoriesGetPage(uniqueid,controlid,pageno)
{
    Modules.Brands.Categories.GetPage(uniqueid,controlid,pageno,document.location.pathname,GetPageCallBack);
}

function BrandProductGetPage(uniqueid,controlid,pageno)
{
    Modules.Brands.Products.GetPage(uniqueid,controlid,pageno,document.location.pathname,GetPageCallBack);
}

function ItemListerBaseGetPage(uniqueid,controlid,pageno)
{
    ItemListerBase.GetPage(uniqueid,controlid,pageno,document.location.pathname,GetPageCallBack);
}

function ListerBaseGetPage(uniqueid,controlid,pageno)
{
    ListerBase.GetPage(uniqueid,controlid,pageno,document.location.pathname,GetPageCallBack);
}

function GetPageCallBack(res)
{
    if(res.error != null)
    {
        Write(res.error.Message) ;
        return;
    }  
    var div =  document.getElementById('div'+res.value[0]);
    div.innerHTML = res.value[1]; 
}

//Switching between design mode and normal mode
function ChangeMode()
{
    Default.SwitchMode(ChangeModeCallBack);
}

//Default ChangeMode callback function
function ChangeModeCallBack(res)
{
    document.location.reload();
}

function ChangePageLang(value)
{
    Default.ChangeLanguage(value,ChangeModeCallBack);
}

function UndoChanges()
{
    Default.UndoConfigChanges(ChangeModeCallBack);
}

function ChangeShowProperties()
{
    Default.SwitchShowProperties(ChangeShowPropertiesCallBack);
}

function ChangeShowPropertiesCallBack(res)
{
    document.location.reload();
}

var SizeTypes=['px', '%', 'pt', 'em'];

function GetSizeValue(_value,_default)
{
    if(_value)
    {
        for(i=0;i<SizeTypes.length;i++)
            _value = _value.replace(SizeTypes[i],'');
        return _value;
    }
    return _default;
}

function GetSizeType(_value,_default)
{
    if (_value&&_value.length > 2)
    {
        for (i = 0; i < SizeTypes.length; i++)
            if (_value.indexOf(SizeTypes[i]) > 0) return SizeTypes[i];
    }
    return _default;
}
var arrMsg=new Array();
function GetMessage(messageId)
{
    var _msg=arrMsg[messageId];
    if(_msg)
    {
        return _msg;
    }
    else
    {
        var res=Default.GetAdminString(messageId);
        if(res.error) return messageId;
        arrMsg[messageId]=res.value;
        return res.value;
    }
}
function GetString(_key)
{
    return GetMessage(_key);
}

function IsColumnExist(_table,_column)
{
    for(var i=0;i<_table.Columns.length;i++)
        if(_table.Columns[i].Name==_column)
            return true;
    return false;
}
var m_generalTimeout;
function ShowMenuItem(_controlid,_selected)
{
    clearTimeout(m_generalTimeout);
    var _cnt=document.getElementById(_controlid+'MenuMain');
    if(_cnt==null) return;
    var _count=parseInt(_cnt.getAttribute('MenuCount'));
    for(var i=0;i<_count;i++)
    {
        var ele=document.getElementById(_controlid+'Menu'+i);
        if(i==_selected)
            ele.style.display='';
        else
            ele.style.display='none';        
    }
}
function HideMenuItem(_controlid)
{
    m_generalTimeout=setTimeout('ShowMenuItem("'+_controlid+'",-1)', 1000);
}

function ShowHideExpandable(controlid,state)
{
    var cnt=document.getElementById(controlid+'Detail');
    var imgC=document.getElementById(controlid+'Collapsed');
    var imgE=document.getElementById(controlid+'Expanded');

    if(cnt && state)
    {
        cnt.style.display='';
        imgC.style.display='none';
        imgE.style.display='';
    }
    else
    {
        cnt.style.display='none';
        imgC.style.display='';
        imgE.style.display='none';
    }
}

function GetCurrentPage()
{
    var _qs=document.location.pathname;
    _qs = _qs.replace('.aspx','');
    return _qs.split('/')[1];
}

function GetCurrentTab()
{
    var _qs=document.location.pathname;
    _qs = _qs.replace('.aspx','');
    var _temp=_qs.split('/');
    if(_temp.length>=4)
        return _temp[3];
    else
        return _temp[2];
}

function GetCurrentPageAndTabName(sperator)
{
    var _page=GetCurrentPage();
    var _tab=GetCurrentTab();
    
    var ret=_page;
    
    if(_tab) ret=ret+sperator+_tab;
    if(ret=='default') ret='';
    return ret;
}

function ShowExpandList(controlid,count,type)
{
    if(type=='OneSelect')
    {
        var cntprefix=controlid.substring(0,controlid.indexOf('_'));
        for(var i=0;i<count;i++)
        {
            var cnt=document.getElementById(cntprefix+'_'+i+'Detail');
            if(cnt) cnt.style.display='none';
        }
        var currentcnt=document.getElementById(controlid+'Detail');
        if(currentcnt) currentcnt.style.display='';
    }
    else
    {
        var currentcnt=document.getElementById(controlid+'Detail');
        if(currentcnt.style.display=='')
            currentcnt.style.display='none';
        else
            currentcnt.style.display='';
    }
}

function UserLogin(_user,_pass)
{
    var res=Default.UserLogin(_user,_pass,document.location.pathname);
    var errtext='';
    if(res.error)
        errtext=res.error.Message;
    else
        errtext=res.value;
    if(errtext!='')
    {
        alert(errtext);
    }
    else
        document.location.reload();
}
function UserLogout()
{
    var res=Default.UserLogout();
    var errtext='';
    if(res.error)
        errtext=res.error.Message;
    else
        errtext=res.value;
    if(errtext!='')
    {
        alert(errtext);
    }
    else
        document.location.reload();
}
function ShowDetailMenu(_clientId,_current,_count)
{
    var _currentMenu=document.getElementById(_clientId+_current);
    if(_currentMenu)
        _currentMenu.style.display='';
    
    for(var i=0;i<_count;i++)
    {
        if(i==_current) continue;
        var _menu=document.getElementById(_clientId+i);
        if(_menu)
            _menu.style.display='none';
        else
            return;
    }
}

function GetUserCntValue(_cntid)
{
    var _cnt=document.getElementById(_cntid);
    if(_cnt)
        return _cnt.value;
    else
        return '';
}

function SaveUser(_controlId)
{
    var cnts = new Array();
    var vals = new Array();
    
    var _Uname=GetUserCntValue(_controlId+'_Name');
    var _Usurname=GetUserCntValue(_controlId+'_SurName');
    var _Ucompany=GetUserCntValue(_controlId+'_Company');
    var _Umail=GetUserCntValue(_controlId+'_Mail');
    var _Uuser=GetUserCntValue(_controlId+'_UserName');
    var _Upass=GetUserCntValue(_controlId+'_Password');
    var _Urepass=GetUserCntValue(_controlId+'_RePassword');
    if(_Uname==''||_Usurname==''||_Umail==''||_Uuser==''||_Upass=='')
    {
        alert(GetString('gNoEmptyFields'));
        return;
    }
    
    if(_Upass != _Urepass)
    {
        alert(GetString('gBadPass'));
        return;
    }
    
    cnts.push('Name');
    vals.push(_Uname);
    cnts.push('SurName');
    vals.push(_Usurname);
    cnts.push('Company');
    vals.push(_Ucompany);
    cnts.push('Mail');
    vals.push(_Umail);
    cnts.push('UserName');
    vals.push(_Uuser);
    cnts.push('Password');
    vals.push(_Upass);
    
    var res=Default.SaveUser(cnts,vals,_controlId,window.location.pathname);
    if(res.error)
    {
        alert(res.error.Message);
        return;
    }
    if(res.value=='OK')
        document.location.href='RegistrationOK.aspx';
}

function UserActivate(_controlId)
{
    var _cntMail=document.getElementById(_controlId+'UserMail');
    var _cntCode=document.getElementById(_controlId+'ActivationCode');
    if(_cntMail&&_cntCode&&_cntMail.value&&_cntCode.value)
    {
        var res=Default.RegisterUser(_cntMail.value, _cntCode.value);
        if(res.error)
        {
            alert(res.error.Message);
            return;
        }
        if(res.value=='OK')
        {
            alert(GetString("gRegisterSuccessfully"));            
            document.location.href='Intro.aspx';
        }
    }
}