function openWindow(url, name) {
  popupWin = window.open(url, name, 'scrollbars=yes,resizable=yes,status=no,personal=no,menu=no,location=no,toolbar=no,left=20,top=20,width=770,height=500')
}

//useage: put each parameter in single quotes example: openSizedWindow('http://www.mercuryinteractive.com','myName','400','500');
function openSizedWindow(url, name, myHeight, myWidth) 
{
 popupWin = window.open(url, name, 'scrollbars=yes,resizable=yes,status=no,personal=no,menu=no,location=no,toolbar=no,left=20,top=20,width=' +myWidth+',height='+myHeight+'');
}

function openSizedWindowPlain(url, name, myHeight, myWidth) 
{
 popupWin = window.open(url, name, 'scrollbars=no,resizable=no,status=no,personal=no,menu=no,location=no,toolbar=no,left=20,top=20,width=' +myWidth+',height='+myHeight+'');
}

function openSizedWindowUI(url, name, myHeight, myWidth) 
{
 popupWin = window.open(url, name, 'scrollbars=yes,resizable=yes,status=yes,personal=no,menubar=yes,location=yes,toolbar=yes,left=20,top=20,width=' +myWidth+',height='+myHeight+'');
}


function openSizedCenterWindow(mypage, myname, myHeight, myWidth) {
	var winl = (screen.width - myWidth) / 2;
	var wint = (screen.height - myHeight) / 2;
	winprops = 'height='+myHeight+',width='+myWidth+',top='+wint+',left='+winl+',scrollbars=no,resizable=yes,status=no,personal=no,menu=no,location=no,toolbar=no'
	popupWin = window.open(mypage, myname, winprops)
}

/* The following are all MIC specific functions */

function goPage(Menu) {
   var Index = Menu.selectedIndex;
   var pURL = Menu.options[Index].value
   document.location.href = pURL;
}

// for changing results per page
function goRPage(Menu) {
   var Index = Menu.selectedIndex;
   var rppg = Menu.options[Index].value;
   Menu.form.rppg.value = rppg;
   Menu.form.submit();
}

// for changing page in multi-page result set
function goPPage(Menu) {
   var Index = Menu.selectedIndex;
   var offset = Menu.options[Index].value;
   Menu.form.offset.value = offset;
   Menu.form.submit();
}

function goLastPage( offset ){
   document.paginateForm.offset.value = offset;
   document.paginateForm.submit();
}

function goNextPage( offset ){
   document.paginateForm.offset.value = offset;
   document.paginateForm.submit();
}

function goLast(url) {
   var pURL = url;
   document.location.href = pURL;
}

function goNext(url) {
   var pURL = url;
   document.location.href = pURL;
}

function goSubmit() {
   document.forms[0].submit();
}

var newWin;

function printWin( loc ){
	newWin = window.open( loc, "Print", "scrollbars=yes,resizable=yes,toolbar=yes,top=0,left=0,height=500,width=650" );
	newWin.focus();
	//if( newWin.opener == null ) newWin.opener = self;
   newWin.print();
}

function confirmDelete( url, item ) {
    var x = confirm( "Are you sure you want to delete " + item + "?\n\nPress 'OK' to continue with the delete.  Press 'CANCEL' to close this window without deleting." );
    if ( x ) {
        document.location.href = url;
    }
}

function confirmRCDelete( url, item ) {
    var x = confirm( "Are you sure you want to completely delete " + item + " for all geosites and all partners?\n\nPress 'OK' to continue with the delete.  Press 'CANCEL' to close this window without deleting." );
    if ( x ) {
        document.location.href = url;
    }
}

function confirmDeleteWin( url, selectID, item )
{
    var loc = url + "&item=" + item + "&selectID=" + selectID + "&format=popup";
    //alert("location: " + loc);
    openSizedWindowPlain(loc, "Delete", 300, 500);
}

function openPopup( url )
{
   var loc = url + "&format=popup";
   openSizedCenterWindow( loc, "Info", 300, 500);
}

function orderBy( orderby ){
	document.searchForm.orderby.value = orderby;
	document.searchForm.submit();
}

function submitForm( myForm, action ){
	myForm.fuseaction.value = action;
	myForm.submit();
}

function setFormElement( formElement, value ){
	formElement.value = value;
}

function toggleRadioButton( button ){
	if( button.checked = "checked" ){
		button.checked = false;
	}else{
		button.checked = "checked";
	}
}

/* auto select country based on state selection */
function updateCountry(Menu, CountryField) {
   var Index = Menu.selectedIndex;
   var state = Menu.options[Index].value;
   var country = 1;
   if( state == 'AB' || state == 'BC' || state == 'MB' || state == 'NB' || state == 'NL' || state == 'NT' || state == 'NS' || state == 'NU' || state == 'ON' || state == 'PE' || state == 'QC' || state == 'SK' || state == 'YT' ){
      country = 2;
   }
   CountryField.selectedIndex = country;
}

function configPostal( Menu, postalDiv1, postalDiv2 )
{
   // postalDiv1 should SHOW if country = 1 or country = 2
   // ELSE postalDiv should HIDE
   // postalDiv2 should always show (just in case)
   var Index = Menu.selectedIndex;
   var country = Menu.options[Index].value;
   var displayUSCA = false;
   if( country == 1 || country == 12 )
   {
      displayUSCA = true;
   }
   if( displayUSCA )
   {
      toggleDiv( postalDiv1, 'hiddenFormField' );   
      //toggleDiv( postalDiv1, postalDiv2 );   
   }
   else
   {
      toggleDiv( postalDiv2, postalDiv1 );   
   }
}

/* update address fields from other fields */
function updateAddress( FormN, Prefix1, Prefix2 ) 
{
/*
   FormN = actual form object
   Prefix1 = field prefix we're copying data from
   Prefix2 = field prefix we're copying data to
   ------
   Fields (* is the prefix):
   *addr_street = text box
   *addr_city = text box
   *addr_state = drop down menu
   *addr_state_name = text box
   *addr_postal = text box
   *addr_countryID = drop down mneu
   *phone_number = text box
   *phone_fax = text box
   -------
   sample code to take take selected index from drop down menu
   
   var index = FormN[ Prefix1 + "addr_state"].selectedIndex;
   FormN[ Prefix2 + "addr_state"].selectedIndex = index;
*/
   FormN[ Prefix2 + "addr_street" ].value = FormN[ Prefix1 + "addr_street" ].value;
   FormN[ Prefix2 + "addr_city" ].value = FormN[ Prefix1 + "addr_city" ].value;
   FormN[ Prefix2 + "addr_state_name" ].value = FormN[ Prefix1 + "addr_state_name" ].value;
   FormN[ Prefix2 + "addr_postal" ].value = FormN[ Prefix1 + "addr_postal" ].value;
   FormN[ Prefix2 + "phone_number" ].value = FormN[ Prefix1 + "phone_number" ].value;
   FormN[ Prefix2 + "phone_fax" ].value = FormN[ Prefix1 + "phone_fax" ].value;

   var index = FormN[ Prefix1 + "addr_state"].selectedIndex;
   FormN[ Prefix2 + "addr_state"].selectedIndex = index;

   index = FormN[ Prefix1 + "addr_countryID"].selectedIndex;
   FormN[ Prefix2 + "addr_countryID"].selectedIndex = index;

}

function updateAddressCondense( Form, Prefix1, AddrField ) 
{
/*
    Similar to updateAddress except that it takes data from 
    the first address fields (listed in updateAddress ftn)
    and condenses them into one text string which is then
    written to the AddrField field.  e.g. AddrField = string;
*/
}

/* auto select another field based on country selection */
function updateDeliveryMethod(Country, DeliveryField, Value){
   if( Country != 1 && Country != 12 ){
      // auto select 2 day delivery   
      DeliveryField[ Value - 1 ].checked = "yes";
   }
}

function bookmark() {
    var url = this.location;
    var who = document.title;
    var ver = navigator.appName;
    var num = parseInt( navigator.appVersion );
    if ( (ver == "Microsoft Internet Explorer" ) && ( num >= 4 ) ) {
       window.external.AddFavorite( url,who );
    }else{

    }
}

function launchIntro( prefix ) { 

   var page = "/intro.php";

   pop_up = window.open( page,'pop_up','scrollbars=no,resizable=no,width=650,height=456,left=325,top=150,screenX=325,screenY=150');
   pop_up.focus ();
}

function expandEmail( linkObj, subject, msgbody )
{
  if ( linkObj.href.indexOf('BODY') < 0 ) 
  {
       if( document.all )
       {
           var extra = '';
           if( subject ) extra = extra + '&subject=' + subject;
           if( msgbody ) extra = extra + '&body=' + msgbody;
           linkObj.href = linkObj.href + extra;
       }
  }
}

/* BASIC TOOLS */
function isAlien(a) {
   return isObject(a) && typeof a.constructor != 'function';
}
function isArray(a) {
    return isObject(a) && a.constructor == Array;
}
function isBoolean(a) {
    return typeof a == 'boolean';
}
function isEmpty(o) {
    var i, v;
    if (isObject(o)) {
        for (i in o) {
            v = o[i];
            if (isUndefined(v) && isFunction(v)) {
                return false;
            }
        }
    }
    return true;
}
function isFunction(a) {
    return typeof a == 'function';
}
function isNull(a) {
    return typeof a == 'object' && !a;
}
function isNumber(a) {
    return typeof a == 'number' && isFinite(a);
}
function isObject(a) {
    return (a && typeof a == 'object') || isFunction(a);
}
function isString(a) {
    return typeof a == 'string';
}
function isUndefined(a) {
    return typeof a == 'undefined';
} 
//Powered by DCScript
function DOMCall(name) 
{
    if (document.getElementById)
    return document.getElementById(name);
    if (document.layers) 
    return document.layers[name];
    else if (document.all)
    return document.all[name];
}
function showHide( myDiv, showHide )
{
	if(document.createTextNode)
	{
      myElem = DOMCall(myDiv);
      if( myElem )
      {
         if( showHide == 'show' )
      		myElem.style.display = "block";
         else
      		myElem.style.display = "none";
      }
      // else alert('myDiv: ' + myDiv );
	}
}
// similar to showHide EXCEPT that this ftn checks the "checked" status of passed
// element to determine whether the div is shown or hidden
// if checked then showHide is applied to myDiv, else opposite
function checkShowHide( myCheckElement, myDiv, myShowHide )
{
   if( !myShowHide ) myShowHide = 'show';
   if( myCheckElement.checked )
   {
      showHide( myDiv, myShowHide );
   }
   else
   {  // pass in the opposite
      if( myShowHide == 'hide' )
         showHide( myDiv, 'show' );
      else
         showHide( myDiv, 'hide' );   
   }
}

// unset all radio options & set "defaultOption" if set
function resetRadios( subElem, defaultOption )
{
   if( subElem )
   {
      var subLen = subElem.length;
      //alert('resetRadios...'+elemName+' found - length:' + subLen);
      /*
      for( var j = 0; j < subLen; j++ )
      {
         var subOption  = subElem[j];
         var subChecked = subOption.checked;
         var subValue   = subOption.value;
         alert( j+': checked -'+subChecked+' value - '+subValue);
      }                        
      */
      subElem[ defaultOption ].click();
   }
   //else alert ('resetRadios...not found');
}
// opt1 = show
// opt2 = hide
function toggleDiv(opt1,opt0)
{
	if(document.createTextNode)
	{
		DOMCall(opt0).style.display = "none";
		DOMCall(opt1).style.display = "block";			
	}
}

// toggle field
function toggleFields(checkObj,elementID,opt1,opt0)
{
	if(document.createTextNode)
	{
		var options = DOMCall(elementID);
      if( options )
      {
         var chosenOption = options.getElementsByTagName("div");
   		if(checkObj.checked)
   		{
   			chosenOption[1].style.display = "none";
   			xbObj(opt1).style.display = "block";
   		}
   		else
   		{
   			chosenOption[1].style.display = "none";
   			xbObj(opt0).display = "block";
   		}
      }
	}
}

// Needed to pass users over to the MDF store pages
function passMDFAddress()
{
    document.addressForm.target = "hiddenFrame";
    document.addressForm.submit();
    return true;
}
function resize_iframe( myElement )
{
   myObj = DOMCall( myElement );
  //resize the iframe according to the size of the window
   myObj.height=document.body.offsetHeight - myObj.offsetTop - 26;
}
function resize_col( myElement, parentElement )
{
   myObj = DOMCall( myElement );
   parentObj = DOMCall( parentElement );
   
   var temp = document.body.height;
  //resize the iframe according to the size of the window
   //alert("body height: " + temp);
//   myObj.height = parentObj.height - 25;
}

function hiliteMissingFields( formName ){
   // this is here so that pages don't baulk when this isn't defined 
   // e.g. before a page is submitted
   return false;
}

// -- DEBUG FUNCTIONS --//
function iterateForm( myForm )
              {
                  document.write("OUTPUTTING FORM-----<br />");
                 for( var i = 0; i < myForm.elements.length; i++ )
                 {
                     var eElem = document.adminDetail.elements[i];
                     var eName = eElem.name;
                     var eType = eElem.type;
                     document.write( i+': '+eName+' ('+eType+')<br />');

                     if( eType == 'radio' )
                     {
                           var subElem = eval("myForm." + eName);
                           var subLen = subElem.length;
                           document.write('&nbsp;&nbsp;&nbsp;--LENGTH: '+subLen+'<br />');

                           for( var j = 0; j < subLen; j++ )
                           {
                              var subOption  = subElem[j];
                              var subChecked = subOption.checked;
                              var subValue   = subOption.value;
                              document.write( '&nbsp;&nbsp;&nbsp;--'+j+': checked -'+subChecked+' value - '+subValue+'<br />');
                           }                        
                     }
                 }
                  document.write("END OUTPUTTING FORM-----<br />");
               }
               
function setDateSelectsToday( myForm, fieldName )
{  
   if( myForm.elements[ fieldName+'[month]'] )
   {
   	currentDate = new Date();
      var fMon  = myForm.elements[ fieldName+'[month]' ];
      var fDay  = myForm.elements[ fieldName+'[day]' ];
      var fYear = myForm.elements[ fieldName+'[year]' ];
      var curMon = currentDate.getMonth() + 1;
      var curDay = currentDate.getDate();
      var curYear = currentDate.getFullYear();
      // do the month & day ... easy ones as values match keys
      fMon.selectedIndex = curMon;
      fDay.selectedIndex = curDay;
      // now pull the index that matches the curYear value
      for( var i = 0; i < fYear.options.length; i++ )
      {
         if( fYear.options[i].value == curYear )
         {
            fYear.selectedIndex = i;
            break;
         }
      }      
   }
}

