﻿
/*----------------------------------*/
/*      	Common.asp	    */
/*----------------------------------*/
function ConfirmDelete(msg)
{
    if(msg==null)
        msg=DeleteRecordMsg;
    return confirm(msg);
}
function isEmpty(s) {   
	return ((s == null) || (s.length == 0))
}

function isWhitespace(s) {   
	var i;    var whitespace = " \t\n\r";    if (isEmpty(s)) return true;    for (i = 0; i < s.length; i++) {var c = s.charAt(i);
        if (whitespace.indexOf(c) == -1) return false;
    }
    return true;    
}

function isNumeric(s){
    var i;
    var c;
    if (isWhitespace(s)) return false;
	   for (i=0; i<s.length; i++){
 		c = s.charAt(i);
		if (!((c >= "0") && (c <= "9")))
			return false;
		}
	return true;
}

function isBool(s){

s = s.toUpperCase();
if (s == "TRUE" || s == "FALSE" )
	return true;
else
	return false;
}

function isDecimal(s){
    var i,dotCounter;
    var c;
    dotCounter = 0;
    if (isWhitespace(s)) return false;
	   for (i=0; i<s.length; i++){
 		c = s.charAt(i);
		if (!((c >= "0") && (c <= "9")))
		   if ( c !=".")
			  return false;
		   else
		   	  ++dotCounter;
		}
		if (dotCounter > 1)
			return false;
		else if (dotCounter == s.length)
			return false;
	return true;
}

function isDateValid(s){
	var i,j,k;
    var c;
    j = 0;
    k = 0;
    if (isWhitespace(s)) return false;
	   for (i=0; i<s.length; i++){
 		c = s.charAt(i);
 		if (!((c >= "0") && (c <= "9")))
			if (c!="/")
				return false;
			else
				if (k == 0)
					if ((i < 1) || (i > 2))
						return false;
					else
					{	
						var No;
						k = i;
						No = GetValue(s,0,i);
						if (!((No > 0) && (No <= 12)))
							return false;
						else
							j +=1;	
					}	
				else
					if ((i-k-1)>2)
						return false;
					else
					{
						var No;
						No = GetValue(s,k+1,i);
						if (!((No > 0) && (No<=31)))
							return false;
						else
						{
							j +=1;
							if (j != 2)
								return false;
							k = i;
						}
					}
		}
		
		if (j != 2)
			return false;
			
		j = s.substring(k+1,s.length);
		if (j.length > 4 || j.length < 4 )
			return false;
	return true;
}

function GetValue(s,from,to)
{
	var Str,No;
	Str = s.substring(from,to);
	if (Str.length == 2)
	{
		No = (Str.charAt(0)-"0")*10;
		No +=(Str.charAt(1)-"0");
	}
	else
		No = (Str.charAt(0)-"0");
	
	return No;	
}

function Validate(fieldName,fieldValue,fieldMaxRange,fieldMinRange,fieldType,IsNullable,status)
{
    var error;
    if (status.search("disable") != -1)
    {
    	if (IsRecLoaded.search("refresh") != -1)
		{
			if (!isEmpty(fieldValue))
			{
				alert('Field '+fieldName+' is read only.');
				return false;
			}	
		}
    }
    if (IsNullable == false)
    {
        if (isEmpty(fieldValue))
           {
             error = "Field "+ fieldName +" can not be left empty";
             alert(error);
             return false;
           }
        else
            if (isWhitespace(fieldValue))
            {
                error = "Field "+ fieldName +" should have some valid value";
                alert(error);
                return false;
            }
    } // end if IsRequired
   if (fieldValue.length != 0)
   {
		if (fieldType.search("Int16") != -1 || fieldType.search("Int32") != -1 || fieldType.search("Int64") != -1 || fieldType.search("Byte") != -1 || fieldType.search("UNSIGNEDLONG") != -1)
		   if (!isNumeric(fieldValue))
		   {
		         error = "Only digits are allowed in "+ fieldName;
		         alert(error);
		         return false;
		   }
		   else if (!validateRange(fieldName,fieldValue,fieldMaxRange,fieldMinRange,fieldType))
				 return false;
		if (fieldType.search("Single") != -1 || fieldType.search("Double") != -1 || fieldType.search("Decimal") != -1)
		    if (!isDecimal(fieldValue))
		     {
		         error = "Only digits and decimal point is allowed in "+ fieldName;
		         alert(error);
		         return false;
		     }
		     else if (!validateRange(fieldName,fieldValue,fieldMaxRange,fieldMinRange,fieldType))
				 return false;
		if (fieldType.search("DateTime") != -1)
		    if (!isDateValid(fieldValue))
		     {
		         error = "Please give valid date in field "+ fieldName + " (mm/dd/yyyy)";
		         alert(error);
		         return false;
		     }
		     else if (!validateRange(fieldName,fieldValue,fieldMaxRange,fieldMinRange,fieldType))
				 return false;
		if (fieldType.search("Boolean") != -1)
		    if (!isBool(fieldValue))
		     {
		         error = "Please give valid boolean value in field "+ fieldName + ".Value can either be 'true' or 'false'.";
		         alert(error);
		         return false;
		     }	
   }
   return true;
}// end validate

function validateRange(Name,value,maxRange,minRange,type)
{
   
	if (maxRange == 0 && minRange == 0)
		return true;
	if (maxRange != 0)
	{
		if (type.search("Int16") != -1 || type.search("Int32") != -1 ||
			type.search("Int64") != -1 || type.search("Byte") != -1 ||
			type.search("Single") != -1 || type.search("Double") != -1 || 
			type.search("Decimal") != -1)
		{
			if (value > maxRange)
			{
				//error = "Value of " + Name + " must be less than " + maxRange;
			    //alert(error);
			    return false;
			}
		}
		else if (type.search("UNSIGNEDLONG") != -1)
		{
			if (value < maxRange)
			{
				//error = "Value of " + Name + " must be greater than 0";
			    //alert(error);
			    return false;
			}
		}
	}
	if (minRange != 0)
	{
		if (type.search("Int16") != -1 || type.search("Int32") != -1 ||
			type.search("Int64") != -1 || type.search("Byte") != -1 ||
			type.search("Single") != -1 || type.search("Double") != -1 ||
			type.search("Decimal") != -1)
		{
			if (value < minRange)
			{
				//error = "Value of " + Name + " must be greater than " + minRange;
			   // alert(error);
			    return false;
			}
		}
		else if (type.search("UNSIGNEDLONG") != -1)
		{
			if (value < minRange)
			{
				//error = "Value of " + Name + " must be greater than 0";
			  //  alert(error);
			    return false;
			}
		}
	}
	return true;
}

function TimeStampValidation(value)
{
	var stringArray,date,time;
	if (value.length < 14)
	   return false;
	stringArray = value.split(" ");
	if (stringArray.length > 2 || stringArray.length == 1)
	   return false;
        date = stringArray[0].split("-");
	time = stringArray[1].split(":");
   	if (date.length == 3)
      	{
	   var month,day;
           if (date[0].length != 4)
	      return false;
           else if (!checkNumeric(date[0]))
	      return false;
           if (date[1].length > 2)
              return false;
	   else if (!checkNumeric(date[1]))
	      return false;
	   else
	   {
		month = GetNo(date[1]);
                if (month == 0 || month > 12)
		    return false;
	   }
	   if (date[2].length > 2)
              return false;
	   else if (!checkNumeric(date[2]))
	      return false;
	   else
	   {
		day = GetNo(date[2]);
                if (day == 0 || day > 31)
		    return false;
	   }	
        }
       else
	   return false;
       if (time.length == 3)
       {
	  var thour,tsec,tmin;
	  if (time[0].length > 2)
	     return false;
          else if (!checkNumeric(time[0]))
	     return false;
          else
	   {
		thour = GetNo(time[0]);
                if (thour > 60)
		    return false;
	   }
	  if (time[1].length > 2)
            return false;
	  else if (!checkNumeric(time[1]))
	    return false;
	  else
	   {
		tmin = GetNo(time[1]);
                if (tmin > 60)
		    return false;
	   }  
          if (time[2].length > 2)
	  {
	     var index;
	     index = time[2].indexOf(".");
	     if (index == -1)
	        return false;
	     else
	     {
		var second,milisecond;
		second = time[2].substring(0,index);
		milisecond = time[2].substring(index+1,time[2].length);
		if (second.length > 2 )
		   return false;
		else if (!checkNumeric(second))
	   	   return false;
		else
	        {
		   tsec = GetNo(time[2]);
                   if (tsec > 60)
		      return false;
	        }
                if (milisecond.length == 0)
		   return false;
		else if (milisecond.length > 3)
		   return false;
		else if (!checkNumeric(milisecond))
	   	   return false;
		}
	  }
	  else if (!checkNumeric(time[2]))
	       return false;
          else
	   {
		tsec = GetNo(time[2]);
                if (tsec > 60)
		    return false;
	   }
       }
       else
	   return false;
       if (value.length != stringArray[0].length + stringArray[1].length + 1)
	  return false;
       if (value.search(date[0]) != 0)
	  return false;
       return true;
}

function validateTime(value)
{
     	var stringArray;
	var hour,min,sec;
        if (value.length < 5)
	   return false;
	stringArray = value.split(":");
	if (stringArray.length != 3)
	   return false;
	else
	{
	   if (stringArray[0].length > 2)
	      return false;
           else if (!checkNumeric(stringArray[0]))
	      return false;
           else
	   {
		hour = GetNo(stringArray[0]);
                if (hour > 60)
		    return false;
	   }
	   if (stringArray[1].length > 2)
              return false;
	   else if (!checkNumeric(stringArray[1]))
	      return false;
           else
	   {
		min = GetNo(stringArray[1]);
                if (min > 60)
		    return false;
	   }
	   if (stringArray[2].length > 2)
              return false;
           else if (!checkNumeric(stringArray[2]))
	      return false;
           else
	   {
		sec = GetNo(stringArray[2]);
                if (sec > 60)
		    return false;
	   }
	}
        return true;
}

function GetNo(s)
{
     var No;
     if (s.length == 2)
	{
		No = (s.charAt(0)-"0")*10;
		No +=(s.charAt(1)-"0");
	}
     else
		No = (s.charAt(0)-"0");
	return No;	
}

function checkNumeric(s){
    var i;    
    var c;
    if (isWhitespace(s)) return false;
    for (i=0; i<s.length; i++){
 	c = s.charAt(i);
        if (! ((c >= "0") && (c <= "9")))
           return false;
    }
 	return true;	 
}

/******************************************************************/
function IsString(s){
    var i;    
    var c;
    if (isWhitespace(s)) return false;
    s = s.toUpperCase();
    for (i=0; i<s.length; i++){
 	c = s.charAt(i);
        if (! (((c >= "A") && (c <= "Z")) || c==" "))
           return false;
    }
 	return true;	 
}
/******************************************************************/
//alborz moghadam
function isMail(s){
    var reg = new RegExp("\\w+([-+.\']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*");
   if(s.match(reg)==null)return false;
   return(s.match(reg)[0] == s);
}
//-----------------------------------------------------------------------

 function ValidateInput(source, arguments)
 {
  arguments.IsValid = true;
  
  
  switch(document.all[source.controltovalidate].ValidationType)
  {
    case "EMail":
     arguments.IsValid = isMail(arguments.Value);
     break;
    case "Integer":
     arguments.IsValid = isNumeric(arguments.Value);
     break;
    case "SimpleString":
     arguments.IsValid = true ;//|| IsString(arguments.Value);
     break;
    case "FloatDigit":
     arguments.IsValid = isDecimal(arguments.Value);
     break;
    case "Byte":
     arguments.IsValid = isNumeric(arguments.Value) && validateRange('',arguments.Value,255,0,'Byte');
     break;
    default :
     arguments.IsValid = true;
     break;
  }
  
  if(arguments.IsValid  && document.all[source.controltovalidate].Min !=null)
  {
     arguments.IsValid = validateRange('',arguments.Value,parseFloat(document.all[source.controltovalidate].Max),parseFloat(document.all[source.controltovalidate].Min),'Int64');
  }

}
//----------------------------------------------------------------
function addFromTo(source,dest)
{
   var op = new Option();
   op.innerText = source.options(source.selectedIndex).innerText;
   op.value = source.value;
   dest.appendChild(op);
   source.removeChild(source.options(source.selectedIndex));
}


/************************************************Loading*****************************************************/
function Loading(msg,isShow)
{

    var loading = document.getElementById('divLoading');
    if(loading == null )
    { 
        document.write("<div align=\"center\" id=\"divLoading\" class=\"loading\" style=\"position:absolute;width:200 ; height:30;left:10; top:" + 100 + document.body.scrollTop + "px;z-index:100\"></div>");
    }      

    loading = document.getElementById('divLoading');
    if(isShow)
    {
         loading.innerHTML=msg;
         loading.style.left = document.body.clientWidth/2+50
         loading.style.top = document.body.scrollTop + 300;
         loading.style.display="block";
         
    }
    else
    {
        loading.style.display="none";
    }
}
function GetProperty(obj,propertyName)

{
    var o=obj.attributes.getNamedItem(propertyName);

    if(o!=null)

        return o;
}
