var oPopup = window.createPopup();
var returnObj = null;
var selected = '';
	
function trim(strText) { 
	while (strText.substring(0,1) == ' ') {
    	strText = strText.substring(1, strText.length);
    }
    
	while (strText.substring(strText.length-1,strText.length) == ' ') {
    	strText = strText.substring(0, strText.length-1);
    }
    
	return strText;
	
} 

function verifycolor() {
	
	if (selected == "") {
		alert("Invalid color!!!"); 
		return null;
	}
	
	selected = selected.replace(/#/,"");
	selected = trim(selected); 
	if (selected.length != 6) {
		alert("Invalid color!!!"); 
		return null;
	}
  	stdstr = '0123456789abcdefABCDEF';

  	for (i=0; i<selected.length; i++) {
    	chr = selected.charAt(i);
   		if (stdstr.indexOf(chr) == -1) {
   			alert("Invalid color!!!"); 
   			return null;
   		}
  	}
  	return selected;
}

function customcolor(){
	str = verifycolor();
	if (str != null){preview(str)};
}

function preview(obj, hex, color) {
	selected = color;
	obj.style.backgroundColor = color;
	hex.value = '#' + color;
}

function getColor() { 
	
	color = verifycolor();
	
	if (color == null) {
		alert("Invalid color!!!");
	} else {
		returnObj.value = '' + color.toUpperCase();
		returnObj.style.backgroundColor = '#' + color.toUpperCase();
	}
	
	oPopup.hide();
	
}

function palete_close() {
	oPopup.hide();
}

function showPallete(obj, event) {
	
	returnObj = document.getElementById(obj);
	menu = "colorPicker";
	width = 251;
	height = 183;

	var oPopBody = oPopup.document.body;

	height = parseInt(height);
	
	var HTMLContent = '<html><head></head><body>';	
	HTMLContent += document.getElementById(menu).innerHTML
	HTMLContent += '</body></html>';
	

	oPopBody.innerHTML = HTMLContent;
	
	oPopup.show(event.clientX + 2, event.clientY + 2, width, height, document.body);
	
}