// this will allow user to enter only 'maxlimit' number of character
function maxcharacter(field, maxlimit) {
	if (field.value.length > maxlimit) // if too long...trim it!
	{
		alert('Maximum Character i.e ' + maxlimit + ' Limit Exceeds');
		field.value = field.value.substring(0, maxlimit);
	}
}


function userdata(uname, uid, uemail) {
	document.getElementById('username').value = uname;
	document.getElementById('user_id').value= uid;
	document.getElementById('user_email').value= uemail;
}


function Checkall(chk,flag,link)
{
   //var chk = document.inbox.chked_mail;
   //var flag = document.inbox.selectall;
    var i,res;
    if(link == 1) {
		if(flag.checked == true) {
			flag.checked = false;
			res = false;
		} else {
			flag.checked = true;
			res = true;
			
		}	
	} else {
		if(flag.checked == true) {
			res = true;
		} else {
			res = false;
		}
	}	

   if (!chk.length) 
       chk.checked = res;

   for (i = 0; i < chk.length; i++) {
       if (chk[i].checked != res) {
           chk[i].checked = res;
       }
   }
}



function addElement(x,y)
{
    for (i=0; i < x.length; i++)
    {
        if(x.options[i].selected) {
            append(y,x.options[i].text,x.options[i].value);
        }
    }
   remove(x);
}

function append(theSel, newText, newValue)
{
  if (theSel.length == 0) {
    var newOpt1 = new Option(newText, newValue);
    theSel.options[0] = newOpt1;
    theSel.selectedIndex = 0;
  } else if (theSel.selectedIndex != -1) {
    var selText = new Array();
    var selValues = new Array();
    var selIsSel = new Array();
    var newCount = -1;
    var newSelected = -1;
    var i;
    for(i=0; i<theSel.length; i++)
    {
      newCount++;
      selText[newCount] = theSel.options[i].text;
      selValues[newCount] = theSel.options[i].value;
      selIsSel[newCount] = theSel.options[i].selected;

      if (newCount == theSel.selectedIndex) {
        newCount++;
        selText[newCount] = newText;
        selValues[newCount] = newValue;
        selIsSel[newCount] = false;
        newSelected = newCount - 1;
      }
    }
    for(i=0; i<=newCount; i++)
    {
      var newOpt = new Option(selText[i], selValues[i]);
      theSel.options[i] = newOpt;
      theSel.options[i].selected = selIsSel[i];
    }
  }
}

function remove(theSel)
{
  var selIndex = theSel.selectedIndex;
  if (selIndex != -1) {
    for(i=theSel.length-1; i>=0; i--)
    {
      if(theSel.options[i].selected)
      {
        theSel.options[i] = null;
      }
    }
    if (theSel.length > 0) {
      theSel.selectedIndex = selIndex == 0 ? 0 : selIndex - 1;
    }
  }
}

function removeElement(x,y)
{
    for (i=0; i < y.length; i++)
    {
        if(y.options[i].selected) {
            append(x,y.options[i].text,y.options[i].value);
        }
    }
   remove(y);
}


function joinvalues(fieldname,storfield)
{
        if( fieldname.length) {
            var val = fieldname.options[0].text;
            for(i=1; i < fieldname.length; i++) {
               val = val + "~~" + fieldname.options[i].text;
            }
         storfield.value = val;
       }
}

function GetXmlHttpObject()
{
    var xmlHttp=null;
    try
    {
        // Firefox, Opera 8.0+, Safari
        xmlHttp=new XMLHttpRequest();
    } catch (e) {
        // Internet Explorer
        try
        {
            xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
        } catch (e)
        {
            xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
    }
   return xmlHttp;
}
function trim(inputString)
{
   // Removes leading and trailing spaces from the passed string. Also removes
   // consecutive spaces and replaces it with one space. If something besides
   // a string is passed in (null, custom object, etc.) then return the input.

	if (typeof inputString != "string")
	{
		return inputString;
	}
   	var retValue = inputString;

	// Check for spaces at the beginning of the string
   	var ch = retValue.substring(0, 1);
   	while (ch == " ")
	{
      	retValue = retValue.substring(1, retValue.length);
      	ch = retValue.substring(0, 1);
   	}

	// Check for spaces at the end of the string
   	ch = retValue.substring(retValue.length-1, retValue.length);
   	while (ch == " ")
	{
	  	retValue = retValue.substring(0, retValue.length-1);
      	ch = retValue.substring(retValue.length-1, retValue.length);
   	}

	//Commented by kavita- 25th feb 2004
	// Note that there are two spaces in the string - look for multiple spaces within the string
	/* while (retValue.indexOf("  ") != -1)
	{
    	retValue = retValue.substring(0, retValue.indexOf("  ")) + retValue.substring(retValue.indexOf("  ")+1, retValue.length); // Again, there are two spaces in each of the strings
	}*/

   	return retValue; // Return the trimmed string back to the user
} // Ends the "trim" function


function setFocus(field)
{
	var formName = document.forms[0];
	var objfield = eval("formName."+field);
	objfield.focus();
} //end of function

function checkEmail(emailStr)
{
	var checkTLD=1;
    var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;
    var emailPat=/^(.+)@(.+)$/;
    var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";
    var validChars="\[^\\s" + specialChars + "\]";
    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.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)");
        return false;
    }
    var user=matchArray[1];
    var domain=matchArray[2];

    // Start by checking that only basic ASCII characters are in the strings (0-127).

    for (i=0; i<user.length; i++) {
        if (user.charCodeAt(i)>127) {
        //alert("Ths username contains invalid characters.");
           return false;
        }
    }
    for (i=0; i<domain.length; i++) {
        if (domain.charCodeAt(i)>127) {
           //alert("Ths domain name contains invalid characters.");
           return false;
        }
    }

    // See if "user" is valid
    if (user.match(userPat)==null) {
       // user is not valid
       //alert("The username doesn't seem to be valid.");
       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!");
          return false;
          }
       }
       return true;
    }

    // Domain is symbolic name.  Check if it's valid.

    var atomPat=new RegExp("^" + atom + "$");
    var domArr=domain.split(".");
    var len=domArr.length;
    for (i=0;i<len;i++) {
        if (domArr[i].search(atomPat)==-1) {
           //alert("The domain name does not seem to be valid.");
           return false;
        }
    }

    /* domain name seems valid, but now make sure that it ends in a
    known top-level domain (like com, edu, gov) or a two-letter word,
    representing country (uk, nl), and that there's a hostname preceding
    the domain or country. */

    if (checkTLD && domArr[domArr.length-1].length!=2 && domArr[domArr.length-1].search(knownDomsPat)==-1) {
       //alert("The address must end in a well-known domain or two letter " + "country.");
       return false;
    }

    // Make sure there's a host name preceding the domain.
    if (len<2) {
       //alert("This address is missing a hostname!");
       return false;
    }

    // If we've gotten this far, everything's valid!
    return true;
}

/**
*	Function Name:	checkPasswd()
*	Description:	Validates password string. Password should not contain ',','.' and '=' characters.
*	In Parameter:	pwd string
*	Out Parameter:	true boolean - if pattern is matched.
*			false boolean - if pattern is not matched.
*/
function checkPasswd(pwd)
{
	var pattern = /[,:=\s]/;
	if (pwd.match(pattern) == null)
	{
		return	true;
	}
	else
	{
		return false;
	}
}


/**
*	Function Name:	checkSpace()
*	Description:	checks for white space.
*	In Parameter:	username string
*	Out Parameter:	true boolean - if pattern is matched.
*			false boolean - if pattern is not matched.
*/
function checkSpace(username)
{
	var pattern = /[\s]/;
	if (username.match(pattern) == null)
	{
		return	true;
	}
	else
	{
		return false;
	}
}

function checkCharacters(val)
{
	//var pattern = /[$\\@\\\#%\^\&\*\(\)\[\]\+\_\{\}\`\~\=\|]/;
	// removed & and hyphen in the pattern

	var pattern = /[$\\@\\\#%\^\!\*\(\)\[\]\+\_\{\}\`\~\=\|]/;

	if (val.match(pattern) == null)
	{
		return	true;
	}
	else
	{
		return false;
	}
}

/**
*	Function Name:	isNumber()
*	Description:	Check for digits.
*	In Parameter:	num string
*	Out Parameter:	true boolean - if pattern is matched.
*			false boolean - if pattern is not matched.
*/
function isNumber(num)
{
	var pattern = /^[\d-]+$/;
	if (num.match(pattern) != null)
	{
		return	true;
	}
	else
	{
		return false;
	}
}


/**
*	Function Name:	isPhoneNumber()
*	Description:	Check for digits.
*	In Parameter:	num string
*	Out Parameter:	true boolean - if pattern is matched.
*			false boolean - if pattern is not matched.
*/
function isPhoneNumber(num)
{
	var pattern = /^[\d\s]+$/;
	if (num.match(pattern) != null)
	{
		return	true;
	}
	else
	{
		return false;
	}
}

/**
*	Function Name	:	checkName()
*	Description		:	Check for first/last name.
*	In Parameter	:	name string
*	Out Parameter	:	true boolean - if pattern is matched.
*						false boolean - if pattern is not matched.
*/
function checkName(name)
{
	var pattern = /^[A-Za-z][A-Za-z-\s]+[A-Za-z]$/;
	if (name.match(pattern) != null)
	{
		return	true;
	}
	else
	{
		return false;
	}

}

function checkSiteName(name)
{
	var pattern = /^[A-Za-z][\w\s-.,']*$/;
	if (name.match(pattern) != null)
	{
		return	true;
	}
	else
	{
		return false;
	}

}

/**
*	Function Name	:	checkURL()
*	Description		:	Check for valid URL.
*	In Parameter	:	url string
*	Out Parameter	:	true boolean - if pattern is matched.
*						false boolean - if pattern is not matched.
*/
function checkURL(url)
{

	//var pattern = /^(http:\/\/|https:\/\/)((([a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})(:[0-9]{2,4})*)|(([0-9]{1,3}\.){3}([0-9]{1,3}))(:[0-9]{2,4})*)((\/|\?)[a-z0-9~#%&'_\+=:\?\.-]*)*)$/;
	//Changed - kavita 21st march 05
	//length for top level domain which is {2,3} is removed. Also any characters are allowed in parameters including unicode also..
	//To resolve production bug.

	//	var pattern = /^(http:\/\/|https:\/\/)((([A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*(\.[A-Za-z]{2,3})(:[0-9]{2,4})*)|((\d{1,3}\.)(\d{1,3}\.)(\d{1,3}\.)(\d{1,3}))(:\d{2,4})*)((\/|\?)[A-Za-z0-9~#%&'_\+=:\?\.-]*)*)$/;
	var pattern = /^(http:\/\/|https:\/\/)((([A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*(\.[A-Za-z]{2,})(:[0-9]{2,4})*)|((\d{1,3}\.)(\d{1,3}\.)(\d{1,3}\.)(\d{1,3}))(:\d{2,4})*)((\/|\?)(.)*)*)$/;

	if (url.match(pattern) != null)
	{
		return	true;
	}
	else
	{
		return false;
	}
}


