/* Scripts for GreenFuji.com */


/* echck(str) checks an email address to confirm it's an email address */

   function echeck(str) {
	var at="@"; 
	var dot=".";
	var lat=str.indexOf(at);
	var lstr=str.length;
	var ldot=str.indexOf(dot);

		if (str.indexOf(at)==-1){
			alert("Invalid E-mail ID");
			return false;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
			alert("Invalid E-mail ID");
			return false;
		}
																
		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
			alert("Invalid E-mail ID");
			return false;
		}
																
		if (str.indexOf(at,(lat+1))!=-1){
			alert("Invalid E-mail ID");
			return false;
		}
																
		if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){ 
			alert("Invalid E-mail ID");
			return false;
		}	

		if (str.indexOf(dot,(lat+2))==-1){
			alert("Invalid E-mail ID");
			return false;
		}
		
		if (str.indexOf(" ")!=-1){
			alert("Invalid E-mail ID");
			return false;
		}

	return true					
}
									
				
function urlencode(str) {
return escape(str).replace(/\+/g,'%2B').replace(/ /g, '+').replace(/\*/g, '%2A').replace(/\//g, '%2F').replace(/@/g, '%40');
}

function urldecode(str) {
return unescape(str.replace('+', ' '));
}


/* joine_e(){ checks em1 and em2 to make sure they are equal, then confirms em2 is in email format user@domain.com then it hides the form if true and loads mysubscirbe.php in the page we will 
   send the other fields through to mysubscribe.php to it can add everything to the mySQL db and the mailing list as well as a message to greenfuji anouncing that someone has joined          */

   function join_e(what){

	f = document.forms[0];

	e1 = f.Email.value;
	e2 = f.Email2.value;
	if( (what) && what != '' && what == 'special' ){
		
		// if ref = special -- referrer is special-promotions page so we know what vars we need
		fname = f.Full_Name.value;
		coments = f.Comments.value;
		promo = f.Promo.value

		fname = urlencode( fname );
		coments = urlencode( coments );
		
		fn = fname;
		
		if( !fn || fn == '' ){
			alert("You must enter your full name in the field provided to be added to the Special Promotions list");
			return false;
		}
		
		if( e1 != e2 ){
			alert("Your Email Addresses Do Not Match. Please Correct!");
			return false;
			}
		else{
			if( echeck(e2) === true ){
				email = e2.replace("@","_at_");
				fname = fname.replace(" ","%20");
				coments = coments.replace(/ /g,"%20");
				promo = promo.replace(/ /g,"%20");
				
				$('#sub_div').hide('slow');
				//$('#subscribe').load('mysubscribe.php?what=special&email='+email+'&fname='+fname+'&coments='+coments);	// add them to the mailing list
				$('#subscribe2').html("<br><br><br><br><p>We've added " + email + " to the Special Promotions List</p><p>You should receive a confirmation e-mail shortly.</p><p>Thank you.</p><br><br><br><br><br>");
				$('#subscribe').load('add-to-list.php?what=special&email='+email+'&fname='+fname+'&coments='+coments+'&promo='+promo);	// add them to the mysql db

				//$('#subscribe').load('mysubscribe.php?what=special&email='+email+'&fname='+fname+'&coments='+coments);	// add them to the mailing list
				
			}
		}
		
	}
	
	else if( ref == 'contact' ){
		// if ref = contact -- referer is contact-us.html so we can get our vars
		
		// add vars
		}
		
	else{ return false; }
															
   }												
