

function sendFindReq(){   
     var form = document.findForm;
     new Ajax.Request('/findlogin',{method:'post', postBody:Form.serialize(form),asynchronous:true,onComplete:showFindResp});   
}


function sendLoginReq(){  
    var form = document.loginForm;
    new Ajax.Request('/login',{method:'post', postBody:Form.serialize(form),asynchronous:true,onComplete:showLoginResp});
}



function submitComment(){
   new Ajax.Request('/isLoggedIn',{method:'post',asynchronous:true,onComplete:function(req){
															var json = req.responseText.evalJSON(); 
															//alert(json.status);
														    if ( json.status == 'yes'){
																document.commentForm.submit();
														    }
														    else{
                                                              $('loginMesg').innerHTML = "<span style='color:red;font-family:arial,helvetica,verdana,sans-serif;font-size:12px;'>Session Timed Out</span>";
														       showLogin();
														    }
									}
                                    });
}


function submitComic(){
   new Ajax.Request('/isLoggedIn',{method:'post',asynchronous:true,onComplete:function(req){
															var json = req.responseText.evalJSON(); 
															//alert(json.status);
														    if ( json.status == 'yes'){
																document.comicForm.submit();
														    }
														    else{
                                                               $('loginMesg').innerHTML = "<span style='color:red;font-family:arial,helvetica,verdana,sans-serif;font-size:12px;'>Session Timed Out</span>";
														       showLogin();
														    }
									}
                                    });
}



function submitHref(href){
   new Ajax.Request('/isLoggedIn',{method:'post',asynchronous:true,onComplete:function(req){
                                           var json = req.responseText.evalJSON(); 
										    //alert(json.status);
										   if ( json.status == 'yes'){
												window.location=href;
										    }
										    else{
                                               $('loginMesg').innerHTML = "<span style='color:red;font-family:arial,helvetica,verdana,sans-serif;font-size:12px;'>Session Timed Out</span>";
										       showLogin();
										    }
                                  }
                                  });
   
   
}


function submitVote(url,tagID){
  new Ajax.Request('/isLoggedIn',{method:'post',asynchronous:true,onComplete:function(req){
                                           var json = req.responseText.evalJSON(); 
										    //alert(json.status);
										    if ( json.status == 'yes'){
												updateVote(url,tagID)										    }
										    else{
                                               $('loginMesg').innerHTML = "<span style='color:red;font-family:arial,helvetica,verdana,sans-serif;font-size:12px;'>Session Timed Out</span>";
										       showLogin();
										    }
                                  }
                                  });
}

function updateVote(url,tagID){
   new Ajax.Request(url,{method:'post',asynchronous:true,onComplete:function(req){                                 
                                           var p = eval("(" + req.responseText + ")");

										    if ( p.message == 'True'){                                              
                                                num = parseInt($('voteNum'+tagID).innerHTML) +1;
                                                $('voteNum'+tagID).innerHTML = '<span style="color:red">'+num+'&#160;votes</span>';								  
                                            }										    
                                  }
                                  });
   
}





function sendReport(id,type){
   msg = 'comic'
   if (type == '1'){
     msg = 'comment'
   }

   //var yes = confirm('Are you sure want to report this ' +msg+ '?');

   //if(yes){
	//	url = '/report/'+id+'/'+type;
	//	new Ajax.Request(url,{method:'post',asynchronous:true});
   //}

   reportDialog(id,type,msg);
   YAHOO.comic.container.reportdialog.show();
}


   
function showFindResp(req){
        
	 var p = eval("(" + req.responseText + ")");
	 
	 if(p.message == 'success'){
		YAHOO.comic.container.forgotDialog.hide();
        YAHOO.comic.container.findDialogSuccess.show();
	 }
	 else{
		$('forgotMesg').innerHTML = "<span style='color:red;font-family:arial,helvetica,verdana,sans-serif;font-size:12px;'>"+p.message+"</span>";
	 } 
		 		 
}


function showLoginResp(req){
	var p = eval("(" + req.responseText + ")");

    if(p.message == 'success'){
		    YAHOO.comic.container.loginDialog.hide();
            window.location.reload();
	 }
	 else{
		$('loginMesg').innerHTML = "<span style='color:red;font-family:arial,helvetica,verdana,sans-serif;font-size:12px;'>"+p.message+"</span>";
	 } 
     
}



function updateLoginText(val){
    $('loginlbl').innerHTML = val;
}







//Hide and display cimic images
function hideall(){
 temp = $('left').getElementsByClassName('comicImage');
 temp.each(Element.hide);
}

function hideimage(element){
  $(element).hide();
}

function showimage(element){
  $(element).show();
}

function showLogin(){
   YAHOO.comic.container.loginDialog.show();
}

function showForgotLogin(){
   YAHOO.comic.container.loginDialog.hide();
   $('forgotMesg').innerHTML = "<span >&#160;</span>";
   YAHOO.comic.container.forgotDialog.show();
}


YAHOO.namespace("comic.container");


function init() {
	
	// Define various event handlers for Dialog
	var handleSubmit = function() {
		sendLoginReq();
	};
	var handleCancel = function() {
		this.cancel();
	};
	var handleSuccess = function(o) {
		
	};
	var handleFailure = function(o) {
		alert("Submission failed: " + o.status);
	};

	// Instantiate the Dialog
	YAHOO.comic.container.loginDialog = new YAHOO.widget.Dialog("loginDialog", 
																{ width : "200px",
																  fixedcenter : true,
																  visible : false, 
                                                                  modal:true,
																  constraintoviewport : true,
																  buttons : [ { text:"Login", handler:handleSubmit, isDefault:true },
																			  { text:"Cancel", handler:handleCancel } ]
																 } );
	
	// Validate the entries in the form to require that both first and last name are entered
	YAHOO.comic.container.loginDialog.validate = function() {
		var data = this.getData();
		if (data.firstname == "" || data.lastname == "") {
			alert("Please enter your first and last names.");
			return false;
		} else {
			return true;
		}
	};

	// Wire up the success and failure handlers
	YAHOO.comic.container.loginDialog.callback = { success: handleSuccess,
												 failure: handleFailure };
	
	// Render the Dialog
	YAHOO.comic.container.loginDialog.render('container');

	createforgetDialog();
    createSuccessDialog();
    createVoteToolTip();

}

function createVoteToolTip(){
  contextElements = elementsById('modupbtn');
  YAHOO.comic.container.tt1 = new YAHOO.widget.Tooltip("vtip",{ context:contextElements,text:"Click To Vote" });
}

function elementsById(id){
  var nodes = [];
  var tmpNode = document.getElementById(id);
  while(tmpNode){
    nodes.push(tmpNode);
    tmpNode.id = "";
    tmpNode = document.getElementById(id);
  }
  for(var x=0; x<nodes .length; x++){
    nodes[x].id = id;
  }
  return nodes;
}

function createforgetDialog(){


    // Define various event handlers for Dialog
	var handleSubmit = function() {
        var data = this.getData();
		if (data.email == "") {
			$('forgotMesg').innerHTML = "<span style='color:red;font-family:arial,helvetica,verdana,sans-serif;font-size:12px;'>Please enter an Email Address</span>";
			return false;
		} else {
			sendFindReq();
		}
		
	};
	var handleCancel = function() {
		this.cancel();
	};
	var handleSuccess = function(o) {
		
	};
	var handleFailure = function(o) {
		alert("Forgot failed: " + o.status);
	};



   // Instantiate the Dialog
	YAHOO.comic.container.forgotDialog = new YAHOO.widget.Dialog("forgotDialog", 
																{ width : "210px",
																  fixedcenter : true,
																  visible : false, 
                                                                  modal:true,
																  constraintoviewport : true,
																  buttons : [ { text:"Submit", handler:handleSubmit, isDefault:true },
																			  { text:"Cancel", handler:handleCancel } ]
																 } );





	// Wire up the success and failure handlers
	YAHOO.comic.container.forgotDialog.callback = { success: handleSuccess,
												 failure: handleFailure };
	
	// Render the Dialog
	YAHOO.comic.container.forgotDialog.render('container');
    
}


function createSuccessDialog(){
  
    // Define various event handlers for Dialog
	var handleOK = function() {
		this.hide();
	};
	

	// Instantiate the Dialog
	YAHOO.comic.container.findDialogSuccess = new YAHOO.widget.SimpleDialog("findDialogSuccess", 
																			 { width: "300px",
																			   fixedcenter: true,
																			   visible: false,
																			   draggable: false,
                                                                               modal:true,
																			   close: true,
																			   text: "User ID and New Password sent to Email Address",
																			   icon: YAHOO.widget.SimpleDialog.ICON_INFO,
																			   constraintoviewport: true,
																			   buttons: [ { text:"OK", handler:handleOK, isDefault:true }]
																			 } );
	YAHOO.comic.container.findDialogSuccess.setHeader("Success");

	// Render the Dialog
	YAHOO.comic.container.findDialogSuccess.render(document.body);
  
    
}




 function reportDialog(id,type,msg) {
                               
		var msg = "Are you sure want to report this " +msg+ "?"; 
		
		// Define various event handlers for Dialog
		var handleYes = function() {
			url = '/report/'+id+'/'+type;
			new Ajax.Request(url,{method:'post',asynchronous:true});
			this.hide();
		};
		var handleNo = function() {
			this.hide();
		};

		// Instantiate the Dialog
		YAHOO.comic.container.reportdialog = new YAHOO.widget.SimpleDialog("reportdialog", 
														{ width: "270px",
															fixedcenter: true,
														visible: false,
                                                        modal:true,
														draggable: false,
														close: true,
														text: msg,
														icon: YAHOO.widget.SimpleDialog.ICON_HELP,
														constraintoviewport: true,
														buttons: [ { text:"Yes", handler:handleYes, isDefault:true },
																							  { text:"No",  handler:handleNo } ]
														} );
		YAHOO.comic.container.reportdialog.setHeader("Are you sure?");
		
		// Render the Dialog
		YAHOO.comic.container.reportdialog.render(document.body);

		
}

YAHOO.util.Event.onDOMReady(init);