

// 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) {
  var Obj = {}; 
  Obj.error = false; 
  var valid = new Validate(); 
  Obj.email = document.forms[form].email.value; 
  Obj.name = document.forms[form].name.value; 
  Obj.type = $('procat').getValue(); 
  $('bn').hide();
  $('be').hide();
  $('bt').hide();
  if(Obj.type == '-1') {
    $('bt').show();
	$('bt').innerHTML = "required";
    Obj.error = true;
  }
  if(valid.isEmpty(Obj.name)) {
    $('bn').show();
	$('bn').innerHTML = "required";
    Obj.error = true;
  }
  if(!valid.isEmail(Obj.email)) {
    $('be').show();
	$('be').innerHTML = "required";
    Obj.error = true;
  }
  if(!Obj.error) {
    Obj.name = Url.encode(Obj.name);
    var rand=Math.random()*5;
    rand=Math.floor(rand);
	Obj.u = new Ajax.Request('/ajax/dupemail.php',
    {
            asynchronous : false,
	        method:'get',
            parameters: {rand:rand,email:Obj.email},
	        onSuccess: function(transport)
			{
	          var res = transport.responseText || "638"; 
			  if (res == 'NODUP') 
              {
                window.location.href = '/process/processProSignup.php?n='+Obj.name+'&e='+Obj.email+'&t='+Obj.type; 
			  }
              else
			  {
                Obj.dup = true;
			  }
			},
	        onFailure: function(){ Obj.error = true; }
	});
  }
  if(Obj.dup) {
    $('be').show();
	$('be').innerHTML = "duplicate email";
	return false;
  }
}

function validateJoin(form,emphasis) {
  var Obj = {};
  Obj.error = false;
  var valid = new Validate();
  Obj.email = document.forms[form].email.value; 
  Obj.name = document.forms[form].name.value; 
  $('bn').hide();
  $('be').hide();
  if(valid.isEmpty(Obj.name)) {
    $('bn').show();
	$('bn').innerHTML = "required";
    Obj.error = true;
  }
  if(!valid.isEmail(Obj.email)) {
    $('be').show();
	$('be').innerHTML = "email validity";
    Obj.error = true;
  }
  if(!Obj.error) {
    Obj.name = Url.encode(Obj.name);
    var rand=Math.random()*5;
    rand=Math.floor(rand);
	Obj.u = new Ajax.Request('/ajax/dupemail.php',
    {
	        method:'get',
            asynchronous: false,
            parameters: {rand:rand,email:Obj.email},
	        onSuccess: function(transport)
			{
	          var res = transport.responseText || "638"; 
			  if (res == 'NODUP') 
              {
                window.location.href = '/process/processSignup.php?em='+emphasis+'&n='+Obj.name+'&e='+Obj.email;                
			  }
              else
			  {
                Obj.dup = true;
			  }
			},
	        onFailure: function(){ Obj.error = true; }
	});
  }
  if(Obj.dup) {
    $('be').show();
	$('be').innerHTML = "duplicate email";
	return false;
  }
}

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


function forgotpassword()
{
  $('error').hide();
  $('bun').hide();
  $('bpw').hide();
  $('msgfp').hide();

  var valid = new Validate();
  var Obj = {};
  Obj.error = false;
  Obj.un = $('un').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(Obj.error)
  {
    return false;
  }
  else 
  {
    var rand=Math.random()*5;
    rand=Math.floor(rand);
	Obj.u = new Ajax.Request('/ajax/forgotpassword.php',
    {
	        method:'get',
            asynchronous : false,
            parameters: {rand:rand,un:Obj.un},
	        onSuccess: function(transport)
			{
			  var res = transport.responseText || "638"; 
              if(res == 'OK')
			  {	
                $('msgfp').innerHTML = "Password sent to " + Obj.un; 
                $('msgfp').show(); 
              }
              else
			  {	   
                $('msgfp').innerHTML = "Email not found"; 
                $('msgfp').show(); 
              }
			},
	        onFailure: function(){ Obj.error = true; }
	});
  }
}
// xxxxxxxxxxxxxxx top xxxxxxxxxxxxxxxxxxxxxxxxxxxx

/*
function getElement(name)
{
 	if ( document.getElementById )
 	{
		return document.getElementById(name);
	}
	else if ( document.all )
	{
		return document.all[name];
	}
	else if ( document.layers )
	{
		return document[name];
	}
}
*/



function isNumeric(str)
{
  return inValidCharSet(str, ".0123456789");
}

function inValidCharSet(str, charset)
{
	var result = true;
    var i;
	for ( i = 0; i < str.length; i++ )
		if ( charset.indexOf(str.substr(i,1)) < 0 )
		{
			result = false;
			break;
		}

	return result;
}

function trim(s)
{
    if( s.length <= 0 )
    {
        return "";
    }

    var str = s;
    var startIndex = 0;
    var endIndex = 0;

    //find index of starting character
    for( i = 0; i < str.length; i++ )
    {
        if( str.charAt(i) != " " )
        {
            startIndex = i;
            break;
        }
    }

    //find index of ending character
    for(i = str.length - 1; ; i--)
    {
        if(str.charAt(i) != " ")
        {
            endIndex = i + 1;
            break;
        }
    }

    //return str.substring(startIndex, endIndex);
    return trimSpaces(str.substring(startIndex, endIndex));
}

function trimSpaces(s)
{
    var str = s;
    return str.replace(/\s/g, '');
}

function findSpaces(s)
{
    var myRegExp = /\s/g;
    return myRegExp.test(s);
    //return s.search(/\s/g);
}




