var browserName=navigator.appName;
var browserVer=parseInt(navigator.appVersion);

if (browserName=="Netscape" && browserVer>=3)
	version = "n3";
if (browserName=="Microsoft Internet Explorer" && browserVer>=4)
  version="ie4";
else
  version="n2";

/* common variables */
var max_note_len = 255;

function isEmptyOption(a, b)
{
 var x="";
 if (a.selectedIndex >= 0) x=a.options[a.selectedIndex].value;
 if (x == "")
 {
  if (b != null) {
	window.alert(b);
  }
  a.focus();return true;
 }
 return false;
}

function getRadioValue(a) {
 var b = "", i = 0;
 while (a[i] != null) {
  if (a[i].checked == true)
	{ b = a[i].value };
  i++;
 }
 return b;
}

function setCheckedValue(a) {
 if (a.checked == true) a.value = 'Y'; else a.value = '';
}

function setCheckedProp(a, b) {
 a.value = b;
 if (a.value == 'Y') a.checked = true;
 else a.checked = false;
}

function printData()
{
 this.focus();
 if (window.print) window.print();
 return;
}

function emailCheck (emailStr) {

 /* The following variable tells the rest of the function whether or not
 to verify that the address ends in a two-letter country or well-known
 TLD.  1 means check it, 0 means don't. */

 var checkTLD=0;

 /* The following is the list of known TLDs that an e-mail address must end with. */

 var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

 /* The following pattern is used to check if the entered e-mail address
 fits the user@domain format.  It also is used to separate the username
 from the domain. */

 var emailPat=/^(.+)@(.+)$/;

 /* The following string represents the pattern for matching all special
 characters.  We don't want to allow special characters in the address.
 These characters include ( ) < > @ , ; : \ " . [ ] */

 var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";

 /* The following string represents the range of characters allowed in a
 username or domainname.  It really states which chars aren't allowed.*/

 var validChars="\[^\\s" + specialChars + "\]";

 /* The following pattern applies if the "user" is a quoted string (in
 which case, there are no rules about which characters are allowed
 and which aren't; anything goes).	E.g. "jiminy cricket"@disney.com
 is a legal e-mail address. */

 var quotedUser="(\"[^\"]*\")";

 /* The following pattern applies for domains that are IP addresses,
 rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
 e-mail address. NOTE: The square brackets are required. */

 var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

 /* The following string represents an atom (basically a series of non-special characters.) */

 var atom=validChars + '+';

 /* The following string represents one word in the typical username.
 For example, in john.doe@somewhere.com, john and doe are words.
 Basically, a word is either an atom or quoted string. */

 var word="(" + atom + "|" + quotedUser + ")";

 /* The following pattern describes the structure of the user*/

 var userPat=new RegExp("^" + word + "(\\." + word + ")*$");

 /* The following pattern describes the structure of a normal symbolic
 domain, as opposed to ipDomainPat, shown above. */

 var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

 /* Finally, let's start trying to figure out if the supplied address is valid. */

 /* Begin with the coarse pattern to simply break up user@domain into
 different pieces that are easy to analyze. */

 var matchArray=emailStr.match(emailPat);

 if (matchArray==null) {

 /* Too many/few @'s or something; basically, this address doesn't
 even fit the general mould of a valid e-mail address. */

 return false;
 }
 var user=matchArray[1];
 var domain=matchArray[2];

 /* Start by checking that only basic ASCII characters are in the strings (0-127).*/

 for (i=0; i<user.length; i++) {
 if (user.charCodeAt(i)>127) {
 return false;
   }
 }
 for (i=0; i<domain.length; i++) {
 if (domain.charCodeAt(i)>127) {
 return false;
   }
 }

 /* See if "user" is valid*/

 if (user.match(userPat)==null) {
 return false;
 }

 /* if the e-mail address is at an IP address (as opposed to a symbolic
 host name) make sure the IP address is valid. */

 var IPArray=domain.match(ipDomainPat);
 if (IPArray!=null) {
 for (var i=1;i<=4;i++) {
 if (IPArray[i]>255) {
 return false;
   }
 }
 return true;
 }

 /* Domain is symbolic name.  Check if it's valid.*/

 var atomPat=new RegExp("^" + atom + "$");
 var domArr=domain.split(".");
 var len=domArr.length;
 for (i=0;i<len;i++) {
 if (domArr[i].search(atomPat)==-1) {
 return false;
   }
 }

 /* domain name seems valid, but now make sure that it ends in a
 known top-level domain (like com, edu, gov) or a two-letter word,
 representing country (uk, nl), and that there's a hostname preceding
 the domain or country. */

 if (checkTLD && domArr[domArr.length-1].length!=2 &&
 domArr[domArr.length-1].search(knownDomsPat)==-1) {
 return false;
 }

 /* Make sure there's a host name preceding the domain.*/

 if (len<2) {
 return false;
 }

 /* If we've gotten this far, everything's valid!*/
 return true;
}


////////////////////////////////////////
function rTrim(a) {
	var x=a;
	var c=x.substring(x.length-1,x.length);
	while (c==' ' || c=='\n' || c=='\r') {
		x=x.substring(0,x.length-1);
		c=x.substring(x.length-1,x.length);
	}
	return x;
}

function lTrim(a) {
 var x = a;
 var c=x.substring(0,1);
 while (c==' ' || c=='\n' || c=='\r') {
  x=x.substring(1);
  c=x.substring(0,1);
 }
 return x;
}

function isEmpty(a, b)
{
 if (rTrim(lTrim(a.value)) == "")
 {
  if (b)
  {
	window.alert(b);
  }
  a.focus();
  return true;
 }
 return false;
}


function isValidId(a, b, r)
{
	var x	=	a.value;
// 	var r	=	'^\\S+$';
	var myRE=	new RegExp(r);
	var m1	=	x.match(myRE);

	if (m1 == null)
	{
		if (b != null) alert(b);
		a.select();
		a.focus();
		return false;
	}else
		return true;
}

//Change first char to uppercase
function firstCase(str){
	aa = str.charAt(0).toUpperCase() + str.substr(1,str.length-1);
	return aa;
}

function focusnext(fromobj, n, toobj)
{
   if(fromobj.value.length >= n) {
	  toobj.focus(); toobj.select();
   }
}

function isValidStr(obj, n, a0, n0, a1, n1, a2, n2, msg)
{
	var numstring = obj.value;
	var l=numstring.length;
	if (numstring == "" ){
		alert( "Please input "+msg);
		a0.select();a0.focus();
		return false;
	}else if (l != n){
		alert("Please input "+n+" digits of "+msg);
		if (a0.value.length < n0){	a0.select();a0.focus();}
		else if (a1.value.length < n1) {	a1.select();a1.focus();}
		else if (a2.value.length < n2) {	a2.select();a2.focus();}
		return false;
	}
	for(i = 0; i < l; i++) {
		c = numstring.charAt(i);
		if (!((c >= "0" && c <= "9") || (c>="a" && c<="z") || (c>="A" && c<="Z"))){
			alert("Invalid "+msg+".");
			if (i <n0){
				a0.select();a0.focus();
			}else if (i<(n0+n1)){
				a1.select();a1.focus();
			}else if (i<n){
				a2.select();a2.focus();
			}
		return false;
		}
	}
	return true;
}


function isdefined(variable)
{
	return (typeof(window[variable]) == "undefined")?  false: true;
}
function hi()
{
	alert("hi");
}

function isInt(a, msg) {
	var numstring = a.value;

	if (rTrim(lTrim(numstring)) == "" )
	{
		if (msg) alert(msg);
		a.select();
		a.focus()
		return false;
	}

	var l=numstring.length;

	for(i = 0; i < l; i++) {
		c = numstring.charAt(i);
		if (!(c >= "0" && c <= "9"))
		{
			if (msg) alert(msg);
			a.select();
			a.focus()
			return false;
		}
	}
	return true;
}


function isFloat(a, msg) {
	var numstring = a.value;

	if (rTrim(lTrim(numstring)) == "" )
	{
		if (msg) alert(msg);
		a.select();
		a.focus()
		return false;
	}

	var l=numstring.length;

	for(i = 0; i < l; i++) {
		c = numstring.charAt(i);
		if (!( (c >= "0" && c <= "9") || (c == '.') ))
		{
			if (msg) alert(msg);
			a.select();
			a.focus()
			return false;
		}
	}
	return true;
}


function isValidPhoneNumber(a, a0, a1, a2, a3) {

	var numstring = a.value;
	if (numstring == "" ){
		alert("Please input your phone number.");
		a0.select();a0.focus();
		return false;
	}

	var l=numstring.length;

	if (l != 10){
		alert("Please input 10 digits phone number. ");
		if (a0.value.length < 3){	a0.select();a0.focus();}
		else if (a1.value.length < 3) { a1.select();a1.focus();}
		else if (a2.value.length < 4) { a2.select();a2.focus();}

		return false;
	}

	for(i = 0; i < l; i++) {
		c = numstring.charAt(i);
		if (!(c >= "0" && c <= "9")){
			alert("Invalid phone number.");
			if (i <3){
				a0.select();a0.focus();
			}else if (i<6){
				a1.select();a1.focus();
			}else if (i<10){
				a2.select();a2.focus();
			}
			return false;
		}
	}

	if (isdefined(a3))
		for(i = 0; i < a3.value.length; i++) {
			c = a3.value.charAt(i);
			if (!(c >= "0" && c <= "9")){
				alert("Invalid extention phone number.");
				a3.select(); a3.focus();
				return false;
			}
		}

	return true;
}


function test_valid ( ctrlValue )
{
  var TrimmedValue = rTrim(lTrim(ctrlValue));

  if ( TrimmedValue.length < 1 || TrimmedValue == '*' || TrimmedValue == '?' || TrimmedValue == '%' || TrimmedValue == '_')
  {
	return ( false ) ;
  }
  else
  {
	return ( true ) ;
  }
} ;


function validate_civic_number ( ctrlValue )
{
  wildcards = "?*" ;

  testString = rTrim(lTrim(ctrlValue.toUpperCase ( ))) ;

  if ( testString.length < 1 )
  {
	return ( true ) ;
  }
  else
  {
	if (
	  ( testString.indexOf ( wildcards.charAt ( 0 ) ) > -1 )
	  || ( testString.indexOf ( wildcards.charAt ( 1 ) ) > -1 )
	)
	{
	  alert (
		"Wildcards (* or ?) cannot be used in the Street Number field. Please enter a street number."
		) ;

	  return ( false ) ;
	}
	else
	{
		var validNum = "0123456789";
		var i = count = 0;
		var streetnum

		streetnum = testString;

		for (i = 0; i < streetnum.length; i++)
		{

			if (validNum.indexOf(streetnum.substring(i, i+1)) == "-1")
			{
				alert("Please enter only numeric values for Street Number. Try Street Number without letter.");
				return (false);
			}
		}
	}
  }
  return ( true ) ;
} ;

function validate_suite_number ( ctrlValue )
{
  wildcards = "?*" ;

  testString = rTrim(lTrim(ctrlValue.toUpperCase ( ))) ;

  if ( testString.length < 1 )
  {
	return ( true ) ;
  }
  else
  {
	if (
	  ( testString.indexOf ( wildcards.charAt ( 0 ) ) > -1 )
	  || ( testString.indexOf ( wildcards.charAt ( 1 ) ) > -1 )
	)
	{
	  alert (
		"Wildcards (* or ?) cannot be used in the Unit/Suite/Apt field. Please enter a suite number."
		) ;

	  return ( false ) ;
	}
	else
	{
	  return ( true ) ;
	}
  }
} ;

function validate_delivery_number ( ctrlValue )
{
  wildcards = "?*" ;

  testString = rTrim(lTrim(ctrlValue.toUpperCase ( ))) ;

  if ( testString.length < 1 )
  {
	return ( true ) ;
  }
  else
  {
	if (
	  ( testString.indexOf ( wildcards.charAt ( 0 ) ) > -1 )
	  || ( testString.indexOf ( wildcards.charAt ( 1 ) ) > -1 )
	)
	{
	  alert (
		"PO Box or Route Number may not have wildcards."
		) ;

	  return ( false ) ;
	}
	else
	{
	  return ( true ) ;
	}
  }
} ;

function isValidPostalCode(a,b)
{
 var x=a.value;
 var m1=x.match(/^[a-zA-Z][0-9][a-zA-Z][0-9][a-zA-Z][0-9]$/);
 var m2=x.match(/^[a-zA-Z][0-9][a-zA-Z][' '][0-9][a-zA-Z][0-9]$/);
 if ( (!m1 && !m2) || a.length < 6)
 {
	if (b != null) alert(b);
	a.select();a.focus();return false
 }

 return true;
}


function validate_postal_code ( ctrlValue )
{
  letters  = "?*ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;

  digits   = "?*0123456789" ;

  wildcard = "*" ;

  testString = rTrim(lTrim(ctrlValue.toUpperCase ( ))) ;

/*	if ( testString.length < 1 )
  {
	return ( true ) ;
  }
  else
*/	{
	if ( testString.length > 6 )
	{
	  alert ( "Postal Code must be six characters long." ) ;

	  return ( false ) ;
	}
	else
	{
	  if ( testString.length < 6 )
	  {
		if ( testString.indexOf ( wildcard.charAt ( 0 ) ) > -1 )
		{
		  return ( true ) ;
		}
		else
		{
		  alert ( "Postal Code must be six characters long." ) ;

		  return ( false ) ;
		}
	  }
	  else // postal code length = 6
	  {
		if (
			 ( letters.indexOf ( testString.charAt ( 0 ) ) > -1 )
		  && ( digits.indexOf  ( testString.charAt ( 1 ) ) > -1 )
		  && ( letters.indexOf ( testString.charAt ( 2 ) ) > -1 )
		  && ( digits.indexOf  ( testString.charAt ( 3 ) ) > -1 )
		  && ( letters.indexOf ( testString.charAt ( 4 ) ) > -1 )
		  && ( digits.indexOf  ( testString.charAt ( 5 ) ) > -1 )
		)
		{
		  return ( true ) ;
		}
		else
		{
		  alert (
			"The postal code format should follow the following format ANANAN where N represents a number and A a letter."
			) ;

		  return ( false ) ;
		}
	  }
	}
  }
} ;


// When submit form, check the address information
// street_number, street_name, city, Province must be input
function runAdvancedSearch (form, type)
{
  var fieldCount = 0 ;

  var canSubmit = false ;


  if ( test_valid ( form.street_number.value ) )
  {
	fieldCount = fieldCount + 1 ;
  }else{
		alert ("The street number field is mandatory.");
		form.street_number.focus();
		return false;
  }

  if ( ! validate_civic_number ( form.street_number.value) )
  {
	form.street_number.focus();
	canSubmit = false ;
		return false;
  }

  if ( ! validate_suite_number ( form.unit_number.value ) )
  {
	form.unit_number.focus();
	canSubmit = false ;
		return false;
  }


  if ( test_valid ( form.street_name.value ) )
  {
	fieldCount = fieldCount + 1 ;
  }else{
		alert ("The street name field is mandatory.");
		form.street_name.focus();
		return false;
  }

  if ( test_valid ( form.city.value ) )
  {
	fieldCount = fieldCount + 1 ;
  } else{
		alert ("The Municipality field is mandatory.");
		form.city.focus();
		return false;
  }

	if (form.prov.value == -1){
		alert ("The Province field is mandatory.");
		form.prov.focus();
		return false;
	}else
		fieldCount = fieldCount + 1 ;

/*	if (form.suffix.value.length <= 3)
  {
	fieldCount = fieldCount + 1 ;
  }
*/


  //if ( test_street_type ( form.elements[streetTypeIndex].value ) )
  //{
  //  fieldCount = fieldCount + 1 ;
  //}

  //if ( test_street_direction ( form , streetDirectionIndex ) )
  //{
  //  fieldCount = fieldCount + 1 ;
  //}

  //if ( form.delivery_mode.value.length <= 2)
  {
   // fieldCount = fieldCount + 1 ;
  }

  //if ( test_valid ( form.delivery_number.value ) )
  {
  //  fieldCount = fieldCount + 1 ;
  }

  if ( fieldCount < 3 )
  {
 //   alert (
//		"One of the following fields must be entered:\n\t- Number suffix\n\t- Street Name\n\t- Municipality\n\t- Delivery Mode\n\t- PO Box or Route Number"
   //	"One of the following fields must be entered:\n\t- Number suffix\n\t- Street Name"
	//	) ;
	canSubmit = false ;
  }
  else
  {
	 canSubmit = true ;
  }
  //if ( ! validate_delivery_number ( form.delivery_mode.value ) )
  {
  //  canSubmit = false ;
  }

  if ( ! validate_postal_code ( form.pcode.value ) )
  {
	form.pcode.focus();
	canSubmit = false ;
		return false;
  }

	if (type ==1 )
	{
		if (!canSubmit)
		 alert("Address is invalid.");
	}
  return ( canSubmit ) ;
} ;

// When click Help button, popup a window to show the eligible addresses
// User can choose one address from the list. The choosed values are sent back to opener window
function helpaddress(form)
{
	//alert(form);
//	if (runAdvancedSearch(form, 1))
	{
		pcode = form.pcode.value;
		city = form.city.value;
		prov = form.prov.value;
		strname = form.street_name.value;
		strnum = form.street_number.value;
		strtype = form.street_type.value;

		if ( (pcode == "")
			&& ( city == "" || strname == "" || strnum == "" || prov == -1)
			)
		{
			alert(
				"The following fields must be entered: \n\t- Street Number \n\t- Street name \n\t- Municipality \n\t- Province \n\t or\n\t- Postal code"
			);
			return false;
		}

		url='helpaddr.php?form='+ form +'&pcode=' +pcode+ '&city=' +city+ '&prov=' + prov+ '&strname=' +strname+ '&strnum=' + strnum+ '&strtype='+ strtype;
		OpenWindow(url, 700, 350, 'left=200, top=150, toolbar=0, location=0, directories=0, status=0, menubar=0, scrollbars=1, resizable=1', 'HelpAddress');
	}
}

// DON'T ACCEPT ENTER AS SUBMIT CLICK
function noenter() {
  return !(window.event && window.event.keyCode == 13);
}

// For editing Select object options
//		selectall(frmsel)
//		Addsel(addcode, frmsel)
//		delsel(frmsel)
//		Addarea(frm)
function selectall(frmsel)
{
	for (i=0; i<frmsel.length; i++){
		frmsel.options[i].selected = true;
	}
}

function Addsel(addcode, frmsel)
{
	for (i=0; i<frmsel.length; i++)
	{
		optionstr = rTrim(lTrim(frmsel.options[i].value)) ;
		if ((optionstr!='') && ( addcode==optionstr)){
			alert ("This area is already included.");
			return;
		}
	}
	frmsel.options[frmsel.length] = new Option(addcode, addcode, false, false);
	return ( true ) ;
}

function delsel(frmsel, cfm)
{
//	selindex = frmsel.options.selectedIndex;
	len = frmsel.length;
	if (!cfm)
		ret = confirm('Are you sure to delete the area code?');
	else
		ret = true;

	if (ret == true)
	for (i=len-1; i>=0; i--)
		if (frmsel.options[i].selected == true)
		{
			frmsel.options[i] = null;
		}
}

function movesel(from, to)
{
	flen = from.length;
	tlen = to.length;

	for (i=flen-1; i>=0; i--)
	{
		if ( (rTrim(lTrim(from.options[i].value)) != "") && (from.options[i].selected == true) )
		{
			if ( (tlen > 0) && (rTrim(lTrim(to.options[0].value)) == "") )
			{
				to.options[0] = null;
				tlen = 0;
			}
			to.options[tlen++] = new Option(from.options[i].text, from.options[i].value, false, false);
			from.options[i] = null;
		}
	}
}


function transpostalcode(a)
{
 var x=a.value;
 var pos = 0;
 while (pos < x.length)
 {
	 ch = x.substr(pos,1);
	 if (ch == ' ') x=x.substr(0,pos) + x.substr(pos+1);
	 else pos++;
 }
 x = x.toUpperCase();
 return x;
}

function checkPostalFormat(pstr)
{
	letters  = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
	digits	 = "0123456789" ;

	testString = rTrim(lTrim(pstr.toUpperCase( )));
// 	testString = transpostalcode(testString);

	ret = true;
	if (testString.length != 0){
		for (i=0; i<testString.length; i++)
		{
			if (
				(((i%2) == 0) && ( letters.indexOf ( testString.charAt ( i ) ) > -1) )
			 || (((i%2) != 0) && ( digits.indexOf ( testString.charAt ( i ) ) > -1) )
				)
			{
				continue;
			}else{
				ret = false;
				break;
			}

		}
	}else{
		ret = false;
	}

	if (ret == false)
		alert (
		"The postal code format should follow the following format ANANAN where N represents a number and A a letter."
		) ;

	return ret;

}

function Addarea(frm)
{
	letters  = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" ;
	digits	 = "0123456789" ;

	areacode = frm.addarea.value;
	testString = rTrim(lTrim(areacode.toUpperCase ( ))) ;

	if (testString.length != 0){

		if ( checkPostalFormat(testString) )
		{
			for (i=0; i<frm.area.length; i++)
			{
				optionstr = rTrim(lTrim(frm.area.options[i].value.toUpperCase ( ))) ;
				if ((optionstr!='') && ( areacode.indexOf(optionstr) != -1 )){
					alert ("This area is already included.");
					return false;
				}
	//			if (optionstr == '') frm.area.options[i] = null;

			}
			frm.area.options[frm.area.length] = new Option(areacode, areacode, false, false);
			return ( true ) ;
		}else{
			frm.addarea.focus();
			frm.addarea.select();
			return ( false ) ;
		}
	}else{
		alert('Please input the area code.');
		return false;
	}
}


function OpenWindow( url, width, height, options, name )
{
	 if ( ! width ) width = 640;
	 if ( ! height ) height = 420;
	 if ( ! options || options == "") options = "left=300, top=100, directories=0, scrollbars=1,menubar=0,toolbar=0,location=0,status=0,resizable=1";
	 if ( ! name ) name = "NewWindow";

// 	options = "left=100, top=50, toolbar=0, location=0, status=0, menubar=0, scrollbars=0, resizable=0";
// 	alert(url+","+ name+","+ "width=" + width + ",height=" + height + "," + options);

	 var newWin = window.open( url, name, "width=" + width + ",height=" + height + "," + options );
	 newWin.focus();
}

function ModalDialogRemoveWatch()
{
	ModalDialog.value = '';
	ModalDialog.eventhandler = '';
}

function ModalDialogMaintainFocus()
{
  try
  {
	if (ModalDialogWindow.closed)
	 {
		window.clearInterval(window.ModalDialogInterval);
// 		eval(ModalDialog.eventhandler);
		return;
	 }
 	if (window.top == window)
		ModalDialogWindow.focus();
  }
  catch (everything) {	 }
}

function OpenModalWindow( url, width, height, options, name )
{

	if ( ! width ) width = "440px";
	if ( ! height ) height = "420px";
	// modal=yes for firefox
	if ( ! options ) options = "modal=yes, left=300, top=100, directories=0, scrollbars=1,menubar=0,toolbar=0,location=0,status=0,resizable=no";
	if ( ! name ) name = "outsideSiteWindow";

	//left=100, top=50, toolbar=0, location=0, status=0, menubar=0, scrollbars=0, resizable=0, width=450, height=400

	if (version == "ie4")
	{
		ModalDialogWindow = window.showModalDialog( url, window, "dialogHeight: "+height+"; dialogWidth: "+width+"; dialogTop: 30px; dialogLeft: 46px; edge: Raised; center: Yes; help: Yes; resizable: Yes; status: no;");
// ("SMD_target.htm","Dialog Box Arguments # 1","dialogHeight: 231px; dialogWidth: 303px; dialogTop: 197px; dialogLeft: 46px; edge: Raised; center: Yes; help: Yes; resizable: Yes; status: Yes;");

	} else	{	//firefox

		ModalDialogWindow = window.open( url, name, "width=" + width + ",height=" + height + "," + options );
		ModalDialogInterval = window.setInterval("ModalDialogMaintainFocus()",5);
		ModalDialogWindow.focus();
	}
}

// ref != 0, Refresh window
function back2openerclose(ref, url)
{
	if (opener && !opener.closed)
	{
		opener.focus();
		if (url)
			opener.location.href = url;
		if (ref != 0)
			opener.history.go(0);
	}
	return window.close();
}

// To check if the two select field selected the same item, used to check if the sender and recipient have the same address
function issame(sel1, sel2, msg) {
	if ( (sel1.options[sel1.selectedIndex].value!= -1) && (sel1.options[sel1.selectedIndex].value == sel2.value))
	{
		alert(msg);
		sel1.selectedIndex=0;
	}
}

function changebg()
{
	this.bgColor= "#006599";
}
function dumb()
{
}
// change btn bgcolor to show it as active one
function setBtnActive(id1)
{
	obj = document.getElementById(id1);
	obj.bgColor = "#006599";
// 	obj.addEventListener("mouseover", changebg,false);
	obj.onmouseover = changebg;
	obj.onmouseout = changebg;
// 	alert(obj.onmouseover+","+obj.bgColor);

	if (version == "ie4")
	{
	//	obj.children[0].children[0].className='linkw';	// only works in IE
		obj.childNodes[0].childNodes[0].className='linkw';
	} else {
		obj.childNodes[1].childNodes[0].className='linkw';		// only works in firefox
// 		alert(obj.innerHTML  );
	}
}



function showfields(obj)
{
	if (! obj.trt) istrt = -1;
	else istrt = obj.trt.value;

	var sevtype = document.getElementById('sevtype');
	var ptype = document.getElementById('ptype');
	var pweight = document.getElementById('pweight');
	var pakdim = document.getElementById('pakdim');
// 	var pinsure = document.getElementById('pinsure');
	var pvalue = document.getElementById('pvalue');
	var stotalwt = document.getElementById('stotalwt');
	var stotalvl = document.getElementById('stotalvl');
	var pready = document.form1.pready;
	var showpickdate = document.getElementById('showpickdate');
	var showpicktime = document.getElementById('showpicktime');
	var insure;

// 	if (obj.insure[0].checked)
// 		insure	=	obj.insure[0].value;
// 	else
// 		insure	=	obj.insure[1].value;
	insure	=	1;

	if (pready)
	{
		if (pready[0].checked) {	//YES
			showpickdate.style.display = '';
			showpicktime.style.display = '';
		}else{
			showpickdate.style.display = '';
			showpicktime.style.display = '';
		}
	}

// 	pinsure.style.display = 'none';
	if (istrt == -1) {
		sevtype.style.display = '';
		ptype.style.display = '';
// 		pinsure.style.display = '';
		if (document.form1.paknum.value == 1)
		{
// 			pweight.style.display = 'block';	// doesn't work on firefox
			pweight.style.display = '';
			if ( maxdimunit[obj.paktype.value] != -1 )
				pakdim.style.display = '';
			else
				pakdim.style.display = 'none';

			if (insure == 1)
				pvalue.style.display = '';
			else
				pvalue.style.display = 'none';

			stotalwt.style.display = 'none';
			stotalvl.style.display = 'none';
		}else{

			pweight.style.display = 'none';
			pakdim.style.display = 'none';
			pvalue.style.display = 'none';
			stotalwt.style.display = '';
			if (insure == 1)
				stotalvl.style.display = '';
			else
				stotalvl.style.display = 'none';

		}
		/*
		sevtype.style.visibility = 'visible';
		ptype.style.visibility = 'visible';
		pweight.style.visibility = 'visible';
		pakdim.style.visibility = 'visible';
		*/
	}else{
		document.getElementById('pakedit').style.display = 'none';
		sevtype.style.display = 'none';
		ptype.style.display = 'none';
		pweight.style.display = 'none';
		pakdim.style.display = 'none';
// 		pinsure.style.display = '';
		if (insure == 1)
			pvalue.style.display = '';
		else
			pvalue.style.display = 'none';

		stotalwt.style.display = 'none';
		stotalvl.style.display = 'none';
		/*
		sevtype.style.visibility = 'hidden';
		ptype.style.visibility = 'hidden';
		pweight.style.visibility = 'hidden';
		pakdim.style.visibility = 'hidden';
		*/
	}
}


function paknumchange(obj)
{
	if (
		(obj.options[obj.selectedIndex].value>1)
		&& (document.form1.trt.value == -1)
		&& (document.form1.paktype.value != -1)		// Select paktype first
		&& (document.form1.paktype.value != 1)		// Letters can only have one package
		)
	{
		document.getElementById('pakedit').style.display = 'inline';
		url = 'ordpacknew.php?pnum='+obj.options[obj.selectedIndex].value;
		OpenModalWindow(url, '400px','440px');
	}else{
		document.getElementById('pakedit').style.display = 'none';

		if (document.form1.trt.value == -1)
		{
			if (document.form1.paktype.value == -1)
			{
				alert('Please select package type first');
				obj.selectedIndex = 0;
			}
			if (document.form1.paktype.value == 1)
			{
				alert('Multiple packages are not available for letter size.');
				obj.selectedIndex = 0;
			}
		}

	}

	showfields(document.form1);
}

////////////////////  Show pop window ////////////////////////////
var poptimer	=	new Array();
var popidname	=	new Array();

function setTimer(id, name, timelen)
{
	if (!timelen) timelen=5000;

	poptimer[id]	=	setTimeout("hidepop("+ id +")", timelen);
	popidname[id]	=	name;
}
function clearTimer(id)
{
	clearTimeout(poptimer[id]);
}

// Temporary variables to hold mouse x-y pos.s
var tempX = 0
var tempY = 0

function showpop(id, name, e)
{
	// to retrieve mouse x-y pos.s
	if (version == "ie4")
	{ // grab the x-y pos.s if browser is IE
		tempX = event.clientX + document.body.scrollLeft + document.documentElement.scrollLeft;
		tempY = event.clientY + document.body.scrollTop + document.documentElement.scrollTop;
	} else {  // grab the x-y pos.s if browser is NS
		tempX = e.pageX;
		tempY = e.pageY;
	}
	// catch possible negative values in NS4
	if (tempX < 0){tempX = 0; }
	if (tempY < 0){tempY = 0; }

	for (i=1; i<poptimer.length; i++)
	{
		hidepop(i);
	}

// 	var selarr	=	document.getElementsByTagName("SELECT");
// 	for (i=0; i<selarr.length; i++)
// 	{
// 		selarr[i].style.visibility='hidden';
// 	}

	if (obj = document.getElementById(name))
	{
		obj.style.left = tempX + 5 + 'px';
		obj.style.top = tempY + 5 + 'px';

		obj.style.display = "block";
		setTimer(id, name);
	}
}

function hidepop(id)
{
	clearTimer(id);
	if (obj = document.getElementById(popidname[id]))
	{
		obj.style.display = "none";
	}

// 	var selarr	=	document.getElementsByTagName("SELECT");
// 	for (i=0; i<selarr.length; i++)
// 	{
// 		selarr[i].style.visibility='visible';
// 	}

}
////////////////////  Show pop window ////////////////////////////

