/**
 * 
 * Loads previously entered information into form fields
 * 
 * @param (Object) s select box object
 * @param {Array} a an array (created with json_encode) with address information.
 */
function loadAddressInfo(s, a)
{
	if (s.options[s.selectedIndex].value != 0)
	{
	  info = a[s.selectedIndex - 1];

	  document.getElementById('pickup-company').value = info['company_name'];
	  
	  // Make country select box have the correct country selected
	  countries = document.getElementById('pickup-country');

		for (i = 1; i < countries.length; i++)
		{
			if (countries.options[i].value == info['country'])
			{
				countries.options[i].selected = 'selected'; 
			}
		}
		
	  document.getElementById('pickup-city').value = info['city'];
	  document.getElementById('pickup-zip-code').value = info['zipcode'];
	  //document.getElementById('pickup-region').value = info['region'];
	  document.getElementById('pickup-address').value = info['address'];
	  document.getElementById('pickup-person').value = info['contact_person'];
	  document.getElementById('pickup-phone').value = info['contact_phone'];
	  document.getElementById('pickup-email').value = info['email'];
	} 
	else
	{
    // Clear those form fields 
    document.getElementById('pickup-company').value = '';
    document.getElementById('pickup-city').value = '';
    document.getElementById('pickup-zip-code').value = '';
    //document.getElementById('pickup-region').value = '';
    document.getElementById('pickup-address').value = '';
    document.getElementById('pickup-person').value = '';
    document.getElementById('pickup-phone').value = '';
    document.getElementById('pickup-email').value = ''; 
	}
	
}

/**
 * 
 * Clears E@syShip form fields
 */
function easyshipClear()
{
  document.getElementById('easyship-company-name-id').value = '';
  document.getElementById('easyship-company-code-id').value = '';
  document.getElementById('easyship-name-id').value = '';
  document.getElementById('easyship-surname-id').value = '';
  document.getElementById('easyship-job-title-id').value = '';
  document.getElementById('easyship-address-id').value = '';
  document.getElementById('easyship-city-id').value = '';
  document.getElementById('easyship-postal-code-id').value = '';
  document.getElementById('easyship-country-id').value = '';
  document.getElementById('easyship-phone-number-one-id').value = '';
  document.getElementById('easyship-phone-number-two-id').value = '';
  document.getElementById('easyship-fax-number-id').value = '';
  document.getElementById('easyship-email-id').value = '';
  document.getElementById('easyship-url-id').value = '';
  document.getElementById('easyship-niche-id').value = '';  
}

/**
 * 
 * Redirects to the appropriate contacts page based on country
 * 
 * @param (Object) s
 */
function buildContactsSelect(s)
{
	location.href = '?country=' + s.options[s.selectedIndex].value;
}

/**
 * 
 * Redirects to the appropriate contacts page
 * 
 * @param {Object} s
 */
function contactsSelect(s)
{
	if (location.href.indexOf("?country") == -1)
	{
		location.href = '?center=' + s.options[s.selectedIndex].value; 
	} 
	else
	{
	 location.href += '&center=' + s.options[s.selectedIndex].value;	
	}
}

/**
 * 
 * Checks if the given value is a digit
 * 
 * @param (String) s
 */
function isDigit(s)
{
  return ((s >= "0") && (s <= "9"));
}

/**
 * 
 * Pops up a package tracking window
 * 
 * @param {Object} url
 * @param {Object} fieldId
 * @param {Object} width
 * @param {Object} height
 */
function popupTrack(url, fieldId, width, height, errorString)
{
  trackId = document.getElementById(fieldId).value;

  win = window.open(url + trackId, '', 'height=' + height + ', width=' + width + ',scrollbars=1,resizable=0');
  win.focus();
}

/**
 * 
 * Pops up a support window
 * 
 * @param {Object} url
 * @param {Object} width
 * @param {Object} height
 */
function popupSupport(url, width, height, custID)
{
  if (custID)
  {
    url = url + '?cust=' + custID;
  }
  
  win = window.open(url, '', 'height=' + height + ', width=' + width + ',scrollbars=0,resizable=1');
  win.focus();  
}

/**
 * 
 * Pops up a custom window
 * 
 * @param {Object} url
 * @param {Object} width
 * @param {Object} height
 */
function popupCustom(url, width, height, param1, param2) {
  if (param1 != false) {
    url = url + '?param1=' + param1;
  }
  if (param2 != false) {
    url = url + '?param2=' + param2;
  }
  
  win = window.open(url, '', 'height=' + height + ', width=' + width + ',scrollbars=0,resizable=1');
  win.focus();  
}

var blocks_exeption = 0;
