var objXmlHttp
var elemId
function ajaxEngine(Id,url,method,params)
{ 
	elemId=Id
	document.getElementById(elemId).innerHTML='<b>LOADING PLEASE WAIT....</b>'
	try
    {
	    objXmlHttp=new XMLHttpRequest();
    }
	catch (e)
    {
	    try
    	{
	      objXmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    	}
	    catch (e)
    	{
	    	try
        	{
    	    	objXmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        	}
	    	catch (e)
        	{
        		alert("Your browser does not support AJAX!");
	        	return false;
        	}
      	}
    }
	
    objXmlHttp.onreadystatechange=ajaxStateManager	
	if(method=="POST")
	{
	    objXmlHttp.open("POST",url,true);
		objXmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		objXmlHttp.setRequestHeader("Content-length", params.length);
		objXmlHttp.setRequestHeader("Connection", "close");
	    objXmlHttp.send(params);
	}
	else
	{
/*	    objXmlHttp.open("GET",url,true);
		objXmlHttp.setRequestHeader("Cache-Control", "no-cache");
	    objXmlHttp.send(null); */
	    
	    var arrUrl;
	    
	    arrUrl = url.split("?");
	    params = arrUrl[1];
	    objXmlHttp.open("POST",url,true);
		objXmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
		objXmlHttp.setRequestHeader("Content-length", params.length);
		objXmlHttp.setRequestHeader("Connection", "close");
	    objXmlHttp.send(params);
	}
} 

function ajaxStateManager() { 	
	if (objXmlHttp.readyState==4 || objXmlHttp.readyState=="complete")
	{ 	
		if(objXmlHttp.responseText.search("Fatal error")>0)
		{
			document.getElementById(elemId).innerHTML="<b>Data is out of range.</b>"
		}
		else
		{
			document.getElementById(elemId).innerHTML='';
			document.getElementById(elemId).innerHTML=objXmlHttp.responseText
		}	
	}	
} 

function AjaxValidation(strValidateStr,objValue,strError) 
{ 
   	var epos = strValidateStr.search("="); 
    var  command  = ""; 
   	var  cmdvalue = ""; 
    if(epos >= 0) 
   	{ 
	    command  = strValidateStr.substring(0,epos); 
   		cmdvalue = strValidateStr.substr(epos+1); 
    } 
   	else 
    { 
		command = strValidateStr; 
	} 
	
	    switch(command) 
    	{ 
        case "req": 
        case "required": 
         { 
           
           if(objValue.value<=0)
           {
            	alert(strError); 
				objValue.focus();
    	        return false; 
           }           
           if(eval(objValue.value.length) == 0) 
           { 
              if(!strError || strError.length ==0) 
              { 
                strError = objValue.name + " : Required Field"; 
              }//if 
              alert(strError); 
			  objValue.focus();
              return false; 
           }//if 
           break;             
         }//case required 
        case "maxlength": 
        case "maxlen": 
          { 
             if(eval(objValue.value.length) >  eval(cmdvalue)) 
             { 
               if(!strError || strError.length ==0) 
               { 
                 strError = objValue.name + " : "+cmdvalue+" characters maximum "; 
               }//if 
               alert(strError + "\n[Current length = " + objValue.value.length + " ]"); 
			   objValue.focus();
               return false; 
             }//if 
             break; 
          }//case maxlen 
        case "minlength": 
        case "minlen": 
           { 
             if(eval(objValue.value.length) <  eval(cmdvalue)) 
             { 
               if(!strError || strError.length ==0) 
               { 
                 strError = objValue.name + " : " + cmdvalue + " characters minimum  "; 
               }//if               
               alert(strError + "\n[Current length = " + objValue.value.length + " ]"); 
			   objValue.focus();
               return false;                 
             }//if 
             break; 
            }//case minlen 
        case "alnum": 
        case "alphanumeric": 
           { 
              var charpos = objValue.value.search("[^A-Za-z0-9]"); 
              if(objValue.value.length > 0 &&  charpos >= 0) 
              { 
               if(!strError || strError.length ==0) 
                { 
                  strError = objValue.name+": Only alpha-numeric characters allowed "; 
                }//if 
                alert(strError + "\n [Error character position " + eval(charpos+1)+"]"); 
			    objValue.focus();
                return false; 
              }//if 
              break; 
           }//case alphanumeric 
        case "num": 
        case "numeric": 
           { 
              var charpos = objValue.value.search("[^0-9\,.]"); 
              if(objValue.value.length > 0 &&  charpos >= 0) 
              { 
                if(!strError || strError.length ==0) 
                { 
                  strError = objValue.name+": Only digits allowed "; 
                }//if               
                alert(strError + "\n [Error character position " + eval(charpos+1)+"]"); 
			    objValue.focus();
                return false; 
              }//if 
              break;               
           }//numeric 
        case "alphabetic": 
        case "alpha": 
           { 
              var charpos = objValue.value.search("[^A-Za-z ]"); 
              if(objValue.value.length > 0 &&  charpos >= 0) 
              { 
                  if(!strError || strError.length ==0) 
                { 
                  strError = objValue.name+": Only alphabetic characters allowed "; 
                }//if                             
                alert(strError + "\n [Error character position " + eval(charpos+1)+"]"); 
			    objValue.focus();
                return false; 
              }//if 
              break; 
           }//alpha 
		case "alnumhyphen":
			{
              var charpos = objValue.value.search("[^A-Za-z0-9\-_ ]"); 
              if(objValue.value.length > 0 &&  charpos >= 0) 
              { 
                  if(!strError || strError.length ==0) 
                { 
                  strError = objValue.name+": characters allowed are A-Z,a-z,0-9,- and _"; 
                }//if                             
                alert(strError + "\n [Error character position " + eval(charpos+1)+"]"); 
			    objValue.focus();
                return false; 
              }//if 			
			break;
			}
        case "email": 
          { 
               if(!validateEmailv2(objValue.value)) 
               { 
                 if(!strError || strError.length ==0) 
                 { 
                    strError = objValue.name+": Enter a valid Email address "; 
                 }//if                                               
                 alert(strError); 
			     objValue.focus();
                 return false; 
               }//if 
           break; 
          }//case email 
        case "lt": 
        case "lessthan": 
         {          
            if(isNaN(objValue.value)) 
            { 
              alert(objValue.name+": Should be a number "); 
			  objValue.focus();
              return false; 
            }//if 
            if(eval(Math.abs(objValue.value)) >=  eval(cmdvalue)) 
            { 
              if(!strError || strError.length ==0) 
              { 
                strError = objValue.name + " : value should be less than "+ cmdvalue; 
              }//if               
              alert(strError); 
			  objValue.focus();
              return false;                 
             }//if             
            break; 
         }//case lessthan 
        case "gt": 
        case "greaterthan": 
         { 
            if(isNaN(objValue.value)) 
            { 
              alert(objValue.name+": Should be a number "); 
		      objValue.focus();
              return false; 
            }//if 
             if(eval(Math.abs(objValue.value)) <=  eval(cmdvalue)) 
             { 
               if(!strError || strError.length ==0) 
               { 
                 strError = objValue.name + " : value should be greater than "+ cmdvalue; 
               }//if               
               alert(strError); 
			   objValue.focus();
               return false;                 
             }//if             
            break; 
         }//case greaterthan 
        case "regexp": 
         { 
		 	if(objValue.value.length > 0)
			{
	            if(!objValue.value.match(cmdvalue)) 
	            { 
	              if(!strError || strError.length ==0) 
	              { 
	                strError = objValue.name+": Invalid characters found "; 
	              }//if                                                               
	              alert(strError); 
   			      objValue.focus();
	              return false;                   
	            }//if 
			}
           break; 
         }//case regexp 
        case "dontselect": 
         { 
            if(objValue.selectedIndex == null) 
            { 
              alert("BUG: dontselect command for non-select Item"); 
			  objValue.focus();
              return false; 
            } 
            if(objValue.selectedIndex == eval(cmdvalue)) 
            { 
             if(!strError || strError.length ==0) 
              { 
              strError = objValue.name+": Please Select one option "; 
              }                                                               
              alert(strError); 
 		      objValue.focus();
              return false;                                   
             } 
             break; 
         }//case dontselect 
    }//switch 
    return true; 
}


