// JavaScript Document

/*
Check Login
============
take input send back to login.taf (with added arg to tell login.taf to return in xml)

called from
header_1.htm
*/

function checkLogin(email,pass)
{
	
	//alert(email + ' = ' + pass);
	
    var callAjax = 0;
    //var email = $('#login_email_address').val();
    //var pass = $('#login_password').val();
    var postData = 'output=xml';
    if (email != '') {
        postData += "&email="+email;
        callAjax += 1;
	} 
	if (pass != "") {
        postData += "&password="+pass;
        callAjax += 1;
	} 

    if (callAjax == 2) {
		var results = new Array();
        $.ajax({ type: "POST",
              url:  '/login.taf',
              type: 'POST',
			  data: postData,
              dataType: 'xml',
              complete: function(xml,type)
              {
                   if ($(xml.responseXML).find('Status').text() == '1') {
					   //extract info from xml
                       $(xml.responseXML).find('Results').each(function(iter)
                       {
                             results[iter] = $(this).find("Description").text();  
                       });
                   	   //display info on page
					   var htmlContent = '<a href="/myaccount.taf" title="Go to My Account"><strong>'+results[1]+'&rsquo;s</strong> Account</a> &nbsp;<span class="header-logout">(<a href="/logout.taf" title="Log Out of My Account">logout</a>)</span>';
					   $('#signin_text').empty();
					   $('#signin_text').html(htmlContent);
						$.fn.colorbox.close();

                   } else {
					   //extract info from xml
                       $(xml.responseXML).find('Results').each(function(iter)
                       {
                             results[iter] = $(this).find("Description").text();  
                       });
                   	   //display info on page
   			   		   var htmlContent = '<span style="color:#FF0000;">'+results[0]+'</span>';
					   $('#signin_status').html(htmlContent);
				   }
              }
        });

    }
}

/*
Check Forgot
============
take input send back to failed_login.taf (with added arg to tell failed_login.taf to return in xml)

called from
header_1.htm
*/

function checkEmail(email)
{
	
	//alert(email + ' = ' + pass);
	
    var callAjax = 0;
    //var email = $('#login_email_address').val();
    //var pass = $('#login_password').val();
    var postData = 'output=xml&_function=send';
    if (email != '') {
        postData += "&email_send="+email;
        callAjax += 1;
	} 

    if (callAjax == 1) {
		var results = new Array();
        $.ajax({ type: "POST",
              url:  '/failed_login.taf',
              type: 'POST',
			  data: postData,
              dataType: 'xml',
              complete: function(xml,type)
              {
                   if ($(xml.responseXML).find('Status').text() == '1') {
					   //extract info from xml
                       $(xml.responseXML).find('Results').each(function(iter)
                       {
                             results[iter] = $(this).find("Description").text();  
                       });
                   	   //display info on page
					   var htmlContent = results[0];
					   $('#sendemail_text').empty();
					   $('#sendemail_text').html(htmlContent);

                   } else {
					   //extract info from xml
                       $(xml.responseXML).find('Results').each(function(iter)
                       {
                             results[iter] = $(this).find("Description").text();  
                       });
                   	   //display info on page
					   var htmlContent = results[0];
					   $('#sendemail_text').empty();
					   $('#sendemail_text').html(htmlContent).colorbox.resize();

				   }
              }
        });

    }
}

//email validation function 
function checkEmail(email) {
	var filter = /^([a-zA-Z0-9_.-])+@(([a-zA-Z0-9-])+.)+([a-zA-Z0-9]{2,4})+$/;
	if (!filter.test(email)) {
		return false;
	} else {
		return true;
	}
}

//filter out characters to prevent hack attempts
function filterString(string) {
	var patt=/[^a-zA-Z0-9\ !\'\?@\.]+/g;
	return string.replace(patt,"");
}


function trim(stringToTrim) {
	return stringToTrim.replace(/^\s+|\s+$/g,"");
}


/*
Submit Review
============
take input send back to review.taf (with validations checks to return in xml)

called from
detail_eng.inc


*/

function submitReview(id,name,email,title,review)
{
    var callAjax = 0;
	//clear the errors
	$("#write_a_review_nameerror").html("");
	$("#write_a_review_emailerror").html("");
	$("#write_a_review_titleerror").html("");
	$("#write_a_review_reviewerror").html("");
	
	//validate and filter inputs
	//validate name
	if (trim(name) == "") {
		$("#write_a_review_nameerror").html("Required");
		return
	}
	name = filterString(name);
	if (name == "") {
		$("#write_a_review_nameerror").html("Invalid");
		return
	}
	//validate email
	email = filterString(email);
	if (trim(email) == "") {
		$("#write_a_review_emailerror").html("Required");
		return
	}
	if (!checkEmail(email)) {
		$("#write_a_review_emailerror").html("Invalid");
		return
	}
	//filter title
	title = filterString(title);
	if (trim(title) == "") {
		$("#write_a_review_titleerror").html("Required");
		return
	}
	//filter review
	review = filterString(review);
	if (trim(review) == "") {
		$("#write_a_review_reviewerror").html("Required");
		return
	}
	
    //var email = $('#login_email_address').val();
    //var pass = $('#login_password').val();
    var postData = '_function=insertajax';
    if (name != '') {
        postData += "&id="+id;
        postData += "&name="+name;
        postData += "&email="+email;
        postData += "&title="+title;
        postData += "&review="+review;
		
		//alert(postData);
		
        callAjax += 1;
	} 

    if (callAjax == 1) {
		var results = new Array();
        $.ajax({ type: "POST",
              url:  '/review.taf',
              type: 'POST',
			  data: postData,
              dataType: 'xml',
              complete: function(xml,type)
              {
                   if ($(xml.responseXML).find('Status').text() == '1') {
					   //extract info from xml
                       $(xml.responseXML).find('Results').each(function(iter)
                       {
                             results[iter] = $(this).find("Description").text();  
                       });
                   	   //display info on page
					   var htmlContent = results[0];
					   $('#write_a_review').empty();
					   $('#write_a_review').html(htmlContent).colorbox.resize();

                   } else {
					   //extract info from xml
                       $(xml.responseXML).find('Results').each(function(iter)
                       {
                             results[iter] = $(this).find("Description").text();  
                       });
                   	   //display info on page
					   var htmlContent = results[0];
					   $('#write_a_review[error]').empty();
					   $('#write_a_review[error]').html(htmlContent);

				   }
              }
        });

    }
}




