

// 6842775

var dtCh= "/";
var minYear=1900;
var maxYear=2100;

function isInteger2(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 stripCharsInBag2(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
}

function isDate(dtStr){
	var daysInMonth = DaysArray(12)
	var pos1=dtStr.indexOf(dtCh)
	var pos2=dtStr.indexOf(dtCh,pos1+1)
	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 (pos1==-1 || pos2==-1){
//		alert("The date format should be : mm/dd/yyyy")
		return false;
	}
	if (strMonth.length<1 || month<1 || month>12){
//		alert("Please enter a valid month")
		return false;
	}
	if (strDay.length<1 || day<1 || day>31 || (month==2 && day>daysInFebruary(year)) || day > daysInMonth[month]){
//		alert("Please enter a valid day")
		return false;
	}
	if (strYear.length != 4 || year==0 || year<minYear || year>maxYear){
//		alert("Please enter a valid 4 digit year between "+minYear+" and "+maxYear)
		return false;
	}
	if (dtStr.indexOf(dtCh,pos2+1)!=-1 || isInteger2(stripCharsInBag2(dtStr, dtCh))==false){
//		alert("Please enter a valid date")
		return false;
	}
return true;
}

var Url = {  
     encode : function (string) {  
         return escape(this._utf8_encode(string));  
     },  
     decode : function (string) {  
         return this._utf8_decode(unescape(string));  
     },  
     _utf8_encode : function (string) {  
         string = string.replace(/\r\n/g,"\n");  
         var utftext = "";  
   
         for (var n = 0; n < string.length; n++) {  
   
             var c = string.charCodeAt(n);  
   
             if (c < 128) {  
                 utftext += String.fromCharCode(c);  
             }  
             else if((c > 127) && (c < 2048)) {  
                 utftext += String.fromCharCode((c >> 6) | 192);  
                 utftext += String.fromCharCode((c & 63) | 128);  
             }  
             else {  
                 utftext += String.fromCharCode((c >> 12) | 224);  
                 utftext += String.fromCharCode(((c >> 6) & 63) | 128);  
                 utftext += String.fromCharCode((c & 63) | 128);  
             }  
         }  
         return utftext;  
     },  
     _utf8_decode : function (utftext) {  
         var string = "";  
         var i = 0;  
         var c = c1 = c2 = 0;  
         while ( i < utftext.length ) {  
             c = utftext.charCodeAt(i);  
             if (c < 128) {  
                 string += String.fromCharCode(c);  
                 i++;  
             }  
             else if((c > 191) && (c < 224)) {  
                 c2 = utftext.charCodeAt(i+1);  
                 string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));  
                 i += 2;  
             }  
             else {  
                 c2 = utftext.charCodeAt(i+1);  
                 c3 = utftext.charCodeAt(i+2);  
                 string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));  
                 i += 3;  
             }  
         }  
         return string;  
     }  
 };

function emailCheck(emailaddr) {
var s=/^(.+)@(.+)\.(.+)$/;
  if (emailaddr.match(s)==null) {       
	return false;       
  }
return true;
}

function checkPhone (strng) {

var stripped = strng.replace(/[\(\)\.\-\ ]/g, ''); //strip out acceptable non-numeric characters
	if (isNaN(parseInt(stripped))) {
	   return false; 
	}
	if (!(stripped.length == 10)) {
		return false; 
	} 		
return true;
}	
var whitespace = " \t\n\r";
var validUSPhoneChars = digits + phoneNumberDelimiters;
var digits = "0123456789";
var phoneNumberDelimiters = "()- ";
var validUSPhoneChars = digits + phoneNumberDelimiters;
var digitsInUSPhoneNumber = 10;
var ZIPCodeDelimiters = "-";
var ZIPCodeDelimeters = "-";
var validZIPCodeChars = digits + ZIPCodeDelimiters;
var digitsInZIPCode1 = 5;
var digitsInZIPCode2 = 9;

function getRadioValue(idOrName) {
        var value = null;
        var element = document.getElementById(idOrName);
        var radioGroupName = null;  
        
        // if null, then the id must be the radio group name
        if (element == null) {
                radioGroupName = idOrName;
        } else {
                radioGroupName = element.name;     
        }
        if (radioGroupName == null) {
                return null;
        }
        var radios = document.getElementsByTagName('input');
        for (var i=0; i<radios.length; i++) {
                var input = radios[ i ];    
				if (input.type == 'radio' && input.name == radioGroupName && input.checked) {                          
						value = input.value;
                        break;
                }
        }
        return value;
}

function validateProJoin(form) {
  error = false;

  var email = $('#email').val();
  var program = $('#theprogram').val();
  var name = $('#name').val();
  var vemail = validateEmail(email);
  if((vemail == true) || email == '' || name == '' || program == "-1") {
      error = true;
  }
  if(!error) {
    name = Url.encode(name);
    var rand=Math.random()*5;
    rand=Math.floor(rand);

       $.ajax({
           type: "POST",
           url: '/ajax/dupemail.php',
           data: "email="+email+"&rand="+rand,
           success: function(data)
           {
//               alert(data);
//               return false;
              var res = data;
	      if (res == 'NODUP')
              {
                   $.ajax({
                       type: "POST",
                       url: '/process/prosignup.php',
                       data: 'n='+name+'&e='+email+"&p="+program,
                       success: function(data)
                       {
                           window.location.href = 'http://trisystem.com/youthree';
                       },
                       cache : false,
                       async : false
                    });
              }
              else
	      {
                  $('#joinerrorduplicate').dialog('open');
	      }
           },
           cache : false,
           async : false
        });
  }
  else {
      $('#joinerror').dialog('open');
  }
}

function validateEmail(fld) {
    var error = false;
    var filter = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
    if (!filter.test(fld)) {
        error = true;
    }
    return error;
}

function forgotpassword()
{
  var error = false;
  var un = $('#remail').val();
  var vemail = validateEmail(un);
  if((vemail == true) || un == '' ) {
      error = true;
  }

  if(error)
  {
      $('#remail').css('border','1px solid #ff6666');
      return false;
  }
  else
  {
    var rand=Math.random()*5;
    rand=Math.floor(rand);
       $.ajax({
           type: "POST",
           url: '/ajax/forgotpassword.php',
           data: "rand="+rand+"&un="+un,
           success: function(data)
           {
              //alert(data);
	      var res = data;
              if(res == 'OK')
	      {
                  $('#recoverpassword').html('');
                  $('#recoverpassword').html('Password sent to ' + un + '.');
              }
              else
	      {
                  $('#recoverpassword').html('');
                  $('#recoverpassword').html('That email was not found on our system.');
              }
           },
           cache : false,
           async : false
        });
  }
}

function trylogintwo() {

  var error = false;
  var un = $('#un').val();
  var pw = $('#pass').val();
  var vemail = validateEmail(un);
  if((vemail == true) || un == '' || pw == '' ) {
      error = true;
  }

  if(error == true)
  {
    $('#loginerror').dialog('open');
  }
  else
  {
    var rand=Math.random()*5;
    rand=Math.floor(rand);

       $.ajax({
           type: "POST",
           url: '/ajax/login2.php',
           data: "rand="+rand+"&un="+un+"&pw="+pw,
           success: function(data)
           {
//               alert(data);
              //return false;
              var res = data.split('~');
              //alert('http://web.trisystem.com/process/login.php?login=' + res[1]);
//              return false;
              if (res[0] == 'OK')
              {
                window.location.href = 'http://web.trisystem.com/process/login.php?login=' + res[1];
              }
              else if (res[0] == 'NEEDS')
              {
                window.location.href = 'http://trisystem.com/notactivated?e=' + un;
              }
              else if (res[0] == 'MISSED')
              {
                $('#loginerror').dialog('open');
              }
           },
           cache : false,
           async : false
        });
  }
}


function validateJoin(form,emphasis) {
  error = false;

  var thepro = $('#thepros').val();
  var email = $('#email').val();
  var name = $('#name').val();
  var vemail = validateEmail(email);
  if((vemail == true) || email == '' || name == '' ) {
      error = true;
  }
  if(!error) {
    name = Url.encode(name);
    var rand=Math.random()*5;
    rand=Math.floor(rand);

       $.ajax({
           type: "POST",
           url: '/ajax/dupemail.php',
           data: "email="+email+"&rand="+rand,
           success: function(data)
           {
//               alert(data);
//               return false;
              var res = data;
	      if (res == 'NODUP')
              {
                   $.ajax({
                       type: "POST",
                       url: '/process/signup.php',
                       data: 'n='+name+'&e='+email+'&t='+emphasis+'&p='+thepro,
                       success: function(data)
                       {
                              window.location.href = 'http://trisystem.com/youtwo';
                      
                       },
                       cache : false,
                       async : false
                    });
              }
              else
	      {
                  $('#joinerrorduplicate').dialog('open');
	      }
           },
           cache : false,
           async : false
        });
  }
  else {
      $('#joinerror').dialog('open');
  }
}

function activateWeb()
{
  $('bv').hide();
  var Obj = {};
  Obj.error = false;
  var valid = new Validate();
  Obj.verify = $('myaccount').getValue(); 
  if(valid.isEmpty(Obj.verify)) {
    $('bv').show();
	$('bv').innerHTML = "<span style='color:#ff6666;'>Please enter the verification number above.</span>";
    return false;    
  }
  else 
  {
    var rand=Math.random()*5;
    rand=Math.floor(rand);
	Obj.u = new Ajax.Request('/ajax/verify.php',
    {
	        method:'get',
            parameters: {rand:rand,verify:Obj.verify},
	    onSuccess: function(transport)
	    {
	          var res = transport.responseText || "638"; 

		  if (res == 'OK') 
                  {
                    $('confirmed').show();
		  }
		  else if (res == 'GOLOGIN') 
                  {
                    window.location.href = '/memberprofile';
		  }
                  else
		  {
                    $('bv').show();
	            $('bv').innerHTML = "<span style='color:#ff6666;'>The numbers do not match, re-enter.</span>";
		  }
	    },
	        onFailure: function(){ Obj.error = true; }
	});
  }
}

function trylogin() {
  $('error').hide();
  $('bun').hide();
  $('bpw').hide();

  var valid = new Validate();
  var Obj = {};
  Obj.error = false;
  Obj.un = $('un').getValue(); 
  Obj.pw = $('pass').getValue(); 
  if(valid.isEmpty(Obj.un) || !valid.isEmail(Obj.un)) {
    $('bun').show();
	$('bun').innerHTML = "<span style='color:#ff6666;'>email missing or invalid.</span>";
    Obj.error = true;
  }
  if(valid.isEmpty(Obj.pw)) {
    $('bpw').show();
	$('bpw').innerHTML = "<span style='color:#ff6666;'>password required.</span>";
    Obj.error = true;
  }
  if(Obj.error)
  {
    return false;
  }
  else 
  {
    var rand=Math.random()*5;
    rand=Math.floor(rand);
	Obj.u = new Ajax.Request('/ajax/login.php',
    {
	        method:'get',
            parameters: {rand:rand,un:Obj.un,pw:Obj.pw},
	        onSuccess: function(transport)
			{
	          var res = transport.responseText || "638"; 
			  if (res == 'OK') 
              {
                window.location.href = '/memberprofile';
			  }
			  else if (res == 'MISSED') 
              {
                $('error').show();
			  }
			  else if (res == 'NEEDS') 
              {
                window.location.href = '/activatenow';
			  }
			},
	        onFailure: function(){ Obj.error = true; }
	});
  }
}
 


