//	common.js


//	Close the first menu view instructional box, and update db to not open the instructional box next time
//
function set_first_menu_view_closed(req_tsacctid) {
   if( req_tsacctid ) {
      jQuery("#menu_first_time").hide();
      var script = "set_first_menu_view_closed_ajax.php";
      var str = 'tsacctid=' + req_tsacctid;
      jQuery.getJSON(script + '?' +  str + '&jsoncallback=?', set_first_menu_view_closed_ajax_response);
   }
}
//
function set_first_menu_view_closed_ajax_response(data){
   if(data){
      // nothing to do for this (box already hidden, ajax updates a flag to it remains hidden next time)
   }
}


function signup_page_1_validate_form( invite_required ){
   var result		= true;
   var errors		= '';
   var email		= document.signup_form.email.value;
   var password		= document.signup_form.password.value;
   var password2	= document.signup_form.password2.value;
   var i_agree		= document.signup_form.i_agree.checked;

   errors		+= validate_email_field( email );
   errors		+= validate_password_field( password, password2 );

   if ( !i_agree ) {
      errors		+= "Please review and confirm that you Agree with the TwitSweeper Terms of Service.\n\n";
   }

   if ( errors.length > 0 ) {
      alert('OOPS....\n\n'+errors+'Thank you.');
      result		= false;
   }
   return result;
}


function validate_settings_form(){  
   var result		= true;
   var errors		= '';
   var email		= jQuery('#email').val();
   var password		= jQuery('#password').val();
   var password2	= jQuery('#password2').val();

   errors		+= validate_email_field( email );
   errors		+= validate_password_field( password, password2 );

   if ( errors.length > 0 ) {
      alert('OOPS....\n\n'+errors+'Thank you.');
      result = false;
   }
   if(!result){
      return false;
   }
}


function validate_password_field( password, password2 ) {
   var errors		= '';
   var min_password_len	= 8;
   var max_password_len	= 20;
   if ( password.length < min_password_len ) {
      errors		+= "The Password is too short. Please enter a Password that is at least "+min_password_len+" characters long (using any combination of mixed-case letters, numbers and hyphen).\n\n";
   }
   if ( password.length > max_password_len ) {
      errors		+= "The entered Password is too long. Please enter a Password that is not more than "+max_password_len+" characters long (using any combination of mixed-case letters, numbers and hyphen).\n\n";
   }
   if ( password.length > 0 && password != password2 ) {
      errors		+= "Password and Confirm Password must be the same.\n\n";
   }
   return errors;
} // end of validate_password_field


function validate_email_field( email ) {
   var errors		= '';
   if ( email.length < 1 ) {
      errors		+= "Please enter your Email Address. It will be your TwitSweeper Login ID.\n\n";
   } else {
      if ( !e2_check_email_validity(email) ) {
         errors		+= "Invalid Email Address of '"+email+"'. Please enter a valid Email Address. It will be your TwitSweeper Login ID.\n\n";
      }
   }
   return errors;
} // end of validate_email_field


function e2_check_email_validity(str){
   var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i;
   if (filter.test(str))
      return true;
   else{
      return false;
   }
}


function show_pricing_in_popup( base_href ){
   var tmpwidth		= 1000;
   var tmpheight	= 375;
   var availWidth	= screen.availWidth;
   if(availWidth < 1) {
      availWidth	= screen.width;
   }
   if(availWidth > 0 && tmpwidth > availWidth) {
      tmpwidth		= availWidth - 20;
   }
   var availHeight	= screen.availHeight;
   if(availHeight < 1) {
      availHeight	= screen.height;
   }
   if(availHeight > 0 && tmpwidth > availHeight) {
      tmpheight		= availHeight - 20;
   }
   if ( tmpwidth < 100 ) {
      tmpwidth = 100;
   }
   if ( tmpheight < 100 ) {
      tmpheight = 100;
   }
   if ( tmpwidth > 1100 ) {
      tmpwidth = 1100;
   }
   if ( tmpheight > 400 ) {
      tmpheight = 400;
   }
   features = 'width=' + tmpwidth + ',height=' + tmpheight + ',top=20,left=20,' + 'toolbar=no,scrollbars=yes,location=no,status=yes,menubar=no,resizable=yes';
   url = base_href + 'pricing_in_popup.php';
   window_name	= 'pricing_window';
   result = window.open(url,window_name,features);
   result.focus();
}


function show_terms( base_href ){
   var tmpwidth = (screen.availWidth - 40);
   var tmpheight = parseInt(screen.availHeight * .9);
   if ( tmpwidth < 100 ) {
      tmpwidth = (screen.width - 20);
   }
   if ( tmpheight < 100 ) {
      tmpheight = parseInt(screen.height * .9);
   }
   if ( tmpwidth < 100 ) {
      tmpwidth = 760;
   }
   if ( tmpheight < 100 ) {
      tmpheight = 460;
   }
   if ( tmpwidth > 1000 ) {
      tmpwidth = 1000;
   }
   if ( tmpheight > 1000 ) {
      tmpheight = 1000;
   }
   features = 'width=' + tmpwidth + ',height=' + tmpheight + ',top=5,left=10,' + 'toolbar=no,scrollbars=yes,location=no,status=yes,menubar=no,resizable=yes';
   url = base_href + 'terms_in_popup.php';
   window_name	= 'terms_window';
   result = window.open(url,window_name,features);
   result.focus();
}

function get_state_options(country_code, selected_state){
   var stateData;
   if(country_code == 'US'){
      stateData = new Array ({
         data: 'AK',
         label: 'Alaska'
      }, {
         data: 'AL',
         label: 'Alabama'
      }, {
         data: 'AR',
         label: 'Arkansas'
      }, {
         data: 'AZ',
         label: 'Arizona'
      }, {
         data: 'CA',
         label: 'California'
      }, {
         data: 'CO',
         label: 'Colorado'
      }, {
         data: 'CT',
         label: 'Connecticut'
      }, {
         data: 'DE',
         label: 'Delaware'
      }, {
         data: 'DC',
         label: 'District of Columbia'
      }, {
         data: 'FL',
         label: 'Florida'
      }, {
         data: 'GA',
         label: 'Georgia'
      }, {
         data: 'HI',
         label: 'Hawaii'
      }, {
         data: 'IA',
         label: 'Iowa'
      }, {
         data: 'ID',
         label: 'Idaho'
      }, {
         data:'IL',
         label: 'Illinois'
      }, {
         data: 'IN',
         label: 'Indiana'
      }, {
         data: 'KS',
         label: 'Kansas'
      }, {
         data: 'KY',
         label: 'Kentucky'
      }, {
         data: 'LA',
         label: 'Louisiana'
      }, {
         data: 'MA',
         label: 'Massachusetts'
      }, {
         data: 'MD',
         label: 'Maryland'
      }, {
         data:'ME',
         label: 'Maine'
      }, {
         data: 'MI',
         label: 'Michigan'
      }, {
         data: 'MN',
         label: 'Minnesota'
      }, {
         data: 'MS',
         label: 'Mississippi'
      }, {
         data: 'MO',
         label: 'Missouri'
      }, {
         data: 'MT',
         label: 'Montana'
      }, {
         data: 'NC',
         label: 'North Carolina'
      }, {
         data:'ND',
         label: 'North Dakota'
      }, {
         data: 'NE',
         label: 'Nebraska'
      }, {
         data: 'NH',
         label: 'New Hampshire'
      }, {
         data: 'NJ',
         label: 'New Jersey'
      }, {
         data: 'NM',
         label: 'New Mexico'
      }, {
         data: 'NV',
         label: 'Nevada'
      }, {
         data: 'NY',
         label:'New York'
      }, {
         data: 'OH',
         label: 'Ohio'
      }, {
         data: 'OK',
         label: 'Oklahoma'
      }, {
         data: 'OR',
         label: 'Oregon'
      }, {
         data: 'PA',
         label: 'Pennsylvania'
      }, {
         data: 'RI',
         label: 'Rhode Island'
      }, {
         data: 'SC',
         label: 'South Carolina'
      }, {
         data:'SD',
         label: 'South Dakota'
      }, {
         data: 'TN',
         label: 'Tennessee'
      }, {
         data: 'TX',
         label: 'Texas'
      }, {
         data: 'UT',
         label: 'Utah'
      }, {
         data: 'VA',
         label: 'Virginia'
      }, {
         data: 'VT',
         label: 'Vermont'
      }, {
         data: 'WA',
         label: 'Washington'
      }, {
         data:'WI',
         label: 'Wisconsin'
      }, {
         data: 'WV',
         label: 'West Virginia'
      }, {
         data: 'WY',
         label: 'Wyoming'
      } );
   }
   else if(country_code == 'CA'){
      stateData = new Array({
         data: 'AB',
         label: 'Alberta'
      }, {
         data: 'BC',
         label: 'British Columbia'
      }, {
         data: 'MB',
         label: 'Manitoba'
      },{
         data: 'NB',
         label: 'New Brunswick'
      },{
         data: 'NF',
         label: 'New Foundland'
      },{
         data: 'NT',
         label: 'Northwest Territories'
      },{
         data: 'NU',
         label: 'Nunavut'
      },{
         data: 'NS',
         label: 'Nova Scotia'
      },{
         data: 'ON',
         label: 'Ontario'
      },{
         data: 'PE',
         label: 'Prince Edward Island'
      },{
         data: 'PQ',
         label: 'Quebec'
      },{
         data: 'SK',
         label: 'Saskatchewan'
      },{
         data: 'YT',
         label: 'Yukon Territories'
      });

   }
   if(stateData){
      var html = '';
      $(stateData).each(function(){
         if(this.data != selected_state){
            html += "<option value='"+this.data+"'>"+this.label+"</option>";
         }else{
            html += "<option value='"+this.data+"' selected>"+this.label+"</option>";
         }
      });
      return html;
   }
   return false;
}
