
String.prototype.hexTOrgb = function() {
	for ( var hex, i = 0, colors = this.match( /\w\w/g ); ( hex = parseInt( "0x" + colors[i] ) ); i++ )
	colors[i] = hex;
	return colors.join( ',' );
}

String.prototype.rgbTOhex = function() {
	for ( var rgb, i = 0, colors = this.split(','); ( rgb = colors[i] ); i++ )
	colors[i] = parseInt( rgb ).toString( 16 ).toUpperCase().leadingZeros( 1, 2 );
	return "#" + colors.join( '' );
}

String.prototype.leadingZeros = function( qty, totalPlaces ) {
	var s = this;
	if ( this.length + qty > totalPlaces ) return s;
	for ( var i=0; i<qty; i++ )
	s = "0" + s;
	return s;
}

String.prototype.anyToHex = function() {
	if( this.length <= 7 ) {
		return this.toUpperCase();
	} else {
		result = this.substr(4);
		result = result.substr( 0, (result.length-1) );
		return result.rgbTOhex();
	}
}

String.prototype.getFileName = function() {
	var result = this.replace( /\//gi, "\\" );
	resultArr = result.split( '\\' );
	return( resultArr[ resultArr.length - 1 ] );
}

function flip( thisObj, newSrc ) {
	if( thisObj && newSrc ) thisObj.src = newSrc;
	return( true );
}

function flipClass( thisObj, newClass ) {
	if( thisObj && newClass ) thisObj.className = newClass;
	return( true );
}

function goTo( url ) {
	if( url ) document.location.href = url;
}

function deleteId( itemId, deleteScript ) {
  if( itemId && deleteScript ) {
    if( confirm( "Are you sure you want to delete this entry?" ) ) {
      goTo( deleteScript + '?id=' + itemId );
    }
  } 
}


function updateActive( whitepaperId, fileId ) {
  dojo.xhrGet( {
    url: "whitepapers.files.list.php?id=" + whitepaperId + "&toggle=" + fileId, 
    handleAs: "text",
    timeout: 5000, // Time in milliseconds
    load: function(response, ioArgs) {
      parent.dojo.byId("fileListId").innerHTML = response; 
      return response;
    },
    error: function(response, ioArgs) {
      console.error("HTTP status code: ", ioArgs.xhr.status);
      return response;
    }
  } );
}

function deleteFile( whitepaperId, fileId ) {
  dojo.xhrGet( {
    url: "whitepapers.files.list.php?id=" + whitepaperId + "&delete=" + fileId, 
    handleAs: "text",
    timeout: 5000, // Time in milliseconds
    load: function(response, ioArgs) {
      parent.dojo.byId("fileListId").innerHTML = response; 
      return response;
    },
    error: function(response, ioArgs) {
      console.error("HTTP status code: ", ioArgs.xhr.status);
      return response;
    }
  } );
}

function toggleDisplay( divId, toggleImg ) {
	if( divId ) {
		divObj = document.getElementById( divId );
		toggleImg.src = ( divObj.style.display == "none" ? "images/btn.list.open.gif" : "images/btn.list.closed.gif" );
		document.getElementById( divId ).style.display = ( divObj.style.display == "none" ? "block" : "none" );		
	}
}

function sendFile() {
  dojo.byId('uploadFormId').submit();
  setTimeout( "dojo.byId('fileInputId').value = ''", 2000 );
}
