// JavaScript Document

 /* function showMydiv(id){
	var myId
	myId = id
	$("#"+myId).slideDown("slow");
      }

  
 function fadeDivPlease(id){
		var myId
		myId = id
		//$("#"+myId).hide(); 
		$("#"+myId).fadeOut("slow");

	}*/
	
	var timer;
	timer = 30000;
var COOKIE_NAME;
var _options;
$(document).ready(function(){

		COOKIE_NAME = 'login_cookie';
		//COOKIE_NAME2 = 'isLog_cookie';
		_options = { path: '/', expires: 30 };
						   //validating the form
						   $("#log_form").validate();
						   $("#welcome").css({'display':'none'});
						   //we give the div (#loginDiv) a display none, with JS in case client doesnt have JS enable he 'll be able to log still.
					$("#loginDiv").css({ 'display' : 'none'   });
					$("a.login").css({ 'cursor' : 'pointer'   });
					$("a.logout").css({ 'cursor' : 'pointer'   });
					$("#search").css({ 'cursor' : 'pointer'   });
					$(".pointer").css({ 'cursor' : 'pointer'   });
					
					//binding the click event to different objects
					$("#closing").bind('click',slideDivUpDown);
				($("#log_OUT").length)?	$("#log_OUT").bind('click',logout) : null;
					$("#forgot").bind('click', retrieve_pass);
					
					//txt search get the focus it gets empty
					$("#txtsearch").focus(function () {
       						 $(this).attr("value", "")
   					 });
					//on focus we add more time to the timer so the login div doesn't hide
					$("#M_UserName").focus(function () {
       						 timer += 30000;
   					 });
					//on focus we add more time to the timer so the login div doesn't hide
					$("#M_pass").focus(function () {
       						 timer += 30000;
							  $("#M_pass").keypress(function (e) {
    																if(e.which == 13)
																	{
																		submitLoginForm ();
																	}
   									 							});
   					 });
					
					
					
					//on the <a> clic event we trigger a function.
					$("#log_IN").bind('click',login);
						//when we hover in the logindiv
						$("#loginDiv").one('hover',
						  function () {
							  //the login input get the focus
							$("#M_UserName").focus();
						  }, 
						  function () {
							  //when we hover out we slide the div up !
						   //$("#loginDiv").slideUp();
						   
						  }
						);
						
						$("#search").click(function () {	
							$("#txtsearch").focus();		
							 });						

  });
function slideDivUpDown()
{
	if ($("#loginDiv").is(":hidden")) {
		//nothing happen!	
	} 
	 else {  
		// if the div is visible, it'll slide up
		$("#loginDiv").slideUp();
	}	
}
function loadingGif()
{
YAHOO.namespace("example.container");



        if (!YAHOO.example.container.wait) {

            // Initialize the temporary Panel to display while waiting for external content to load

            YAHOO.example.container.wait = 
                    new YAHOO.widget.Panel("wait",  
                                                    { width: "240px", 
                                                      fixedcenter: true, 
                                                      close: false, 
                                                      draggable: false, 
                                                      zindex:1000,
                                                      modal: true,
                                                      visible: false
                                                    } 
                                                );
    
            YAHOO.example.container.wait.setHeader("Loading, please wait...");
            YAHOO.example.container.wait.setBody("<img src=\"Media/rel_interstitial_loading.gif\"/>");
            YAHOO.example.container.wait.render(document.body);

        }

        // Define the callback object for Connection Manager that will set the body of our content area when the content has loaded


    
        // Show the Panel
        YAHOO.example.container.wait.show();	
}
function submitLoginForm ()
{
	
	//if the login form passes validation then
	if($("#log_form").validate().form())
	{
		
		
		loadingGif();
		//we get the login and email:
		var strlogin = $("#M_UserName").val();
		var strpass = $("#M_pass").val();
		$.cookie(COOKIE_NAME, strlogin, _options);

		//we use Jquery Ajax Cross Domains using JSONP see for more info the following web site: http://www.ajaxlines.com/ajax/stuff/article/jquery_ajax_cross_domains_using_jsonp_getjson.php
			//ajax function
			
		// Connect to our data source and load the data 
     $.ajax({
						dataType: 'jsonp',
						data:{ M_UserName:strlogin, M_pass:strpass},
						//this is very important since it's the callback we will and that allow cross domain
						url: 'http://oacnet.ca/oom/traitLogin.asp',
						//function we trigger on succes
						success: function (json){ParseJson(json);},
						error: function (XMLHttpRequest, textStatus, errorThrown) {
							// typically only one of textStatus or errorThrown
							// will have info
							alert("Error occured textStatus=" + textStatus + " errorThrown=" + errorThrown);
						  }
					});
		
		/*   $.jsonp({
    url: "http://oacnet.ca/oom/C/traitLogin.asp?M_UserName="+strlogin+"&M_pass="+strpass,
    success: function(json) {
       alert('booh!');
    },
    error: function() {
       // This will be called in case of error no matter the callback name
    }
});*/
	}

}

function retrieve_pass()
{
	slideDivUpDown();
	tb_show("<table border='0' cellspacing='0' cellpadding='0' height='21'><tr><td align='left' valign='middle'><img src='Media/buffolo.gif' width='33' height='21' align='absmiddle'  />&nbsp;<span class='Titrethink'>Forgot your password ?	</span></td></tr></table>", "sendEmail_ThickB.html?height=130&width=350");
}
function ParseJson(json)
{
	//test if json is empty
	if (!json.optician[0].opt_id)
	{
		
//alert(json.optician[0].opt_id);
		//the timer get 5 sec extend.
		timer += 5000;
		//if login failed, we inform the user:
	   $("#wrong").replaceWith("<label id='wrong2' class='error2'>unable to log you in!</label>");
	   YAHOO.example.container.wait.hide();
	}
	else
	{
		//we collect the data from the login query, id, licence, province...etc
		var Opt_id = json.optician[0].opt_id;
		var Opt_LicProv = json.optician[0].opt_licprov;
		var Opt_login = json.optician[0].opt_username;
		var Opt_pass = json.optician[0].opt_password;
		var Opt_credits = json.optician[0].opt_credits;
		var Opt_StuNum = json.optician[0].opt_stunum;
		var Opt_firstname = json.optician[0].opt_firstname;
		var Opt_lastname = json.optician[0].opt_name;
		var Opt_dateObirth = json.optician[0].opt_yb;
		var Opt_address = json.optician[0].opt_add1;
		var Opt_postcode = json.optician[0].opt_cp;
		var Opt_city = json.optician[0].opt_city;
		var Opt_province = json.optician[0].opt_prov;
		var Opt_tel = json.optician[0].opt_tel;
		var Opt_fax = json.optician[0].opt_fax;
		var Opt_email = json.optician[0].opt_email;
		var Opt_mail_default = json.optician[0].opt_defaultcontact;
		var Opt_Email_default = 'home';
		var Opt_Buisiness = json.optician[0].opt_comp;
		var Opt_Baddress = json.optician[0].opt_cadd1;
		var Opt_Bpostcode = json.optician[0].opt_ccp;
		var Opt_Bcity = json.optician[0].opt_ccity;
		var Opt_Bprovince = json.optician[0].opt_cprov;
		var Opt_Btel = json.optician[0].opt_ctel;
		var Opt_Bfax = json.optician[0].opt_cfax;
		var Opt_Bemail =  json.optician[0].opt_cemail;
		var YB =  json.optician[0].opt_stubirth;
		var mem_type = json.optician[0].mem_type;
		var fee_item_id = json.optician[0].fee_item_id;
		var is_vboard_member = (json.optician[0].vb_opt_id ? true : false );
		//alert(opt_id);
		//$.cookie(COOKIE_NAME2, Opt_id, _options);
		//and we send this data to be stored in session since it comes from a != server
		 $.ajax({
			   type: "POST",
			   async:false,
			   url: "login.asp",
			   data: "opt_id="+Opt_id+"&Opt_LicProv="+Opt_LicProv+"&Opt_login="+Opt_login+"&Opt_credits="+Opt_credits+"&Opt_StuNum="+Opt_StuNum+"&Opt_firstname="+Opt_firstname+"&Opt_lastname="+Opt_lastname+"&Opt_dateObirth="+Opt_dateObirth+"&Opt_address="+Opt_address+"&Opt_postcode="+Opt_postcode+"&Opt_city="+Opt_city+"&Opt_province="+Opt_province+"&Opt_tel="+Opt_tel+"&Opt_fax="+Opt_fax+"&Opt_email="+Opt_email+"&Opt_mail_default="+Opt_mail_default+"&Opt_Email_default="+Opt_Email_default+"&Opt_Buisiness="+Opt_Buisiness+"&Opt_Baddress="+Opt_Baddress+"&Opt_Bpostcode="+Opt_Bpostcode+"&Opt_Bcity="+Opt_Bcity+"&Opt_Bprovince="+Opt_Bprovince+"&Opt_Btel="+Opt_Btel+"&Opt_Bfax="+Opt_Bfax+"&Opt_Bemail="+Opt_Bemail+"&Opt_pass="+Opt_pass+"&YB="+YB+"&mem_type="+mem_type+"&fee_item_id="+fee_item_id+"&is_vboard_member="+is_vboard_member,
			   success: function(msg){
				   //the msg parameter is empty here
				   						//re;omving potential waring from previous authentifications
										 $("#wrong2").replaceWith("<label id='wrong'></label>");
										 //the anchor ligon read now logout
										 $("#log_IN").replaceWith("<a id='log_OUT' class='logout'>Logout</a>");
										 //we could change the bison picture or not!
										 //$("#bison").attr('src','Media/cornerAppSearch_log.jpg');
										 // the login div slides up
										 $("#loginDiv").slideUp();
										 //we attache a click event and a function to logout (to actually logout)
										 $("#log_OUT").bind('click',logout);
										 
										 //$("#welcome").css({'display':'block'});
										 $("#name").text(Opt_firstname);

										//var api = $("#welcome").expose({api: true}).load(); 
/*										   $("#welcome").animate({
														   top:184
														}, 500, "linear", function(){
															$("#welcome").css({'top':'184px'});
															$("#welcome").css({'background-color':'white'});
															api.close(); 
															} );   */
 											$("#welcome").fadeIn("slow");
									//reload the page if already on the event page page when loging in
											if (location.href.indexOf("opt_event") != -1) 
											{ 
												_reload();
											}
											else
											{
											 window.location = "http://www.opticiansofmanitoba.ca/index.asp";
											}
											YAHOO.example.container.wait.hide();
			   						}
				});
		
	
	}
	
}
function Fetch(data)
{
		var dsLocalJSON = new YAHOO.util.LocalDataSource(data);

YAHOO.example.ClientPagination = function() {
        var myColumnDefs = [
          {key:"opt_id", label:"id", sortable:true},
            {key:"opt_username", label:"login", sortable:true}
           
        ];

        var myDataSource = dsLocalJSON;
        myDataSource.responseType = YAHOO.util.DataSource.TYPE_JSON;
        myDataSource.responseSchema = {
            resultsList: "optician",
            fields: ["opt_id","opt_username"]
        };
		
        var oConfigs = {
                paginator: new YAHOO.widget.Paginator({
                    rowsPerPage: 1
                }),
                initialRequest: "results=504"
        };
        var myDataTable = new YAHOO.widget.DataTable("paginated", myColumnDefs,
                myDataSource, oConfigs);
                
        return {
            oDS: myDataSource,
            oDT: myDataTable
        };
    }();


}

 function logout () {	
	 loadingGif();
						 $.ajax({
							   type: "post",
							   url: "logout.asp",
							   dataType:'text',
							    data:{},
        						processData: true,
								async:false,
							   complete:function(){YAHOO.example.container.wait.hide();},
							   success: function(msg){
								  
$("#log_OUT").replaceWith("<a id='log_IN' class='login'>Login</a>");
$("#log_IN").bind('click',login);
$("#welcome").html("<span>good bye!</span>");
$("#welcome").fadeIn("slow");
setTimeout ('_reload()',1000);
//$("#bison").attr('src','Media/cornerAppSearch.jpg');

							   }
							 });

 }
 function _reload()
 {
	 window.location = location.href;
 }
 function login () {

						//if the div is hidden, it is shown								
						 if ($("#loginDiv").is(":hidden")) {
							//here it's actually slide down for more fun
							$("#loginDiv").slideDown("slow");
							setTimeout("slideDivUpDown()",timer);
							var cookie_u = ($.cookie(COOKIE_NAME).length>0) ? this.toString() : '';
							$('#M_UserName').val($.cookie(COOKIE_NAME));
						} 
						 else {  
						 	// if the div is visible, it'll slide up
							$("#loginDiv").slideUp();
						}		
 					
 }
function submitSearchForm (form_name)
{
	if (document.getElementById(form_name).txtsearch.value.length == 0 || document.getElementById(form_name).txtsearch.value == "search is empty..." ) 
	{
		 $("#txtsearch").attr("value","search is empty..."); 
		 //document.getElementById(form_name).txtsearch.focus();
	}
	else
	{
		 document.getElementById(form_name).submit();
	}
	
}
function is_opt_logged_in(guid)
{
	var status;
guid = (guid.length == 0) ? '' : guid;
 $.ajax({
   type: "post",
   dataType:"json",
   url: "http://www.opticiansofmanitoba.ca/securite/is_logged.asp",
   data: {variable_to_test:guid},
   async:false,
   success: function(msg){
      status = msg.lastline[0].guid;
   },
  error: function (XMLHttpRequest, textStatus, errorThrown) {
		status = errorThrown;
			},
   onComplete: function ()
   {
	  
   }
 });
	 return status;
}

<!--

var key;

function createKey(value)
{

	var keySize =value;
	if (keySize == 128) {
		setMaxDigits(19);
		key = new RSAKeyPair(
		 "10001",
		 "202700adbd85e2d7182720c3a0ee19c1",
		 "30db31542ace0f7d37a629ee5eba28cb"
		);
	}
	else if (keySize == 256) {
		setMaxDigits(38);
		key = new RSAKeyPair(
		 "10001",
		 "8064edb1f26944f6bec2b68789db7ffd08b074d0953b76feca71dc8265c60e9",
		 "2687f5ac6c70f9cab32fcbded7059502f4c7cc95fc3e09a560c68975ac4bf5e3"
		);
	}
	else if (keySize == 512) {
		setMaxDigits(76);
		key = new RSAKeyPair(
		 "10001",
		 "59fed719f8959a468de367f77a33a7536d53b8e4d25ed49ccc89a94cd6899da90415623fb73386e9635034fb65ad5f248445a1c66703f760d64a8271ad342b1",
		 "8de7066f67be16fcacd05d319b6729cd85fe698c07cec504776146eb7a041d9e3cacbf0fcd86441981c0083eed1f8f1b18393f0b186e47ce1b7b4981417b491"
		);
	}
	else if (keySize == 1024) {
		setMaxDigits(130);
		key = new RSAKeyPair(
		 "10001",
		 "12e8da920d4599458e84ec5ef1656161807f427d05eb79182b7418259d6f6c14364d1f5caf9130c8d9d9d6ea71d1bdbc87781a46a16bcb9e672814fed3b9c96ddffe0a1b0955ae68055c8f92fef518a04fc32a2ea8390e617cc5556a251f9ae9eee70a32e579cb3e9f298848a9b3aaf634f5930ffbf74473f7cb6c0cefee1751",
		 "130ebebd67b16a9ab2c53a437badbf8f01a80c750095a7fcfe95742c3d5ed1abb318babc5cb5d9350fee4da65ee074f65e1758117e6945f0fcfc8137528053ce9d1da8618890dee24e5e0bf8c87795bb1d09eddd544640824ee0dd0ea9fd908d27b0f8a1ae5c37f3647fbf2f5795500ad76c195b3387d0458a8f51b701472301"
		);
	}
	else if (keySize == 2048) {
		setMaxDigits(260);
		key = new RSAKeyPair(
		 "10001",
		 "409c6fe2b6474762b5c07f4e55ef80d174814dc1fb0fb58e979691116fb3dc433f759ff8a88d1a0f0666862b0b3758c54b7355fa87ee827369381e1f97c5d74944e032c7186b51a956fb49d6deb3aee0b2c7e65fc53bfd46d217764850667ed0363de143f3f3d06d5a0018693ad3dacdf78a18d037ceeccb7508776f27b30852b8b505666a8dca5bfbb455d2f85918f8b5295061c97673c78802c5f5cf4581c7215dc32af8dfb6fc10e9ba51fb5a88abab94157ccecf615e104a91a45e9bee072fe7b388344c1bbad4a8f7d5daeccbadf778d59eff2a491a067bba5343c5a094c61b575fe367ecfcc01c3d208c2f8c05b9496a929b2b72e70160d07d07f248f1",
		 "9800012b1e533c2c28187424e1289fd4f7fe67487058f5ac7f27f18476c6c93db20b6d2c63d04ff310c1e7211cf8014adc006176529abc53fd1780274fc2629cf51d627c7465c3cbf4f110c3560e2128b97c4ea8a431f0b2a326fc31899790515ad45874ca75c68ee6695558736490ea895d598b8525bccab3156104d360b115ae25e99e9d899a2219136bad0336eeee0c6d725aa9c3b6b923c1ad95a9057b9deb7b563e05614acc800d9d8ec5de405d74feea722c5146feb80829508180ab5c80bf792b83f07c04c73ce0b3cf0d9f74aa92a4704819d103e58f5d4b8ca750148ba1cbab8eb55f92775b18da427c3a0b592809f3853274841a44b7129ec6a623"
		);
	}
}

function bodyLoad()
{
	createKey(128);
	key = new RSAKeyPair("5ABB", "01146BD07F0B74C086DF00B37C602A0B",
	                     "01D7777C38863AEC21BA2D91EE0FAF51");
}

function cmdShowKeyClick()
{
	var m = biToString(key.m, key.radix);
	var e = biToString(key.e, key.radix);
	var d = biToString(key.d, key.radix);
}

function cmdEncryptClick(value)
{
		return encryptedString(key, value);
}

function cmdVerifyClick(value,key)
{
	
		return decryptedString(key,value);
		
}


// -->

