top.mywin = null;


function new_window(mylink, windowname, refocus)
{
  //Name the Window, so the remote can target it
  parent.self.name = "Parent_Window";

  var href = "";

  if (typeof(mylink) == 'string')
       href=mylink;
  else
       href=mylink.href;
    top.mywin = window.open('', windowname, 'width=690,height=430,left=100, right=1000,resizable=yes, menubar=yes, scrollbars=yes, toolbar=no, status=yes');


  // if we just opened the window
  if (
       top.mywin.closed ||
       (! top.mywin.document.URL) ||
       (top.mywin.document.URL.indexOf("about") == 0)
     )
     top.mywin.location=href;
  else if (refocus)
       top.mywin.focus();

  return false;
}

function close_window (mylink)
{

  if (top.mywin)
  {

      top.mywin.close();
  }

  parent.document.location = mylink;
  return false;

}

function PrintThisPage()
{
    
  var sWinHTML  = parent.window['MainPage'].document.getElementById('contentstart').innerHTML;
  var winprint  = parent;
  var isHitList = parent.window['MainPage'].document.getElementById('isHitList');

  winprint.document.open();
  winprint.document.write('<html>\n');
  winprint.document.write('<head>\n');
  if (isHitList)
  {
    // include stylesheet to suppress the hide/show detail buttons
    winprint.document.write('<style type="text/css" media="screen">\n');
    winprint.document.write('  	<!-- @import "/css/printpreview.css"; -->\n');
    winprint.document.write('</style>\n');
  }

  // common elements
  winprint.document.write('<style type="text/css">\n');
  winprint.document.write('  	<!-- @import url("/css/taxnet.css"); -->\n');
  winprint.document.write('</style>\n');
  winprint.document.write('</head>\n');
  winprint.document.write('\n');
  winprint.document.write('<body BGCOLOR="White">\n');


  // output the contents of the page  
  winprint.document.write(sWinHTML);
  winprint.document.write('</body></html>');
  // Fix for Bugzilla ticket 588
  flush();
  winprint.document.close();
}

//Trigger the click on the link on the toolbar with the ID equals to 
//the input parameter elementId. We assume that the link is on the frame
//with the frame name equals to the input parameter frameName. Also changes
//the form value passed through the URL link. The form key and the form value 
//are specified in input parameters

function clickButtonHandlerForLink(frameName,elementId, keyValueDelimiter, pairDelimiter, formKey, formValue){

	var frameElement=window.parent.frames[frameName];
	var elementOnFrame=frameElement.document.getElementById(elementId);
	//alert(elementOnFrame.href);
	var updatedHref=getURLStringWithNewFormValue(elementOnFrame.href, formKey, keyValueDelimiter, pairDelimiter, formValue);
	elementOnFrame.href=updatedHref;
	alert(updatedHref);
	elementOnFrame.click();
}

//If the string has a form, e.g:
//"host:port?param1=value1&param2=value2param3..."
//this function gives a result string without specified 
//key and value in the input parameter. So if we specify 
// "param2" as the input parameter, we get:
// "host:port?param1=value1&param3..."

function getURLStringWithNewFormValue(inputString, key, keyValueDelimiter, pairDelimiter, newValue){
	//Reurn the string with everything after the 'key' stripped of
	var delimiterString=key+keyValueDelimiter;
	//alert('BOGI:'+delimiterString);	
	var tokenArray=inputString.split(delimiterString);
	//alert('RV:'+tokenArray);	
	var extensionString='';
	var resultString='';
	if(tokenArray.length<2){
		alert("The array for URL["+inputString+"] must have at least 2 elements.");
		return "";
	}
	
	var arrayWithValue=tokenArray[1].split(pairDelimiter);
	if(arrayWithValue.length>1){
		extensionString=arrayWithValue[1];
	}
	
	resultString=tokenArray[0]+delimiterString+newValue+extensionString;
	//alert("DN:"+resultString);	
	return (resultString);
}









function changeHandlerForExportDocFormat(){
	var element= event.srcElement;
	var optionValue=element.options[element.selectedIndex].value;
	var optionText=element.options[element.selectedIndex].text;
	document.getElementById('ph11').innerHTML=optionText;
	document.getElementById('ph12').innerHTML=optionText;
	document.getElementById('ph2').innerHTML=getExportExtension(optionValue);
	document.getElementById('ph3').innerHTML=getExportExtension(optionValue);
}

function clickSaveAsLinkHandler(thisElement,frameName,elementId){
	//Set the value of the documentExportFormat in the URL string to the
	//empty string ( i.e. if it has some value, simply replace that value with "")
	thisElement.href=getUrlStringWithoutValueByKey(thisElement.href, 'documentExportFormat=');
	var finalUrl=thisElement.href;
	//alert("FFLH:"+thisElement.href);
	//set the value of the documentExportFormat to the value from the dropdown menu on
	//the main document page savesend.htt
	finalUrl+=getElementValueFromFrame(frameName, elementId);
	thisElement.href=finalUrl;
//	alert("FLH:"+thisElement.href);
}

function clickSaveAsButtonHandler(elementIdForValue,frameName,elementId){
	//Trigger the click on the "Send As" link on the toolbar
	var frameElement=window.parent.frames[frameName];
	var elementOnFrame=frameElement.document.getElementById(elementId);
	elementOnFrame.click();
}

//IMPORTANT NOTE:
//Extension values in the function getExportExtension have the same values as the 
//directive EXPORT_EXTENSION on the template savesend.htt

function getExportExtension(exportValue){
	var extension='';
	if(exportValue == 'WORD')
	{
		extension = '.doc';
	}
	else if(exportValue == 'TEXT' || exportValue == 'UNICODE')
	{
		extension = '.txt';
	}
	else if(exportValue == 'RTF')
	{
		extension = '.rtf';
	}
	else if(exportValue == 'HTML')
	{
		extension = '.html';
	}
	else if(exportValue == 'PDF')
	{
		extension = '.pdf';
	}
	
	return extension;
}

//Gets the selected value of the dropdown menu( frameName should be the existing frame name,
//and elementId should be the ID of an existing SELECT element on that frame )

function getElementValueFromFrame(frameName, elementId){
	var frameElement=window.parent.frames[frameName];
	var elementOnFrame=frameElement.document.getElementById(elementId);
	//alert(elementOnFrame.options[elementOnFrame.selectedIndex].value);
	return elementOnFrame.options[elementOnFrame.selectedIndex].value;
}

//Gets the selected link href attribute value ( frameName should be the existing frame name,
//and elementId should be the ID of an existing LINK element on that frame )

function getLinkHrefFromFrame(frameName, elementId){
	var frameElement=window.parent.frames[frameName];
	var elementOnFrame=frameElement.document.getElementById(elementId);
	//alert("HREF:"+elementOnFrame.href);
	return elementOnFrame.href;
}

// Returns the input string stripped of everything after the pattern=input 
// parameter 'key'

function getUrlStringWithoutValueByKey(inputString, key){
	//Reurn the string with everything after the 'key' stripped of
	var array=inputString.split(key,1);
	if(array.length==0){
		alert("The array for URL["+inputString+"] must have at least one element.");
		return "";
	}
	
	return (array[0]+key);
}

function isInArray(inputArray, element,ignoreCase){
	var ret=false;
	if(inputArray!=null){
		for(i=0;i<inputArray.length;i++){
			if((inputArray[i]==element) ||
			   ((inputArray[i].toLowerCase()==element.toLowerCase()) && ignoreCase)){
				ret=true;
				break;
			}
		}
	}
	return ret;
}

function isValidValue(elementId)
{
  var isValid=true;
  var elementValue = document.getElementById(elementId).value;
  var validNumbers=new Array();
  validNumbers.push('2007-101');
  validNumbers.push('P-052');
  validNumbers.push('GI-009');
  validNumbers.push('89-2R2');
  validNumbers.push('2002-09-30');
  validNumbers.push('RC4008');
  validNumbers.push('MD20-1999');
  validNumbers.push('GST Memoranda (New Series) 2.1');
  validNumbers.push('400-1-2');
  validNumbers.push('B-001');
  
  if(isInArray(validNumbers,elementValue,true)==false){
	isValid=false;    
  }  
  return isValid;
}

function trimAll(stringParam) 
{
	// "Left" trim
	while (stringParam.substring(0,1) == ' ')
	{
		stringParam = stringParam.substring(1, stringParam.length);
	}
	// "Right" trim
	while (stringParam.substring(stringParam.length-1, stringParam.length) == ' ')
	{
		stringParam = stringParam.substring(0,stringParam.length-1);
	}
	return stringParam;
}


