var $j = jQuery.noConflict();

$j(document).ready(function() {
						   
//When you click on a link with class of poplight and the href starts with a # 
	$j('a.poplight[href^=#]').click(function() {
        var popID = $j(this).attr('rel'); //Get Popup Name
        var popURL = $j(this).attr('href'); //Get Popup href to define size
    
        //Pull Query & Variables from href URL
        var query= popURL.split('?');
        var dim= query[1].split('&');
        var popWidth = dim[0].split('=')[1]; //Gets the first query string value
    
        //Fade in the Popup and add close button
        $j('#' + popID).fadeIn().css({ 'width': Number( popWidth ) }).prepend('<a href="#" class="close"><img src="modx/assets/images/close_pop.png" class="btn_close" title="Close Window" alt="Close" /></a>');
    
        //Define margin for center alignment (vertical   horizontal) - we add 80px to the height/width to accomodate for the padding  and border width defined in the css
        var popMargTop = ($j('#' + popID).height() + 80) / 2;
        var popMargLeft = ($j('#' + popID).width() + 80) / 2;
    
        //Apply Margin to Popup
        $j('#' + popID).css({
            'margin-top' : -popMargTop,
            'margin-left' : -popMargLeft
        });
    
        //Fade in Background
        $j('body').append('<div id="fade"></div>'); //Add the fade layer to bottom of the body tag.
        $j('#fade').css({'filter' : 'alpha(opacity=80)'}).fadeIn(); //Fade in the fade layer - .css({'filter' : 'alpha(opacity=80)'}) is used to fix the IE Bug on fading transparencies 
    
        return false;
    });
    
    //Close Popups and Fade Layer
    $j('a.close, #fade').live('click', function() { //When clicking on the close or fade layer...
        $j('#fade , .popup_block').fadeOut(function() {
            $j('#fade, a.close').remove();  //fade them both out
        });
        return false;
    });
    
    
    $j('.error').hide();
    
    $j('#emailPage').click(function(){
    	
		// validate and process form here
		
		$j('.error').hide();
		var name = $j("input#name").val();
		if (name == "") {
			$j("span#name_error").show();
			$j("input#name").focus();
		    return false;
		}
		var email = $j("input#email").val();
		if (email == "") {
			$j("span#email_error").show();
			$j("input#email").focus();
		    return false;
		}
		if (!validateEmail(email)) {
			$j("span#email_error_invalid").show();
			$j("input#email").focus();
		    return false;
		}
		var toemail = $j("input#toemail").val();
		if (toemail == "") {
			$j("span#toemail_error").show();
			$j("input#toemail").focus();
		    return false;
		}
		if (!validateEmail(toemail)) {
			$j("span#toemail_error_invalid").show();
			$j("input#toemail").focus();
		    return false;
		}
		var subject = $j("textarea#subject").val();
		var message = $j("textarea#message").val();

		var pathname = window.location.pathname; 
    	var dataString = 'name='+ name +
    		'&email=' + email + 
    		'&toemail=' + toemail +
    		'&subject=' + subject + 
    		'&message=' + message + 
    		'&url=' + pathname;
   
	    //alert (dataString);return false;
	    $j.ajax({
	      type: "POST",
	      url: "modx/assets/functions/emailPage.php",
	      data: dataString,
	      datatype: "xml",
	      error: function (XMLHttpRequest, textStatus, errorThrown){
	    	$j('#tellafriend_form').html("<div id='message'></div>");
	        $j('#message').html("<h2>Email send failed. "+textStatus + "<br>Please try again.</h2>");
		  },
		  complete: function(xml){
			var rtn = $j(xml.responseXML).find('rtn').text();
			if (rtn != 'OK'){
				if (rtn == 'Errors Found') {
					displaymsg = "<div style='border:1px solid black;font-weight:bold;color:Red;padding:5px;'>Please correct the following errors:\n<ul>";					
					$j(xml.responseXML).find('error').each(function() { 
						displaymsg = displaymsg + '<li>'+$j(this).text()+'</li>\n';
					});
					displaymsg = displaymsg + '</ul></div>';
					$j('#msgLine').html(displaymsg); 
				} else {
					displaymsg = "<div style='font-weight:bold;color:Red;padding:5px;'>\n";					
					displaymsg = displaymsg + rtn +'\n';
					displaymsg = displaymsg + '</div>';
					$j('#msgLine').html(displaymsg); 
				}
			}else{	
		        $j('#tellafriend_form').html("<div id='message'></div>");
		        $j('#message').html("<h2>Email Sent!</h2>");
			}
	    }});
	    return false;
    });

});
function validateEmail(elementValue){
	   var emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
	   return emailPattern.test(elementValue);
}
function toggleForgotPassword(){
	$j('div#loginDiv').toggle();
	$j('div#forgotPwdDiv').toggle();
	if ($j('#linkText').html()== "Login") {
		$j('#linkText').html("Forgot Password");
	}else {
		$j('#linkText').html("Login");
	}

}
