/* @copyright Copyright (C) 2007 Baby Gekko IT Consulting. www.babygekko.com. All rights reserved.
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see LICENSE.php
 * GNU General Public License or other free or open source software licenses.
 * See COPYRIGHT.php for copyright notices and details.
*/

function TrimString(sInString) {
	   if ( sInString )
	   {
		  sInString = sInString.replace( /^\s+/g, "" );// strip leading
		  return sInString.replace( /\s+$/g, "" );// strip trailing
	   }
	}
	
	function createNamedElement(type, name) {
	   var element = null;
	   // Try the IE way; this fails on standards-compliant browsers
	   try {
		  element = document.createElement('<'+type+' name="'+name+'">');
	   } catch (e) {
	   }
	   if (!element || element.nodeName != type.toUpperCase()) {
		  // Non-IE browser; use canonical method to create named element
		  element = document.createElement(type);
		  element.name = name;
	   }
	   return element;
	}	
// Added Nov 9, 2007
	function is_province_empty(formname,provincefieldname)
	{
		var provincelist = formname.elements[provincefieldname];
	    if ((provincelist.type == 'select-one'  && provincelist.selectedIndex ==0)  ||
	        ( provincelist.type == 'text'  && provincelist.value.length==0))
		{
			alert ('Please fill in your State/Province/Region...');
			return true;
		}
		return false;
	}
	//
	function set_province(formname,countryfieldname, provincefieldname)
	{
		var provincelist = formname.elements[provincefieldname];
	    var countrylist = formname.elements[countryfieldname]; // country list
		/////
		var selected_value = countrylist.value;
  	    var selected_text = countrylist.options[countrylist.selectedIndex].text;
	    var foundState = false;

		 //DEBUG
		/* alert (selected_text + selected_value); */
	   // Empty options just in case new drop down is shorter
	   //
	   if ( provincelist.type == 'select-one' ) {
		  provincelist.options.length = 0;
		  provincelist.options[0] = new Option('Please select State/Province/Region','');
		  provincelist.selectedIndex = 0;
	   }
	   // Populate the drop down with states from the selected country
	   //

		var stateLineArray   = state.split("|");        // Split into lines
   		var optionCntr = 1;
   		for (var loop = 0; loop < stateLineArray.length; loop++) {
			  lineArray = stateLineArray[loop].split(":");
			  countryCode  = TrimString(lineArray[0]);
			  stateCode    = TrimString(lineArray[1]);
			  stateName    = TrimString(lineArray[2]);
			/* //DEBUG
				if (countryCode == selected_text) {	alert(stateCode); } 
			*/
			  if ( countryCode == selected_text && countryCode != '' )
			  {
						 if ( provincelist.type == 'text' )
						 {

					        parentObj = provincelist.parentNode;
							parentObj.removeChild(provincelist);
							var inputSel = createNamedElement("SELECT",provincefieldname);
							inputSel.setAttribute("name",provincefieldname);
							parentObj.appendChild(inputSel) ;
							provincelist = document.getElementsByName(provincefieldname) [0];
							provincelist.options[0] = new Option('Please select State/Province/Region','');
							provincelist.selectedIndex = 0; 
							provincelist.focus();
						 }
						if ( stateCode != '' ) {provincelist.options[optionCntr] = new Option(stateName, stateCode);}
         				foundState = true;
         				optionCntr++
			  } // endif countryCode == selected_text
			  
		}	// end for loop
	   if ( ! foundState )
	   {
		  parentObj = provincelist.parentNode;
		  parentObj.removeChild(provincelist);
      // Create the Input Field
		  var inputEl = createNamedElement("INPUT", provincefieldname);
		  inputEl.setAttribute("type", "text"); 
		  inputEl.setAttribute("size", 30); 
		  inputEl.setAttribute("value", ""); 
		  parentObj.appendChild(inputEl);
   		  inputEl.focus();
   		}
	}	// end countrylist.onchange
