//Scripts for the complete site.

// Functions for trailing spaces.

function trim(strText) 
{ 
		// this will get rid of leading spaces
		while (strText.substring(0,1) == ' ')
			strText = strText.substring(1, strText.length);

		// this will get rid of trailing spaces
		while (strText.substring(strText.length-1,strText.length) == ' ')
			strText = strText.substring(0, strText.length-1);
		var pos=0
		var tevePos=0
		while(strText.indexOf("\n",pos)>-1)
		{
			tevePos=strText.indexOf("\n",pos)
			pos=tevePos+1
		}
	   return strText;
}
							
//	MULTIPLE DROP DOWN HAS BEEN SELECTED OR NOT .

function multipleDropDown(Category)
{
	for(var i=0; i<document.frm.elements.length; i++)
	{
		if(document.frm.elements[i].name == Category)
		{
			for(var j=0; j<document.frm.elements[i].options.length; j++)
			{
				//alert(document.frm.elements[i].options[j].value);
				//alert(document.frm.elements[i].options[j].selected);
				if(document.frm.elements[i].options[j].selected)
				{
					return true;
				}
			}
		}
	}
	return false;
}

// Functions for combo selected.

function selectcombo(strFormName,strCboName,strval)
{
		for (var i=0; i<eval("document."+strFormName+"."+strCboName+".options.length"); i++)
		{
			if (eval("document."+strFormName+"."+strCboName+".options[i].value")==strval)
			{
				var objFrm = eval("document."+strFormName);
				objFrm[strCboName].options[i].selected=true;
				break;
			}
		}
}

//Function for select multiple checkbox

function selchkmultiple(strFormName,strchkName,strval)
{
	var mystring = new String(strval);
	var arr = mystring.split(',');
	//var arr = strval;
	var objFrm = eval("document."+strFormName);
	for (var i=0; i <objFrm.elements.length; i++)
	{
		if(objFrm.elements[i].type=="checkbox")
		{
			if(objFrm.elements[i].name==strchkName)
			{
				for (var j=0; j<arr.length; j++)
				{
					//alert(arr[j]);
					if(objFrm.elements[i].value==trim(arr[j]))
					{
						objFrm.elements[i].checked=true;
					}
				}
				
			}
		}
	}
}

// Functions for radio selected.

function checkedselectradio(strFormName,strradName,strval)
{
	flagchk=false;
		var objFrm = eval("document."+strFormName);
		for (var i=0; i <objFrm.elements.length; i++)
		{
			if(objFrm.elements[i].type=="radio")
			{
				if(objFrm.elements[i].name == strradName)
				{
					if(objFrm.elements[i].checked == true)
					{								
						flagchk=true;
						break;
					}
				}
			}
		}
		if(flagchk==false)
		{
			alert("Please Select "+strval+" Radio.");
			return false;
		}
		else
		{
			return true;
		}
}

// Functions for checkbox selected.

function clickchkbox(strFormName,strchkName,strval)
{
		flagchk=false;
		var chkids = "";
		var objFrm = eval("document."+strFormName);
		for(var i=0;i<objFrm.elements.length;i++)
		{
			 if(objFrm.elements[i].type=="checkbox")
			{
				 if(objFrm.elements[i].name==strchkName)
				{
					if(objFrm.elements[i].checked==true)
					 {
						chkids = chkids + objFrm.elements[i].value + ",";
						flagchk=true;
					 }
				}
			}
		} 
		if(flagchk==false)
		{
			alert("Please Select "+strval+" Box.");
			return false;
		}
		else
		{
			return chkids;
		}
}

//Function for alpha-numeric|numeric check

function keyRestrict(e, validchars) {
	 var key='', keychar='';
	 key = getKeyCode(e);
	 if (key == null) return true;
	 keychar = String.fromCharCode(key);
	 keychar = keychar.toLowerCase();
	 validchars = validchars.toLowerCase();
	 if (validchars.indexOf(keychar) != -1)
	  return true;
	 if ( key==null || key==0 || key==8 || key==9 || key==13 || key==27 )
	  return true;
	 return false;
}

function getKeyCode(e) {
	 if (window.event)
		return window.event.keyCode;
	 else if (e)
		return e.which;
	 else
		return null;
}

//Function for image type check

/*function chkimage(image)
{
	var val = trim(image);
	strlen = val.length;
	strcnt = val.lastIndexOf("\\");
	newstr = val.substr(strcnt+1,strlen);
	tempval = val.toUpperCase();
	val = tempval.substr(val.lastIndexOf("."),val.length);

	if(((val.length==4 )&&((val!='.JPG')  &&  (val!='.GIF')  &&  (val!='.PNG'))) || ((val.length==5 )&&(val!='.JPEG'))) 
	{
		return false;
	}
	else
	{
		return true;
	}
}*/

//Function for .SWF file check
function chkSWFfile(file)
{
	var val = trim(file);
	strlen = val.length;
	strcnt = val.lastIndexOf("\\");
	newstr = val.substr(strcnt+1,strlen);
	tempval = val.toUpperCase();
	val = tempval.substr(val.lastIndexOf("."),val.length);

	if((val.length==4)&&((val!='.SWF')  &&  (val!='.FLV')) )
	{
		return false;
	}
	else
	{
		return true;
	}
}

//Function for checking pdf or doc

function chkpdfdoc(image)
{
	var val = trim(image);
	strlen = val.length;
	strcnt = val.lastIndexOf("\\");
	newstr = val.substr(strcnt+1,strlen);
	tempval = val.toUpperCase();
	val = tempval.substr(val.lastIndexOf("."),val.length);

	if((val!='.PDF') || (val!='.DOC'))   
	{
		return false;
	}
	else
	{
		return true;
	}
}

//Function for voice type check

function chkvoice(voice)
{
	var val = trim(voice);
	strlen = val.length;
	strcnt = val.lastIndexOf("\\");
	newstr = val.substr(strcnt+1,strlen);
	tempval = val.toUpperCase();
	val = tempval.substr(val.lastIndexOf("."),val.length);

	if((val.length==4)&&((val!='.MP3')) )
	{
		return false;
	}
	else
	{
		return true;
	}
}

//Function For Text Counter

function textCounter(field,cntfield,maxlimit) 
{
	if (field.value.length > maxlimit) // if too long...trim it!
	{
		field.value = field.value.substring(0, maxlimit);
	}	
	else // otherwise, update 'characters left' counter
	{
		cntfield.value = maxlimit - field.value.length;
	}
}

//Function for checking valid email address 

function emailCheck(emailStr) 
{ 
	var emailPat=/^(.+)@(.+)$/
	/* The following string represents the pattern for matching all special
	   characters.  We don't want to allow special characters in the address.
	   These characters include ( ) < > @ , ; : \ " . [ ]    */
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	/* The following string represents the range of characters allowed in a
	   username or domainname.  It really states which chars aren't allowed. */
	var validChars="\[^\\s" + specialChars + "\]"
	/* The following pattern applies if the "user" is a quoted string (in
	   which case, there are no rules about which characters are allowed
	   and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
	   is a legal e-mail address. */
	var quotedUser="(\"[^\"]*\")"
	/* The following pattern applies for domains that are IP addresses,
	   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
	   e-mail address. NOTE: The square brackets are required. */
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	/* The following string represents an atom (basically a series of
	   non-special characters.) */
	var atom=validChars + '+'
	/* The following string represents one word in the typical username.
	   For example, in john.doe@somewhere.com, john and doe are words.
	   Basically, a word is either an atom or quoted string. */
	var word="(" + atom + "|" + quotedUser + ")"
	// The following pattern describes the structure of the user
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	/* The following pattern describes the structure of a normal symbolic
	   domain, as opposed to ipDomainPat, shown above. */
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")


	/* Finally, let's start trying to figure out if the supplied address is
	   valid. */

	/* Begin with the coarse pattern to simply break up user@domain into
	   different pieces that are easy to analyze. */
	var matchArray=emailStr.value.match(emailPat)
	if (matchArray==null) 
	{
	  /* Too many/few @'s or something; basically, this address doesn't
		 even fit the general mould of a valid e-mail address. */
		//alert("Email address seems incorrect (check @ and .'s)")
		emailStr.focus()
		//return false
		return "Email address seems incorrect (check @ and .'s)"
	}
	var user=matchArray[1]
	var domain=matchArray[2]

	// See if "user" is valid
	if (user.match(userPat)==null)
	{
		// user is not valid
		//alert("The username doesn't seem to be valid.")
		emailStr.focus()
		//return false
		return "The username doesn't seem to be valid.";
	}

	/* if the e-mail address is at an IP address (as opposed to a symbolic
	   host name) make sure the IP address is valid. */
	var IPArray=domain.match(ipDomainPat)
	if (IPArray!=null) 
	{
		// this is an IP address
		 for (var i=1;i<=4;i++)
		{
			if (IPArray[i]>255)
			{
				//alert("Destination IP address is invalid!")
				emailStr.focus()
				//return false
				return "Destination IP address is invalid!"
			}
		}
		return true
	}


	// Domain is symbolic name
	var domainArray=domain.match(domainPat)
	if (domainArray==null)
	{
		//alert("The domain name doesn't seem to be valid.")
		emailStr.focus()
		//return false
		return "The domain name doesn't seem to be valid."
	}

	/* domain name seems valid, but now make sure that it ends in a
	   three-letter word (like com, edu, gov) or a two-letter word,
	   representing country (uk, nl), and that there's a hostname preceding
	   the domain or country. */

	/* Now we need to break up the domain to get a count of how many atoms
	   it consists of. */
	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>3) 
	{
	   // the address must end in a two letter or three letter word.
	   //alert("The address must end in a three-letter domain, or two letter country.")
	   emailStr.focus()
	   //return false
	   return "The address must end in a three-letter domain, or two letter country."
	}

	// Make sure there's a host name preceding the domain.
	if (len<2) 
	{
	   var errStr="This address is missing a hostname!"
	   //alert(errStr)
	   emailStr.focus()
	   //return false
		return errStr;
	}
	// If we've gotten this far, everything's valid!
	return false
}

	function Checkemail(emailStr) 
	{ 
		var emailPat=/^(.+)@(.+)$/
			/* The following string represents the pattern for matching all special
			   characters.  We don't want to allow special characters in the address.
			   These characters include ( ) < > @ , ; : \ " . [ ]    */
			var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
			/* The following string represents the range of characters allowed in a
			   username or domainname.  It really states which chars aren't allowed. */
			var validChars="\[^\\s" + specialChars + "\]"
			/* The following pattern applies if the "user" is a quoted string (in
			   which case, there are no rules about which characters are allowed
			   and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
			   is a legal e-mail address. */
			var quotedUser="(\"[^\"]*\")"
			/* The following pattern applies for domains that are IP addresses,
			   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
			   e-mail address. NOTE: The square brackets are required. */
			var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
			/* The following string represents an atom (basically a series of
			   non-special characters.) */
			var atom=validChars + '+'
			/* The following string represents one word in the typical username.
			   For example, in john.doe@somewhere.com, john and doe are words.
			   Basically, a word is either an atom or quoted string. */
			var word="(" + atom + "|" + quotedUser + ")"
			// The following pattern describes the structure of the user
			var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
			/* The following pattern describes the structure of a normal symbolic
			   domain, as opposed to ipDomainPat, shown above. */
			var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")


			/* Finally, let's start trying to figure out if the supplied address is
			   valid. */

			/* Begin with the coarse pattern to simply break up user@domain into
			   different pieces that are easy to analyze. */
			var matchArray=emailStr.value.match(emailPat)
			if (matchArray==null) 
			{
			  /* Too many/few @'s or something; basically, this address doesn't
				 even fit the general mould of a valid e-mail address. */
				alert("Email address or username seems incorrect (check @ and .'s)")
				emailStr.focus()
				return false
			}
			var user=matchArray[1]
			var domain=matchArray[2]

			// See if "user" is valid
			if (user.match(userPat)==null)
			{
			    // user is not valid
			    alert("The username or username doesn't seem to be valid.")
			    emailStr.focus()
			    return false
			}

			/* if the e-mail address is at an IP address (as opposed to a symbolic
			   host name) make sure the IP address is valid. */
			var IPArray=domain.match(ipDomainPat)
			if (IPArray!=null) 
			{
			    // this is an IP address
				 for (var i=1;i<=4;i++)
				{
					if (IPArray[i]>255)
					{
						alert("Destination IP address is invalid!")
						emailStr.focus()
						return false
					}
			    }
			    return true
			}


			// Domain is symbolic name
			var domainArray=domain.match(domainPat)
			if (domainArray==null)
			{
				alert("The domain name doesn't seem to be valid.")
				emailStr.focus()
			    return false
			}

			/* domain name seems valid, but now make sure that it ends in a
			   three-letter word (like com, edu, gov) or a two-letter word,
			   representing country (uk, nl), and that there's a hostname preceding
			   the domain or country. */

			/* Now we need to break up the domain to get a count of how many atoms
			   it consists of. */
			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>3) 
			{
			   // the address must end in a two letter or three letter word.
			   alert("The address or username must end in a three-letter domain, or two letter country.")
			   emailStr.focus()
			   return false
			}

			// Make sure there's a host name preceding the domain.
			if (len<2) 
			{
			   var errStr="This address or username is missing a hostname!"
			   alert(errStr)
			   emailStr.focus()
			   return false
			}
			

// If we've gotten this far, everything's valid!
return true
}

