


var emailDialog;

// handle submit
function efhandleSubmit() {
        // Set waiting
        document.body.style.cursor = 'wait';
        var url = "/php/promo/main/emailToFriend.php?"+Object.toQueryString( {emailObjId: $('emailObjId').value,
					 emailObjType: $('emailObjType').value,
					 recipients: $('recipient_list').value,
					 emailbody: $('emailbody').value,
					 copy: $('copy').checked} );

	// Fire off AJAX
	var req = new Ajax(url,
	  {
	    asynchronous:true,
	    method:'get',
	    onSuccess: function(u){
		if(u.match(/Error/i)){
		    showInfoDialogText( $('emailfailuretitle').innerHTML, u, 'WARN' ); 
		}else{
			emailDialog.hide();
			resetEmailDialog(); 
			showInfoDialogText( $('emailsuccesstitle').innerHTML,u+$('emailwhatnext').innerHTML , 'INFO' );
		}
	     },
	     onFailure: function(){ alert('Internal Error, Please check if you are currently logged in and try again.') }
	   }
	   );
	req.request();
	document.body.style.cursor = 'default';
};

function efhandleCancel(){
	emailDialog.hide();
        resetEmailDialog();
};

function initEmailDialog() {
  // Define various event handlers for Dialog
  var handleSubmit = function() {
    this.submit();
  };
  var handleCancel = function() {
    this.cancel();
  };
  var handleSuccess = function(o) {
    var response = o.responseText;
    response = response.replace(/^\s*/, "").replace(/\s*$/, "");
    if( response !== '') {
      $("emailErrorMsg").innerHTML = response;
      showInfoDialogText( $('emailfailuretitle').innerHTML, response, 'WARN' ); 
    }
    else {
      showInfoDialog( 'emailsuccesstitle', 'emailsuccessbody', 'INFO' );
    }
  };
  var handleFailure = function(o) {
    showInfoDialog( 'emailfailuretitle', 'emailfailurebody', 'WARN' );
  };
  var closeEmailDialog = function() {
    currentRownumDialog = -1;
    resetEmailDialog();
};

  emailDialog = new YAHOO.widget.Dialog("emaildialog", 
    { width : "360px",
      fixedcenter : true,
      visible : false, 
      constraintoviewport : true,
      effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.3}
    } );

					
  // Wire up the success and failure handlers
  emailDialog.callback = { success: efhandleSubmit, failure: handleFailure };

  // Render the Dialog
  emailDialog.render();
  emailDialog.beforeHideEvent.subscribe( closeEmailDialog, emailDialog, true );
	removeClassName( emailDialog.innerElement, 'displayNone' );
}


var currentRownumDialog = -1;

/**
 * When the user is not logged in, ask the user to log in.
 */
function notLoggedInDialog() {
  showInfoDialog( 'notloggedintitle', 'notloggedinbody', 'INFO' );
}

/**
 * When there is no profile
 */
function noProfile() {
  showInfoDialog( 'emailnoprofiletitle', 'emailnoprofilebody', 'INFO' );
}

/**
 * Toggle display of the email dialog.
 * If the user switches to another email dialog, reset the error msgs.
 */
function toggleEmailDialog( objId, objTitle, objUrl ) {
  if( currentRownumDialog == objId ) {
    currentRownumDialog = -1;
	emailDialog.hide();
  }
  else {
	if (!emailDialog) {
	    initEmailDialog();
	}
    currentRownumDialog = objId;
    resetEmailErrors();
    emailDialog.show();
 //   $('emailTitle').innerHTML = objTitle;
    $('emailObjId').value = objId;
    $('emailUrl').value = objUrl;
    var content = $('emailtemplate').innerHTML;
    $("emailbody").value = content.replace(/<br>/gi,'\n')+'\n';
  }
}

/**
 * Reset the email dialog fields
 */
function resetEmailDialog() {
        resetEmailErrors();
	$("recipient_list").value = '';
	$("emailbody").value = $('emailtemplate').innerHTML;
 	document.emailfriendform.emailbody.value = '';
 	simpleTextCounter(document.emailfriendform.emailbody,$('charLimit'),1000);
}

/**
 * Reset the error messages on the email dialog.
 */
function resetEmailErrors() {
  $("recipient_list").className = '';
  $("emailbody").className = '';
  $("emailErrorMsg").innerHTML = '';
}



var addToListDialog;


var currEleId = "";

function initaddToListDialog() {
  

	// Define various event handlers for Dialog
  var atlhandleSubmit = function() {
    this.submit();
  };
  var atlhandleCancel = function() {
    this.cancel();
  };
  var atlhandleSuccess = function(o) {
    var response = o.responseText;
    if( response.length > 0 && (response.indexOf('success') == -1)) {
      showInfoDialogText( $('addToListfailuretitle').innerHTML, response, 'WARN' ); 
    }
    else {
     // showInfoDialog( 'addToListsuccesstitle', 'addToListsuccessbody', 'INFO' );
	  displayOnWatchList();
    }
  };
  var atlhandleFailure = function(o) {
    showInfoDialog( 'addToListfailuretitle', 'addToListfailurebody', 'WARN' );
  };
  var closeaddToListDialog = function() {
    currentRownumDialog = -1;
    resetaddToListDialog();
  };

  addToListDialog = new YAHOO.widget.Dialog("addToListdialog", 
    { width : "400px",
      fixedcenter : true,
      visible : false, 
      constraintoviewport : true,
      effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.3},
      buttons : [ { text:"", handler:atlhandleSubmit, isDefault:true },
        { text:"", handler:atlhandleCancel } ]
    } );
					
  // Wire up the success and failure handlers
  addToListDialog.callback = { success: atlhandleSuccess, failure: atlhandleFailure };

  // Add validation
  addToListDialog.validate = function() {
    var data = this.getData();
    resetaddToListErrors();

    return true;
  };
  // Render the Dialog
  addToListDialog.render();
  addToListDialog.beforeHideEvent.subscribe( closeaddToListDialog, addToListDialog, true );
  addToListDialog.innerElement.style.display = '';

	// add button styles
	addToListDialog.firstButton.className = "submit";
	addToListDialog.lastButton.className = "cancel";
}


var currentRownumDialog = -1;

/**
 * When the user is not logged in, ask the user to log in.
 */
function atlnotLoggedInDialog() {
  showInfoDialog( 'atlnotloggedintitle', 'atlnotloggedinbody', 'INFO' );
}

function atlAlreadyAddedDialog(objName) {
  showInfoDialogText( $('atlalreadyaddedtitle').innerHTML, objName+' '+$('atlalreadyaddedbody').innerHTML, 'INFO' );
}

function notAuthorizedDialog() {
  showInfoDialogText( $('notauthorizedtitle').innerHTML, $('notauthorizedbody').innerHTML, 'WARN' );
}

/**
 * Toggle display of the addToList dialog.
 * If the user switches to another addToList dialog, reset the error msgs.
 */
function toggleaddToListDialog( objId, objType) {
  if( currentRownumDialog == objId ) {
    currentRownumDialog = -1;
    addToListDialog.hide();
  }
  else {
	if (!addToListDialog) {
		initaddToListDialog();
	}
    currentRownumDialog = objId;
    resetaddToListErrors();
    addToListDialog.show();
    $('addToListObjId').value = objId;
    $('addToListTitle').innerHTML = objType;
    $('addToListObjName').value = objType;
  }
}

/**
 * Reset the addToList dialog fields
 */
function resetaddToListDialog() {
  resetaddToListErrors();
 $("addToListObjId").value = '';
 $("comments").value = '';
}

/**
 * Reset the error messages on the addToList dialog.
 */
function resetaddToListErrors() {
}



function addToList(objId, objType, eleId){
   var url = '/php/myelance/main/addWatchList.php?mode=check&addToListObjType='+$('addToListObjType').value+'&addToListObjId='+objId+'&addToListObjName='+objType;
   var req = new Ajax(url,
   {
     asynchronous:true,
     method:'get',
     onSuccess: function(t){
         //set global var
		 currEleId = eleId;
		 var celldata = t.split('##');
         if (celldata[0].indexOf('NoAuth') == 0) { 
            notAuthorizedDialog();
            return;
         }
         
		 // check to see if we got no for our question, does the user already exist?
         if(celldata[0].indexOf('No') != -1){
			toggleaddToListDialog(celldata[1], celldata[2]);
         }else{
			 displayOnWatchList();
			 atlAlreadyAddedDialog(celldata[2]);
         }
     },
     onFailure: function(){ alert('Internal Error, Please check if you are currently logged in and try again.') }
   }
   );
   req.request();
}

function displayOnWatchList(){
	$(currEleId).getFirst().setStyle('display','');
	$(currEleId).getLast().setStyle('display','none');
}

function updateParent(){
	$(window.opener.document.getElementById('watchlistid')).getFirst().setStyle('display','');
	$(window.opener.document.getElementById('watchlistid')).getLast().setStyle('display','none');
}

