//JAVASCRIPT MODULE CORRESPONDING TO THE PHP Q360 MODULE

var timeout=3;
var logout = null;


function q360_newuser()
{
	open_window('show_quick360_registeruser','','',mod_quick360_datafeed,'reactive');
}

function quick360_emailapp(appid)
{
	if (parseInt(appid)>0)
	{
		core_rpc('process_quick360_sendemails',mod_quick360_datafeed,'&appid='+appid);
	}
}

function quick360_newapp()
{
	
	var thevars = Form.serialize('newapp');
	
	if (parseInt($F('ajax_appschema'))>0)
	{
		if ( ($F('ajax_firstname').length>1) && ($F('ajax_surname').length>1) ) 
		{
			if (emailCheck($F('ajax_email')))
			{
//				open_window('ajax_savenewapp','&'+thevars+fromcode,'',mod_app_datafeed);
				//1 means disable status message
				core_rpc('process_quick360_createappraisalappraisee',mod_quick360_datafeed,'&'+thevars);
			}
			
		}
		else
			{
				alert('You must enter the appraisees details');
			}
	}
	else 
		{
			alert('You must select a Template to base this appraisal on');
		}
}

function quick360_saveappraisor()
{
	
	var thevars = Form.serialize('newapp');
	
	if (parseInt($F('ajax_appid'))>0)
	{
		if ( ($F('ajax_firstname').length>1) && ($F('ajax_surname').length>1) ) 
		{
			if (emailCheck($F('ajax_email')))
			{
//				open_window('ajax_savenewapp','&'+thevars+fromcode,'',mod_app_datafeed);
				//1 means disable status message
				core_rpc('process_quick360_createappraisalappraisee',mod_quick360_datafeed,'&'+thevars);
			}
			
		}
		else
			{
				alert('You must enter the contacts details');
			}
	}
	else 
		{
			alert('Error: Appraisal not selected, please go back.');
		}
}

function q360_highlightrow(me,newclass)
{
//	alert($(me));
	$(me).className=newclass;
}

function q360_backhome(setasloggedin)
{
	var extra = "";
		if (typeof(setasloggedin)!='undefined')
		{
			extra = "&loggedin=1";
		}	
		Tips.removeAll();
		core_updatesystem_rpc('show_quick360_homepage',mod_quick360_datafeed,extra,'quick360 - My Home Page',0);
}

function q360_refreshapps()
{
	Tips.removeAll();
	scrollpos = $('applist').scrollTop;
	core_updatesystem_rpc('show_quick360_manageappraisals',mod_quick360_datafeed,'&scrollpos='+scrollpos,'Monitor / Manage Your Appraisals',0);
}

function q360_editapp(appid)
{
	//remove all tips so they dont stay on the screen
	Tips.removeAll();
	//save the scroll position so we return back to it on back press.
	scrollpos = $('applist').scrollTop;
	//tell the system we are coming from the manageapp screen
	core_updatesystem_rpc('show_quick360_createappraisalusers',mod_quick360_datafeed,'&scrollpos='+scrollpos+'&from=manage&appid='+appid,'Edit / Manage Appraisal',0);
}

function q360_editappraisor(apprid)
{
	//remove all tips so they dont stay on the screen
	//Tips.removeAll();
	//save the scroll position so we return back to it on back press.
	//scrollpos = $('applist').scrollTop;
	//tell the system we are coming from the manageapp screen
	//core_updatesystem_rpc('show_quick360_createappraisalusers',mod_quick360_datafeed,'&scrollpos='+scrollpos+'&from=manage&appid='+appid,'Edit / Manage Appraisal',0);
	open_window2('show_quick360_createappraisalappraisee','&ic_appraisorid='+apprid,'',mod_quick360_datafeed,'reactive');
}

function q360_genapp(appid)
{
	if (confirm('Are you sure you wish to generate this report and close all outstanding feedback?'))
	{
		//protonew for new prototype 1.6.1
		core_rpc('ajax_appraisalgen',mod_report_datafeed,'&protonew=true&from_mod=quick360&appid='+appid);
	}
}

function q360_processemail(mode)
{
		var savestring = Form.serialize('signupForm');
		core_rpc('q360_updateandsendemail',mod_quick360_datafeed,'&'+savestring+'&mode='+mode);	
}

function q360_addappraisor(appid)
{
	//we dont pass ic_appraisorid and the mode turns to add
	open_window2('show_quick360_createappraisalappraisee','&appid='+appid,'',mod_quick360_datafeed,'reactive');
}

function q360_fixemail(apprlinkid)
{
	//we dont pass ic_appraisorid and the mode turns to add
	open_window2('show_quick360_resendemail','&mode=fix&apprlinkid='+apprlinkid,'',mod_quick360_datafeed,'reactive');
}
function q360_resendemail(apprlinkid)
{
	//we dont pass ic_appraisorid and the mode turns to add
	open_window2('show_quick360_resendemail','&mode=resend&apprlinkid='+apprlinkid,'',mod_quick360_datafeed,'reactive');
}
function q360_deleteappraisor(apprlinkid)
{
	if (confirm('Are you sure you want to delete this person?'))
	{
		core_rpc('q360_deleteperson',mod_quick360_datafeed,'&apprlinkid='+apprlinkid);
	
	}
}
	

function quick360newaccount()
{
	
	var savestring = Form.serialize('signupForm');
	
	if ($F('ajax_firstName').length<2)
	{
		alert('Your first name must be over 1 character long');
	}
	else
	{
		if ($F('ajax_lastName').length<2)
		{
			alert('Your last name must be over 1 character long');
		}
		else
		{
			if (!(q360_emailCheck($F('ajax_email'))))
			{
				//alert('Your email address must be of a valid format');
			}
			else
			{
					core_rpc('process_signupquick360',mod_quick360_datafeed,'&from=ca2&'+savestring);
			}
		}
	}
	return false;
}



function q360_perform_logout()
{

	core_rpc('q360_logout',mod_quick360_datafeed,'');

	
}

function q360_upgradeaccount()
{
	if (confirm("When you click OK your quick360 account will be up-graded to the full reactive360 process.\nreactive360 has several additional functions, we suggest you download the user manual.\nClick OK to up-grade, click CANCEL to continue using quick360."))
	{
		core_rpc('q360_upgradeaccount',mod_quick360_datafeed,'');
	}
	
}

function q360_loggedout()
{
	window.location.href=nossl_site_root;
}

function q360_usetemplate(qtid,title)
{
//remove all tips so they dont stay on the screen
	Tips.removeAll();
	//save the scroll position so we return back to it on back press.
	//scrollpos = $('applist').scrollTop;
	//tell the system we are coming from the manageapp screen
	//core_updatesystem_rpc('show_quick360_createappraisalusers',mod_quick360_datafeed,'&scrollpos='+scrollpos+'&from=manage&appid='+appid,'Edit / Manage Appraisal',0);
	core_updatesystem_rpc('show_quick360_createappraisalappraisee',mod_quick360_datafeed,'&ic_appraisalschemaid='+qtid,title,0);
}

function q360_managefromhome(title)
{
//remove all tips so they dont stay on the screen
	Tips.removeAll();
	//save the scroll position so we return back to it on back press.
	//scrollpos = $('applist').scrollTop;
	//tell the system we are coming from the manageapp screen
	//core_updatesystem_rpc('show_quick360_createappraisalusers',mod_quick360_datafeed,'&scrollpos='+scrollpos+'&from=manage&appid='+appid,'Edit / Manage Appraisal',0);
	core_updatesystem_rpc('show_quick360_manageappraisals',mod_quick360_datafeed,'',title,0);
}

function q360_splash()
{
	open_window('show_quick360_splashpage','','',mod_quick360_datafeed,'reactive');
}

function q360_splashtohome()
{
	open_window('show_quick360_splashpage','','',mod_quick360_datafeed,'reactive');
}

function q360_home()
{
	open_window('show_quick360_homepage','','',mod_quick360_datafeed,'reactive');
}



function q360_isValidEmail(str) {
   return (str.indexOf(".") > 2) && (str.indexOf("@") > 0);
 
}

function q360_emailCheck (emailStr) {
/* The following pattern is used to check if the entered e-mail address
   fits the user@domain format.  It also is used to separate the username
   from the domain. */
var emailPat=/^(.+)@(.+)$/
/* The following string represents the pattern for matching all special
   characters.  We don't want to allow special characters in the address. 
   These characters include ( ) < > @ , ; : \ " . [ ]    */
var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
/* The following string represents the range of characters allowed in a 
   username or domainname.  It really states which chars aren't allowed. */
var validChars="\[^\\s" + specialChars + "\]"
/* The following pattern applies if the "user" is a quoted string (in
   which case, there are no rules about which characters are allowed
   and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
   is a legal e-mail address. */
var quotedUser="(\"[^\"]*\")"
/* The following pattern applies for domains that are IP addresses,
   rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
   e-mail address. NOTE: The square brackets are required. */
var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
/* The following string represents an atom (basically a series of
   non-special characters.) */
var atom=validChars + '+'
/* The following string represents one word in the typical username.
   For example, in john.doe@somewhere.com, john and doe are words.
   Basically, a word is either an atom or quoted string. */
var word="(" + atom + "|" + quotedUser + ")"
// The following pattern describes the structure of the user
var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
/* The following pattern describes the structure of a normal symbolic
   domain, as opposed to ipDomainPat, shown above. */
var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")


/* Finally, let's start trying to figure out if the supplied address is
   valid. */

/* Begin with the coarse pattern to simply break up user@domain into
   different pieces that are easy to analyze. */
var matchArray=emailStr.match(emailPat)
if (matchArray==null) {
  /* Too many/few @'s or something; basically, this address doesn't
     even fit the general mould of a valid e-mail address. */
	alert("Email address seems incorrect (check @ and .'s)")
	return false
}
var user=matchArray[1]
var domain=matchArray[2]

// See if "user" is valid 
if (user.match(userPat)==null) {
    // user is not valid
    alert("The name in the email address doesn't seem to be valid.")
    return false
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
   host name) make sure the IP address is valid. */
var IPArray=domain.match(ipDomainPat)
if (IPArray!=null) {
    // this is an IP address
	  for (var i=1;i<=4;i++) {
	    if (IPArray[i]>255) {
	        alert("Destination IP address is invalid!")
		return false
	    }
    }
    return true
}

// Domain is symbolic name
var domainArray=domain.match(domainPat)
if (domainArray==null) {
	alert("The domain name (text after the @) doesn't seem to be valid.")
    return false
}

/* domain name seems valid, but now make sure that it ends in a
   three-letter word (like com, edu, gov) or a two-letter word,
   representing country (uk, nl), and that there's a hostname preceding 
   the domain or country. */

/* Now we need to break up the domain to get a count of how many atoms
   it consists of. */
var atomPat=new RegExp(atom,"g")
var domArr=domain.match(atomPat)
var len=domArr.length
if (domArr[domArr.length-1].length<2 || 
    domArr[domArr.length-1].length>3) {
   // the address must end in a two letter or three letter word.
   alert("The address must end in a three-letter domain (e.g. .com), or two letter country. (e.g. .uk)")
   return false
}

// Make sure there's a host name preceding the domain.
if (len<2) {
   var errStr="This address is missing a domain name (text normally after the @)"
   alert(errStr)
   return false
}

// If we've gotten this far, everything's valid!
return true;
}



function q360_backtoappmanage(scrollpos,title)
{
	core_updatesystem_rpc('show_quick360_manageappraisals',mod_quick360_datafeed,'&from=editapp&scrollpos='+scrollpos,title,0);
}

//extra js for scrolling the div

Element.addMethods({
  scrollTo: function(element, left, top){
    var element = $(element);
    if (arguments.length == 1){
      var pos = element.cumulativeOffset();
      window.scrollTo(pos[0], pos[1]);
    } else {
      element.scrollLeft = left;
      element.scrollTop  = top;
    }
    return element;
  }
});


Effect.Scroll = Class.create();
Object.extend(Object.extend(Effect.Scroll.prototype, Effect.Base.prototype), {
  initialize: function(element) {
    this.element = $(element);
    if(!this.element) throw(Effect._elementDoesNotExistError);
    this.start(Object.extend({x: 0, y: 0}, arguments[1] || {}));
  },
  setup: function() {
    var scrollOffsets = (this.element == window) 
                ? document.viewport.getScrollOffsets() 
                : Element._returnOffset(this.element.scrollLeft, this.element.scrollTop) ;
    this.originalScrollLeft = scrollOffsets.left;
    this.originalScrollTop  = scrollOffsets.top;
  },
  update: function(pos) {
    this.element.scrollTo(Math.round(this.options.x * pos + this.originalScrollLeft), Math.round(this.options.y * pos + this.originalScrollTop));
  }
});


function google_fix()
{
	 inputList = document.getElementsByTagName("INPUT");
	for(i=0;i<inputList.length;i++){
		Event.observe(inputList[i],'propertychange',google_fix_style);
//        inputList[i].attachEvent("onpropertychange",google_fix_style);
      //  inputList[i].style.backgroundColor = "";
		inputList[i].title='';
		//alert(inputList[i].title);
    }
}

function google_fix_style(){
    if(event.srcElement.title != "")
        event.srcElement.title = "";
}
