var showen = true;
var contactEr = '';

$(document).ready( function(){
   $("#powerImage").click(function () {
	   if(showen) {
           $("#contents").hide();
		   showen = false;
	   } else {
		   $("#contents").show("slow");
		   showen = true;
	   }
   });

   /*$('#contactFrm').ajaxForm(function() { 
            beforeSubmit: validate  
   });*/

    var options = { 
        target:        '#output2',   // target element(s) to be updated with server response 
        beforeSubmit:  showRequest,  // pre-submit callback 
        success:       showResponse  // post-submit callback 
		/*clearForm: true*/
 
        // other available options: 
        //url:       url         // override for form's 'action' attribute 
        //type:      type        // 'get' or 'post', override for form's 'method' attribute 
        //dataType:  null        // 'xml', 'script', or 'json' (expected server response type) 
        //clearForm: true        // clear all form fields after successful submit 
        //resetForm: true        // reset the form after successful submit 
 
        // $.ajax options can be used here too, for example: 
        //timeout:   3000 
    }; 

   /*('#contactFrm').submit(function() { 
        $('#contactFrm').ajaxForm({ 
            // target identifies the element(s) to update with the server response 
            target: '#output2', 
            clearForm: true,
             // success identifies the function to invoke when the server response 
             // has been received; here we apply a fade-in effect to the new content 
             success: function(response) { 
                //$('#output2').fadeIn('slow'); 
			    alert(response);
             } 
        });
        
		return false; 
   });*/
 
    // bind to the form's submit event 
    $('#contactFrm').submit(function() { 
        if(!validateContact()) {
			alert(contactEr);
			return false;
		}

        $('#contactFrm').ajaxSubmit(function(response) {
            $("#output2").html(response);
			$("#output2").show("slow");
			$('#contactFrm').clearForm();
			alert(response);
		});

		return false;
    }); 


});

function isEmpty(myVar){
	if (((myVar==null) || myVar.length==0)){
		return true;
	}else{
		return false;
	}
}

function validateContact() {
	//validate here
	contactEr = '';
	var noError = true;
	var nameInpt = document.getElementById('senderName').value;
	var emInpt = document.getElementById('senderEmail').value;
	var subInpt = document.getElementById('senderSubject').value;
	var mesInpt = document.getElementById('senderMessage').value;
    if(isEmpty(nameInpt)) {
		noError = false;
		contactEr = 'Sender name is required\n'; 
	}
    
	if(isEmpty(emInpt)) {
		noError = false;
		contactEr += 'Email is required.\n'; 
	} else {
		if(!isValidEmail(emInpt)) {
			noError = false;
		    contactEr += 'A valid e-mail address is required.\n';
		}
	}


	if(isEmpty(subInpt)) {
		noError = false;
		contactEr += 'Subject field is required\n'; 
	}

	if(isEmpty(mesInpt)) {
		noError = false;
		contactEr += 'A message is required\n'; 
	}
    
    

	return noError;
}

function isValidEmail(strEmail){
  validRegExp = /^[^@]+@[^@]+.[a-z]{2,}$/i;

   // search email text for regular exp matches
    if (strEmail.search(validRegExp) == -1) 
   {
      return false;
    } 
    return true; 
}


function validateFrm(formData, jqForm, options) { 
 
    for (var i=0; i < formData.length; i++) { 
        if (!formData[i].value) { 
            alert('All fields are required'); 
            return false; 
        } 
    } 
    return true;
}

// pre-submit callback 
function showRequest(formData, jqForm, options) { 
    // formData is an array; here we use $.param to convert it to a string to display it 
    // but the form plugin does this for you automatically when it submits the data 
    //var queryString = $.param(formData); 
 
    // jqForm is a jQuery object encapsulating the form element.  To access the 
    // DOM element for the form do this: 
    // var formElement = jqForm[0]; 
    $("#output2").hide();
    //alert('About to submit: \n\n' + queryString); 
 
    // here we could return false to prevent the form from being submitted; 
    // returning anything other than false will allow the form submit to continue 
    return validateFrm(formData, jqForm, options);
} 
 
// post-submit callback 
/*function showResponse(responseText, statusText)  { 
    // for normal html responses, the first argument to the success callback 
    // is the XMLHttpRequest object's responseText property 
 
    // if the ajaxSubmit method was passed an Options Object with the dataType 
    // property set to 'xml' then the first argument to the success callback 
    // is the XMLHttpRequest object's responseXML property 
 
    // if the ajaxSubmit method was passed an Options Object with the dataType 
    // property set to 'json' then the first argument to the success callback 
    // is the json data object returned by the server 
    $("#output2").show("slow");
    alert('status: ' + statusText + '\n\nresponseText: \n' + responseText + 
        '\n\nThe output div should have already been updated with the responseText.'); 
} 
*/

// post-submit callback
function showResponse(responseText, statusText)  {
    alert('this: ' + this.tagName +
        '\nstatus: ' + statusText +
        '\n\nresponseText: \n' +
        responseText + '\n\nThe output div should have already been updated with the responseText.');
}

function contactSend(){
		$.ajax({
			type: "POST",
			url: "/contact",
			data: 	"data['Contact']['sender_name']=mmmmm",
			success: function(html){
				$("#output2").html(html);
				$("#output2").show("slow");
			}
		});
		
		return false;
		}
