

/**
 * For optout hiding
 */
function showNoContact(reset)
{
	if(document.getElementById("userinput").checked == true && reset != 1) {
		document.getElementById("userinfo").style.display = 'block'; 
		document.getElementById("userform").style.display = "none";
		document.getElementById("userinfochk").style.display = "none";

		document.getElementById("fnameerr").style.display = "none";
		document.getElementById("lnameerr").style.display = "none";
		document.getElementById("emailerr").style.display = "none";
	}
	else {
		document.getElementById("userinfo").style.display = 'none'; 
		document.getElementById("userform").style.display = 'block';
		document.getElementById("userinfochk").style.display = "block";
	}
}

/**
 * On "other" topic selection
 */
function showOther(reset) {

	var sel = document.getElementById("topic").value;
	
	if((sel == 'other' || sel == 'Other') && reset != 1) {
		document.getElementById("labelOther").style.display = 'block';
		document.getElementById("topicOther").style.display = 'block';
		document.getElementById("topicOther").focus();
	} else {
		document.getElementById("labelOther").style.display = 'none';
		document.getElementById("topicOther").style.display = 'none';
		document.getElementById("othererr").style.display = "none";
		document.getElementById("othererr").innerHTML = "";
	}

}

/**
 * This function will be called on form reset to undo showNoContact & showOther actions
 */
function resetJsActions() {
	showNoContact(1);
	showOther(1);
}


/**
 * Email validation
 */
function validateEmail(email) {
	var reg1 = /(@.*@)|(\.\.)|(@\.)|(\.@)|(^\.)/; // not valid
	var reg2 = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?)$/; // valid
	
	if((email != null) && (email.length > 0) && (!reg1.test(email)) && (reg2.test(email))) {
		return true;
	}
	
	return false;
}



// Declaring required variables for phone nr validation
var digits = "0123456789";
// non-digit characters which are allowed in phone numbers
var phoneNumberDelimiters = "()- ";
// characters which are allowed in international phone numbers
// (a leading + is OK)
var validWorldPhoneChars = phoneNumberDelimiters + "+";
// Minimum no of digits in an international phone no.
var minDigitsInIPhoneNumber = 10;

function isInteger(s) { 
	var i;
   for (i = 0; i < s.length; i++)
   {   
		// Check that current character is number.
      var c = s.charAt(i);
      if (((c < "0") || (c > "9"))) return false;
   }
   // All characters are numbers.
   return true;
}


function stripCharsInBag(s, bag) {
	var i;
   var returnString = "";
   // Search through string's characters one by one.
   // If character is not in bag, append to returnString.
   for (i = 0; i < s.length; i++) {   
   	// Check that current character isn't whitespace.
      var c = s.charAt(i);
      if (bag.indexOf(c) == -1) returnString += c;
   }
   return returnString;
}


function checkInternationalPhone(strPhone)
{
	s=stripCharsInBag(strPhone,validWorldPhoneChars);
	return (isInteger(s) && s.length >= minDigitsInIPhoneNumber);
}


function validatePhone(phonenum) {
	
	if ((phonenum != null) && (phonenum != ""))	{
		return checkInternationalPhone(phonenum);
	} else {
		return false;
	}
}
 
 
// Declaring valid date character, minimum year and maximum year - for date validation
var dtCh= "/";
var dtCh1= "-";
var minYear=1900;
var maxYear=2100;

function isInteger(s) {
	var i;
    for (i = 0; i < s.length; i++){   
        // Check that current character is number.
        var c = s.charAt(i);
        if (((c < "0") || (c > "9"))) return false;
    }
    // All characters are numbers.
    return true;
}

function stripCharsInBag(s, bag) {
	var i;
   var returnString = "";
   // Search through string's characters one by one.
   // If character is not in bag, append to returnString.
   for (i = 0; i < s.length; i++){   
      var c = s.charAt(i);
      if (bag.indexOf(c) == -1) returnString += c;
   }
   return returnString;
}

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;
}

/**
 * Date validation - for future date
 */
function isDate(dtStr){
	var daysInMonth = DaysArray(12);
	if(dtStr.indexOf(dtCh) > -1)
	{
		var pos1=dtStr.indexOf(dtCh);
		var pos2=dtStr.indexOf(dtCh,pos1+1);
	}
	else if(dtStr.indexOf(dtCh1) > -1)
	{
		var pos1=dtStr.indexOf(dtCh1);
		var pos2=dtStr.indexOf(dtCh1,pos1+1);
	}
	else
	{
		 return false;
	}	 	
	var strMonth=dtStr.substring(0,pos1);
	var strDay=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(strYear.length < 4)
	{
		year = 2000 +year;
	}
	if (pos1==-1 || pos2==-1){
		return false;
	}
	if (strMonth.length<1 || month<1 || month>12){
		return false;
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
		return false;
	}
	if (strYear.length > 4 || strYear.length < 2 || strYear.length == 3 || year==0){
		return false;
	}
	if(dtStr.indexOf(dtCh) > -1)
	{
	
		if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh))==false){
			return false;
		}
	}
	if(dtStr.indexOf(dtCh1) > -1)
	{
		if(dtStr.indexOf(dtCh1,pos2+1)!=-1 || isInteger(stripCharsInBag(dtStr, dtCh1))==false)
		{
			return false;
		}
	}	

	var now = new Date();
	var yeartoday = now.getYear();
	if(yeartoday < 1000) {
		year = year - 1900;
	}
	var monthtoday = now.getMonth() + 1;
	var datetoday = now.getDate();

	if(year < yeartoday) {
		return false;
	} else if(year == yeartoday) {
		if(month == monthtoday) {
			return (day > datetoday);
		} else if(month > monthtoday) {
			return true;
		} else {
			return false;
		}
	} 
	
	return true;

}

// Function to set focus
function setfocus(focusid) {
	var elmt = document.getElementById(focusid);
	if(elmt) {
		elmt.focus();
	}
}

// validate zip - US & CANADA
function isvalidzip(str) 
{
  	var reg1 = /[ABCEGHJKLMNPRSTVXY]\d[A-Z]((\s){0,1}\d[A-Z]\d){0,1}/;
  	var reg2 = /[ABCEGHJKLMNPRSTVXY]\d[A-Z]\d[A-Z]\d/;
  	var reg3 = /^\d{5}([\-]\d{4})?$/ //us
  	if((str != null) && (str != ""))
  	{
	  	if(reg1.test(str) || reg2.test(str) || reg3.test(str))
	 	{
			return true;
	  	}
  	}
  	return false;
}


