﻿var Class = {
  create: function() {
    return function() {
      this.initialize.apply(this, arguments);
    }
  }
} 
var Abstract = new Object();

Object.extend = function(destination, source) {
  for (property in source) {
    destination[property] = source[property];
  }
  return destination;
} 

Object.extend(String.prototype,
 { 
    Format:function ()
    {     
        var format = this;
        for (var i = 0; i < arguments.length; i++) 
        {
            var regexp = '\\{'+i+'\\}';            
            var re = new RegExp(regexp,"mi"); 
            do{
                format = format.replace(re,arguments[i]);
                var found = format.match(re);                
            }while(found!=null)
        }
        return format; 
    },
    Trim: function()
	{		
		return this.replace(/^\s+/, '').replace(/\s+$/, '');
	}	
    
 });

function $el(id)
{
	return $('#'+id);
}
function Trim(string)
{
		return string.replace(/^\s+/, '').replace(/\s+$/, '');
}	



function FEC_OnChange(tbid, maxLength, lbid, message)
{		
	var val = $el(tbid).val();
	if(val.length>maxLength){
		val = val.substring(0,maxLength);
		$el(tbid).val(val);
	}
	var ost = maxLength - val.length;		
	$el(lbid).html(message.Format(ost));
}

function FEC_IsValidZipCode(source, arguments)
{			
	if(!CountryControlId || !Expression)			
		alert('CountryControlId or Expression = null');			
				
		var tb_country = document.getElementById(CountryControlId);
		if(!tb_country)
		{			
			alert('tb_country is null'+' id:'+CountryControlId);
			return; 
		}
		var country = Trim(tb_country.value);
		if(IsInCountries(country) && !CheckZip(arguments.Value))
		{				
			arguments.IsValid = false;
			return false;
		}			
		arguments.IsValid = true;			
		return true;
}
function CheckZip(value)
{
	var re = new RegExp(Expression);
	var newArray = value.match(re);						
	if(!newArray)
	{			
		return false;
	}
	return true;
}

function FEC_OnCountryChanged(tb, zip_id, validator_id)
	{			
		var validator = $el(validator_id);
		if(!IsInCountries(Trim(tb.value)))
		{			
			validator.hide();
		}
		else if(!CheckZip($('#'+zip_id).val()))
		{
			validator.show(); 	
		}
	}
	
	
	
function IsInCountries(value)
{
	if(!CountriesList)
	{
		alert('CountriesList is undefined');			
	}
	for(var i=0;i<CountriesList.length;i++)
	{						
		if(value.toLocaleLowerCase() == CountriesList[i].toLocaleLowerCase())
		{
			return true;			
		}
	}
	return false;
}

// Aforms autosuggest field where ampersand is not working from xslt
function GetSuggestUrl(mediaid){
    return '/sitecore modules/addition/aform/processActions.aspx?mode=ajax&suggestid=' + mediaid;
}
