// JavaScript Document
/*------------------------------------------------------------------------
Description - This Js file contains all the possbile validation for a
              Data entry form and some other function which are used to
			  handle the custom controls.
--------------------------------------------------------------------------*/

//Validation for Form

function f(val)
{
	document.location.href=val;
}
function submit1_onclick(objForm)
{
	//alert(objForm);
	
	var blExit=false;
	
	for(var intCounter=0;intCounter<objForm.length && blExit==false;intCounter++)
	{
//		alert(objForm[intCounter].name);
		strt=objForm[intCounter].name;
		//alert(strt.indexOf('Inside'));
		
		
	if(strt.indexOf('Header1')<0 && strt.indexOf('txtMessage')<0)
	{	strTemp=objForm[intCounter].alt.split("|");
//	alert(strTemp[1]);
		if(strTemp[0]=="REQ")
		{
			if(strTemp[1]=="TEXT")
			{
				Trim(objForm[intCounter])
				if(strTemp[2]=="")
				{
					if(objForm[intCounter].value=="")
					{
						alert(strTemp[3]);
						objForm[intCounter].focus();
						blExit=true;
					}
				}
				else if(strTemp[2]=="NUMERIC")
				{
					if(IsNumeric(objForm[intCounter])==false)
					{
						alert(strTemp[3]);
						objForm[intCounter].focus();
						blExit=true;
					}
				}
				else if(strTemp[2]=="STRING")
				{
					if(IsCharacter(objForm[intCounter])==false)
					{
						alert(strTemp[3]);						
						objForm[intCounter].focus();
						blExit=true;
					}
				}
				else if(strTemp[2]=="IP")
				{
					if(IsIP(objForm[intCounter])==false)
					{
						alert(strTemp[3]);
						objForm[intCounter].focus();
						blExit=true;
					}
				}
				else if(strTemp[2]=="EMAIL")
				{
					if(IsEmail(objForm[intCounter])==false)
					{
						objForm[intCounter].focus();
						blExit=true;
					}
				}
				else if(strTemp[2]=="PATH")
				{
					if(IsPath(objForm[intCounter])==false)
					{
						objForm[intCounter].focus();
						blExit=true;
					}
				}
				else if(strTemp[2]=="NOSPL")
				{
					if(IsSpl(objForm[intCounter])==false)
					{
						alert(strTemp[3]);
						objForm[intCounter].focus();
						blExit=true;
					}
				}
				else if(strTemp[2]=="NUMCOMMA")
				{
					if(IsNumComma(objForm[intCounter])==false)
					{
						alert(strTemp[3]);
						objForm[intCounter].focus();
						blExit=true;
					}
				}
				else if(strTemp[2]=="INT")
				{
					if(IsInt(objForm[intCounter])==false)
					{
						alert(strTemp[3]);
						objForm[intCounter].focus();
						blExit=true;
					}
				}
				else if(strTemp[2]=="IMAGE")
				{
					if(IsImage(objForm[intCounter])==false)
					{
						alert(strTemp[3]);
						objForm[intCounter].focus();
						blExit=true;
					}
				}
				else if(strTemp[2]=="PHONE")
				{
					if(IsPhone(objForm[intCounter])==false)
					{
						alert(strTemp[3]);
						objForm[intCounter].focus();
						blExit=true;
					}
				}
				else if(strTemp[2]=="ALPHANUMERIC")
				{
					if(IsAlphaNumeric(objForm[intCounter])==false)
					{
						alert(strTemp[3]);
						objForm[intCounter].focus();
						blExit=true;
					}
				}
				else if(strTemp[2]=="NAME")
				{
					if(IsName(objForm[intCounter])==false)
					{
						alert(strTemp[3]);
						objForm[intCounter].focus();
						blExit=true;
					}
				}
			}
			else if(strTemp[1]=="CHECK")
			{
				if(objForm[intCounter].checked==false)
				{
					alert(strTemp[3]);
					objForm[intCounter].focus();
					blExit=true;
				}
			}
			else if(strTemp[1]=="SELECT")
			{
				Trim(objForm[intCounter])
				if(strTemp[2]=="")
				{
					if(objForm[intCounter].value=="0")
					{
						alert(strTemp[3]);
						//objForm[intCounter].selectedIndex=0;
						objForm[intCounter].focus();
						blExit=true;
					}
				}
			}
			else if(strTemp[1]=="HIDDEN")
			{
				Trim(objForm[intCounter])
				if(strTemp[2]=="")
				{
					if(objForm[intCounter].value=="")
					{
						alert(strTemp[3]);
						objForm[intCounter].selectedIndex=0;
						blExit=true;
					}
				}
			}
		}
		else if(strTemp[0]=="NOTREQ")
		{
			Trim(objForm[intCounter])
			if(objForm[intCounter].value!="")
			{
				if(strTemp[1]=="TEXT")
				{
					if(strTemp[2]=="")
					{
						if(objForm[intCounter].value=="")
						{
							alert(strTemp[3]);
							objForm[intCounter].focus();
							blExit=true;
						}
					}
					else if(strTemp[2]=="NUMERIC")
					{
						if(IsNumeric(objForm[intCounter])==false)
						{
							alert(strTemp[3]);
							objForm[intCounter].focus();
							blExit=true;
						}
					}
					else if(strTemp[2]=="INT")
					{
						if(IsInt(objForm[intCounter])==false)
						{
							alert(strTemp[3]);
							objForm[intCounter].focus();
							blExit=true;
						}
					}
					else if(strTemp[2]=="STRING")
					{
						if(IsCharacter(objForm[intCounter])==false)
						{
							alert(strTemp[3]);
							objForm[intCounter].focus();
							blExit=true;
						}
					}
					else if(strTemp[2]=="IP")
					{
						if(IsIP(objForm[intCounter])==false)
						{
							alert(strTemp[3]);
							objForm[intCounter].focus();
							blExit=true;
						}
					}
					else if(strTemp[2]=="EMAIL")
					{
						if(IsEmail(objForm[intCounter])==false)
						{
							objForm[intCounter].focus();
							blExit=true;
						}
					}
					else if(strTemp[2]=="PATH")
					{
						if(IsPath(objForm[intCounter])==false)
						{
							objForm[intCounter].focus();
							blExit=true;
						}
					}
					else if(strTemp[2]=="NOSPL")
					{
						if(IsSpl(objForm[intCounter])==false)
						{
							alert(strTemp[3]);
							objForm[intCounter].focus();
							blExit=true;
						}
					}
					else if(strTemp[2]=="IMAGE")
					{
						if(IsImage(objForm[intCounter])==false)
						{
							//alert(strTemp[3]);
							objForm[intCounter].focus();
							blExit=true;
						}
					}
					else if(strTemp[2]=="PHONE")
					{
						if(IsPhone(objForm[intCounter])==false)
						{
							alert(strTemp[3]);
							objForm[intCounter].focus();
							blExit=true;
						}
					}
					else if(strTemp[2]=="NAME")
					{
						if(IsName(objForm[intCounter])==false)
						{
							alert(strTemp[3]);
							objForm[intCounter].focus();
							blExit=true;
						}
					}
					else if(strTemp[2]=="ALPHANUMERIC")
					{
						if(IsAlphaNumeric(objForm[intCounter])==false)
						{
							alert(strTemp[3]);
							objForm[intCounter].focus();
							blExit=true;
						}
					}
				}
			}
		}
	}
	}
	if(blExit==false)
	{
		return true;
	}
	else
	{
		return false;
	}
}

//This function works as Trim function of VB
function Trim(item)
{
  var tmp = "";
  var item_length = item.value.length;
  var item_length_minus_1 = item.value.length - 1;
  for (index = 0; index < item_length; index++)
  {
    if (item.value.charAt(index) != ' ')
    {
      tmp += item.value.charAt(index);
    }
    else
    {
      if (tmp.length > 0)
      {
        if (item.value.charAt(index+1) != ' ' && index != item_length_minus_1)
        {
          tmp += item.value.charAt(index);
        }
      }
    }
  }
  item.value = tmp;
}

//  check for valid numeric strings	
function IsNumeric(oObject)
{
   var strValidChars = "0123456789.";
   var strChar;
   var blnResult = true;
   var strString=oObject.value;	
   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   s=strString.split('.')   
   if(s.length>2)
   {
	blnResult=false;
	}
	if(strString==".")
	{
	blnResult=false;
	
	}
   return blnResult;
}

//  check for valid Name strings	
function IsName(oObject)
{
   var strValidChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";
   var strChar;
   var blnResult = true;
   var strString=oObject.value;	
   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   s=strString.split('.')   
   if(s.length>2)
   {
	blnResult=false;
	}
	if(strString==".")
	{
	blnResult=false;
	
	}
   return blnResult;
}

//  check for valid numeric strings	
function IsAlphaNumeric(oObject)
{
   var strValidChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcedfghijklmnopqrstuvwxyz0123456789 .-()";
   var strChar;
   var blnResult = true;
   var strString=oObject.value;	
   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   s=strString.split('.')   
   if(s.length>2)
   {
	blnResult=false;
	}
	if(strString==".")
	{
	blnResult=false;
	
	}
   return blnResult;
}

function IsImage(oObject)
{
	var ext=oObject.value;	
	if(ext.length>0)
	{	   
		ext = ext.substring(ext.length-3,ext.length);
		ext = ext.toLowerCase();
		if(ext != 'jpg'&& ext != 'gif' && ext != 'bmp' && ext != 'jpeg')
		{
		alert('You selected a .'+ext+' file; please select a image file instead!');
		return false;
		}
		else
		return true; 
	}
		return true;
}
//check specifically for Tax
function IsNumComma(oObject)
{
   var strValidChars = "0123456789,";
   var strChar;
   var blnResult = true;
   var strString=oObject.value;	
   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
   {
      strChar = strString.charAt(i);
      if(strValidChars.indexOf(strChar) == -1)
      {
         blnResult = false;
      }
   }
   return blnResult;
}

//check specifically for Integer Data
function IsInt(oObject)
{
   var strValidChars = "0123456789";
   var strChar;
   var blnResult = true;
   var strString=oObject.value;	
   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
   {
      strChar = strString.charAt(i);
      if(strValidChars.indexOf(strChar) == -1)
      {
         blnResult = false;
      }
   }
   return blnResult;
}
//check specifically for Phone Data
function IsPhone(oObject)
{
   var strValidChars = "0123456789()-+/. ";
   var strChar;
   var blnResult = true;
   var strString=oObject.value;	
   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
   {
      strChar = strString.charAt(i);
      if(strValidChars.indexOf(strChar) == -1)
      {
         blnResult = false;
      }
   }
   return blnResult;
}

//  check for valid strings	
function IsCharacter(oObject)
{
   var strValidChars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_. ,'";
   var strChar;
   var blnResult = true;
   var strString=oObject.value;	
   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
 //        alert("Please Enter Correct Value");
         blnResult = false;
         
         }
      }
   return blnResult;
}

//Validation for Proper Ip Address
function IsIP (oObject) {
var IPvalue=oObject.value;
errorString = "";
theName = "IPaddress";

var ipPattern = /^(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})$/;
var ipArray = IPvalue.match(ipPattern); 

if (IPvalue == "0.0.0.0")
errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
else if (IPvalue == "255.255.255.255")
errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
if (ipArray == null)
errorString = errorString + theName + ': '+IPvalue+' is not a valid IP address.';
else {
for (i = 0; i < 4; i++) {
thisSegment = ipArray[i];
if (thisSegment > 255) {
errorString = errorString + theName + ': '+IPvalue+' is not a valid IP address.';
i = 4;
}
if ((i == 0) && (thisSegment > 255)) {
errorString = errorString + theName + ': '+IPvalue+' is a special IP address and cannot be used here.';
i = 4;
      }
   }
}
extensionLength = 3;
if (errorString == "")
return true;
else
return false;
}

//Validation for proper Email address
function IsEmail(oObject) {
var emailStr=oObject.value;
var emailPat=/^(.+)@(.+)$/;
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]";
var validChars="\[^\\s" + specialChars + "\]";
var quotedUser="(\"[^\"]*\")";
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;
var atom=validChars + '+';
var word="(" + atom + "|" + quotedUser + ")";
var userPat=new RegExp("^" + word + "(\\." + word + ")*$");
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

var matchArray=emailStr.match(emailPat)
if (matchArray==null) {
	alert("Email address seems incorrect (check @ and .'s)");
	return false;
}
var user=matchArray[1];
var domain=matchArray[2];

if (user.match(userPat)==null) {
    alert("The EmailID doesn't seem to be valid. Please Check");
    return false;
}
var IPArray=domain.match(ipDomainPat);
if (IPArray!=null) {
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	        alert("Destination IP address is invalid!");
		return false;
	    }
    }
    return true;
}

var domainArray=domain.match(domainPat);
if (domainArray==null) {
	alert("The domain name doesn't seem to be valid.");
    return false;
}
var atomPat=new RegExp(atom,"g");
var domArr=domain.match(atomPat);
var len=domArr.length;
if (domArr[domArr.length-1].length<2 || 
    domArr[domArr.length-1].length>4) {
   alert("The Email address must be in proper format");//end in a three-letter domain, or two letter country.
   return false;
}

if (len<2) {
   var errStr="This Email address is missing a hostname!";
   alert(errStr);
   return false;
}
return true;
}

//Validation for proper directory path
function IsPath(oObject)
{
	strPath=oObject.value;
	var strValidChars = "abcdefghigklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
	if(strPath=="")
	{
		alert("Enter proper path");
	}
	var strChar=strPath.charAt(0);
	if (strValidChars.indexOf(strChar) == -1)
	{
		alert("Drive letter is missing")
		return false;
	}
	strChar=strPath.charAt(1);
	if(strChar!=":")
	{
		alert(": missing after drive letter");
		return false;
	}
	strChar=strPath.charAt(2);
	if(strChar!="\\")
	{
		alert("\\ slash is missing after drive letter")
		return false;
	}
	strChar=strPath.charAt(strPath.length-1);
	if(strChar=="\\")
	{
		strChar="";
		for(var intI=0;intI<(strPath.length-1);intI++)
		{
			strChar=strChar + strPath.charAt(intI);
		}
		strPath=strChar;
		oObject.value=strPath;
	}
	return true;
}
function ValidatePhone(oObject)
    {
var p1;	//conversion phone number
var p2; //conversion phone text
var mask="()-"; //this is what phone numbers look like
var oldvalue=""; //the last thing in phonetxt

    	p1=oObject.value
    	if (p1.length > oldvalue.length)
        	{
        		p1=p1.replace("(","");
        		p1=p1.replace(")","");
        		p1=p1.replace("-","");
        		while (p1.search(" ") != -1)
            		{
            			p1=p1.replace(" ","");
            		}            		
            		oObject.value = p1;
            		if (p1.length < 4)
                		{
                			p2=mask.substring(p1.length + 1,13);
                			oObject.innerText="(" + p1 + p2;
                    		} else {
                    			if (p1.length < 7)
                        			{
                        				p2=mask.substring(p1.length + 2,13);
                        				oObject.innerText="(" + p1.substring(0,3) + ")" + p1.substring(3,6) + p2;
                            			} else {
                            				p2=mask.substring(p1.length + 3,13)
                            				oObject.innerText="(" + p1.substring(0,3) + ")" + p1.substring(3,6) + "-" + p1.substring(6,10) + p2;
                            			}
                            		}
                            	}
                            	oldvalue=oObject.value;
		}

//  check for valid strings	
function IsSpl(oObject)
{
   var strValidChars = "1234567890abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-_. ,";
   var strChar;
   var blnResult = true;
   var strString=oObject.value;	
   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
}

//This function is used to delete the records
function delete_onclick(oObject)
{
	if(confirm("Are your sure that you want to delete this record ?"))
	{
		oObject.value="delete";
		return true;
	}
	else
	{
		return false;
	}
}

//This function is used for adding the values in selection List control
function AddValue(oObject,oObject1)
{
	if(oObject.selectedIndex!=-1)
	{
		oObject1.options[oObject1.length]=new Option(oObject.options[oObject.selectedIndex].text,oObject.options[oObject.selectedIndex].value)
		oObject.options[oObject.selectedIndex]=null;
	}
}
//This function is used for deleting the values in selection List control
function DeleteValue(oObject,oObject1)
{
	if(oObject1.selectedIndex!=-1)
	{
		oObject.options[oObject.length]=new Option(oObject1.options[oObject1.selectedIndex].text,oObject1.options[oObject1.selectedIndex].value)
		oObject1.options[oObject1.selectedIndex]=null;
	}
}
//This function is used for adding all the values in selection List control
function AddAll(oObject,oObject1)
{
	var intPos=oObject.options.length-1;
	for(var intCounter=oObject.options.length-1;intCounter>=0;intCounter--)
	{
		intPos=intPos-intCounter;
		oObject1.options[oObject1.length]=new Option(oObject.options[intPos].text,oObject.options[intPos].value)
		oObject.options[intPos]=null;
		intPos=oObject.options.length-1;
	}
}
//This function is used for deleting all the values in selection List control
function DeleteAll(oObject,oObject1)
{
	var intPos=oObject1.options.length-1;
	for(var intCounter=oObject1.options.length-1;intCounter>=0;intCounter--)
	{
		intPos=intPos-intCounter;
		oObject.options[oObject.length]=new Option(oObject1.options[intPos].text,oObject1.options[intPos].value)
		oObject1.options[intPos]=null;
		intPos=oObject1.options.length-1;
	}
}

//This function is used for changing the postion of values in selection List control
function LevelUp(oObject)
{
	if(oObject.selectedIndex>0)
	{
		var strValue=oObject.options[oObject.selectedIndex].value;
		var strText=oObject.options[oObject.selectedIndex].text;
		oObject.options[oObject.selectedIndex].value=oObject.options[oObject.selectedIndex-1].value;
		oObject.options[oObject.selectedIndex].text=oObject.options[oObject.selectedIndex-1].text;
		oObject.options[oObject.selectedIndex-1].value=strValue;
		oObject.options[oObject.selectedIndex-1].text=strText;
		oObject.selectedIndex=oObject.selectedIndex-1;
	}	
}

//This function is used for changing the postion of values in selection List control
function LevelDown(oObject)
{
	if(oObject.selectedIndex<oObject.options.length-1 && oObject.selectedIndex!=-1)
	{
		var strValue=oObject.options[oObject.selectedIndex].value;
		var strText=oObject.options[oObject.selectedIndex].text;
		oObject.options[oObject.selectedIndex].value=oObject.options[oObject.selectedIndex+1].value;
		oObject.options[oObject.selectedIndex].text=oObject.options[oObject.selectedIndex+1].text;
		oObject.options[oObject.selectedIndex+1].value=strValue;
		oObject.options[oObject.selectedIndex+1].text=strText;
		oObject.selectedIndex=oObject.selectedIndex+1;
	}	
}

//This function select all the values in SelectionList control
function SelectAllSelection(objList)
{
	objList.multiple=true;
	if (objList.options.length!=-1)
	{
    	if(objList.options.length>1)
    	{
			for(var intCounter=0;intCounter<objList.options.length;intCounter++)
       		{
				objList.options[intCounter].selected=true;
       		}
    	}
	}
  	else
  	{
      objList.options[0].selected=true;
   	}
}

//This function checks for the limit of Characters entered by the user
function LimitChar(objField,intLimit)
{
	if(objField.value.length>intLimit)
	{
		alert("You cannot enter more then " + intLimit + " characters in this field !");
		objField.value=objField.value.substring(0,intLimit);
		objField.focus();
	}
}

//this function selects all the checkboxes in grid
function SelectAllCheckbox(oParent,oChild)
{
	if(oParent.checked==true)
	{
		if(oChild!=null)
		{
			if(oChild.length!=null)
			{
				for(var intCounter=0;intCounter<oChild.length;intCounter++)
				{
					oChild[intCounter].checked=true;
				}
			}
			else
			{
				oChild.checked=true;
			}
		}
	}
	else if(oParent.checked==false)
	{
		if(oChild!=null)
		{
			if(oChild.length!=null)
			{
				for(var intCounter=0;intCounter<oChild.length;intCounter++)
				{
					oChild[intCounter].checked=false;
				}
			}
			else
			{
				oChild.checked=false;
			}
		}
	}
}

//This file will check the extension for upload component

// This function checks the maxlength for textarea / multiline asp:textbox
function funcCalcCharLeft1(obj1,val)
{
	maxLength = val;
	if(obj1.value.length > maxLength)
	{
		obj1.value = obj1.value.substring(0,maxLength);
		alert("You can enter upto " + maxLength + " characters only")
	}
}

// this function will open a window with a text area with the content of the textarea of parent form
// also saves its contents to the textbox of the parent form
function winnewopen(fname,obj,obj1,txt,title,maxLength)
{
	var ht=screen.height-324;
	var wt=screen.width-250;
	var strQs="";
	var strTxt="";
	strTxt=txt;
	strQs=fname;
	window.open(strQs+"?obj="+obj+"&frm="+obj1+"&title="+title+"&len="+maxLength,"","left=150,top=162,height="+ht+",width="+wt+",screenX=162,screenY=125");
}
