﻿
/*------------------------------------------------------------------------------------------*/  
//
//
//           Content Managment System (CMS) JavaScript Library. 2006 - 2007
//
//
/*------------------------------------------------------------------------------------------*/  

function MM_findObj(n, d)
 { //v3.0
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document); return x;
}

/*------------------------------------------------------------------------------------------*/  
// this function checks if a field is empty or not
/*------------------------------------------------------------------------------------------*/  
function isEmptyTextField (FieldName) {

  var objField = MM_findObj(FieldName);
  if ( objField.value == "" ){
      return true;
  }else {
    return false;
  }  
}

/*------------------------------------------------------------------------------------------*/  
// this function checks if a field contains only Spaces or not
/*------------------------------------------------------------------------------------------*/  
function isSpace(FieldName){ 
		var objField = MM_findObj(FieldName);
        var i;
        for (i=0;i<=objField.value.length-1;i++){ // this counter begins from the first charactar of a field
        // to the last character
        if (objField.value.charAt(i)!=" "){ // if charactar position (i) is not a back space then it means that
        // this field have at least one character and it's not empty
        return false; // so it returns false in another meanning it's not empty
         }
         }
         return true; // if the upper loop ends without  find any character then it means that this field is empty or
         // have just back spaces so it return true
      }

/*------------------------------------------------------------------------------------------*/  
// this function checks if the email is valid or not
/*------------------------------------------------------------------------------------------*/  
      function IsValidEmailAddress(src) {
   			   var email = MM_findObj(src);
               var result = true
               var theStr = new String(email.value)
               var index = theStr.indexOf("@");
              if (index > 0)
              {
                  var pindex = theStr.indexOf(".",index);
                  if ((pindex > index+1) && (theStr.length > pindex+1))
                   	result = false;
              } 
            
			return result;                          
     }
         
/*------------------------------------------------------------------------------------------*/  
// this function checks if the feild is not a number 
/*------------------------------------------------------------------------------------------*/  
function isNaNumber(FieldName) 
{
	var objField = MM_findObj(FieldName);
	
	if ( isNaN( objField.value ) ) {   
		return true;
	}else {
		return false;
	}
	
}

/*------------------------------------------------------------------------------------------*/  
// this function checks if the feild is a valid date
/*------------------------------------------------------------------------------------------*/  			
function isDate(DateToCheck,ctl)
{

    var x1 = new String(DateToCheck);
    if (x1.length > 0 && x1.length < 10){
      return false; 
    }

    if (x1.charAt(2) != '/'){
      return false; 
    }

    if(DateToCheck==""){return true;}
    var m_strDate; // = FormatDate(DateToCheck);
    m_strDate = DateToCheck

    if(m_strDate==""){
    return false;
    }
    var m_arrDate = m_strDate.split("/");
    var m_DAY = m_arrDate[0];
    var m_MONTH = m_arrDate[1];
    var m_YEAR = m_arrDate[2];

    if (m_DAY < 1 || m_DAY > 31) {return false;}

    if (m_MONTH < 1 || m_MONTH > 12) {return false;}

    if (m_YEAR < 1900 || m_YEAR > 5000) {return false;}

    if(m_YEAR.length > 4 ){return false;}

    m_strDate = m_MONTH + "/" + m_DAY + "/" + m_YEAR;

    var testDate=new Date(m_strDate);
    if(testDate.getMonth()+1==m_MONTH){
    m_strDate = m_DAY + "/" + m_MONTH  + "/" + m_YEAR;
    } 
    else{
    return false;
    }
}

/*------------------------------------------------------------------------------------------*/  
// this function formats the date
/*------------------------------------------------------------------------------------------*/  

function FormatDate(DateToFormat,FormatAs)
{
        if(DateToFormat==""){return"";}
        if(!FormatAs){FormatAs="dd/mm/yyyy";}

        var strReturnDate;
        FormatAs = FormatAs.toLowerCase();
        DateToFormat = DateToFormat.toLowerCase();
        var arrDate
        var arrMonths = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
        var strMONTH;
        var Separator;

        while(DateToFormat.indexOf("st")>-1){
        DateToFormat = DateToFormat.replace("st","");
        }

        while(DateToFormat.indexOf("nd")>-1){
        DateToFormat = DateToFormat.replace("nd","");
        }

        while(DateToFormat.indexOf("rd")>-1){
        DateToFormat = DateToFormat.replace("rd","");
        }

        while(DateToFormat.indexOf("th")>-1){
        DateToFormat = DateToFormat.replace("th","");
        }

        if(DateToFormat.indexOf(".")>-1){
        Separator = ".";
        }

        if(DateToFormat.indexOf("-")>-1){
        Separator = "-";
        }


        if(DateToFormat.indexOf("/")>-1){
        Separator = "/";
        }

        if(DateToFormat.indexOf(" ")>-1){
        Separator = " ";
        }

        arrDate = DateToFormat.split(Separator);
        DateToFormat = "";
	        for(var iSD = 0;iSD < arrDate.length;iSD++){
		        if(arrDate[iSD]!=""){
		        DateToFormat += arrDate[iSD] + Separator;
		        }
	        }
        DateToFormat = DateToFormat.substring(0,DateToFormat.length-1);
        arrDate = DateToFormat.split(Separator);

        if(arrDate.length < 3){
        return "";
        }

        var DAY = arrDate[0];
        var MONTH = arrDate[1];
        var YEAR = arrDate[2];




        if(parseFloat(arrDate[1]) > 12){
        DAY = arrDate[1];
        MONTH = arrDate[0];
        }

        if(parseFloat(DAY) && DAY.toString().length==4){
        YEAR = arrDate[0];
        DAY = arrDate[2];
        MONTH = arrDate[1];
        }


        for(var iSD = 0;iSD < arrMonths.length;iSD++){
        var ShortMonth = arrMonths[iSD].substring(0,3).toLowerCase();
        var MonthPosition = DateToFormat.indexOf(ShortMonth);
	        if(MonthPosition > -1){
	        MONTH = iSD + 1;
		        if(MonthPosition == 0){
		        DAY = arrDate[1];
		        YEAR = arrDate[2];
		        }
	        break;
	        }
        }

        var strTemp = YEAR.toString();
        if(strTemp.length==2){

	        if(parseFloat(YEAR)>40){
	        YEAR = "19" + YEAR;
	        }
	        else{
	        YEAR = "20" + YEAR;
	        }
        }

	        if(parseInt(MONTH)< 10 && MONTH.toString().length < 2){
	        MONTH = "0" + MONTH;
	        }
	        if(parseInt(DAY)< 10 && DAY.toString().length < 2){
	        DAY = "0" + DAY;
	        }
	        switch (FormatAs){
	        case "dd/mm/yyyy":
	        return DAY + "/" + MONTH + "/" + YEAR;
	        case "mm/dd/yyyy":
	        return MONTH + "/" + DAY + "/" + YEAR;
	        case "dd/mmm/yyyy":
	        return DAY + " " + arrMonths[MONTH -1].substring(0,3) + " " + YEAR;
	        case "mmm/dd/yyyy":
	        return arrMonths[MONTH -1].substring(0,3) + " " + DAY + " " + YEAR;
	        case "dd/mmmm/yyyy":
	        return DAY + " " + arrMonths[MONTH -1] + " " + YEAR;	
	        case "mmmm/dd/yyyy":
	        return arrMonths[MONTH -1] + " " + DAY + " " + YEAR;
	        }

        return DAY + "/" + strMONTH + "/" + YEAR;

} 

/*------------------------------------------------------------------------------------------*/  
// this function checks if the feild is not a date
/*------------------------------------------------------------------------------------------*/  
function isNaDate(FieldName) 
{
	var objField = MM_findObj(FieldName);
	
	if ( isNaN( objField.value ) ) {   
		return true;
	}else {
		
		if ( objField.value >= 1800 && objField.value <= 2100 ) {
			return false;
		}
		else {
			return true;
		}	
	}	
}


/*------------------------------------------------------------------------------------------*/  
// this function 
/*------------------------------------------------------------------------------------------*/  
	
