﻿if( !window.showModalDialog || !window.showModelessDialog)
{
  window.showModalDialog= function( url, input, option)
  {
    if( input!= null)
      alert('mozilla doesnt support Input data');
    return window.open(url, '', option+ ',modal=yes');
  }
  window.showModelessDialog= function( url, input, option)
  {
    if( input!= null)
      alert('mozilla doesnt support Input data');
    return window.open(url, '', option+ ',modal=no');
  }
}

if(typeof HTMLElement!="undefined" && !HTMLElement.prototype.insertAdjacentElement)
{
  HTMLElement.prototype.insertAdjacentElement = function (where,parsedNode)
  { 
    switch (where)
    {
      case 'beforeBegin': 
      this.parentNode.insertBefore(parsedNode,this) 
      break; 
      case 'afterBegin': 
      this.insertBefore(parsedNode,this.firstChild);
      break; 
      case 'beforeEnd': 
      this.appendChild(parsedNode); 
      break; 
      case 'afterEnd': 
      if (this.nextSibling) 
      this.parentNode.insertBefore(parsedNode,this.nextSibling); 
      else this.parentNode.appendChild(parsedNode); 
      break; 
    } 
  } 

  HTMLElement.prototype.insertAdjacentHTML = function (where,htmlStr)
  { 
//    var r = this.ownerDocument.createRange(); 
//    r.setStartBefore(this); 
//    var parsedHTML = r.createContextualFragment(htmlStr); 
//    this.insertAdjacentElement(where,parsedHTML) 
    var span= document.createElement('span');
    span.innerHTML= htmlStr;
    this.insertAdjacentElement(where,span) 
  } 

  HTMLElement.prototype.insertAdjacentText = function (where,txtStr) 
  { 
    var parsedText = document.createTextNode(txtStr);
    this.insertAdjacentElement(where,parsedText);
  } 
}

var Loco=
{
  document:
  {
    onreadystatechange: ''
  },
  Components:
  {
    DateTime:
    {
      show: function( clientId, culture, pattern)
      {
        var Culture= 'Culture_'+ culture;
        try{ eval( 'Culture_'+ culture);}
        catch( msg){ Culture= 'DefaultCulture';}
        
        Culture= eval( Culture);

        try{ pattern= eval( 'Culture.'+ pattern+ 'Pattern');}
        catch( msg){ pattern= Culture.LongDateTimePattern;}

        var splt= $( clientId).innerText.split(',');
        
        $( clientId).innerText= Culture.ParsePattern( pattern, splt[0], splt[1], splt[2], splt[3]);
      }
    },
    Calendar:
    {
      initialize: function( clientId, culture, styleGroup, minYear, maxYear, caption)
      {
        var Culture= 'Culture_'+ culture;

        try{ eval( 'Culture_'+ culture);}
        catch( msg){ Culture= 'DefaultCulture';}

        var calendar= $( clientId+ '_Calendar');
        var selectedDate= $( clientId);
        calendar.culture= Culture;
        calendar.styleGroup= styleGroup;
        calendar.minYear= minYear;
        calendar.maxYear= maxYear;
        var splt= selectedDate.value.split(',');
        calendar.year= splt[0];
        calendar.month= splt[1];
        calendar.day= splt[2];
        calendar.caption= caption;

        this.render( clientId);
        this.select( clientId, splt[2]);
      },
      open: function( clientId)
      {
        $( clientId+ '_Calendar').style.display= 'block';
      },
      close: function( clientId)
      {
        $( clientId+ '_Calendar').style.display= 'none';
      },
      next: function( clientId)
      {
        var calendar= $( clientId+ '_Calendar');
        if( calendar.curMonth== 12)
        {
          if( calendar.curYear>= calendar.maxYear)
            return;
          this.render( clientId, parseInt(calendar.curYear)+ 1, 1);
        }
        else
          this.render( clientId, calendar.curYear, parseInt(calendar.curMonth)+ 1);
      },
      previous: function( clientId)
      {
        var calendar= $( clientId+ '_Calendar');
        if( calendar.curMonth== 1)
        {
          if( calendar.curYear<= calendar.minYear)
            return;
          this.render( clientId, parseInt(calendar.curYear)- 1, 12);
        }
        else
          this.render( clientId, calendar.curYear, parseInt(calendar.curMonth)- 1);
      },
      select: function( clientId, day)
      {
        var calendar= $( clientId+ '_Calendar');
        calendar.year= calendar.curYear;
        calendar.month= calendar.curMonth;
        calendar.day= day;
        $( clientId).value= calendar.curYear+ ','+ calendar.curMonth+ ','+ day;
        $( clientId+ '_Monitor').innerHTML= '['+ eval(calendar.culture).ParsePattern( eval(calendar.culture).LongDatePattern, calendar.curYear, calendar.curMonth, day)+ ']';
        this.render( clientId);
        this.close( clientId);
      },
      render: function( clientId, year, month)
      {
        var calendar= $( clientId+ '_Calendar');
        var day= 0;
        if( year== null)
        {
          if( calendar.curYear== null)
          {
            calendar.curYear= calendar.year;
            calendar.curMonth= calendar.month;
          }
          year= calendar.curYear;
          month= calendar.curMonth;
        }
        else
        {
          calendar.curYear= year;
          calendar.curMonth= month;
        }

        if( calendar.curYear== calendar.year && calendar.curMonth== calendar.month)
          day= calendar.day;

        var html= '<table cellpadding="0" cellspacing="0" class="'+ calendar.styleGroup+ '-holder"><tr><td width="100%">'+
          '<table cellpadding="0" cellspacing="0" width="100%" class="'+ calendar.styleGroup+ '-header"><tr><td class="'+ calendar.styleGroup+ '-next"onclick="Loco.Components.Calendar.next(\''+ clientId+ '\');">&nbsp;</td>'+
          '<td width="*" align="center">'+ eval(calendar.culture).ParsePattern( eval(calendar.culture).YearMonthPattern, year, month, day)+'</td>'+
          '<td class="'+ calendar.styleGroup+ '-previous" onclick="Loco.Components.Calendar.previous(\''+ clientId+ '\');">&nbsp;</td></tr></table>'+
          '<table cellpadding="0" cellspacing="0" width="100%" class="'+ calendar.styleGroup+ '-body"><tr><td width="100%">'+
          '<table cellpadding="0" cellspacing="0" class="'+ calendar.styleGroup+ '-Title"><tr>';

        for( var i= 0; i< eval(calendar.culture).AbbreviatedDayNames.length; i++)
          html+= '<td class="'+ calendar.styleGroup+ '-cell">'+ eval(calendar.culture).AbbreviatedDayNames[i]+ '</td>';

			  html+='</tr></table><table cellpadding="0" cellspacing="0" width="140"><tr>';

			  var j= 1;
			  for( var i= 1; i< eval(calendar.culture).Calendar.GetDayOfWeek( year, month, 1); i++, j++)
			    html+= '<td class="'+ calendar.styleGroup+ '-cell-disable">&nbsp;</td>';

			  for( var i= 1; i<= eval(calendar.culture).Calendar.GetDaysInMonth( year, month); i++, j++)
			  {
			    if( j> 7)
			    {
			      j= 1;
			      html+= '</tr><tr>';
			    }
          var dayEvent= eval(calendar.culture).Calendar.GetDayEvent( year, month, i);
			    html+= '<td class="'+ calendar.styleGroup+ '-cell'+ ( i== day? '-selected': '')+ '" '+
			      'onmouseover="this.className=\''+ calendar.styleGroup+ '-cell-over\'" '+
			      'onmouseout="this.className=\''+ calendar.styleGroup+ '-cell'+ ( i== day? '-selected': '')+ '\'" '+
			      'onclick="Loco.Components.Calendar.select(\''+ clientId+ '\', \''+ i+ '\');" '+
			      ''+ ( dayEvent== null? '': 'title="'+ dayEvent+ '" style="color:red"')+ '>'+ i+ '</td>';
			  }

        dayEvent= eval(calendar.culture).Calendar.GetDayEvent( year, month, day);
			  html+= '</tr></table></td></tr></table><table cellpadding="0" cellspacing="0" width="100%" class="'+ calendar.styleGroup+ '-footer">'+
	        '<tr><td class="'+ calendar.styleGroup+ '-select" onclick="Loco.Components.Calendar.render(\''+ clientId+ '\', '+ calendar.year+ ', '+ calendar.month+ ');">&nbsp;</td>'+
	        '<td width="*" id="'+ clientId+ '_Caption" align="center">&nbsp;'+ (dayEvent== null? calendar.caption: dayEvent)+ '&nbsp;</td><td class="'+ calendar.styleGroup+ '-close" onclick="Loco.Components.Calendar.close(\''+ clientId+ '\');">&nbsp;</td></tr></table></td></tr></table>';

        calendar.innerHTML= html;
      }
    },
    Element:
    {
      Mask:
      {
        Show:function(id, message)
        {
          var Elem= $(id);
          if( Elem== null || Elem.value!= '')
            return;
          Elem.maskData= Elem.className;
          Elem.value= message;
          Elem.className= 'input-mask';
        },
        Hide:function(id)
        {
          var Elem= $(id);
          if( Elem== null || Elem.maskData== null)
            return;
          Elem.value= '';
          Elem.className= Elem.maskData;
          Elem.maskData= null;
        }
      },
      Fading:
      {
        Start:1.5,
        Speed:20,
        Alpha:0,
        Show:function(clientId, now)
        {
          Loco.Components.Element.Fading.Hide();
	        Loco.Components.Element.Fading.Box= $( clientId);
	        if( Loco.Components.Element.Fading.Box== null)
	          return;
	        Loco.Components.Element.Fading.Timeout= setTimeout( 'Loco.Components.Element.Fading.Interval= setInterval( "Loco.Components.Element.Fading.IncreaseAlpha();", '+ (now? 10: 'Loco.Components.Element.Fading.Speed* 10')+ ');', (now? 10: Loco.Components.Element.Fading.Start* 750));
        },
        IncreaseAlpha:function()
        {
          if( Loco.Components.Element.Fading.Box== null)
            return;
	        if( Loco.Components.Element.Fading.Alpha> 90 && Loco.Components.Element.Fading.Timeout!= null)
	        {
	          Loco.Components.Element.Fading.ClearFadingProcess();
 		        Loco.Components.Element.Fading.Alpha= 0;
 		        return;
	        }
	        Loco.Components.Element.Fading.Box.style.filter= "alpha(opacity="+ Loco.Components.Element.Fading.Alpha+ ")";
	        if( Loco.Components.Element.Fading.Box.style.display== 'none')
	          Loco.Components.Element.Fading.Box.style.display= 'block';
	        Loco.Components.Element.Fading.Alpha+= Loco.Components.Element.Fading.Speed;
        },
        Hide:function()
        {
	        if( Loco.Components.Element.Fading.Box== null)
	          return;
	        Loco.Components.Element.Fading.Box.style.display= "none";
	        Loco.Components.Element.Fading.ClearFadingProcess();
 	        Loco.Components.Element.Fading.Alpha= 0;
 	        Loco.Components.Element.Fading.Box= null;
        },
        ClearFadingProcess:function()
        {
	        clearTimeout( Loco.Components.Element.Fading.Timeout);
	        clearInterval( Loco.Components.Element.Fading.Interval);
	        Loco.Components.Element.Fading.Timeout= null;
	        Loco.Components.Element.Fading.Interval= null;
        }
      }
    },
    Language:
    {
      initialize: function( placeHolder, template, hidden, select)
      {
        this.placeHolder=$(placeHolder);
        this.template=$(template);
        this.hidden=$(hidden);
        this.select=$(select);
        this.template.style.display= 'none';
      },
      add: function()
      {
        var item= this.select.item( this.select.selectedIndex);
        if($( item.value+ '_form')!= null)
          return;
        var HTML= this.template.innerHTML;
        HTML= Loco.Text.Replace( HTML, '[%LanguageId%]', item.value);
        HTML= Loco.Text.Replace( HTML, '[%LanguageName%]', item.text);
        HTML= Loco.Text.Replace( HTML, '[%Delete%]', 'javascript:Loco.Component.Language.del(\''+ item.value+ '\');');
        //HTML= Loco.Text.Replace( HTML, '[%Add%]', 'javascript:Loco.Component.Language.add();');
        var Panel= document.createElement('span');
        Panel.innerHTML= HTML;
        Panel.id= item.value+ '_form';
        this.placeHolder.insertAdjacentElement( 'beforeEnd', Panel);
        this.updateHidden();
      },
      del: function(lang)
      {
        $( lang+ '_form').outerHTML= '';
        this.updateHidden();
      },
      updateHidden: function()
      {
        var result= '';
        for( var i= 0; i< this.select.length; i++)
        {
          var lang= this.select.item(i).value;
          if($( lang+ '_form')!= null)
            result+= lang+ ',';
        }
        if( result!= '')
          result= result.substring(0, result.length- 1);

        this.hidden.value= result;
      }
    },
    Menu:
    {
      initialize: function( clientId, MenuDatas, menuTheme, boxTheme, itemTheme, overTheme, nodeTemplate, delay, splitter, nodeWidth, nodeHeight, hIndent, vIndent, isHorizontal, direction/*isRTL*/, selectedTheme)
      {
        var info= new Object();
        info.defBoxTheme= boxTheme;
        info.defItemTheme= itemTheme;
        info.defOverTheme= overTheme;
        info.nodeTemplate= nodeTemplate;
        info.width= nodeWidth;
        info.height= nodeHeight;
        info.vIndent= vIndent;
        info.hIndent= hIndent;
        info.delay= delay;
        info.clientId= clientId;
        if( selectedTheme== null)
          info.selectedTheme= overTheme;
        else
          info.selectedTheme= selectedTheme;

        var MenuInstance= $( clientId+ '_Menu');

        var width= 0;
        var hwidth= 0;
        for( var i= 0; i< MenuDatas.length; i++)
        {
          var data= this.renderMenu( info, MenuDatas[i], isHorizontal, direction, 1);
          MenuInstance.insertAdjacentElement( "beforeEnd", data.Menu);
          if( data.SubMenu!= null)
            data.Menu.insertAdjacentElement( "beforeEnd", data.SubMenu);
          width+= parseInt(data.width)+ 2;
          if( data.width> hwidth)
            hwidth= data.width;
        }
        
        var w= this.GetWidth( menuTheme);
        if( w.indexOf('%')!= -1 || parseInt(width)< parseInt(w))
          width= w;
        MenuInstance.extraTheme= '';//';width:'+ (isHorizontal? width: hwidth)+ 'px;';
        this.setTheme( info, MenuInstance, menuTheme, 0);
      },
      GetWidth: function( cssText)
      {
        var sp= document.createElement( 'span');
        sp.style.cssText= cssText;
        return sp.style.width.replace('px', '');
      },
      renderMenu: function( info, menu, isHorizontal, direction, zIndex)
      {
        var menuElem= document.createElement( 'div');
        var subElem;

        var tempElem= this.renderTemplate( info, menu, isHorizontal, direction);
        var width= this.GetWidth( menu.it);
        if( width== null || width== '')
          width= info.width;

        menuElem.extraTheme= ';z-index:'+ zIndex+ ';height:'+ info.height+ 'px;'+ (isHorizontal== true? ('Float:'+ (direction? 'right;': 'left;')): '')+ ( isHorizontal? 'width:'+ width+ 'px;': '');

        var loc= window.location+ '&';
        var type= 1;
        if(menu.l!= '' && loc.indexOf(menu.l+ '&')> -1)
          type= 3;

        menuElem.type= type;
        this.setTheme( info, menuElem, menu.it, type);
        menuElem.insertAdjacentHTML( "afterBegin", tempElem);

        menuElem.info= info;
        menuElem.it= menu.it;
        menuElem.ot= menu.ot;
        menuElem.onmouseover= Loco.Components.Menu.menuItemOver;
        menuElem.onmouseout= Loco.Components.Menu.menuItemOut;
        if( menu.s!= null)
        {
          subElem= document.createElement( 'div');
          
          var swidth= 0;
          for( var i= 0; i< menu.s.length; i++)
          {
            var data= this.renderMenu( info, menu.s[i], false, direction, parseInt(zIndex)+ 1);
            subElem.insertAdjacentElement( "beforeEnd", data.Menu);
            if( data.SubMenu!= null)
              data.Menu.insertAdjacentElement( "beforeEnd", data.SubMenu);
            swidth+= parseInt(data.width);
          }

          subElem.extraTheme= ';z-index:'+ zIndex+ ';position:absolute;margin: '+ (isHorizontal?(1):(parseInt(info.vIndent)-1))+ 'px '+ (isHorizontal?'-1':(parseInt(info.width)+ parseInt(info.hIndent)))+ 'px 0px 0px;';//+'width:'+ swidth+ 'px;';
          this.setTheme( info, subElem, menu.bt, 0);
          subElem.style.display= 'none';

          var id= Math.round( Math.random()* 100000);
//TODO: commented because of a conflict with MOOTOOLS
//          while( $('SubId_'+ id)!= null)
//            id= Math.round( Math.random()* 100000);
          subElem.id= 'SubId_'+ id;

          menuElem.subId= subElem.id;
        }
        var result= new Object();
        result.Menu= menuElem;
        result.SubMenu= subElem;
        result.width= width;
        return result;
      },
      setTheme: function( info, element, theme, type)
      {
        var temp= '';
        if( theme== null)
        {
          switch( type)
          {
            case 0:
              temp= info.defBoxTheme;
              break;
            case 1:
              temp= info.defItemTheme;
              break;
            case 2:
              temp= info.defOverTheme;
              break;
            case 3:
              temp= info.selectedTheme;
              break;
          }
        }
        else
          temp= theme;
        element.style.cssText= temp+ ';'+ element.extraTheme;
      },
      menuItemOver: function()
      {
        Loco.Components.Menu.setTheme( this.info, this, this.ot, 2);
        for( var obj= $(this.subId); obj!= null; obj= $(obj.subId))
        {
          if( obj.style.display== 'block')
            break;
	        obj.style.display = 'block';
//	        obj.addBehavior('#default#time2');
//	        $('menuTransition').TARGETELEMENT= obj.id;
//	        $('menuTransition').BEGIN= obj.id+ '.begin';
//	        obj.beginElement();
//	        obj.removeBehavior('#default#time2');
	      }
      },
      menuItemOut: function()
      {
        Loco.Components.Menu.setTheme( this.info, this, this.it, this.type);
        for( var obj= $(this.subId); obj!= null; obj= $(obj.subId))
        {
          if( obj.style.display== 'none')
            break;
          obj.style.display= 'none';
        }
      },
      renderTemplate: function( info, menu, isHorizontal, direction)
      {
        var result= info.nodeTemplate;
        result= Loco.Text.Replace( result, '[%Title%]', menu.t);
        result= Loco.Text.Replace( result, '[%Link%]', menu.l);
        var temp= menu.i;
        if( temp!= null)
          temp= '<img src="'+ menu.i+ '" style="position:absolute;"/>';
        result= Loco.Text.Replace( result, '[%Icon%]', temp== null? '': temp);
        var img= '&nbsp;';
        if( menu.s!= null)
          img= isHorizontal? '<img src="images/menu_arrow_down.gif"/>': ( direction? '<img src="images/menu_arrow_left.gif"/>': '<img src="images/menu_arrow_right.gif"/>');
        return '<table style="" cellpadding="0" cellspacing="0"><tr align=center valign=middle width=100%><td width="*">'+ result+ '</td><td width="10px">'+ img+ '</td></tr></table>';
        //return '<div style="position:absolute;">'+ result+ img+ '</div>';
      }
    }
  },
  Image:
  {
    Fade: function( ImageElem, Src)
    {
      ImageElem.style.filter= 'blendTrans(duration=2)';
      if( ImageElem.filters.blendTrans.status!= 2)
      {
        ImageElem.insertAdjacentHTML( 'afterEnd', '<img id="'+ ImageElem.id+ '_Fadding" src="'+ Src+ '" style="display:none"/>');
        $( ImageElem.id+ '_Fadding').onreadystatechange= function()
        {
          var Status= $( ImageElem.id+ '_Fadding').readyState;
          $( ImageElem.id+ '_Fadding').outerHTML= '';
          if(Status!='complete')
            return Loco.Image.Fade( ImageElem, Src);

          ImageElem.filters.blendTrans.apply( );
          ImageElem.src= Src;
          ImageElem.filters.blendTrans.play( );
        }
      }
    }
  },
  Text:
  {
    Fade: function( TextElem, innerText)
    {
      TextElem.innerText= innerText;
    },
    Summerize: function( Source, length)
    {
      if( Source.length<= length)
        return Source;
      return Source.substr(0, length- 3)+ '...';
    },
    Replace: function( String, Pattern, Value)
    {
      while( String.indexOf( Pattern)!= -1)
        String= String.replace( Pattern, Value);
      return String;
    },
    Length: function( str, style)
    {
      var sp= $( 'Ruler');
      if( sp== null)
      {
        var sp= document.createElement('span');
        sp.id= 'Ruler';
        document.body.insertBefore( sp);
      }

      if(style!= null)
        sp.style.cssText= style;
      sp.style.visibility= "hidden";
      sp.innerHTML= str;
      return sp.offsetWidth;
    }
  },
  Language: function(element, formats)
  {
    element.formats= formats;
    element.onkeypress= function()
    {
      for( var i= 0; i< this.formats.length; i++)
      {
        switch( this.formats[ i])
        {
          case Loco.LanguageType.Farsi:
            this.dir= 'rtl';
            event.keyCode= Loco.String.Farsi( event.keyCode);
            break;
          case Loco.LanguageType.English:
            this.dir= 'ltr';
            event.keyCode= Loco.String.English( event.keyCode);
            break;
        }
      }
    }
  },
  Format: function(element, formats)
  {
    element.formats= formats;
    element.onkeyup= function()
    {
      for( var i= 0; i< this.formats.length; i++)
      {
        switch( this.formats[ i])
        {
          case Loco.FormatType.Number:
            this.value= Loco.String.Number( this.value);
            break;
          case Loco.FormatType.Price:
            this.value= Loco.String.Number( this.value);
            this.value= Loco.String.Price( this.value);
            break;
          case Loco.FormatType.SSN:
            this.value= Loco.String.Number( this.value);
            this.value= Loco.String.SSN( this.value);
            break;
          case Loco.FormatType.PostalCode:
            this.value= Loco.String.PostalCode( this.value);
            break;
          case Loco.FormatType.Alphabet:
            if( this.Language== Loco.LanguageType.Farsi)
              this.value= Loco.String.Alphabet( this.value, Loco.LanguageType.Farsi);
            else
              this.value= Loco.String.Alphabet( this.value, Loco.LanguageType.English);
            break;
          case Loco.FormatType.CertificateNumber:
            this.value= Loco.String.CertificateNumber( this.value);
            break;
          case Loco.FormatType.DashNumber:
            this.value= Loco.String.DashNumber( this.value);
            break;
        }
      }
    }
    element.rawValue= function()
    {
      var result= this.value;
      for( var i= 0; i< this.formats.length; i++)
      {
        switch( this.formats[ i])
        {
          case Loco.FormatType.Number:
            break;
          case Loco.FormatType.Price:
            result= Loco.String.removePrice( result);
            break;
          case Loco.FormatType.SSN:
            result= Loco.String.removeSSN( result);
            break;
        }
        return result;
      }
    }
  },
  FormatType:
  {
    Number: 'Number',
    Price: 'Price',
    SSN: 'SSN',
    PostalCode: 'PostalCode',
    CertificateNumber: 'CertificateNumber',
    DashNumber: 'DashNumber'
  },
  LanguageType:
  {
    Farsi: 'fa',
    English: 'en'
  },
  String:
  {
    Alphabet: function(source, languageType)
    {
      source+='';
      switch( languageType)
      {
        case Loco.LanguageType.Farsi:
          var regexp= new RegExp( "[^a-z';,`\\[\\]]" );
          do
          {
            source= source.replace ( regexp, '');
          }
          while( source.search( regexp)>= 0 )
          break;
        case Loco.LanguageType.English:
          var regexp= new RegExp( "[^a-zA-Z]" );
          do
          {
            source= source.replace ( regexp, '');
          }
          while( source.search( regexp)>= 0 )
          break;
      }
      return source;
    },
    Number: function(source)
    {
      source+='';
      var regexp= new RegExp( "\\D" );
      do
      {
        source= source.replace ( regexp, '');
      }
      while( source.search( regexp)>= 0 )
      return source;
    },
    Price: function( source)
    {
      source+='';
      source= source.replace( new RegExp( ',', 'g' ), '' );
      var regexp = new RegExp( "\\B(\\d{3})(,|$)" );
      do
      {
        source= source.replace ( regexp, ",$1" );
      }
      while( source.search( regexp)>= 0 )
      return source;
    },
    removePrice: function( source)
    {
      return source.replace( new RegExp( ',', 'g' ), '' );
    },
    DashNumber: function(source)
    {
      source+= '';
      var regexp= new RegExp( "[^0-9\\-]");
      do
      {
        source= source.replace ( regexp, '');
      }
      while( source.search( regexp)>= 0 )
      return source;
    },
    CertificateNumber: function(source)
    {
      source+='';
      var regexp= new RegExp( "[^0-9/\\-\\\\]");
      do
      {
        source= source.replace ( regexp, '');
      }
      while( source.search( regexp)>= 0 )
      return source;
    },
    SSN: function(source)
    {
      source+='';
      source= source.replace( new RegExp( '-', 'g' ), '' );
      regexp= new RegExp( "^(\\d{3})(\\d{1})" );
      if( source.search( regexp)>= 0 )
        source= source.replace ( regexp, "$1-$2" );

      regexp= new RegExp( "^(\\d{3})(-)?(\\d{6})(\\d{1})" );
      if( source.search( regexp)>= 0 )
        source= source.replace ( regexp, "$1-$3-$4" );
        
      regexp= new RegExp( "^(\\d{3})(-)?(\\d{6})(-)?(\\d)(\\d)+" );
      if( source.search( regexp)>= 0 )
        source= source.replace ( regexp, "$1-$3-$5" );

      return source;
    },
    removeSSN: function( source)
    {
      return source.replace( new RegExp( '-', 'g' ), '' );
    },
    PostalCode: function(source)
    {
      source+='';
      source= source.replace( new RegExp( '-', 'g' ), '' );
      regexp= new RegExp( "^(\\d{5})(\\d{1})" );
      if( source.search( regexp)>= 0 )
        source= source.replace ( regexp, "$1-$2" );

      regexp= new RegExp( "^(\\d{5})(-)?(\\d{5})(\\d)+" );
      if( source.search( regexp)>= 0 )
        source= source.replace ( regexp, "$1-$3" );

      return source;
    },
    Farsi: function( Key)
    {
      if( Key> 127)
        return Key;
      switch( Key)
      {
        case 72:
          return 1570;
        case 104:
          return 1575;
        case 102:
        case 70:
          return 1576;
        case 96:
          return 1662;
        case 106:
        case 74:
          return 1578;
        case 101:
        case 69:
          return 1579;
        case 91:
          return 1580;
        case 93:
          return 1670;
        case 112:
        case 80:
          return 1581;
        case 111:
        case 79:
          return 1582;
        case 110:
        case 78:
          return 1583;
        case 98:
        case 66:
          return 1584;
        case 118:
        case 86:
          return 1585;
        case 99:
        case 67:
          return 1586;
        case 92:
          return 1688;
        case 115:
        case 83:
          return 1587;
        case 97:
        case 65:
          return 1588;
        case 119:
        case 87:
          return 1589;
        case 113:
        case 81:
          return 1590;
        case 120:
        case 88:
          return 1591;
        case 122:
        case 90:
          return 1592;
        case 117:
        case 85:
          return 1593;
        case 121:
        case 89:
          return 1594;
        case 116:
        case 84:
          return 1601;
        case 114:
        case 82:
          return 1602;
        case 59:
          return 1603;
        case 39:
          return 1711;
        case 103:
        case 71:
          return 1604;
        case 108:
        case 76:
          return 1605;
        case 107:
        case 75:
          return 1606;
        case 44:
          return 1608;
        case 105:
        case 73:
          return 1607;
        case 100:
          return 1610;
        case 68:
          return 1609;
        case 109:
        case 77:
          return 1574;
        default:
          return Key;
      }
    },
    English: function( Key)
    {
      if( Key< 128)
        return Key;
      switch( Key)
      {
        case 1570:
          return 72;
        case 1575:
          return 104;
        case 102:
        case 1576:
          return 70;
        case 1662:
          return 96;
        case 106:
        case 1578:
          return 74;
        case 101:
        case 1579:
          return 69;
        case 1580:
          return 91;
        case 1670:
          return 93;
        case 112:
        case 1581:
          return 80;
        case 111:
        case 1582:
          return 79;
        case 110:
        case 1583:
          return 78;
        case 98:
        case 1584:
          return 66;
        case 118:
        case 1585:
          return 86;
        case 99:
        case 1586:
          return 67;
        case 1688:
          return 92;
        case 115:
        case 1587:
          return 83;
        case 97:
        case 1588:
          return 65;
        case 119:
        case 1589:
          return 87;
        case 113:
        case 1590:
          return 81;
        case 120:
        case 1591:
          return 88;
        case 122:
        case 1592:
          return 90;
        case 117:
        case 1593:
          return 85;
        case 121:
        case 1594:
          return 89;
        case 116:
        case 1601:
          return 84;
        case 114:
        case 1602:
          return 82;
        case 1603:
          return 59;
        case 1711:
          return 39;
        case 103:
        case 1604:
          return 71;
        case 108:
        case 1605:
          return 76;
        case 107:
        case 1606:
          return 75;
        case 1608:
          return 44;
        case 105:
        case 1607:
          return 73;
        case 1610:
          return 100;
        case 1609:
          return 68;
        case 109:
        case 1574:
          return 77;
        default:
          return Key;
      }
    }
  }
}

function $( element)
{
  if( typeof arguments[ 0]== 'string')
    return document.getElementById( arguments[ 0]);
  return arguments[ 0];
}

function locoPopupV2( url, popupPage, width, isModal, clearCache)
{
	if( !popupPage || popupPage == '' )
		popupPage = "Popup.aspx";
	if( width == null )
		width = 600;
		
	if( isModal== null )
		isModal = true;
	
	var features = "height=500;resizeable=no;scrollbars=yes;status=0;dialogWidth=" + width + "px";
	var popupCode = '';
	if( isModal )
		popupCode = 'window.showModalDialog';
	else
		popupCode = 'window.open';
	
	
	if( url.charAt(0) != '?')
		popupCode += "('" + popupPage +"?";
	else
		popupCode += "('" + popupPage;

	popupCode +=  url+ '&rnd='+ Math.random()+ "', 'LocoPopup', '" + features + "');";
	return eval( popupCode);
	
}

if( typeof(document.onreadystatechange)=="undefined")
{
  window.setTimeout( "eval( Loco.document.onreadystatechange);", 2000);
}
else
{
  document.onreadystatechange= function()
  {
    if( document.readyState== 'complete')
      eval( Loco.document.onreadystatechange);
  }
}

var DefaultCulture=
{
  AbbreviatedDayNames: new Array( 'ش', 'ی', 'د', 'س', 'چ', 'پ', 'ج'),
  AbbreviatedMonthNames: new Array( 'فروردين', 'ارديبهشت', 'خرداد', 'تير', 'مرداد', 'شهريور', 'مهر', 'آبان', 'آذر', 'دي', 'بهمن', 'اسفند'),
  DayNames: new Array( 'شنبه', 'یکشنبه', 'دوشنبه', 'سه شنبه', 'چهارشنبه', 'پنجشنبه', 'جمعه'),
  MonthNames: new Array( 'فروردين', 'ارديبهشت', 'خرداد', 'تير', 'مرداد', 'شهريور', 'مهر', 'آبان', 'آذر', 'دي', 'بهمن', 'اسفند'),

  DateSeperator: '/',

  FullDateTimePattern: '[W] [d] [M] [y] [t]',
  LongDateTimePattern: '[d] [M] [y] [t]',
  ShortDateTimePattern: '[y][/][m][/][d] [t]',
  LongDatePattern: '[W] [d] [M] [y]',
  ShortDatePattern: '[y][/][m][/][d]',
  TimePattern: '[t]',
  MonthDayPattern: '[d] [M]',
  YearMonthPattern: '[AM] [y]',

  /*
  y -> 1363
  m -> 10
  AM -> دی - مخفف
  M -> دی
  d -> 24
  w -> 1
  AW -> ش
  W -> شنبه
  e -> dayEvent
  / -> seperator
  */
  ParsePattern: function( pattern, year, month, day)
  {
    var result= Loco.Text.Replace( pattern, '[y]', year);
    result= Loco.Text.Replace( result, '[m]', month);
    result= Loco.Text.Replace( result, '[AM]', this.AbbreviatedMonthNames[ month- 1]);
    result= Loco.Text.Replace( result, '[M]', this.MonthNames[ month- 1]);
    result= Loco.Text.Replace( result, '[d]', day);
    result= Loco.Text.Replace( result, '[w]', this.Calendar.GetDayOfWeek( year, month, day));
    result= Loco.Text.Replace( result, '[AW]', this.AbbreviatedDayNames[ this.Calendar.GetDayOfWeek( year, month, day)- 1]);
    result= Loco.Text.Replace( result, '[W]', this.DayNames[ this.Calendar.GetDayOfWeek( year, month, day)- 1]);
    result= Loco.Text.Replace( result, '[e]', this.Calendar.GetDayEvent( year, month, day));
    result= Loco.Text.Replace( result, '[t]', time);
    result= Loco.Text.Replace( result, '[/]', this.DateSeperator);
    return result;
  },

  Calendar:
  {
    GetDayEvent: function( year, month, day)
    {
      return null;
    },
    GetDayOfWeek: function( year, month, day)
    {
	    var tWeek= 1, tDay= 1, tMonth= 1, tYear= 1383;
      var offset= 0;
      if( year>= tYear)
	      for( var i= tYear; i< year; i++)
		      offset+= this.IsLeapYear( i)? 366: 365;
      else
	      for( var i= tYear; i> year; i--)
		      offset-= this.IsLeapYear( i- 1)? 366: 365;

      for( var i= tMonth; i< month; i++)
        offset+= this.GetDaysInMonth( tYear, i);
      offset+= parseInt( day- tDay);

      return(((( offset+ tWeek- 1)% 7)+ 7)% 7)+ 1;
    },
    GetDaysInMonth: function( year, month)
    {
		  var MonthDays= new Array( 31, 31, 31, 31, 31, 31, 30, 30, 30, 30, 30, 29);
		  if( month== 12 && this.IsLeapYear( year))
		    return MonthDays[ month- 1]+ 1;
		  return MonthDays[ month- 1];
    },
    IsLeapYear: function( year)
    {
		  var BreakYears= new Array( -61, 9, 38, 199, 426, 686, 756, 818, 1111, 1181, 1210, 1635, 2060, 2097, 2192, 2262, 2324, 2394, 2456, 3178);
      var len= BreakYears.length;
	    var i= 0;
	    while( i< len && year> BreakYears[ i])
	      i++;
	    return(((( year- BreakYears[ i- 1]+ 1)% 33)- 1)% 4== 0);
    }
  }
}
