// AJAX functions

var xmlapi_location = "/xmlapi.php";


function urlencode(str) {
	str = escape(str);
	str = str.replace('+', '%2B');
	str = str.replace('%20', '+');
	str = str.replace('*', '%2A');
	str = str.replace('/', '%2F');
	str = str.replace('@', '%40');
return str;
}

function urldecode(str) {
	str = str.replace('+', ' ');
	str = unescape(str);
	return str;
}


function generateShortname(str) {
	var url = str.toLowerCase();
  	url = url.replace(/^\s+|\s+$/g, "") // trim leading and trailing spaces		
  	url = url.replace(/[\s]+/g, "_") // change all spaces and underscores to a hyphen
  	url = url.replace(/[^a-z0-9_-]+/g, "") // remove all non-alphanumeric characters except the hyphen
  	url = url.replace(/[_]+/g, "_") // replace multiple instances of the hyphen with a single instance
  	url = url.replace(/[-]+/g, "-")
  	url = url.replace(/^_+|_+$/g, ""); // trim leading and trailing hyphens
	return url;
}


/* This function is called when user selects file in file dialog */
function jsUpload(upload_field, re_text, prefix)
{
	// this is just an example of checking file extensions
	// if you do not need extension checking, remove 
	// everything down to line
	// upload_field.form.submit();
	
	if (re_text == "image") {
		re_text = /\.png|\.gif|\.jpg|\.jpeg/i;
	} else if (re_text == "pdf") {
		re_text = /\.pdf/i;
	}
	
	if (re_text) {
		//var re_text = /\.txt|\.xml|\.zip|\.csv/i;
		var filename = upload_field.value;
		
		//Checking file type
		if (filename.search(re_text) == -1)
		{
			alert("The file you selected is not of the correct type.");
			upload_field.form.reset();
			return false;
		}
	}
	document.getElementById(prefix+'upload_status').innerHTML = "uploading file...";
	upload_field.form.submit();
	upload_field.disabled = true;
	
	return true;
}

function removeFile(fileid, fileselect, prefix) {
	document.getElementById(fileid).value="";
	document.getElementById(fileselect).value="";
	document.getElementById(fileselect).disabled=false;
	document.getElementById(prefix+"upload_status").innerHTML = "file deleted";
	document.getElementById(prefix+"filedisplay").innerHTML = "";
}

function getEventKeycode(e) {
	if( !e ) {
		//if the browser did not pass the event information to the
	    //function, we will have to obtain it from the event register
	    if( window.event ) {
			//Internet Explorer
			e = window.event;
		} else {
			//total failure, we have no way of referencing the event
			alert("total failure");
			return;
		}
	}
	if( typeof(e.keyCode) == 'number'  ) {
		//DOM
		return e.keyCode;
	} else if( typeof( e.which ) == 'number' ) {
		//NS 4 compatible
		return e.which;
	} else if( typeof( e.charCode ) == 'number'  ) {
		//also NS 6+, Mozilla 0.9+
		return e.charCode;
	} else {
		//total failure, we have no way of obtaining the key code
		return null;
	}
}

function clearForm(form) {
	$(':input', form).each(function() {
		var type = this.type;
		var tag = this.tagName.toLowerCase(); // normalize case
		if (type == 'text' || type == 'password' || tag == 'textarea')
			this.value = "";
		else if (type == 'checkbox' || type == 'radio')
			this.checked = false;
		else if (tag == 'select')
			this.selectedIndex = -1;
	});
};

/**
 * Shamelessly stolen from http://www.logiclabz.com/javascript/copy-to-clipboard-with-javascript-on-mozilla-firefox-and-ie.aspx
 * @param s
 * @return
 */
function copy_to_clipboard(text)  
{  
	if(window.clipboardData) {  
		window.clipboardData.setData('text',text);  
	}  
	else {  
		var clipboarddiv=document.getElementById('divclipboardswf');  
		if(clipboarddiv==null) {  
			clipboarddiv=document.createElement('div');  
			clipboarddiv.setAttribute("name", "divclipboardswf");  
			clipboarddiv.setAttribute("id", "divclipboardswf");  
			document.body.appendChild(clipboarddiv);  
		}
		clipboarddiv.innerHTML='<embed src="/scripts/clipboard.swf" FlashVars="clipboard='+encodeURIComponent(text)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
	}
	//alert('The text is copied to your clipboard...');  
	return false;  
}

