var hashCode = 'usedauto';

function tooltip(el){
	$(el).tooltip({ 
	    track: true, 
	    delay: 0, 
	    showURL: false,
	    showBody: ' - ', 
	    opacity: 0.85 
	});
}

function setFormValues(json, form) { 
    if (!json) return;    
    $.each(json, function(k, v) { 
        $("input[@name='"+ k +"']", form).val(v);  
    });
} 

function DialerLoginPopup(self) {	
    popup(self.href, 360, 240);     
    return false;
}

function CheckNotepad() {	
    if (!$('#clipboardCount').html()) { 
    	alert(text['notepad_no_records']); 
    	return false; 
    }
}

function setNotepadCount(el) {
    var notepad = $.cookie('notepad');
	var count = GetNotepadIds(notepad).length; 
   	el.html('('+ count +')'); 
   	
   	return notepad;   	
}

function VipPopup(el) {    
   popup(el.href, 430, 400);                     
   return false; 
}



function EmailPopupHandle(el) {
    $(el).click(function() { 
       popup(this.href, 500, 450);                     
       return false;
    });
}

function GetNotepadIds(notepad_str) {
	if(notepad_str) {
        return notepad_str.split('.'); 	
	} else return new Array();
}

function AddRemoveItem(array, item, add) { 
    var i = 0;
    var removed = false;         
    while (i < array.length) {
        if (array[i] == item) {
            array.splice(i, 1); 
            removed = true;           
        } else i++; 
    }   
  
    if (removed == false) array.push(item);
    return array;   
}

function popup(href, width, height) { 
       var options = 'width='+ width +',height='+ height +',resizable=yes,scrollbars=yes,toolbar=no,left='+ ((screen.width-width)/2) +',top='+ ((screen.height-height)/2) +'';
       var popup = window.open(href, '_blank', options);
}


function InitNotepad() {
    var notepad = setNotepadCount($("#clipboardCount"));
    var count = GetNotepadIds(notepad).length;    
    
    $("#adverts a.add_notepad").each(function (i) {                      
        if (notepad) {  
            var advert_id = $(this).attr('id');  
            if (notepad.indexOf(advert_id)>-1) {
                $(this).addClass('checked').text(text[1]);
            } else {
                $(this).removeClass('checked').text(text[0]);
            }
        }
        
        $(this).click(function () {
            var advert_id = $(this).attr('id');
            var ids = GetNotepadIds($.cookie('notepad'));
            var count = ids.length;                 
            if (ids) ids = AddRemoveItem(ids, advert_id); 
            if (count > ids.length) $(this).removeClass('checked').text(text[0]);                     
            else $(this).addClass('checked').text(text[1]); 

            $.cookie('notepad', ids.join('.'), {expires: 7, path: '/'});
            $("#clipboardCount").html('('+ ids.length +')');
            
            return false;
        });  
    });    
}

function AjaxAction(self, callback, params) {      
    if (!self.href || !callback) return;
    $.getJSON(self.href, function(json){
        callback(json, params);          
    });
}

function URIGetParam(URI) {
    var p;    
    if (URI.indexOf(p +'=') != -1) {
        return URI.replace ('/.*'+ p +'=([0-9]+).*/', '$1');
    } else return;
}

function redirect(url) {
    window.location = url;
}

function append2Form(form, name, value) {
    var input = document.createElement('input');
    input.type = 'hidden';
    input.name = name;
    input.value = value;
    form.appendChild(input);
}

