

dojo.require("dojo.parser");	        
dojo.require("dijit.form.DateTextBox");
var dialog;
	dojo.addOnLoad(function(){
					dojo.require("dojo.parser");
				    dojo.require("dijit.Tooltip");
					
				  
		

                                  //startList();

				   Custom.init;
				    
				    var pre_tip = '<div class="roundcont"> <div class="roundtop"><img src="/images/tl.png" alt="" width="10" height="10" class="corner"  style="display: none" /> </div>  <p>';
				    var post_tip = '</p>   <div class="roundbottom">	 <img src="/images/bl.png" alt=""	 width="10" height="10" class="corner"	 style="display: none" />   </div></div>	';

					if(dojo.byId("help-image")){	
					
						if (document.images)
						{
						//  pic1= new Image(800,229); 
						//  pic1.src="http://www.peepex.com/images/boxes_one.png"; 
						  
						//  pic2= new Image(800,229); 
					//	  pic2.src="http://www.peepex.com/images/boxes_two.png"; 

						  
					//	  pic3= new Image(800,229); 
					//	  pic3.src="http://www.peepex.com/images/boxes_three.png"; 

						//  pic4= new Image(200,82); 
						  //pic4.src="http://www.peepex.com/images/peepex_one.png"; 

					//	  pic5= new Image(200,82); 
					//	  pic5.src="http://www.peepex.com/images/peepex_two.png"; 
//
					//	  pic6= new Image(200,82); 
					//	  pic6.src="http://www.peepex.com/images/peepex_three.png"; 

							
						}
						
					}
					
					
					if(dojo.byId("mybank_rate")){	
					
						//dojo.require("dojox.charting.Chart2D");
						//dojo.require("dojo.currency");
						dojo.require("dijit.form.Slider");
						
						//dojo.require("dojox.charting.action2d.Magnify");
						//dojo.require("dojox.charting.action2d.Tooltip");  
						//dojo.require("dojox.charting.scaler.linear");	
					
					
						createSliders();
						var percent = (rate/100);	
						var pCent = percent*3;
						dojo.byId("mybank_rate").value = roundNumber(rate-percent,4);
						myrate = dojo.byId("rate").value = roundNumber(rate+percent,4);
						dojo.byId("days").value = 20;
						
						updateMyBank();
						updateMyRate();
						//makeCharts();
						
						//var foo = new dijit.Tooltip({ label: "Enter the amount you wish to<br/>change and select a currency pair", connectId: "change_question", position:"right" }); foo.startup();
						var provider = new dijit.Tooltip({ label: pre_tip +  "<b>What is a provider?</b><br/>The provider offers or sells different rates,<br/>e.g. a bank,travel agent or broker"+ post_tip, connectId: "provider_question", position:"right" }); provider.startup();
						var bank = new dijit.Tooltip({ label:  pre_tip + "<b>What is my bank rate?</b><br/>If you have already been quoted a rate enter it here,<br/>to compare it to the other rates available" + post_tip, connectId: "bank_question", position:"right" }); bank.startup();
						var newrate = new dijit.Tooltip({ label:  pre_tip +  "<b>What is state a rate?</b><br/>Sell your currency at a rate of your choosing.<br/>" +
																  "Enter a rate, choose how many days you want make <br/>" +
																  "your rate available to other users and click GO.<br/>" +
																  "The more stars we give you, the better chance your<br/>" +
																  "order has of being filled at the rate you want" + post_tip, connectId: "newrate_question", position:"right" }); newrate.startup();
						
					}
					
					if(dojo.byId("change_question")){	
						//var foo = new dijit.Tooltip({ label: "<div id='round_me' class='sb'><p><b>I want to change</b><br/>Enter the amount you wish to<br/>change and select both currencies</p></div>", connectId: "change_question", position:"right" }); foo.startup();
						//var foo = new dijit.Tooltip({ label: "<b>I want to change</b><br/>Enter the amount you wish to<br/>change and select both currencies", connectId: "change_question", position:"right" }); foo.startup();
					
						var foo = new dijit.Tooltip({ label: pre_tip + "<b>I want to change</b><br/>Enter the amount you wish to<br/>change and select both currencies"+ post_tip, connectId: "change_question", position:"right" }); foo.startup();
						
					}
					
					if(dojo.byId("market_rates_container")){	
						
						  
						  dojo.require("dijit.Dialog");
						
						  dojo.require("dijit.form.Button");
						 
						  dialog = new dijit.Dialog({ title: "PeepEx Market Rates", content: dojo.byId("market_rates_container") });
							dojo.body().appendChild(dialog.domNode);
							dialog.startup();

					}
					
					
					
					if(dojo.byId("risk")){
					
					
						
						var inputs = dojo.query(".risk_dates");
						//console.log(inputs);
						
						var dojoControl;
						
						dojo.forEach(inputs, 
							    function(oneEntry, index, array) {
							        //console.log(oneEntry);
							        dojoControl = new 	dijit.form.DateTextBox({required:false,constraints:{datePattern:"yyyy-MM-dd",min:"2009-07-01",max:"2010-06-30"},name:'risk_date[]'},oneEntry); 
							        //dojoControl.attr('value', '2007-12-30');
							        date = oneEntry.value.replace(/-/g,"/");
							        if(date.length > 4){
								        //console.log(date);
								        myformat = date;  //2008-12-30
								        mydata = new Date(myformat);
								        dojoControl.attr('value', mydata);
							        }
							        dojoControl.startup(); 
							        //dojoControl.value= '2007-12-30'; 
							        
							    }
							);
					
						dojo.require('dijit.form.DateTextBox'); 

						if(dojo.byId("risky")){
							dojo.require("dojox.charting.Chart2D");
						
							drawriskgraph();
							
							
							
														
							
						}
						
					        			           
					}
					
					
					
					
					
				});

	
	dialogShow = function() {
		
		dialog.show();
		
	}

/**

        startList = function() {
            if (document.all&&document.getElementById) {
            navRoot = document.getElementById("nav");
            for (i=0; i<navRoot.childNodes.length; i++) {
            node = navRoot.childNodes[i];
            if (node.nodeName=="LI") {
            node.onmouseover=function() {
            this.className+=" over";
              }
              node.onmouseout=function() {
              this.className=this.className.replace(" over", "");
               }
               }
              }
             }
            }
**/

	drawriskgraph = function(){

		
		
		var riskseriess = dojo.byId("series").value.split(','); 
	
		//alert(riskseriess);
		for ( var i in riskseriess )
		{
			riskseriess[i] = parseInt(riskseriess[i]); 
		}
		
		
		
		var chart1 = new dojox.charting.Chart2D("GraphContainerArea");
		 chart1.addPlot("default", {type: "Lines"});
		
		 

		 chart1.addPlot("Grid", {type: "Grid",
				hAxis: "x",
				vAxis: "y",
				hMajorLines: true,	
				hMinorLines: true,	
				vMajorLines: true,	
				vMinorLines: false
			});
		 
		 chart1.addAxis("x", { 

				min: 0,
				max: 262,
				minorTickStep: 1,
				majorTickStep: 22,
				
				labels: [{value: 0, text: "Jun"}, {value: 22, text: "Jul"}, 
					{value: 44, text: "Aug"}, {value: 66, text: "Sep"}, 
					{value: 88, text: "Oct"}, {value: 110, text: "Nov"},
					{value: 132, text: "Dec"}, {value: 154, text: "Jan"},
					{value: 176, text: "Feb"}, {value: 198, text: "Mar"},
					{value: 220, text: "Apr"}, {value: 242, text: "May"}
					,{value: 262, text:"Jun"}]
				});
		 
		 chart1.addAxis("y", {vertical: true});
		 
		 chart1.addSeries("Series 1",  riskseriess, {stroke: {color: "blue", width: 2}, fill: "lightblue"});
		 
		 chart1.render();
			
		
		 
	}
	
	
updateLiveRate = function() {
	
	
	 var old = dojo.byId("live_rate_value_rate").innerHTML;
	 dojo.byId("live_rate_value_rate").innerHTML = '<img src="/images/loading.gif" class="loading_img"/>  ';
	
	dojo.xhrGet( { // 
        // The following URL must match that used to test the server.
        url: "index/rateupdate", 
        handleAs: "text",

        timeout: 10000, // Time in milliseconds

        // The LOAD function will be called on a successful response.
        load: function(response, ioArgs) { // 
         dojo.byId("live_rate_value_rate").innerHTML = response;
          return response; // 
        },

        // The ERROR function will be called in an error case.
        error: function(response, ioArgs) { // 
          console.error("HTTP status code: ", ioArgs.xhr.status); // 
          dojo.byId("live_rate_value_rate").innerHTML = old;
          return response; // 
          }
        });
	
	
	
}	

rowHoverOut = function(row){
	
	
	var img = dojo.query("#row_"+row+" img");
	//console.log(img[0]);
	img[0].src = img[0].alt;
}

rowHover = function(row){
	

	var img = dojo.query("#row_"+row+" img");
	//console.log(img[0]);
	img[0].alt = img[0].src;
	img[0].src = 'images/1_logo_red.gif';
	
	
	
}
	
showBoxText	= function(box){
	
	try{
	switch(box){
	
	case 'one' :
				
				
				document.getElementById("help-image").style.background="transparent url(/images/boxes_one.png) no-repeat scroll left top";
				document.getElementById("peepex_bar").style.background="#c3e3d8";
				document.getElementById("peepex_main_logo").style.background="#FFFFFF url(/images/peepex_one.png) no-repeat scroll left center";
				document.getElementById("help-text-two").style.display="none";
				document.getElementById("help-text-three").style.display="none";
				document.getElementById("help-text-one").style.display="block";
				break;
	case 'two' :
				
				document.getElementById("help-image").style.background="transparent url(/images/boxes_two.png) no-repeat scroll left top";
				document.getElementById("peepex_bar").style.background="#ddd7e9";
				document.getElementById("peepex_main_logo").style.background="#FFFFFF url(/images/peepex_two.png) no-repeat scroll left center";
				document.getElementById("help-text-three").style.display="none";
				document.getElementById("help-text-one").style.display="none";
				document.getElementById("help-text-two").style.display="block";
				break;
	case 'three' :
				
				document.getElementById("peepex_bar").style.background="#fcd9d8";
				document.getElementById("help-image").style.background="transparent url(/images/boxes_three.png) no-repeat scroll left top";
				document.getElementById("peepex_main_logo").style.background="#FFFFFF url(/images/peepex_three.png) no-repeat scroll left center";
				document.getElementById("help-text-two").style.display="none";
				document.getElementById("help-text-one").style.display="none";
				document.getElementById("help-text-three").style.display="block";
		break;	
	
	
	}
	
	}catch(Exception){}
}




toggleAbout = function(text){ 


try{
	var textblock = document.getElementById(text);
	var parentblock = document.getElementById('x'+text);

	
	
	
	//dojo.query(".more-text").style("display","none");
    
       // dojo.query(".green_bar").style("background","#C5C6C9");
       
	
	
	if(textblock.style.display=="") textblock.style.display="none";
	
	
	
	if(textblock.style.display=="none") {
		textblock.style.display="block";
		parentblock.style.background="#FCD9D8";
		}
	else {
		textblock.style.display="none";
		parentblock.style.background="#C5C6C9";
		}
		
		
		
	}catch(Exception){console.log(Exception);}

}	
	
updateMyBank = function(){
	   
		var mybankrate = dojo.byId("mybank_rate").value;
		 if(IsNumericFloat(mybankrate) == false){
			 alert('Please only enter numbers into this field.');
			 dojo.byId("bank_amount_span").innerHTML = ''; 
				
		 }else
		 {
			 dojo.byId("bank_amount_span").innerHTML = roundNumber(mybankrate*amount,4) + ' ' + currency_b ; 
				
		 }
		
	
	}
	
updateMyRate = function(){
	   
		var myrate = dojo.byId("rate").value;
		dojo.byId("newrate_total_val").innerHTML = '<span id="myrate_total">' + roundNumber(myrate*amount,4) + ' ' + currency_b  + '</span>'; 
		
		
		try{
                  
                  
                    var orderQuality = 0;
                    var daysIndex = Math.floor(dojo.byId("days").value /3);
                   
                    var OTM = ((rate-myrate)/rate)*100;
                    
                    var rateIndex =Math.floor( 10 - (OTM * 100));
                    var rateIndex = Math.floor(10 + OTM);
                  
                     
                    orderQuality = Math.floor(rateIndex + daysIndex);
                   
                    dojo.byId("stength").value = orderQuality;
                    if(orderQuality > 16){
							dojo.byId("newrate_strength_val").innerHTML = '<span id="myrate_strength"><img src="/images/star_green.gif"/><img src="/images/star_green.gif"/><img src="/images/star_green.gif"/><img src="/images/star_green.gif"/><img src="/images/star_green.gif"/></span>';   
                    }else if(orderQuality > 12){
							dojo.byId("newrate_strength_val").innerHTML = '<span id="myrate_strength"><img src="/images/star_green.gif"/><img src="/images/star_green.gif"/><img src="/images/star_green.gif"/><img src="/images/star_green.gif"/><img src="/images/star_grey.gif"/></span>'; 
					}else if(orderQuality > 8){
							dojo.byId("newrate_strength_val").innerHTML = '<span id="myrate_strength"><img src="/images/star_green.gif"/><img src="/images/star_green.gif"/><img src="/images/star_green.gif"/><img src="/images/star_grey.gif"/><img src="/images/star_grey.gif"/></span>';		
                    }else if(orderQuality > 4){
							dojo.byId("newrate_strength_val").innerHTML = '<span id="myrate_strength"><img src="/images/star_green.gif"/><img src="/images/star_green.gif"/><img src="/images/star_grey.gif"/><img src="/images/star_grey.gif"/><img src="/images/star_grey.gif"/></span>';
					}else if(orderQuality < 1){
							dojo.byId("newrate_strength_val").innerHTML = '<span id="myrate_strength"><img src="/images/star_grey.gif"/><img src="/images/star_grey.gif"/><img src="/images/star_grey.gif"/><img src="/images/star_grey.gif"/><img src="/images/star_grey.gif"/></span>'; 							
                    }else{
							dojo.byId("newrate_strength_val").innerHTML = '<span id="myrate_strength"><img src="/images/star_green.gif"/><img src="/images/star_grey.gif"/><img src="/images/star_grey.gif"/><img src="/images/star_grey.gif"/><img src="/images/star_grey.gif"/></span>';  
                    }
                        
                   
			}catch(err){
				console.log(err);
			}
	
	}


	
roundNumber =  function (rnum, rlength) { // Arguments: number to round, number of decimal places
                var times = 1;
				for(var x = 0; x < rlength; x++){
					times = times * 10;
				}
				var newnumber = Math.round(rnum*times)/times
                return newnumber; 
              }	
	
createSliders = function () {

	
	var percent = (rate/100);	
	var pCent = percent*3;


	  var slider = new dijit.form.HorizontalSlider({
      name: "rateslider",
      value:rate+percent,
      minimum:rate-(pCent*5),
	  maximum:rate+(pCent*5),
	  discreteValues:100,
      intermediateChanges: true,
      style: "width:100px;",
      onChange: function(value){
        dojo.byId("rate").value = roundNumber(value,4);updateMyRate();
      }
    }, "rateslider_span");
	
	 var dayslider = new dijit.form.HorizontalSlider({
      name: "dayslider",
      value: 20,
      minimum: 0,
      maximum: 30,
	  discreteValues:30,
      intermediateChanges:true,
       style: "width:100px;",
      onChange: function(value){
        dojo.byId("days").value = roundNumber(value,0);updateMyRate();
      }
    }, "dayslider_span");
}

pairFormCheck = function() {
	
	if(dojo.byId("amount").value == '') {
		alert('Please enter the amount you wish to change.');
		return false;
	}
	if(dojo.byId("currency_a").value == dojo.byId("currency_b").value) {
		alert('Please choose different currencies.');
		return false;
	}
	
	 if(IsNumeric(dojo.byId("amount").value) == false){
		 alert('Please only enter numbers into the amount field.');
		 return false;
	 }
	
	return true;
}


rateFormCheck = function() {
	
	if(dojo.byId("amount").value == '') {
		alert('Please enter the amount you wish to change.');
		return false;
	}
	if(dojo.byId("currency_a").value == dojo.byId("currency_b").value) {
		alert('Please choose different currencies.');
		return false;
	}
	
	 if(IsNumeric(dojo.byId("amount").value) == false){
		 alert('Please only enter numbers into the amount field.');
		 return false;
	 }
	
	if(dojo.byId("n_amount")) {
		dojo.byId("n_amount").value = dojo.byId("amount").value;
		
	}
	
	
	return true;
}

pairRiskFormCheck = function() {
	
	if(dojo.byId("amount").value == '') {
		alert('Please enter the amount you wish to change.');
		return false;
	}
	if(dojo.byId("currency_a").value == dojo.byId("currency_b").value) {
		alert('Please choose different currencies.');
		return false;
	}
	
	
	
	return true;
}

validate_risk_form = function (thisform)
{
with (thisform)
  {
  
  console.log('validate risk form');
  
  
  
	var name_inputs = dojo.query(".name_input");
	var amount_inputs = dojo.query(".amount_input");
	var errors = true;
	var errors2 = true;
	var msg = '';
		
	dojo.forEach(name_inputs, 
		    function(oneEntry, index, array) {
		       
		        if(oneEntry.value.length == 0){
			       
			        if(amount_inputs[index].value.length > 0)
			        {
			        	errors = false;		        	
			        	oneEntry.className = oneEntry.className + ' riskEmpty';
			        }else{
			        	oneEntry.className = 'name_input';
			        }
			       
		        }
		       		        
		    }
		);
	
	dojo.forEach(amount_inputs, 
		    function(oneEntry, index, array) {
		        
		        if(oneEntry.value.length == 0){
			        
			        if(name_inputs[index].value.length > 0)
			        {
			        	errors = false;
			        	oneEntry.className = oneEntry.className + ' riskEmpty';
			        	
			        }else{
			        	oneEntry.className = 'amount_input';
			        }
			       
		        }else if(IsNumeric(oneEntry.value) == false){
		        	oneEntry.className = oneEntry.className + ' riskEmpty';
		        	errors2 = false;
		        }else{
		        	oneEntry.className = 'amount_input';
		        }
	        
		    }
		);
	
  
  if(errors == false  )
  {
  	alert('You have partially completed risks.  Please enter a name and amount for each risk.');
  	return false;
  }
  
  if(errors2 == false)
  {
	  alert( "You have entered non numeric charachters for a risk amount, please only enter numbers here.");
	  return false;
  }
  return true;
  
  }
}


function IsNumeric(sText)

{
   var ValidChars = "0123456789";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
}

function IsNumericFloat(sText)

{
   var ValidChars = "0123456789.";
   var IsNumber=true;
   var Char;

 
   for (i = 0; i < sText.length && IsNumber == true; i++) 
      { 
      Char = sText.charAt(i); 
      if (ValidChars.indexOf(Char) == -1) 
         {
         IsNumber = false;
         }
      }
   return IsNumber;
   
}

implode = function ( glue, pieces ) {
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Waldo Malqui Silva
    // *     example 1: implode(' ', ['Kevin', 'van', 'Zonneveld']);
    // *     returns 1: 'Kevin van Zonneveld'
 
    return ( ( pieces instanceof Array ) ? pieces.join( glue ) : pieces );
}

/*

CUSTOM FORM ELEMENTS

Created by Ryan Fait
www.ryanfait.com

The only thing you need to change in this file is the following
variables: checkboxHeight, radioHeight and selectWidth.

Replace the first two numbers with the height of the checkbox and
radio button. The actual height of both the checkbox and radio
images should be 4 times the height of these two variables. The
selectWidth value should be the width of your select list image.

You may need to adjust your images a bit if there is a slight
vertical movement during the different stages of the button
activation.

Visit http://ryanfait.com/ for more information.

*/

var checkboxHeight = "25";
var radioHeight = "25";
var selectWidth = "150";

/* No need to change anything after this */

document.write('<style type="text/css">input.styled { display: none; } select.styled { position: relative; width: ' + selectWidth + 'px; opacity: 0; filter: alpha(opacity=0); z-index: 5;  font-size:12px; color:#4D4E4F }</style>');

var Custom = {
	init: function() {

		var inputs = document.getElementsByTagName("input"), span = Array(), textnode, option, active;
		for(a = 0; a < inputs.length; a++) {
			if((inputs[a].type == "checkbox" || inputs[a].type == "radio") && inputs[a].className == "styled") {
				span[a] = document.createElement("span");
				span[a].className = inputs[a].type;

				if(inputs[a].checked == true) {
					if(inputs[a].type == "checkbox") {
						position = "0 -" + (checkboxHeight*2) + "px";
						span[a].style.backgroundPosition = position;
					} else {
						position = "0 -" + (radioHeight*2) + "px";
						span[a].style.backgroundPosition = position;
					}
				}
				inputs[a].parentNode.insertBefore(span[a], inputs[a]);
				inputs[a].onchange = Custom.clear;
				span[a].onmousedown = Custom.pushed;
				span[a].onmouseup = Custom.check;
				document.onmouseup = Custom.clear;
			}
		}
		inputs = document.getElementsByTagName("select");
		for(a = 0; a < inputs.length; a++) {
			if(inputs[a].className == "styled") {
				option = inputs[a].getElementsByTagName("option");
				active = option[0].childNodes[0].nodeValue;
				textnode = document.createTextNode(active);
				for(b = 0; b < option.length; b++) {
					if(option[b].selected == true) {
						textnode = document.createTextNode(option[b].childNodes[0].nodeValue);
					}
				}
			
				span[a] = document.createElement("span");
				span[a].className = "select";
				span[a].id = "select" + inputs[a].name;
				span[a].appendChild(textnode);
				inputs[a].parentNode.insertBefore(span[a], inputs[a]);
				
				inputs[a].onchange = Custom.choose;
				
			}
		}
	},
	pushed: function() {
		element = this.nextSibling;
		if(element.checked == true && element.type == "checkbox") {
			this.style.backgroundPosition = "0 -" + checkboxHeight*3 + "px";
		} else if(element.checked == true && element.type == "radio") {
			this.style.backgroundPosition = "0 -" + radioHeight*3 + "px";
		} else if(element.checked != true && element.type == "checkbox") {
			this.style.backgroundPosition = "0 -" + checkboxHeight + "px";
		} else {
			this.style.backgroundPosition = "0 -" + radioHeight + "px";
		}
	},
	check: function() {
		element = this.nextSibling;
		if(element.checked == true && element.type == "checkbox") {
			this.style.backgroundPosition = "0 0";
			element.checked = false;
		} else {
			if(element.type == "checkbox") {
				this.style.backgroundPosition = "0 -" + checkboxHeight*2 + "px";
			} else {
				this.style.backgroundPosition = "0 -" + radioHeight*2 + "px";
				group = this.nextSibling.name;
				inputs = document.getElementsByTagName("input");
				for(a = 0; a < inputs.length; a++) {
					if(inputs[a].name == group && inputs[a] != this.nextSibling) {
						inputs[a].previousSibling.style.backgroundPosition = "0 0";
					}
				}
			}
			element.checked = true;
		}
	},
	clear: function() {
		inputs = document.getElementsByTagName("input");
		for(var b = 0; b < inputs.length; b++) {
			if(inputs[b].type == "checkbox" && inputs[b].checked == true && inputs[b].className == "styled") {
				inputs[b].previousSibling.style.backgroundPosition = "0 -" + checkboxHeight*2 + "px";
			} else if(inputs[b].type == "checkbox" && inputs[b].className == "styled") {
				inputs[b].previousSibling.style.backgroundPosition = "0 0";
			} else if(inputs[b].type == "radio" && inputs[b].checked == true && inputs[b].className == "styled") {
				inputs[b].previousSibling.style.backgroundPosition = "0 -" + radioHeight*2 + "px";
			} else if(inputs[b].type == "radio" && inputs[b].className == "styled") {
				inputs[b].previousSibling.style.backgroundPosition = "0 0";
			}
		}
	},
	choose: function() {
		option = this.getElementsByTagName("option");
		for(d = 0; d < option.length; d++) {
			if(option[d].selected == true) {
				document.getElementById("select" + this.name).childNodes[0].nodeValue = option[d].childNodes[0].nodeValue;
			}
		}
	}
}

window.onload = Custom.init;

     


     