/* Spectrum.js AJAX Functions */

function removeWhitespace(string){
	// Remove whitespace from a javascript string
	
	var tempString = string;
	for(var i = 0; i < tempString.length; i++) {
		if(tempString.slice(i, i+1) == String.fromCharCode(9) || tempString.slice(i, i+1) == String.fromCharCode(10) || tempString.slice(i, i+1) == String.fromCharCode(11) || tempString.slice(i, i+1) == String.fromCharCode(13) || tempString.slice(i, i+1) == String.fromCharCode(32)) {
			tempString = tempString.slice(0, i) + tempString.slice(i+1, tempString.length);
			i--;
		}
	}
	return tempString;
}


function unique(){
	// Workaround browser cacheing by adding a unique timestamp at the end of each call
	
	var now = new Date();
	var ms = now.getTime();
	return ms;
}


function cleanString(string){
	// Remove spaces before and after a string
	var tempString = reverseString(string);
	var firstChar = 0;
	var lastChar = 0;
	var returnString;
	
	for(var i = 0; i < string.length; i++) {
		if(string.slice(i, i+1) != String.fromCharCode(9) && string.slice(i, i+1) != String.fromCharCode(10) && string.slice(i, i+1) != String.fromCharCode(11) && string.slice(i, i+1) != String.fromCharCode(13) && string.slice(i, i+1) != String.fromCharCode(32)) {
			firstChar = i;
			break;
		}
	}
	for(var i = 0; i < tempString.length; i++) {
		if(tempString.slice(i, i+1) != String.fromCharCode(9) && tempString.slice(i, i+1) != String.fromCharCode(10) && tempString.slice(i, i+1) != String.fromCharCode(11) && tempString.slice(i, i+1) != String.fromCharCode(13) && tempString.slice(i, i+1) != String.fromCharCode(32)) {
			lastChar = i;
			break;
		}
	}
	
	returnString = string.slice(firstChar,string.length - lastChar);
	
	return returnString;	
}


function reverseString(inputString) {
  // Function to reverse all characters in a string.
  var newString = '';
  for (var i=(inputString.length - 1); i>=0; i--) {
    newString += inputString.charAt(i);
  }
  return (newString);
}


function parseResult(result,actionTypes,action){
	/* Parse ajax result strings returned in the following format:
	
		Type:Message
		
		For Example: "ERROR:Image too small" will be returned as an error, with the error message "Image too small"
	
	*/
	
	/* Arguments
		result: ajax result string
		actionTypes: list of possible actions, case insensitive
		action: "type" - return the type of result
				"message" - return the message of the result		
	*/
	
	var actions = actionTypes.split(",");
	var returnString = "";

	for(var i = 0; i < actions.length; i++){
		if(result.toLowerCase().indexOf(actions[i].toLowerCase()+":") == 0){
			
			if(action.toLowerCase() == "type"){
				returnString = actions[i];
			} else if (action.toLowerCase() == "message"){
				returnString = result.slice(actions[i].length+1);
			}
			
		}
	}
	
	return returnString;	
}

function callAjax(componentName,functionName,argumentObject,successFunction,failFunction) {
	var ajaxCall = new ajax();
	ajaxCall.setCallbackHandler(successFunction);
	ajaxCall.setErrorHandler(failFunction);
	ajaxCall.setQueryFormat('row');
	ajaxCall.ajaxCall(componentName,functionName,argumentObject);
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}


Array.prototype.findIdx = function(value){
	for (var i=0; i < this.length; i++) {
		if (this[i] == value) {
			return i;
		}
	}
}