﻿function LoadUserDetails(uid,cid)
{
    loaddetails(cid,3,uid,2);
}

function addUser(cid)
{
    loaddetails(cid,4,0,2);
}

function editUser()
{
   var cid = getChoosenID(true);
   
   if(cid=="")
     return;
     
   var arrUd = cid.split(':');
     
   var userid = arrUd[0]*1;
   var clientid = arrUd[1]*1;
   
   LoadUserDetails(userid,clientid);
}

function deleteUser()
{

   var cid = getChoosenID(true);
   
   if(cid=="")
     return;
     
      var arrUd = cid.split(':');
     
   var userid = arrUd[0]*1;
   var clientid = arrUd[1]*1;
     
   var uName = document.getElementById('userName_' +  userid).innerHTML;
   
   if(confirm(GetLocalizedString(47) + ' :\'' + uName + '\'?'))
       loaddetails(clientid,5,userid,2);
}

function getChoosenID(onlyone)
{   
    var arr = new Array();
    var sDelimiter = (onlyone) ? "" : "|";
    var arrInputs = document.getElementById('clientManagment').getElementsByTagName('INPUT');
    for (var i = 0; i < arrInputs.length; i++)
    {
        if (arrInputs[i].checked)
        {
            arr[arr.length] = arrInputs[i].attributes["userid"].value + ":" + arrInputs[i].attributes["clientid"].value;
            if (onlyone)
                break;
        }
    }
    return arr.join(sDelimiter);
}

function loaddetails(cid,mode,uid,rtp)
{
    //mode= 0 - load details; 1 - add client (cid=0); 2 - delete client; 
    //      3 - load user details; 4 - add user (cid=0); 5 - delete user;
    
    todiv = document.getElementById('clientdetails');
    todiv.innerHTML = loadwaitSurvey();

    ToggleDetailsDiv('EditDetails');
    
    if (window.XMLHttpRequest) 
    {
        oXML=new XMLHttpRequest();
    } 
    else 
    {
        oXML = new ActiveXObject("Microsoft.XMLHTTP");
    }
    var strURL = "LoadClientDetails.aspx?clientid=" + cid + "&mode=" + mode + "&userid=" + uid + "&rtp=" + rtp;
    oXML.open("GET", strURL ,true);
    oXML.onreadystatechange=function()
    {
	        if (oXML.readyState==4)
	        {
		        var resp = oXML.responseText;
		        if (resp == 'error')
		        {
		            todiv.innerHTML = '<div class="NoPolls">' + GetLocalizedString(49) + '</div>';
		            return;
		        }
		        
		        if(resp == 'reload')
		        {
		            document.location.href = document.location.href;
		            return;
		        }
		        
		        todiv.innerHTML = resp;
		        oXML = null;
	        }
    }
    oXML.send(null);
}

function LoadClientDetails(cid)
{
    loaddetails(cid,0,0,1);
}

function LoadClientBasicDetails(cid)
{
    loaddetails(cid,6,0,3);
}

function addClient()
{
    loaddetails(0,1,0,1);
}

function editClient()
{
   var cid = getChoosenIDclient(true);
   
   if(cid=="")
     return;
     
   cid = cid*1;
   
   LoadClientDetails(cid);
}

function deleteClient()
{

   var cid = getChoosenIDclient(true);
   
   if(cid=="")
     return;
     
   var clientName = document.getElementById('clientName_' +  cid).innerHTML;
   
   if(confirm(GetLocalizedString(48) + ' :\'' + clientName + '\'?'))
       loaddetails(cid,2,0,1);
}

function getChoosenIDclient(onlyone)
{   
    var arr = new Array();
    var sDelimiter = (onlyone) ? "" : "|";
    var arrInputs = document.getElementById('clientManagment').getElementsByTagName('INPUT');
    for (var i = 0; i < arrInputs.length; i++)
    {
        if (arrInputs[i].checked)
        {
            arr[arr.length] = arrInputs[i].attributes["clientid"].value;
            if (onlyone)
                break;
        }
    }
    return arr.join(sDelimiter);
}

function SubmitForm(formname)
{
    var pv = new proValidations();
	var bRet = pv.ValidateForm(formname);
	
	if(!bRet)
	    return false; 
    
    document.getElementById(formname).submit();
}


function ValidationObject(sName,vtype,regex,min,max,imsg)
{
    this.FieldName = sName;
    this.RegexType = vtype;
    this.Regex = regex;
    this.MinLength = min;
    this.MaxLength = max;
    this.ErrorMessage = imsg;
}

function ValidationObjects()
{

}

function proValidations()
{
    this.Validations = new ValidationObjects();
    
    this.Validations["c_username"] = new ValidationObject("c_username", "loginField", loginField, 2, 20,4);
    this.Validations["c_password"] = new ValidationObject("c_password", "passwordField", passwordField, 4, 20,5);
    this.Validations["c_name"] = new ValidationObject("c_name", "nameField", nameField, 2, 20, 42);
    this.Validations["c_email"] = new ValidationObject("c_email", "emailField",emailField, 5, 100,19);

    this.Validations["c_address"] = new ValidationObject("c_address", "textField", textField, 0, 100, 1);
    this.Validations["c_zip"] = new ValidationObject("c_zip","textField", textField, 0, 10, 1);
    this.Validations["c_tel"] = new ValidationObject("c_tel", "numericField", numericField, 0, 20, 1);
    this.Validations["c_fax"] = new ValidationObject("c_fax", "numericField", numericField, 0, 20, 1);
    this.Validations["c_vat"] = new ValidationObject("c_vat", "textField", textField, 0, 20, 90);
    this.Validations["c_dep"] = new ValidationObject("c_dep", "numericField", numericField, 0, 20, 69);

    this.Validations["res_name"] = new ValidationObject("res_name","nameField", nameField, 2, 100, 89);
    this.Validations["res_logo"] = new ValidationObject("res_logo", "textField", textField, 0, 500, 1);
    
}

proValidations.prototype.CheckRegex = function (vo,sValue,sKey)
{
    var bMandatory = (vo.MinLength == 0) ? false : true;
    
    switch(vo.RegexType)
    {
        case 'emailField':
        {
            if ( ((bMandatory) && (sValue=="")) || ((sValue!="") && ((!ValidateEmailExp(trim(sValue)) || (!ValidateTextLen(vo.MinLength,vo.MaxLength, sValue))))) ) 
            {
                document.getElementById('spanErrMessage').innerHTML = getError(vo.ErrorMessage);
                document.getElementById(sKey).style.backgroundColor='#fffb86';
                document.getElementById(sKey).focus();
                document.getElementById('spanErrMessage').scrollIntoView(true);
                return false;
            }
            
            break;
        }
        default:
        {
            if ( ((bMandatory) && (sValue=="")) || ((sValue!="") && ((!ValidateTextExp(vo.Regex, sValue) || (!ValidateTextLen(vo.MinLength,vo.MaxLength, sValue))))) ) 
            {
                document.getElementById('spanErrMessage').innerHTML = getError(vo.ErrorMessage);
                document.getElementById(sKey).style.backgroundColor='#fffb86';
                document.getElementById(sKey).focus();
                document.getElementById('spanErrMessage').scrollIntoView(true);
                return false;
            }
            
            break;
        }
    }
    
    return true;
}

proValidations.prototype.ValidateForm = function (sFormName)
{

    var curForm = document.getElementById(sFormName);
    
    for(i=0;i<curForm.elements.length;i++)
    {
        if (this.Validations[curForm.elements[i].name] == null)
            continue;
            
        var valObj = this.Validations[curForm.elements[i].name]; 
        
         var bRet = this.CheckRegex(valObj,curForm.elements[i].value,curForm.elements[i].name);
         
         if(!bRet)
            return false;
    }
    
    return true;
}

function ToggleDetailsDiv(divId)
{
    var divNode = document.getElementById(divId);
    var divBG = document.getElementById('surveydiv');
    if (divBG == null) 
    {        
		divBG = document.createElement('DIV');            
		divBG.id = 'surveydiv';
		divBG.className = 'surveyDiv';
		document.getElementsByTagName('BODY')[0].appendChild(divBG);
    }
    if (divNode.style.display=="block")
    {
        divNode.style.display="none";
		divBG.style.display = 'none';
        return;
    } 
    divBG.style.display = 'block';  
    divBG.style.height = document.getElementById('pageTable').offsetHeight + 'px';
    divNode.style.display = "block";
    divNode.style.left = Math.round(document.body.clientWidth / 2 - divNode.offsetWidth / 2) + "px";
    divNode.style.top = Math.round(document.body.clientHeight / 2 - divNode.offsetHeight / 2) + getYScroll() - 100 + "px";
   
    //divNode.scrollIntoView(true);
    
}

function depositAutoRun()
{
	ToggleDetailsDiv('depositDiv');
}

function toggleCBToRadio(sFormId, sInputName)
{
	var arrCB = document.getElementById(sFormId)[sInputName];
	if (document.getElementById(sFormId) != null) if (arrCB != null) for(var i = 0; i < arrCB.length; i++) arrCB[i].onclick = chooseExlusive;
}

function chooseExlusive()
{
	var arrCB = this.form[this.name];	
	for(var i = 0; i < arrCB.length; i++) arrCB[i].checked = arrCB[i] == this;
}