
//For phone number validations
var phone = "()-0123456789";
var numb = "0123456789";
var alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ ";

function res(t,v){
var w = "";
for (i=0; i < t.value.length; i++) {
x = t.value.charAt(i);
if (v.indexOf(x,0) != -1)
w += x;
}
t.value = w;
}

// function for username field validations
function validateName(field) {
var valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 .-_";
var ok = "yes";
var temp;
for (var i=0; i<field.value.length; i++) {
temp = "" + field.value.substring(i, i+1);
if (valid.indexOf(temp) == "-1") ok = "no";
}
if (ok == "no") {
alert("Invalid entry!  Only Aplhanumeric,Dots,Hyphen,Space and Underscores are accepted!");
field.select();
field.focus();
//field.value="";
   }
}

// function for email field validations
function validateEmail(field) {
var valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789.-_@";
var ok = "yes";
var temp;
for (var i=0; i<field.value.length; i++) {
temp = "" + field.value.substring(i, i+1);
if (valid.indexOf(temp) == "-1") ok = "no";
}
if (ok == "no") {
alert("Invalid entry!  Only Aplhanumeric,Dots,Hyphen and Underscores are accepted!");
field.select();
field.focus();
   }
}



function trim(value) {
   var temp = value;
   var obj = /^(\s*)([\W\w]*)(\b\s*$)/;
   if (obj.test(temp)) { temp = temp.replace(obj, '$2'); }
   var obj = / +/g;
   temp = temp.replace(obj, " ");
   if (temp == " "){ 
   temp = ""; 
   }
   return temp;
}

// Function that removes the leading spaces.
function ltrim(sent_str){
var spaces=new String("\t\r");
var str=new String(sent_str);
// This checks that there really leading blanks before the string.
if(spaces.indexOf(str.charAt(0))!=-1){
var j=0, i=str.length;
while(j<i && spaces.indexOf(str.charAt(j))!=-1)
j++;
// Keep iterating until you reach at a character that is not a space.
str=str.substring(j,i);
// Copy the rest of the string.
}
return str;
}


function isEmpty(aTextField){
var re = /\s/g; //Match any white space including space, tab, form-feed, etc. 
RegExp.multiline = true; // IE support
var str = aTextField.replace(re, "");
	if (str.length == 0) {
		return true;
	}else{
		return false;
	}
}

	function otherFieldsValidate(){
	var nm=document.emailform.orgname.value;
	var cp=document.emailform.contperson.value;
	var city=document.emailform.city.value;
	var country=document.emailform.country.value;
	var str="._-";

	if(isEmpty(ltrim(nm))){
	alert("Enter the Organization Name ");
	document.emailform.orgname.focus();
	return false;
	}else if(nm != ""){
		for(i=0;i<str.length;i++)
		if(nm.indexOf(str.charAt(i)) == 0){
		alert("Invalid Entry ! Please Check.. ");
		document.emailform.orgname.focus();
		return false;
		}
	}
	if(document.emailform.orgtype.value==""){
	alert("Select the Organization Type");
	document.emailform.orgtype.focus();
	return false;
	}
	if(isEmpty(trim(cp))){
	alert("Enter the PersonName");
	document.emailform.contperson.focus();
	return false;
	}else if(cp !=""){
		for(i=0;i<str.length;i++)
		if(cp.indexOf(str.charAt(i)) == 0){
		alert("Invalid Entry ! Please Check.. ");
		document.emailform.contperson.focus();
		return false;
		}
	}
	if(document.emailform.phoneno.value==""){
	alert("Enter the PhoneNumber");
	document.emailform.phoneno.focus();
	return false;
	}
	if(document.emailform.email.value==""){
	alert("Enter the Email-Id");
	document.emailform.email.focus();
	return false;
	}
	if(document.emailform.dictSys.value==""){
	alert("Select the Dictation System");
	document.emailform.dictSys.focus();
	return false;
	}
	return true;
	}

	function otherFieldsValidateFeedBack(){
	var nm=document.emailform.consumer_id.value;
	var str="._-";
	if(isEmpty(trim(nm))){
		alert("Enter the Name ");
		document.emailform.consumer_id.focus();
		return false;
	}
	if(nm != ""){
		for(i=0;i<str.length;i++)
		if(nm.indexOf(str.charAt(i)) == 0){
		alert("Invalid Entry ! Please Check.. ");
		document.emailform.consumer_id.focus();
		return false;
		}
	}
	if(document.emailform.email.value==""){
		alert("Enter the Email-Id");
		document.emailform.email.focus();
		return false;
	}
		return true;
	}

function emailCheck(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 + ")"
// The following pattern describes the structure of the user
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
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)");
	document.emailform.email.focus();
	return false;
}
var user=matchArray[1]
var domain=matchArray[2]
// See if "user" is valid 
if (user.match(userPat)==null) {
    alert("The username doesn't seem to be valid.");
	document.emailform.email.focus();
    return false;
}
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!");
			document.emailform.email.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.");
	document.emailform.email.focus();
    return false;
}
/* 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) {
	alert("The address must end in a three-letter domain, or two letter country.");
	document.emailform.email.focus();
	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);
	document.emailform.email.focus();
	return false;
}
// If we've gotten this far, everything's valid!
return true;
}

function validate(field) {
var valid = "0123456789/";
var ok = "yes";
var temp;
	for (var i=0; i<field.value.length; i++) {
	temp = "" + field.value.substring(i, i+1);
	if (valid.indexOf(temp) == "-1") ok = "no";
	}
	if(ok == "no"){
	alert("Invalid entry!  Enter valid date !");
	field.focus();
	field.select();
   	}
}

function CheckFromDate(){
	var nm=document.emailform.orgname.value;
	var cp=document.emailform.contperson.value;
	var city=document.emailform.city.value;
	var country=document.emailform.country.value;
	var str="._-";

	if(isEmpty(ltrim(nm))){
			alert("Enter the Organization Name ");
			document.emailform.orgname.focus();
			return false;
		}else if(nm != ""){
			for(i=0;i<str.length;i++)
			if(nm.indexOf(str.charAt(i)) == 0){
			alert("Invalid Entry ! Please Check.. ");
			document.emailform.orgname.focus();
			return false;
			}
		}
	if(document.emailform.orgtype.value==""){
	alert("Select the Organization Type");
	document.emailform.orgtype.focus();
	return false;
	}
	if(isEmpty(trim(cp))){
	alert("Enter the PersonName");
	document.emailform.contperson.focus();
	return false;
	}else if(cp !=""){
		for(i=0;i<str.length;i++)
		if(cp.indexOf(str.charAt(i)) == 0){
		alert("Invalid Entry ! Please Check.. ");
		document.emailform.contperson.focus();
		return false;
		}
	}
	if(document.emailform.phoneno.value==""){
	alert("Enter the PhoneNumber");
	document.emailform.phoneno.focus();
	return false;
	}
	if(document.emailform.email.value==""){
	alert("Enter the Email-Id");
	document.emailform.email.focus();
	return false;
	}
	if(document.emailform.dictSys.value==""){
	alert("Select the Dictation System");
	document.emailform.dictSys.focus();
	return false;
	}
	if (document.emailform.date.value != '') {
		 var inputStr=document.emailform.date.value;
         var delim1=inputStr.indexOf("/");
		 var delim2=inputStr.lastIndexOf("/");
		if(delim1!=-1 && delim1==delim2){
			 alert("The date entry is not in an acceptable format.");
			  document.emailform.date.focus();
			 return false;
		}
		if(delim1 !=-1)	{
			var mm=parseInt(inputStr.substring(0,delim1),10);
			var dd=parseInt(inputStr.substring(delim1+1,delim2),10);
			var yyyy=parseInt(inputStr.substring(delim2+1,inputStr.length),10);
	}else{
           var mm=parseInt(inputStr.substring(0,2),10);
			var dd=parseInt(inputStr.substring(2,4),10);
			var yyyy=parseInt(inputStr.substring(4,inputStr.length),10);
	}
	if(isNaN(mm) || isNaN(dd) || isNaN(yyyy)){
			 alert("The date entry is not in an acceptable format.");
			   document.emailform.date.focus();
			return false;
	}
	if(mm<1 || mm>12){
			alert ("Month must be entered between the range of 01 and 12");
			  document.emailform.date.focus();
			return false;
	}
	if(dd<1 || dd>31){
			alert ("Date must be entered between the range of 01 and 31");
			  document.emailform.date.focus();
			return false;
	}
	if(yyyy<1900 || mm>3000){
			alert ("enter valid year ");
			  document.emailform.date.focus();
			return false;
	}
		var cdate=new Date();
		var thisyear=cdate.getYear();
		var thismonth=cdate.getMonth()+1;
		var thisdate=cdate.getDate();
	if(yyyy>thisyear){
			alert("Year must be LessThan Or Equal to Current Year");
		   document.emailform.date.focus();
			return false;
	}
		/*
		if(yyyy==thisyear){
			if(mm>thismonth){
			alert("Date must be Less than Current Date");
			document.emailform.date.focus();
			return false;
			}
			else if(dd>thisdate && mm>=thismonth){
			alert("Date must be Less than Current Date");
			document.emailform.date.focus();
			return false;
			}
		}
		*/
		if (!checkMonthLength(mm,dd)){
			return false;
		}
		if(mm==2 || mm==02){
			if(!checkLeapMonth(mm,dd,yyyy))	{
				return false;
			}
		}
	}
return true;

}//end of date function


function checkMonthLength(mm,dd){
var months=new Array("","January","February","March","April","May","June","July","August","September","October","November","December");
	if ((mm==04 || mm==4 || mm==6 || mm==06 || mm==9 || mm==09 || mm==11) && dd>30){
	alert(months[mm] +" has only 30 days.");
	document.emailform.date.focus();
	return false;
	}else if(dd>31){
	alert(months[mm] +" has only 31 days.");
	document.emailform.date.focus();
	return false;
	}
	return true;
}

function  checkLeapMonth(mm,dd,yyyy){
	if (yyyy % 4 > 0 && dd>28)
	{
	alert("February of "+yyyy+" has only 28 days.");
	 document.emailform.date.focus();
	return false;
	}
	else if(yyyy % 4 == 0 && dd>29)
	{
 alert("February of "+yyyy+" has only 29 days.");
  document.emailform.date.focus();
	return false;
	}
	return true;
}
