dojo.require("dojo.cookie");

/* Global Tabs */

function initTabs(){

	var tab_containers = dojo.query('.shopit_tabs');
	
	if(tab_containers.length == 0){
		return;
	}
	
	for(var t=0;t<tab_containers.length;t++){	
			
		var	tab_boxes = dojo.query('.tab_box', tab_containers[t]);
		var container = tab_containers[t].id;
		
		if(container == ''){
			alert('Please set an id for your main tab container\n example: \n<div class="shopit_tabs" id="add_service_container">');
			return false;
		}
		
		try{
			var tabs_ul = document.createElement('<ul class="tabs" id="' + container + '_tabs">');
		}catch(e){
			var tabs_ul = document.createElement('ul');
				tabs_ul.setAttribute('id', container + '_tabs');
				tabs_ul.setAttribute('class', 'tabs');
		}
		
		var i = 0;
		tab_boxes.forEach(
			function(tab_box){
				
				var id_string = tab_box.title
					id_string = id_string.replace(' ','_');
					id_string = id_string.replace('/','_');
					id_string = id_string.replace('-','_');
					id_string = id_string.toLowerCase();
					
					tab_box.id = id_string + '_box';

				try{
					var link = document.createElement('<a href="#" onclick="switchTab(\''+container+'\',\''+id_string+'\');return false;">');
				}catch(e){
					var link = document.createElement('a');
					link.setAttribute('href','#');
					link.setAttribute('onclick','switchTab(\''+container+'\', \''+id_string+'\');return false;');
				}
				
				link.appendChild(document.createTextNode(tab_box.title));
				
				var tab_set = true;
				var tab_cookied = dojo.cookie(container);
				
				if(typeof(tab_cookied) =='undefined' || tab_cookied == null){
					dojo.cookie(container, id_string);
					tab_set = false;
				}
				
				try{
				
					var is_active = '';
				
					if(tab_set==false && i==0){
						is_active += ' class="active"';
						tab_boxes[i].style.display='block';
					}else if(tab_cookied == id_string){
						is_active += ' class="active"';
						tab_boxes[i].style.display='block';
					}
					
					var li = document.createElement('<li id="' + id_string + '_tab"'+is_active+'>');
					
				}catch(e){
				
					var li = document.createElement('li');
						li.setAttribute('id', id_string + '_tab');
						
					if(tab_set==false && i==0){
						li.setAttribute('class','active');
						tab_boxes[i].style.display='block';
					}else if(tab_cookied == id_string){
						li.setAttribute('class','active');
						tab_boxes[i].style.display='block';
					}
				}
				
				tab_box.removeAttribute('title');
				
				li.appendChild(link);
				tabs_ul.appendChild(li);
				i++;
			}
		);
			
		dojo.place(tabs_ul, tab_boxes[0], 'before');
		
	}
}

function switchTab(container, active){
	
	dojo.query('.tab_box', dojo.byId(container)).style('display','none');
	dojo.query('li', dojo.byId(container + '_tabs')).removeClass('active');
	dojo.byId(active + '_box').style.display = 'block';
	dojo.byId(active + '_tab').className = 'active';
	dojo.cookie(container, active);
	
}

// returns true if enter key is presses

function enterKey(e){
	var keycode = (window.event) ? e.keyCode : keycode = e.which;
	return (keycode == 13) ? true : false;
}

function toggle(hide,show){
	dojo.byId(show).style.display='block';
	dojo.byId(hide).style.display='none';
}

function rememberCheckbox(f){

	var input_name = f.name.replace('[]','');
	var state = (f.checked) ? 'add' : 'remove';
	
	f.parentNode.parentNode.style.backgroundColor= (f.checked) ? '#ffc': '#fff';
	
	dojo.xhrGet ({
		url: '/ajax/checkbox/a/'+state+'/c/'+input_name+'/id/'+f.value,
		error: function(error){
			console.error ('Error: ', error);
		}
	});
}

/* 
* returns true | false of checkbox
*/
function isChecked(f){
	return f.checked;
}

/*
* used in conjunction with paging class
*/
function setCurrentPage(p){

	var pager_links_top = dojo.query('a', 'pager_top').removeClass('current_page');
		pager_links_top[p].className='current_page';
		
	var pager_links_bottom = dojo.query('a', 'pager_bottom').removeClass('current_page');
		pager_links_bottom[p].className='current_page';

}

// the class name can be anything but is usually 'success' or 'error'
// if you pass in custom classes, you must style them!
function setMessage(message, message_div, class_name, auto_hide){

    if (message_div) {
	
    	dojo.byId(message_div).innerHTML = message;
    	dojo.byId(message_div).className = class_name;
    	dojo.byId(message_div).style.display='block';
    	
    	if(auto_hide == true){
    		setTimeout('hideMessage(\''+message_div+'\')', 3000);
    	}
    }
}

function hideMessage(div){
	dojo.byId(div).className='hide';
	dojo.byId(div).innerHTML = '';
}

function validateRadioGroup(radio_group) {

    var count = -1;
    
    for (var i=radio_group.length-1; i > -1; i--) {
        if (radio_group[i].checked) {
        	count = i;
        	i = -1;
        }
    }
    if (count > -1) {
    	return radio_group[count].value;
    }else{
    	return false;
    }
}

function toggleDiv(div){
	var s = (dojo.byId(div).style.display=='none') ? 'block' : 'none';
		dojo.byId(div).style.display=s;
}

//prototype string to add trim methods
String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

function getIdSuffix(string){
	
	var id = '';
	
	if(string.indexOf('_') == -1){
		return id;
	}else{
		var id_string_array = string.split('_');
		var id_string = id_string_array[id_string_array.length - 1];
		
		id = ((typeof(id_string) == 'undefined') || isNaN(id_string)) ? '' : '_' + id_string;
		
		return id;
	}
}


function truncate(str, len){
	if (str.length > len) {
		str = str.substring(0, len);
		str = str.replace(/\w+$/, '');
		str += '...';
		return str;
	}
}

function in_array(needle, haystack, argStrict) {
 
    var found = false, key, strict = !!argStrict;
 
    for (key in haystack) {
        if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
            found = true;
            break;
        }
    }
 
    return found;
}