// JavaScript Document

// Function to check null values.
function isBlank(val, errtxt)
{
	if(val==null)
	{
		return true;
	}
	for(var i=0;i<val.length;i++)
	{
		if ((val.charAt(i)!=' ')&&(val.charAt(i)!="\t")&&(val.charAt(i)!="\n")&&(val.charAt(i)!="\r"))
		{
			return false;
		}
	}
	if(errtxt != null)
	{
		alert(errtxt);
	}
	return true;
}
// ********************END********************


// Function to determine whether the email address entered is a valid email
function checkemail(curfield)
{ 
    fieldValue  = curfield;
    fieldLength = curfield.length;

    var err = "Please enter a valid Email address.";

    if (fieldLength <= 8)
	{
		alert(err);
		return false;
    }
	else
	{
        if( /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(fieldValue))
		{
			return true;
        }
		else
		{
            alert(err);
			return false;
        }
    }
}
// ********************END********************

// Function to determine whether the email address entered is a valid email
function checkuseremail(curfield)
{ 
	fieldValue  = curfield;
    fieldLength = curfield.length;
	
	var err = "Please enter valid email.\n User name is your email address.";
    if (fieldLength <= 8)
	{
		alert(err);
		return false;
    }
	else
	{
       if( /^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,4})+$/.test(fieldValue))
		{
			return true;
        }
		else
		{
	           alert(err);
			return false;
        }
    }
  
}

// Function to check whether the string entered has valid characters
function inString(sText, ValidChars, errtxt)
{
   var IsValid=true;
   var Char;

   for (i = 0; i < sText.length && IsValid == true; i++)
	  { 
	  Char = sText.charAt(i); 
	  if (ValidChars.indexOf(Char) == -1) 
		 {
			 if(errtxt != null)
			 {
				 alert(errtxt);
			 }
			 IsValid = false;
		 }
	  }
   return IsValid;
}
// ********************END********************


//Limits the attached file to the file extensions specified in the array extArray below.
function LimitAttach(file, type)
{
	if ( type == 'image' )
		extArray = new Array(".jpg", ".jpeg", ".gif");
	else if ( type == 'pdf' )
		extArray = new Array(".pdf");
	allowSubmit = false;
	if (!file) return;
	while (file.indexOf("\\") != -1)
	file = file.slice(file.indexOf("\\") + 1);

	ext_arr = file.split(".");
	ext = "." + ext_arr[ext_arr.length-1].toLowerCase();

	for (var i = 0; i < extArray.length; i++)
	{
		if (extArray[i] == ext)
		{
			allowSubmit = true;
			break;
		}
	}
	if (allowSubmit)
	{
		allowSubmit = true;
	}
	else
	{
		alert("Please only upload files that end in types:  " 
		+ (extArray.join("  ")) + "\nPlease select a new "
		+ "file to upload and submit again.");
		allowSubmit = false;
	}
	return allowSubmit;
}
// ********************END********************


/* *************************************
Script to check a valid date
**************************************** */
var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function daysInFebruary (year)
{
		// February has 29 days in any year evenly divisible by four,
		// EXCEPT for centurial years which are not also divisible by 400.
		return (((year % 4 == 0) && ( (!(year % 100 == 0)) || (year % 400 == 0))) ? 29 : 28 );
}
function DaysArray(n)
{
		for (var i = 1; i <= n; i++) {
				this[i] = 31
				if (i==4 || i==6 || i==9 || i==11) {this[i] = 30}
				if (i==2) {this[i] = 29}
   }
   return this
}

function isDate(dtStr)
{
		var daysInMonth = DaysArray(12)
		var pos1 = dtStr.indexOf(dtCh)
		var pos2 = dtStr.indexOf(dtCh,pos1+1)
		var strDay = dtStr.substring(0,pos1)
		var strMonth = dtStr.substring(pos1+1,pos2)
		var strYear = dtStr.substring(pos2+1)
		strYr = strYear
		if (strDay.charAt(0)=="0" && strDay.length>1) strDay=strDay.substring(1)
		if (strMonth.charAt(0)=="0" && strMonth.length>1) strMonth=strMonth.substring(1)
		for (var i = 1; i <= 3; i++) {
				if (strYr.charAt(0)=="0" && strYr.length>1) strYr=strYr.substring(1)
		}
		month=parseInt(strMonth)
		day=parseInt(strDay)
		year=parseInt(strYr)
		if ((month == 2 && day > daysInFebruary(year)) || day > daysInMonth[month])
		{
			alert("Please enter a valid day");
			return false
		}
return true
}
// ********************END********************


/* *************************************
Script to trim a string
**************************************** */
function Trim(s)
{
  // Remove leading spaces and carriage returns
  
  while ((s.substring(0,1) == ' ') || (s.substring(0,1) == '\n') || (s.substring(0,1) == '\r'))
  {
    s = s.substring(1,s.length);
  }

  // Remove trailing spaces and carriage returns

  while ((s.substring(s.length-1,s.length) == ' ') || (s.substring(s.length-1,s.length) == '\n') || (s.substring(s.length-1,s.length) == '\r'))
  {
    s = s.substring(0,s.length-1);
  }
  return s;
}
// ********************END********************



// Function to open a popup
function MM_openBrWindow(theURL,winName,features)
{
	window.open(theURL,winName,features);
}

function fnConfirm(question, url)
{
	var yesno = confirm(question);
	if (yesno == true)
	{
		window.location.href = url;
	}
}
//Check phone/fax number is number not text
function isInteger(strString)
{ 
var strValidChars = "0123456789.- ()[]+"; 
var strChar;
var blnResult = true; 
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; 
} 


function CheckAll( formname, chk)
{
for (var i=0;i < document.forms[formname].elements.length;i++)
	{
		var e = document.forms[formname].elements[i];
		if (e.type == "checkbox")
		{
			e.checked = chk.checked;
		}
	}
}

function CheckAllTopics(formname, chk)
{
	var chkAll = document.getElementById(chk.value);
    var checks = document.getElementsByName('cats[]');
	var boxLength = checks.length;
	var allChecked = false;

	for (var i=0; i < boxLength; i++)
		{ 
			if(chkAll.checked == true)
				checks[i].checked = true;
			else
				checks[i].checked = false;
		}
}

function unCheckAll(formname, chk_all)
{
	var chkAll = document.getElementById(chk_all);
    var checks = document.getElementsByName('cats[]');
	var boxLength = checks.length;
	var allChecked = false;

	for ( i=0; i < boxLength; i++ ) {
      if ( checks[i].checked == true ) {
        allChecked = true;
        continue;
      }
      else {
        allChecked = false;
        break;
      }
    }
    if ( allChecked == true ) {
      chkAll.checked = true;
    }
    else {
      chkAll.checked = false;
    }
}

//-------------------------------------------------------------------
// isNumeric(elem, helperMsg)
//   Returns true if value contains all digits
//-------------------------------------------------------------------
function isNumeric(elem, helperMsg){
    var numericExpression = /^[0-9]+$/;
    if(elem.value.match(numericExpression)){
        return true;
    }else{
        alert(helperMsg);
        elem.focus();
        return false;
    }
}

//-------------------------------------------------------------------
// isAlphabet(elem, helperMsg){
//   Returns true if value contains alphabets
//-------------------------------------------------------------------
function isAlphabet(elem, helperMsg){
    var alphaExp = /^[a-zA-Z]+$/;
    if(elem.value.match(alphaExp)){
        isValid = true;
    }
    else{
        alert(helperMsg);
        isValid = false;
    }

    return isValid;
}

function showStatus(sMsg) {
    window.status = sMsg ;
    return true ;
}
//Count words in textarea
function cnt_words(w,x){
var y=w.value;
var r = 0;
a=y.replace(/\s/g,' ');
a=a.split(' ');
for (z=0; z<a.length; z++) {if (a[z].length > 0) r++;}
x.value=r;
}

//Chk Words limit of content given in textarea
function chk_limit(cnt,limit)
{
	if(cnt > limit)
		alert("You are exceeding the word limit");
	return;
}

/***********************************************
Script to Count words in given string
**********************************************/
function CountWords(str){
	var fullStr = str;
	var newStr = Trim(fullStr);
	var cleanedStr = newStr.replace(/\s+/g, " ");
	var splitString = cleanedStr.split(" ");
	var word_count = splitString.length;
	return word_count;
}

//********************END

function isValidLength(val, charLimit, errtxt)
{
  var strToChk = Trim(val);
  if(strToChk.length < charLimit)
  {
    alert(errtxt);
    return false;
  }
  else
    return true;
}
