/***
  *Array Javascript file
  *
  * Provides several array helper function for Javascript
  */


//get index of an specific element of an array
function array_indexOf(array, element){
	
	var returnArray = new Array();
	var help=array.length;
  	for(var i = 0; i < help; i++) {
    	if(array[i] == element) {
      		returnArray.push(i);
    	}
  	}
  	return returnArray;
}

//remove an element of an error or remove a range of elements
function array_remove(array, from, to) {
  	var rest = array.slice((to || from) + 1 || array.length);
  	array.length = from < 0 ? array.length + from : from;
  	return array.push.apply(array, rest);
}

//helper function for print_r which makes the padding from the screen border
function padding(length) {
	result = '';
	for(i = 0; i < length; i++) {
		result = result + '___';
	}
	return result;
}

//output of an error similar to that of the php-function print_r
function print_r(array, level) {
	var result = '';
	for(var value in array) {
		if(typeof array[value] == "object") {
			result = result + ' ' + padding(level) + value + "\n" + print_r(array[value], level + 1);
		}
		else {
			result = result + ' ' + padding(level) + value + ' = ' + array[value] + "\n";
		}
	}

return result;
}

//function to sort the assoc array where the checked domains are stored
function tld_sort(a, b) {
    
    // first of all, sort by priority of the url thingie
    if (typeof a['priority'] != undefined && typeof b['priority'] != undefined) {
        if (a['priority'] < b['priority']) {
            return -1;
        }
        if (a['priority'] > b['priority']) {
            return 1;
        }
    }
    
    // if there is no priority, sort by tld stuff
	var stringa = a['tld'];
	var stringb = b['tld'];
	var length  = Math.min(stringa.length, stringb.length);
	
	for(var i = 0; i < length; i++) {
		if((stringa.charCodeAt(i) - stringb.charCodeAt(i)) < 0) {
			return -1;
		}
		else if((stringa.charCodeAt(i) - stringb.charCodeAt(i)) > 0) {
			return 1;
		}
	}
	return 0;
}

function array_contains(array, string) {
	for(i = 0; i < array.length; i++) {
		if(array[i] == string) {
			return true;
		}
	}
	return false;
}