﻿// JScript File
var receiveReq = getXmlHttpRequestObject();
var receiveCountReq = getXmlHttpRequestObject();
var receivevideoReq = getXmlHttpRequestObject();
var lastMessage = 0;
//Gets the browser specific XmlHttpRequest Object
function getXmlHttpRequestObject() {
	if (window.XMLHttpRequest) {
		return new XMLHttpRequest();
	} else if(window.ActiveXObject) {
		return new ActiveXObject("Microsoft.XMLHTTP");
	} else {
		alert('Status: Cound not create XmlHttpRequest Object.  Consider upgrading your browser.');
	}
}
function swapdivs(showdiv,hidediv)
{
  document.getElementById(hidediv).style.display="none";
  document.getElementById(showdiv).style.display="block";
}   


function formvalidation(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(eval(objValue.value.length) == 0 || objValue.value.charAt(0) == " ") 
           { 
           
              if(!strError || strError.length ==0) 
              { 
                strError = "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 = 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 = "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 = "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 = "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 = "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 "html":
			{
              if(objValue.value.match(/([\<])([^\>]{1,})*([\>])/i)!=null) 
              { 
                  if(!strError || strError.length ==0) 
                { 
                  strError = "HTML tags are not allowed"; 
                }                             
                 alert(strError); 
                 objValue.focus();
                return false; 
              }//if 			
			break;
			}	
		case "phone":
			{
              var charpos = objValue.value.search("[^0-9()-]"); 
              if(objValue.value.length > 0 &&  charpos >= 0) 
              { 
                  if(!strError || strError.length ==0) 
                { 
                  strError = "Please Enter a Valid Phone Number"; 
                }//if                             
                alert(strError + "\n [Error character position " + eval(charpos+1)+"]"); 
                 objValue.focus();
                return false; 
              }//if
              else
              {
                if(objValue.value.length > 0 && objValue.value.length < 10)
                {
                 strError = "phone number should be minimum 10 digits"; 
                 alert(strError);
                 objValue.focus();
                  return false; 
                } 			
              }  
			break;
			}	
        case "email": 
          { 
               if(!ValidMail(objValue.value)) 
               { 
                 if(!strError || strError.length ==0) 
                 { 
                    strError = "Enter a valid Email address "; 
                 }//if                                               
                 alert(strError); 
                 objValue.focus();
                 return false; 
               }//if 
           break; 
          }//case email 
         case "dontselect": 
         { 
            if(objValue.selectedIndex == null) 
            { 
              alert("BUG: dontselect command for non-select Item"); 
              return false; 
            } 
            if(objValue.selectedIndex == eval(cmdvalue)) 
            { 
             if(!strError || strError.length ==0) 
              { 
              strError = objValue.name+": Please Select one option "; 
              }//if                                                               
               alert(strError); 
               objValue.focus();
              return false;                                   
             } 
             break; 
         }//case dontselect   
    }//switch 
    return true; 
}

function ValidMail(email)
{
  var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
  if (filter.test(email)) return true;
  else return false;
}
function Password(objValue1,objValue2)
{
var invalid = " "; // Invalid character is a space
var minLength = 6; // Minimum length
var pw1 = objValue1.value;
var pw2 = objValue2.value;
// check for a value in both fields.
    if (pw1 == '' || pw2 == '') 
    {
    alert('Please enter your password twice.');
    if (pw1 == ''){
    objValue1.select();
    objValue1.focus();
     return false;
    }
     if (pw2 == ''){
    objValue2.select();
    objValue2.focus();
      return false;
    }
   
    }
// check for minimum length
    if (objValue1.value.length < minLength) 
    {
    alert('Your password must be at least ' + minLength + ' characters long. Try again.');
    objValue1.select();
    objValue1.focus();
    return false;
    }
// check for spaces
    if (objValue1.value.indexOf(invalid) > -1)
    {
    alert("Sorry, spaces are not allowed.");
    objValue1.select();
    objValue1.focus();
    return false;
    }
    else
    {
        if (pw1 != pw2) 
        {
        alert ("You did not enter the same new password twice. Please re-enter your password.");
        objValue2.select();
        objValue2.focus();
        return false;
        }   
    }
  return true;  
}

//DateValidation

function validateDate(fld) {
    var RegExPattern = /^(?=\d)(?:(?:(?:(?:(?:0?[13578]|1[02])(\/|-|\.)31)\1|(?:(?:0?[1,3-9]|1[0-2])(\/|-|\.)(?:29|30)\2))(?:(?:1[6-9]|[2-9]\d)?\d{2})|(?:0?2(\/|-|\.)29\3(?:(?:(?:1[6-9]|[2-9]\d)?(?:0[48]|[2468][048]|[13579][26])|(?:(?:16|[2468][048]|[3579][26])00))))|(?:(?:0?[1-9])|(?:1[0-2]))(\/|-|\.)(?:0?[1-9]|1\d|2[0-8])\4(?:(?:1[6-9]|[2-9]\d)?\d{2}))($|\ (?=\d)))?(((0?[1-9]|1[012])(:[0-5]\d){0,2}(\ [AP]M))|([01]\d|2[0-3])(:[0-5]\d){1,2})?$/;
    var errorMessage = 'Please enter valid date as month, day, and four digit year.\nselect the date from date picker.';
    if ((fld.value.match(RegExPattern)) && (fld.value!='')) {
        return true;
    } else {
        alert(errorMessage);
        fld.focus();
        return false;
    } 
}
