/*=========================================*/
/* vBgarage v3.8.4                         */
/* For vBulletin 3.7.x                     */
/* Rewrited by kerk (http://vbsupport.org) */
/*=========================================*/

/********* vBGarage *********/
// Show List models and logo image
function start_wait()
{
    fetch_object('display_model').innerHTML = '';
    fetch_object('marka_wait_select').disabled = true;    
    fetch_object('markalogo').style.display = 'none';
    fetch_object('markalogo_wait_img').style.display = 'inline';
    fetch_object('model_wait_select').style.display = 'inline-block';
    fetch_object('model_select').style.display = 'none';    
    fetch_object('submitbutton').disabled = true;
}
function clear_wait()
{
    fetch_object('marka_wait_select').disabled = false;   
    fetch_object('markalogo').style.display = 'inline';
    fetch_object('markalogo_wait_img').style.display = 'none';
    fetch_object('model_wait_select').style.display = 'none';
    fetch_object('model_select').style.display = 'inline';    
    fetch_object('submitbutton').disabled = false;
}
function display_data(obj)
{
    start_wait();
    var sUrl = 'vbgarage.php';
    var postData = 'do=marka&marka=' + obj.value + '&ajax=1&securitytoken=' + SECURITYTOKEN;
    var handleSuccess = function(o)
    {
        if(o.responseText !== undefined)
        {
            fetch_object('data_marka').innerHTML = o.responseText;
        }
    }
    var handleFailure = function(o)
    {
        if(o.responseText !== undefined)
        {
            alert(o.responseText);
        }
    }
    var callback =
    {
          success: handleSuccess,
          failure: handleFailure,
          timeout: vB_Default_Timeout
    };
    YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, postData);
}
function display_markalogo(obj)
{
    start_wait();
    var sUrl = 'vbgarage.php';
    var postData = 'do=markalogo&markalogo=' + obj.value + '&ajax=1&securitytoken=' + SECURITYTOKEN;
    var handleSuccess = function(o)
    {
        if(o.responseText !== undefined)
        {
            fetch_object('markalogo').innerHTML = o.responseText;
            clear_wait();
        }
    }
    var handleFailure = function(o)
    {
        if(o.responseText !== undefined)
        {
            alert(o.responseText);
        }
    }
    var callback =
    {
          success: handleSuccess,
          failure: handleFailure,
          timeout: vB_Default_Timeout
    };
    YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, postData);
}
// Search functions
function start_wait_search()
{
    fetch_object('display_model').innerHTML = '';
    fetch_object('marka_wait_select').disabled = true;    
    fetch_object('model_wait_select').style.display = 'inline-block';
    fetch_object('model_select').style.display = 'none';
    fetch_object('submitbutton').disabled = true;
}
function clear_wait_search()
{
    fetch_object('marka_wait_select').disabled = false;    
    fetch_object('model_wait_select').style.display = 'none';
    fetch_object('model_select').style.display = 'inline';
    fetch_object('submitbutton').disabled = false;
}
function display_data_search(obj)
{
    start_wait_search();
    var sUrl = 'vbgarage.php';
    var postData = 'do=marka&marka=' + obj.value + '&securitytoken=' + SECURITYTOKEN;
    var handleSuccess = function(o)
    {
        if(o.responseText !== undefined)
        {
            fetch_object('data_marka').innerHTML = o.responseText;
            clear_wait_search();
        }
    }
    var handleFailure = function(o)
    {
        if(o.responseText !== undefined)
        {
            alert(o.responseText);
        }
    }
    var callback =
    {
          success: handleSuccess,
          failure: handleFailure,
          timeout: vB_Default_Timeout
    };
    YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, postData);
}
// Comment functions
var krclicked = false;
function kr_prepare_submit(formobj, maxL)
{
    if(formobj.comment.value.length < 3)
    {
          fetch_object('errors').style.display = 'inline';
          return false;
    }
    else if(formobj.comment.value.length > maxL)
    {
          var overhead = maxL - formobj.comment.value.length;
          fetch_object('charcounter').style.color = '#FF0000';
          fetch_object('charcounter').innerHTML = 'Too long... ' + overhead;
          return false;
    }else{
          if(krclicked == formobj.pbutton.value)
          {
                previewcomment();
                return false;
          }
          else if(krclicked == formobj.sbutton.value)
          {
                formobj.submit();
          }
          return false;
    }
}
// Hide errors <div>
function kr_hide_errors(id)
{
    fetch_object(id).style.display = 'none';
    return false;
}
// Handle whith upload files
window.onunload = function()
{
     // Stupid Firefox bug on history.back() don't enable previosly disabled button by function uploadfile()
     var uplsubmit = fetch_object('upload_submit');
     if(uplsubmit !== null)
     {
          uplsubmit.disabled = false;
     }
}
function prepare_uploadfile(formobj)
{
    this.uploadfile = function()
    {
          fetch_object('upload_wait').style.display = 'inline';
          fetch_object('upload_check').style.display = 'none';
          fetch_object('upload_submit').disabled = true;
          formobj.submit();
          //return true;
    }
    if(fetch_object('upload_file').value == "")
    { 
          fetch_object('upload_wait').style.display = 'none';
          fetch_object('upload_check').style.display = 'block';
          fetch_object('upload_submit').disabled = false;
          alert(vbphrase['vbgarage_uploadform_empty_js']);
          return false;
    }else{
          uploadfile();
    }    
}

function yesno(id, hidden, inline)
{
    fetch_object(id).style.display = (hidden == true ? "none" : "inline");
    fetch_object(id+'_1').style.display = (hidden == false ? "none" : "inline");
    fetch_object('upload_desc').style.display = (hidden == false ? "none" : "inline");
}
// AJAX Show popup garages in postbit
var request_garage = null;
function show_garage(userid, postid)
{
    if(YAHOO.util.Connect.isCallInProgress(request_garage))
    {
        cansel_request_garage();
    }
    var show_load = function()
    {
        var content_container = fetch_object('content_'+postid);
        if(content_container !== null)
        {
             content_container.parentNode.removeChild(content_container);
        }
        fetch_object('href_' + postid).style.display = 'none';
        fetch_object('wait_img_' + postid).style.display = 'inline-block';
        fetch_object('vbgpost_'+postid).style.display = 'none';    
    }
    var hide_load = function()
    {
        fetch_object('href_' + postid).style.display = 'inline-block';
        fetch_object('wait_img_' + postid).style.display = 'none';
        fetch_object('vbgpost_'+postid).style.display = 'block';
    }
    show_load();
    var sUrl = 'vbgarage.php';
    var postData = 'do=ajaxview&userid=' + userid + '&postid=' + postid + '&ajax=1&securitytoken=' + SECURITYTOKEN;
    var handleSuccess = function(o)
    {
        if(o.responseText !== undefined)
        {
            hide_load();
            fetch_object('vbgpost_'+postid).innerHTML = o.responseText;
            center_element(fetch_object('content_'+postid));
        }
    }
    var handleFailure = function(o)
    {
        if(o.responseText !== undefined)
        {
            alert(o.responseText);
        }
    }
    var callback =
    {
          success: handleSuccess,
          failure: handleFailure,
          timeout: vB_Default_Timeout
    };
    request_garage = YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, postData);
    
    cansel_request_garage = function()
    {
        if(YAHOO.util.Connect.isCallInProgress(request_garage))
        {
             YAHOO.util.Connect.abort(request_garage);
        }
        fetch_object('href_' + postid).style.display = 'inline-block';
        fetch_object('wait_img_' + postid).style.display = 'none';
        fetch_object('vbgpost_'+postid).style.display = 'none'; 
    }
    var vbg;  
    YAHOO.util.Event.onDOMReady(function()
    {  
        vbg = new YAHOO.util.DD('content_'+postid);
        vbg.setHandleElId('drag_'+postid+'_drop');
    }); 
}

function hide_obj(objid)
{
     fetch_object(objid).style.display = 'none';
}
// For Admin Control Panel, don't edit!
function kr_check_allgr()
{
	for (var i=0; i < document.cpform.elements.length; i++)
	{
		var e = document.cpform.elements[i];
		if (e.name != 'allboxgr' && e.name != 'allboxfs' && e.type=='checkbox' && e.name.substring(0, 11) == 'editshowcar')
		{
			e.checked = document.cpform.allboxgr.checked;
		}
	}
}
function kr_check_allfs()
{
	for (var i=0;i < document.cpform.elements.length;i++)
	{
		var e = document.cpform.elements[i];
		if (e.name != 'allboxgr' && e.name != 'allboxfs' && e.type=='checkbox' && e.name.substring(0, 11) == 'editforsale')
		{
			e.checked = document.cpform.allboxfs.checked;
		}
	}
}
// Count Chars (Comment Textarea)
var bName = navigator.appName;
function taCount(taObj,Cnt,maxL) 
{ 
	objCnt = fetch_object(Cnt);
	objVal = taObj.value;
    if(objCnt) 
    {
		if(bName == "Netscape")
        {	
			  if(objVal.length > maxL)
			  {
                    objCnt.style.color = '#FF0000';
                    objCnt.textContent = maxL - objVal.length;
			  }else{
			        objCnt.style.color = '#000000';
                    objCnt.textContent = maxL - objVal.length;
			  }
        }else{
              if(objVal.length > maxL)
              {
                    objCnt.style.color = '#FF0000';
                    objCnt.innerHTML = maxL - objVal.length;
              }else{
                    objCnt.style.color = '#000000';
                    objCnt.innerHTML = maxL - objVal.length;
              }
        }
	}
	return true;
}
// Resize Comment Textarea
function resize_txtarea(to, id)
{
    var textarea = fetch_object(id);
    var size_change;
    var change_direction = to == 1 ? 1 : -1;
    this.resize_editor = function(size_change)
    {
         if(to == 1)
         {
               size_change = 99;
         }else{
               size_change = -99;
         }
         var newheight = parseInt(textarea.style.height, 10) + size_change;
         if(newheight >= 60)
	     {
	           textarea.style.height = newheight + 'px';
         }    
    }
    resize_editor(size_change * change_direction);
    //return false;
}

// Preview Comment
var preview_ajax = null;
function disable_ctrl()
{
    fetch_object('wait_span').style.display = 'inline-block';
    fetch_object('errors').style.display = 'inline' ? 'none' : 'inline';
    fetch_object('canselbutton').style.display = 'inline';
    fetch_object('submitbutton').disabled = true;
    fetch_object('previewbutton').disabled = true;
    return false;    
}
function enable_ctrl()
{
    fetch_object('wait_span').style.display = 'none';
    fetch_object('errors').style.display = 'none';
    fetch_object('canselbutton').style.display = 'none';
    fetch_object('submitbutton').disabled = false;
    fetch_object('previewbutton').disabled = false;
}
function cansel_preview()
{
    if (YAHOO.util.Connect.isCallInProgress(preview_ajax))
    {
         YAHOO.util.Connect.abort(preview_ajax);
    }
    enable_ctrl();
    fetch_object('preview_comment').style.display = 'none';
    fetch_object('errors').style.display = 'none';
}
function previewcomment()
{
    var obj = fetch_object('comment');
    var comment = obj.value;
    disable_ctrl();
    comment = encodeURIComponent(comment);
    var sUrl = 'vbgarage.php';
    var postData = 'do=previewcomment&comment='+ comment +'&ajax=1&securitytoken=' + SECURITYTOKEN;
    var handleSuccess = function(o)
    {
        if(o.responseXML !== undefined)
        {
            enable_ctrl();
            var response = o.responseXML.documentElement.getElementsByTagName("comment");
            var htmldata = response[0].childNodes[0].nodeValue;
            fetch_object('preview_comment').innerHTML = (htmldata == '') ? '' : response[0].childNodes[0].nodeValue;
            fetch_object('preview_comment').style.display = (htmldata == '') ? 'none' : 'block';            
        }
    }
    var handleFailure = function(o)
    {
        if(o.responseText !== undefined)
        {
            alert(o.responseText);
        }
    }
    var callback =
    {
          success: handleSuccess,
          failure: handleFailure,
          timeout: vB_Default_Timeout
    };
    if(YAHOO.util.Connect.isCallInProgress(preview_ajax))
    {
         YAHOO.util.Connect.abort(preview_ajax);
    }    
    preview_ajax = YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, postData);
}
// Add comment (not used yet)
function addcomment()
{
    var obj = fetch_object('comment');
    var comment = obj.value;
    disable_ctrl();
    comment = encodeURIComponent(comment);
    var sUrl = 'vbgarage.php';
    var postData = 'do=addcomment&comment='+ comment +'&ajax=1&securitytoken=' + SECURITYTOKEN;
    var handleSuccess = function(o)
    {
        if(o.responseXML !== undefined)
        {
            enable_ctrl();
            var response = o.responseXML.documentElement.getElementsByTagName("comment");
            var htmldata = response[0].childNodes[0].nodeValue;
            fetch_object('preview_comment').innerHTML = (htmldata == '') ? '' : response[0].childNodes[0].nodeValue;
            fetch_object('preview_comment').style.display = (htmldata == '') ? 'none' : 'block';            
        }
    }
    var handleFailure = function(o)
    {
        if(o.responseText !== undefined)
        {
            alert(o.responseText);
        }
    }
    var callback =
    {
          success: handleSuccess,
          failure: handleFailure,
          timeout: vB_Default_Timeout
    };
    if(YAHOO.util.Connect.isCallInProgress(preview_ajax))
    {
         YAHOO.util.Connect.abort(preview_ajax);
    }    
    preview_ajax = YAHOO.util.Connect.asyncRequest('POST', sUrl, callback, postData);
}
// Enable/Disable Update/Delete buttons
function checkradio(id)
{
    var radio = fetch_object('radio_' + id);
    var btn = fetch_object('btnupd');
    var rObj = fetch_object('table_images');
    var rTags = fetch_tags(rObj, 'input');
    for(j = 0; j < rTags.length; j++)
    {
         if(rTags[j].nodeType == 1 && rTags[j].type == 'radio')
         {
              if(radio.disabled && radio.checked)
              {
                    return id;
              }             
         }
    }
}
function checkradiobtn(id)
{
    var btn = fetch_object('btnupd');
    var radio = fetch_object('radio_' + id);
    var res = checkradio(id);
    if(res == id)
    {
          btn.disabled = true;
    }else{
          btn.disabled = false;      
    }
}
var is_checked = false;
function enable_button(formobj)
{
    for(i = 0; i < formobj.elements.length; i++)
    {
         if(formobj[i].nodeType == 1 && formobj[i].type == 'checkbox' && formobj[i].checked)
         {
               return true; 
         }
    }
}
function check_checkbox(chBox)
{
     var formobj = chBox.form;
     if(chBox.checked)
     {
           is_checked = true;
     }else{
           is_checked = enable_button(formobj);
     }
     fetch_object('btndel').disabled = is_checked ? false : true;
}

