/*-------------------------------------------
    New Common JS File.
    Dependency: jQuery 1.2.6
    Gorana A / May 2010
-------------------------------------------*/

var $j = jQuery.noConflict();

function initDatePickers() {
    $j('.datepicker').each(function(){

        $j(this).blur(function() {
                /* Parse the date. (Using DateJS, http://www.datejs.com/) */
                var d = this.value;
                var id = this.id;

                if(d=="") {
                  if(lang=='en')
                      {
                         this.value = "YYYY-MM-DD";
                      }
                      else
                      {
                         this.value = "ÅÅÅÅ-MM-DD";
                      }               
                }

                if(d.length>0)

                {

                    if(d.length==6 && d>100) {
                        d=d.substring(0,2) + '-' + d.substring(2,4) + '-' + d.substring(4);
                        if(d.substr(0,1)=='9' || d.substr(0,1)=='8' || d.substr(0,1)=='7')
                            $j(this).val('19' + d);
                        else
                            $j(this).val('20' + d);
                    }
                    else if(d.length==8 && d>20000) {
                        d=d.substring(0,4) + '-' + d.substring(4,6) + '-' + d.substring(6);
                        $j(this).val(d);
                    }
                    else {
                        try {
                            $j(this).val(parseDate(d,'yyyy-MM-dd'));
                        }
                        catch(err) {
                            //Do nothing, leave it to iserver.
                        }
                    }
                    //setExcelLink();  No custom intervals for excel
                    checkDate(id);

                }
            });

          $j(this).focus(function() {
            var d = this.value;
            //this.style.backgroundColor = "#ffffff";
                if(d=="YYYY-MM-DD" | d=="ÅÅÅÅ-MM-DD") {
                  this.value="";
                }
            });

        });

}

$j(document).ready(function () {

    $j('.datepicker').each(function(){
        if($j(this).attr("value")==""){
          if(lang=='en')
              {
                 $j(this).attr("value", "YYYY-MM-DD");
              }
              else
              {
                 $j(this).attr("value", "ÅÅÅÅ-MM-DD");
              }
        }
    });

});

function checkDate(id) {
    var checkD = document.getElementById (id);

    if(!(checkD.value.match(/^[0-9]{4}\-(0[1-9]|1[012])\-(0[1-9]|[12][0-9]|3[01])/)))
        {
            if(lang=='en')
            {
              alert('Incorrect date format, change to YYYY-MM-DD.');
            }
            else
            {
              alert('Fel datumformat, ange på formen ÅÅÅÅ-MM-DD.');
            }
            //document.getElementById(id).style.backgroundColor = "#fbc9c9"; instead of focus()
            return false;
        }
    return true;

}

function checkAllDates() {
	var errors = false;
	$j('.datepicker').each(function(){
		if(!checkDate($j(this).attr("id")))
		{
			errors = true;
		}
	});

	if (errors)
	{
		return false;
	}

}




