

var textBoxObject;

var txtPhrase = new Array();

var myPopupWindow = '';

function openPopupWindow(url, name, width, height){
	
    //Remove special characters from name
    name = name.replace(/\/|\-|\./gi, "");

    //Remove whitespaces from name
    var whitespace = new RegExp("\\s","g");
    name = name.replace(whitespace,"");

    //If it is already open
    if (!myPopupWindow.closed && myPopupWindow.location)
    {
        myPopupWindow.location.href = encodeUrl(url);
    }
    else
    {
        myPopupWindow= window.open(encodeUrl(url),name, "location=no, scrollbars=yes, resizable=yes, toolbar=yes, menubar=no, width=" + width + ", height=" + height );
        if (!myPopupWindow.opener) myPopupWindow.opener = self;
    }

     //If my main window has focus - set it to the popup
    if (window.focus) {myPopupWindow.focus()}
}


function encodeUrl(url)
{
 	if (url.indexOf("?")>0)
 	{
		encodedParams = "?";
 		parts = url.split("?");
 		params = parts[1].split("&");
 		for(i = 0; i < params.length; i++)
 		{
			if (i > 0)
	 		{
				encodedParams += "&";
			}
			if (params[i].indexOf("=")>0) //Avoid null values
			{
				p = params[i].split("=");
				encodedParams += (p[0] + "=" + escape(encodeURI(p[1])));
			}
			else
			{
				encodedParams += params[i];
			}
		}
		url = parts[0] + encodedParams;
	}
	return url;
}


function addDefaultText(targetField, defaultText){
	textBoxObject = targetField;
	txtPhrase[targetField.id] = defaultText;
	
	textBoxObject.value = defaultText; 
	textBoxObject.style.color="#999999";
	addListener(textBoxObject,"focus", clearDefaultText); 
	addListener(textBoxObject,"blur", resetDefaultText); 
}

function clearDefaultText(e){ 
	var tbObj = e.srcElement ? e.srcElement : e.target;
    if(tbObj.value == txtPhrase[tbObj.id]) {
    	tbObj.value = ''; 
    	tbObj.style.color="#000000";
    }
} 

function resetDefaultText(e){ 
	var tbObj = e.srcElement ? e.srcElement : e.target;
    if(tbObj.value == '') {
    	tbObj.value = txtPhrase[tbObj.id]; 
    	tbObj.style.color="#999999";
    }
} 

function addListener(aElm,aEvent,aFunction) { 
     // IE needs to be registered as "onclick" while a DOM listener uses 
     // just "click" 
     if (aElm.attachEvent)  {
       return aElm.attachEvent("on"+aEvent, aFunction); 
     }
     else {
       return aElm.addEventListener(aEvent,aFunction,false); 
	 }
} 
