// Client Side Helper Functions - 9/02/09

function checksubmitdata(){
var coform = document.signupform;

if(coform.newusername.value.length<=5){
alert("You must choose a longer username.");
coform.newusername.focus();
return false;
}

if(coform.newusername.value.length>=17){
alert("You must choose a shorter username.");
coform.newusername.focus();
return false;
}

if(coform.newpassword.value.length<=8){
alert("You must choose a longer password.");
coform.newpassword.focus();
return false;
}
if(coform.newpassword.value.length>=10){
alert("You must choose a shorter password.");
coform.newpassword.focus();
return false;
}

if(coform.newemail.value.length<=0){
alert("You must enter your email address.");
coform.newemail.focus();
return false;
}
else{
if(validemail(coform.newemail.value)==false){
alert("The email address you entered is invalid.");
coform.newemail.focus();
coform.newemail.select();
return false;
}
}

if(coform.newemail.value != coform.newemail2.value){
alert("Email and Email Confirm not matching, please re-type your email address.");
coform.newemail2.focus();
coform.newemail2.select();
return false;
}

return true;
}


function checkforgotpassword(){
var coform = document.fp;

if(coform.myvalue11fp.value.length<=5 && coform.myvalue55fp.value.length==0){
alert("You must enter a longer username.");
coform.myvalue11fp.focus();
return false;
}
if(coform.myvalue11fp.value.length>=21 && coform.myvalue55fp.value.length==0){
alert("You must shorter a shorter username.");
coform.myvalue11fp.focus();
return false;
}
if(coform.myvalue55fp.value.length>0){
if(validemail(coform.myvalue55fp.value)==false){
alert("The email address you entered is invalid.");
coform.myvalue55fp.focus();
coform.myvalue55fp.select();
return false;
}
}

return true;
}


function checknewpassword(){
var coform = document.cp;
var space = " ";

if(coform.myvalue11cp.value.length<=3){
alert("Oops, username too short.");
coform.myvalue11cp.focus();
return false;
}

if(coform.myvalue11cp.value.length>=21){
alert("Oops, username too long.");
coform.myvalue11cp.focus();
return false;
}

if(coform.myvalue22.value.length<1){
alert("Oops, please enter your old password.");
coform.myvalue22.focus();
return false;
}

if(coform.myvalue23.value.length<7){
alert("Oops, you must choose a longer password.");
coform.myvalue23.focus();
return false;
}
if (coform.myvalue23.value.indexOf(space) > -1) {
alert("Oops, passwords cannot contain a space.");
coform.myvalue23.focus();
return false;
}  
if(coform.myvalue23.value.match(/[^a-zA-Z0-9\#\@\%\!\.\,\_\-]/)){
alert("Oops, you entered an invalid character. Special characters you can use are # @ % ! . , _ -");
coform.myvalue23.focus();
return false;
}

if(coform.myvalue23.value.length>30){
alert("Oops, you must choose a shorter password.");
coform.myvalue23.focus();
return false;
}
if(coform.myvalue23.value != coform.myvalue24.value){
alert("Oops, new password doesn't match the confirm password.");
coform.myvalue23.focus();
return false;
}

return true;
}

function checkpaymentdata(){
var coform = document.paymentform;

if(coform.fname.value.length<=1){
alert("You must enter a longer first name.");
coform.fname.focus();
return false;
}

if(coform.fname.value.length>=32){
alert("You must enter a shorter first name.");
coform.fname.focus();
return false;
}

if(coform.lname.value.length<=1){
alert("You must enter a longer last name.");
coform.lname.focus();
return false;
}

if(coform.lname.value.length>=32){
alert("You must enter a shorter last name.");
coform.lname.focus();
return false;
}

if((coform.ccn.value.length==0) || (coform.ccn.value.length>=18)){
alert("You must enter a valid card number.");
coform.ccn.focus();
return false;
}
else{
// make sure the number is all digits
for( var i = 0; i < coform.ccn.value.length; ++i ) {
var c = coform.ccn.value.charAt(i);
if( c < '0' || c > '9' ) {
alert("Please enter a valid card number. Use only digits, no spaces or hyphens.");
return false;
}
}
}

if((coform.ccv.value.length==0) || (coform.ccv.value.length>5)){
alert("You must enter a valid CCV.");
coform.ccv.focus();
return false;
}
else{
// make sure the number is all digits
for( var i = 0; i < coform.ccv.value.length; ++i ) {
var c = coform.ccv.value.charAt(i);
if( c < '0' || c > '9' ) {
alert("Please enter a valid CCV. Use only digits, no spaces or hyphens.");
return false;
}
}
}

if((coform.cce.value.length==0) || (coform.cce.value.length<5)){
alert("You must enter a valid card expiration date.");
coform.cce.focus();
return false;
}
else{
// make sure the CC EXPIRATION DATE is properly formated as MM/YY
// except for DEMO VERSION SHOULD ACCEPT "MM/YY"
if (coform.cce.value == "MM/YY") {
}
else{
var ArrayCCexpr=coform.cce.value.split("/");
if ((ArrayCCexpr.length!=2) ||
(ArrayCCexpr[0]=="") ||
(isNaN(ArrayCCexpr[0])) ||
(ArrayCCexpr[1]=="") ||
(isNaN(ArrayCCexpr[1])) ||
(ArrayCCexpr[0]<1) ||
(ArrayCCexpr[0]>12)) {

alert("Please enter a valid card expiration date, MM/YY.");
coform.cce.focus();
return false;
}
}
}

// OPTIONAL FIELDS NEED NO VERIFICATION AT THIS TIME...
// address1, city1, state1, zip1, country1, phone

return true;
}

function validemail(emailin){
emailin+="";
if(emailin.length==0){return false;}

var position=emailin.indexOf("@");
if(position<1){return false;}

position=emailin.indexOf(".",position+1);
if(position<3){return false;}

if(position==emailin.length-1){return false;}

return true;
}

function new_random_password(){
var coform = document.signupform;
// define possible characters
var possible = "23456789bcdfghjkmnpqrstvwyz"; 
// define desired length of password 
var length = 9;
var password = '';
var i = 0; 
var char = '';
var index1 = '';
var index2 = '';
var rand_no = '';
// add random characters to password until length is reached
while (i < length) { 
rand_no = Math.floor(possible.length*Math.random());  //make a random number 0-9
// pick a random character from the possible ones
char = possible.substr(rand_no, 1);
// we don't want this character if it's already in the password
if (password.indexOf(char) < 0){ 
password = password + char;
i=i+1;
}
char = '';
index1 = '';
index2 = '';
}
coform.password.value = password;
}

function IE_copy_to_my_clipboard(){
var coform = document.signupform;
var mytext = '';
mytext = coform.newpassword.value;
if(window.clipboardData)
{
window.clipboardData.setData("Text", mytext);
}
coform.newpassword.focus();
coform.newpassword.select();
}

function Add_To_Favorites(){
var url = "http://www.mygirlnextdoor.com/member"; 
var title = "My Girl Next Door Member Homepage"; 
if ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4)) {
 window.external.AddFavorite(url,title);
}
else if (navigator.appName == "Netscape") {
 window.sidebar.addPanel(title,url,"");
}
else {
 alert("Press CTRL-D (Netscape) or CTRL-T (Opera) to bookmark");
}
}

function makelowercase_alphanumeric(){
var coform = document.signupform;
coform.username.value = coform.username.value.toLowerCase();
coform.username.value = coform.username.value.replace(/[^a-z0-9]/g, '');
}

function checkSupportForm() {
while (true) {
if (!document.support.subject.value) {
 alert('Please choose a subject!');
 document.support.subject.focus();
 break;
}
if (!document.support.email.value) { 
 alert('Please fill in your email address!');
 document.support.email.focus();
 break;
}
else {
 re = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
 if (!document.support.email.value.match(re)) {
  alert('You did not supply a valid email address!');
  document.support.email.focus();
  break;
 }
}
if (document.support.subject.value > 0 && document.support.subject.value < 3 && !document.support.user.value) {
 alert('Please supply a user/login name!');
 document.support.user.focus();
 break;
}
if (!document.support.message.value) {
 alert('You forgot to write a message!');
 document.support.message.focus();
 break;
}
document.support.submit();
break;
}
}

// 9.2.09
function chk_un_db(){
var http=false;
if(navigator.appName=="Microsoft Internet Explorer"){
 http=new ActiveXObject("Microsoft.XMLHTTP");
}else{
 http=new XMLHttpRequest();
}
var regexp='';
var r='';
var a=document.signupform.username.value;
var len=document.signupform.username.value.length;
if(len>0){
//<img src="../img/chk-load.gif" class="chkimg" width="16" height="16" border="0" alt="Checking Username Availability..." title="Checking availability of your username..." />
document.getElementById('chkresults').innerHTML='';
http.abort();
http.open("GET", "../check-un-db.php?a=" + a, true);
http.onreadystatechange=function() {
 if(http.readyState==4) {
  r=http.responseText;
  document.getElementById('chkresults').innerHTML=r;
  regexp = /OOPS/;
  if (r.match(regexp)){
   document.getElementById('username').style.fontWeight="normal";
   document.getElementById('usernamerequirements').style.color="#f00";
  }
  regexp = /SUCCESS/;
  if (r.match(regexp)){
   document.getElementById('username').style.fontWeight="bold";
   document.getElementById('username').style.color="#000";
   document.getElementById('usernamerequirements').style.color="#000";
  }
  regexp = /FAIL/;
  if (r.match(regexp)){
   document.getElementById('username').style.fontWeight="bold";
   document.getElementById('username').style.color="#f00";
   document.getElementById('usernamerequirements').style.color="#000";
  }
}
}
http.send(null);
}
}

function checksubmitdatafree(){
var coform = document.signupform;

if(coform.newusername.value.length<=5){
alert("You must choose a longer username.");
coform.newusername.focus();
return false;
}

if(coform.newusername.value.length>=17){
alert("You must choose a shorter username.");
coform.newusername.focus();
return false;
}

if(coform.newpassword.value.length<=8){
alert("You must choose a longer password.");
coform.newpassword.focus();
return false;
}
if(coform.newpassword.value.length>=10){
alert("You must choose a shorter password.");
coform.newpassword.focus();
return false;
}

if(coform.newemail.value.length<=0){
alert("You must enter your email address.");
coform.newemail.focus();
return false;
}
else{
if(validemail(coform.newemail.value)==false){
alert("The email address you entered is invalid.");
coform.newemail.focus();
coform.newemail.select();
return false;
}
}

if(coform.newemail.value != coform.newemail2.value){
alert("Email and Email Confirm not matching, please re-type your email address.");
coform.newemail2.focus();
  coform.newemail2.select();
return false;
}

if(coform.product.value == ''){
alert("Please select your choice for Account Type.");
coform.product.focus();
return false;
}

if(coform.firstname.value.length<2){
alert("Please enter your first name.");
coform.firstname.focus();
return false;
}

if(coform.lastname.value.length<2){
alert("Please enter your last name.");
coform.lastname.focus();
return false;
}

if(coform.postalcode.value.length<2){
alert("Please enter your Zip / Postal Code.");
coform.postalcode.focus();
return false;
}

if(coform.country.value == ''){
alert("Please select your country from the list.");
coform.country.focus();
return false;
}

return true;
}

function checkverificationdata(){
var coform = document.signupform;
return true;
}

