function validateMsisdn(field, value, formatFlag) {
	var CleanedString="";
	var index = 0;
	var LimitCheck;
	var InitialString = value;

	//Get the length of the inputted string, to know how many characters to check
	LimitCheck = InitialString.length;

	//Walk through the inputted string and collect only number characters, appending them to CleanedString
    //The user can enter a MSISDN in any format - including (NNN)NNN-NNNN, NNNNNNNNNN, NNN.NNN.NNNN, NNN-NNN-NNNN.
	if(msisdnFormatCheck(InitialString) == false) {
	    return false;
	}
	while (index != LimitCheck) {
  		if (isNaN(parseInt(InitialString.charAt(index)))) { 
			if ( (InitialString.charAt(index) != '.')  &&  (InitialString.charAt(index) != '-') &&  (InitialString.charAt(index) != '(') &&  (InitialString.charAt(index) != ')')  )  {
		    	//alert('Invalid Format');
		    	return false;
		    }
  		}
  		else { CleanedString = CleanedString + InitialString.charAt(index); }
  		index = index + 1;
 	}

	//If CleanedString is exactly 10 digits long, then format it and allow form submission
	if (CleanedString.length == 10) {
  		if (formatFlag == true) {
		  	eval(field).value = CleanedString.substring(0,3) + "-" + CleanedString.substring(3,6) + "-" + CleanedString.substring(6,10);
		}
  		return true;
 	}

	//If CleanedString is not 10 digits longs, show an alert and cancel form submission
	else {
		CleanedString = InitialString;
		return false;
 	}
}

function msisdnFormatCheck(msisdn) {
   //NNNNNNNNNN, (NNN)NNN-NNNN, NNN.NNN.NNNN,NNN-NNN-NNNN
	if (msisdn.length == 10)
		return true;
	if(msisdn.charAt(0) == '(' && msisdn.charAt(4) == ')' && msisdn.charAt(8) =='-')
		return true;
	if(msisdn.charAt(3) == '.' && msisdn.charAt(7) == '.')
		return true;
	if(msisdn.charAt(3) == '-' && msisdn.charAt(7) == '-')
		return true;
	
	return false;
}
function validateEmail(field, emailStr) {
	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]
	// 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
	var domainArray=domain.match(domainPat)
	if (domainArray==null) {
		//alert("The domain name doesn't seem to be valid.")
	    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 must end in a three-letter domain, or two letter country.")
	   return false
	}

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

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


function textCounter(field, countfield, maxlimit, recipientLength, subjectLength) {
	var maxlimit = maxlimit - recipientLength - subjectLength;
	if (field.value.length <= maxlimit)
		calculate();
	else
		//alert("You have exceeded 160 Characters");
		field.value = field.value.substring(0, maxlimit);
		calculate();
}

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);

   return strText;
}

function validateSelect(selectBox) {
	for (var i = 1; i < selectBox.options.length; i++) {
		if (selectBox.options[i].selected) {
			return true;
		}
	}
	return false;
}
