
function get_states(prepack_folder, id_country) {
	if(id_country === undefined) id_country = jQuery('#country').val();
	url = prepack_folder + 'combo.php?method=get_states&id_parent=' + id_country;
	jQuery('#state').removeOption(/./);
	jQuery('#state').show();
	jQuery('#state').ajaxAddOption(url, {}, true, setState);
}

function get_cities(prepack_folder, id_state) { 
	if(id_state === undefined) id_state = jQuery('#state').val();
	url = prepack_folder + 'combo.php?method=get_cities&id_parent=' + id_state;
	jQuery('#city').removeOption(/./);
	jQuery('#city').show();
	jQuery('#city').ajaxAddOption(url, {}, true, setCity);	
}

function swPage(p){
	jQuery('#hdn_page').val(p);
	jQuery('#frm').submit(); 
}

function isNumberKey(evt) {
   var charCode = (evt.which) ? evt.which : event.keyCode
   if (charCode > 31 && (charCode < 48 || charCode > 57))
      return false;

   return true;
}

/**
 * 
 * @param string prepack_folder
 * @param string success_text
 * @return void
 */
function js_suscribe(prepack_folder, success_text) {  
	
   var err = false;  
   var sus_name = jQuery('#sus_name').val();  
   var sus_email = jQuery('#sus_email').val();  
   var sus_country = jQuery('#sus_country').val();  
		
   if(sus_name =='' ){  
     var err = true;  
     jQuery('#sus_name').css('background', 'red');  
   } else {  
     var err = false;  
     jQuery('#sus_name').css('background', 'white');  
   }  

   if(sus_email.indexOf('@') < 2 ){  
     var err = true;  
     jQuery('#sus_email').css('background', 'red');  
   } else {  
     var err = false;  
     jQuery('#sus_email').css('background', 'white');  
   }  		

   if(err == false) { 
	   jQuery('#sus_div').fadeOut('slow');  		
	   jQuery.get(prepack_folder + 'suscribe.php?sus_name='+sus_name+'&sus_email='+sus_email+'&sus_country='+sus_country);  
	   jQuery('#sus_div_msg').html('<b>' + success_text + '</b>');  
	   jQuery('#sus_div_msg').fadeIn('slow');  
   }  
} 




function TrimString(sInString) {
  if ( sInString ) {
    sInString = sInString.replace( /^\s+/g, "" );// strip leading
    return sInString.replace( /\s+$/g, "" );// strip trailing
  }
}


//Populates the Property Type selected with the types from the prop types list

/**
 * @param string defaultPropType
 * @param string idPropTypeSel
 * @param string lang
 */
function populatePropType(defaultpType, idpTypeSel, lang) {
	 
  var pTypeLineArray = prop_type.split('|');  // Split into lines
  var selObj = document.getElementById(idpTypeSel);
  if(lang == 'sp') {
  	selObj.options[0] = new Option('Indistinto','');
  } else {
  	selObj.options[0] = new Option('All','');
  }
  
  selObj.selectedIndex = 0;
  for (var loop = 0; loop < pTypeLineArray.length; loop++) {
    lineArray = pTypeLineArray[loop].split(':');
    pTypeCode  = TrimString(lineArray[0]);
    pTypeName  = TrimString(lineArray[1]);
    if ( pTypeCode != '' ) {
      selObj.options[loop + 1] = new Option(pTypeName, pTypeCode);
    }
    if ( defaultpType == pTypeCode ) {
      selObj.selectedIndex = loop + 1;
    }
  }
  
}


// Populates the country selected with the counties from the country list

/**
 * @param string defaultCountry
 * @param string id
 * @param string lang
 */
function populateCountry(defaultCountry, idCountrySel, lang) {
 
  var countryLineArray = country.split('|');  // Split into lines
  var selObj = document.getElementById(idCountrySel);
  if(lang == 'sp') {
  	selObj.options[0] = new Option('Indistinto','');
  } else {
  	selObj.options[0] = new Option('All','');
  }
  
  selObj.selectedIndex = 0;
  for (var loop = 0; loop < countryLineArray.length; loop++) {
    lineArray = countryLineArray[loop].split(':');
    countryCode  = TrimString(lineArray[0]);
    countryName  = TrimString(lineArray[1]);
    if ( countryCode != '' ) {
      selObj.options[loop + 1] = new Option(countryName, countryCode);
    }
    if ( defaultCountry == countryCode ) {
      selObj.selectedIndex = loop + 1;
    }
  }
  
}

/**
 * @param string idCountrySel
 * @param string idStateSel
 */
function populateState(idCountrySel, idStateSel, lang, defaultCountry, defaultState) {
  var selObj = document.getElementById(idStateSel);
  selObj.style.display = '';
  var foundState = false;
  // Empty options just in case new drop down is shorter
  if ( selObj.type == 'select-one' ) {
    for (var i = 0; i < selObj.options.length; i++) {
      selObj.options[i] = null;
    }
    selObj.options.length=null;
    if(lang == 'sp') {
    	selObj.options[0] = new Option('Indistinto','');
    } else {
    	selObj.options[0] = new Option('All','');
    }
    selObj.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]);
  if (document.getElementById(idCountrySel).value == countryCode && countryCode != '' ) {
      if ( stateCode != '' ) {
        selObj.options[optionCntr] = new Option(stateName, stateCode);
      }
      // See if it's selected from a previous post
      if ( stateCode == defaultState && countryCode == defaultCountry ) {
        selObj.selectedIndex = optionCntr;
      }
      foundState = true;
      optionCntr++;
    }
  }
}

/**
 * @param string idStateSel
 * @param string idCitySel
 */
function populateCity(idStateSel, idCitySel, lang, defaultState, defaultCity) {
  var selObj = document.getElementById(idCitySel);
  selObj.style.display = '';
  var foundCity = false;
  // Empty options just in case new drop down is shorter
  if ( selObj.type == 'select-one' ) {
    for (var i = 0; i < selObj.options.length; i++) {
      selObj.options[i] = null;
    }
    selObj.options.length=null;
    if(lang == 'sp') {
    	selObj.options[0] = new Option('Indistinto','');
    } else {
    	selObj.options[0] = new Option('All','');
    }
    selObj.selectedIndex = 0;
  }
  // Populate the drop down with cities from the selected country
  var cityLineArray = city.split("|");  // Split into lines
  var optionCntr = 1;
  for (var loop = 0; loop < cityLineArray.length; loop++) {
    lineArray = cityLineArray[loop].split(":");
    stateCode  = TrimString(lineArray[0]);
    cityCode    = TrimString(lineArray[1]);
    cityName    = TrimString(lineArray[2]);
  if (document.getElementById(idStateSel).value == stateCode && stateCode != '' ) {
      if ( cityCode != '' ) {
        selObj.options[optionCntr] = new Option(cityName, cityCode);
      }
      // See if it's selected from a previous post
      if ( cityCode == defaultCity && stateCode == defaultState ) {
        selObj.selectedIndex = optionCntr;
      }
      foundCity = true;
      optionCntr++;
    }
  }
}	

