// JavaScript Document
var blank = new Image();
var myAccessCode = "";
var myAgentCode = "";
var formIsValid = "";
blank.src = 'img/blank.gif';

$(document).ready(function(){
	
	var qstring = location.search;
	var startcode = qstring.indexOf("=") +1;
	var accessString = qstring.substr(startcode,3);
	
	// set up cookie for access code
	if(qstring.indexOf("=") != -1){
		document.cookie = "accessCookie="+accessString;
	}
	
		var getCookie = document.cookie.indexOf("accessCookie=");

		if(getCookie != -1) {
			
			var startCookie = document.cookie.indexOf("accessCookie=");
			startCookie = startCookie + 13;
			
			var theAccess = document.cookie.substr(startCookie,3);
			accessString = theAccess;
			//alert(document.cookie)
		}
		
		if(document.getElementById('accessCode')){
			
			var getAccess = document.getElementById('accessCode');
			getAccess.value = accessString;		
		}	
		
		
		function initAjax(){
			
			var goodcode = "";	
	
			$.ajax({
				type: "POST",
				url: "staplespromo2.xml",
				//url: "accord.php",
				dataType: "xml",
	
				success: function(xml) {
	
					$(xml).find('acode').each(function(){
	
						var myaccess = 	$(this).find('promo').text();
						var myAccessCode = document.getElementById("accessCode").value.toUpperCase();
	
							if(myAccessCode == myaccess){
	
								goodcode = "true";	
		
								var personalCode = $(this).find('personal').text();
								var corpCode = $(this).find('corp').text();
								var offer = $(this).find('offer').text();
								var image = $(this).find('image').text();
								var imagefr = $(this).find('imagefr').text();
								var personalUrl = "https://www.citicards.citibankonline.ca/CAGCB/ICARD/appnew/getApplyNow.do?s="+personalCode+"&p=260061001&ci=p";
								var corpUrl = "https://www.citicards.citibankonline.ca/CAGCB/ICARD/appnew/getApplyNow.do?s="+corpCode+"&p=260061001";
													   
								//$('#currentTitle' + myCell).text(title);
								$('#txtGiveAll').attr("src", image );
								$('#stdApplyLink').attr("href", personalUrl );
								$('#stdApplyLink2').attr("href", personalUrl );
								$('#commApplyLink').attr("href", corpUrl );
							}
	
							if(myAccessCode == ""){
								goodcode = "true";
							}
						}); // close xml

							if(goodcode == "true"){
							//alert(goodcode);
							formIsValid = "";
							//alert("goodcode");
							var step1= document.getElementById('form_step1');
							var step2= document.getElementById('form_step2');
							step1.style.display ="none";
							step2.style.display ="block";
							return formIsValid;	
							
							}
							else{
							//if(myAccessCode !== ""){
							//formIsValid = "Missing or invalid primary area code \n";
							alert("___________________________________________\n\n Your access code is invalid or has expired \n Please try again or enter S9K for our standard offer \n___________________________________________");
							//alert("bsdcode");
							return formIsValid;
							//}
							}
					} // close success
				}); // close ajax
	
				fn_writer();
				return formIsValid;

			} // close initAjax 
								
									 
			function fn_writer(){
				var myCode = document.getElementById('accessCode').value.toUpperCase();
				var iHeard = document.getElementById('heardFrom').value;
				
				$.ajax({
					type: "POST",
					url: "f_write.php",
					data: "accessCode=" +myCode+"&heardFrom="+iHeard,
					//url: "accord.php",
					dataType: "text",

						success: function(xml) {
							
							if (xml.match(/success/ig)) {
							 return;//alert('success');  //file updated
							}
						} //close success
				}); //close ajax
			} //close fn_writer
	
	
		
		$("#btn_go").click(function () {

		pageTracker._trackPageview("/bogi/overview/reg/go");

		var regExp1 = /[^a-zA-Z0-9]/;
		var step1= document.getElementById('form_step1');
		var step2= document.getElementById('form_step2');
		var access = document.getElementById("accessCode").value;

			if(regExp1.test(access) && access.length > 0) {

			alert("Please enter a valid access code");
			document.getElementById("accessCode").focus();

			return false;

			}
			else{
			 //$('#form_step1').fadeOut();
			//$('#form_step2').fadeIn(500);
			initAjax();
			//step1.style.display ="none";
			//step2.style.display ="block";
			}
	   
		});



// POP- UPS -  Dim Screen and Overlay

	// Terms - OPEN
			$("#terms_cond").click(function(){
				//$.dimScreen(1000, 0.7, function() {
					var terms = document.getElementById("termsPopMid");	
					terms.innerHTML ="<iframe src='terms.html' frameBorder='0' width='625' scrolling='yes' height='400'></iframe>"
					//showFrame.style.display ="block";
					$('#termsPop').fadeIn();
				//});	 
			 });
				
	// Terms - CLOSE		
			$("#closeTerms").click(function () {
				//showFrame.style.display ="none";
				$('#termsPop').fadeOut(500);
				//$.dimScreenStop();
			});	

	// Offers - OPEN
			$("#other_offers").click(function(){
				//$.dimScreen(1000, 0.7, function() {
					var terms = document.getElementById("offersPopMid");	
					terms.innerHTML = document.getElementById("otherOffersContent").innerHTML;
					//showFrame.style.display ="block";
					$('#offersPop').fadeIn();
				//});	 
			});
	
	// Offers - CLOSE					
			$("#closeOffers").click(function () {
				//showFrame.style.display ="none";
				$('#offersPop').fadeOut(500);
				//$.dimScreenStop();	 
			});

	// Contact - OPEN
			$("#contact_us").click(function(){
				//$.dimScreen(1000, 0.7, function() {
					var terms = document.getElementById("contactPopMid");	
					terms.innerHTML ="<iframe src='contact_us.html' frameBorder='0' width='625' scrolling='auto' height='400'></iframe>"
					//showFrame.style.display ="block";
					$('#contactPop').fadeIn();
				//});	 
			 });
				
	// Contact - CLOSE		
			$("#closeContact").click(function () {
				//showFrame.style.display ="none";
				$('#contactPop').fadeOut(500);
				//$.dimScreenStop();
			});	



// OVERLAYS -  No Dim Screen and Overlay
 
	// SIGN ON - OPEN
			$("#btn_sign_on").click(function () {
				$('#sign_on_overlay').fadeIn(500);											   
			});

	// SIGN ON - CLOSE
			$("#close_sign_on").click(function () {
				$('#sign_on_overlay').fadeOut(500);						   
			});


// FORMS -  Explain here
 
		function checkcode(){
		var getPriPhone1 = document.getElementById("priPhone1");
		var mycode = document.getElementById("accesscode").value;
		var goodcode = "";	
				   $.ajax({
				   type: "POST",
					url: "staplespromo2.xml",
					//url: "accord.php",
					dataType: "xml",
					success: function(xml) {
									
						$(xml).find('acode').each(function(){
							var trycode = $(this).text();
							if(mycode == trycode){
							//alert("good code");
							goodcode = "true";
							
							//return goodcode;	
							}
							//else{goodcode = "false";}
							
						
						});
				
							if(goodcode == "true"){
						//alert(goodcode);
						getPriPhone1.style.backgroundColor = "#fff";
						getPriPhone1.style.color = "#000";	
						formIsValid = "";
						get(document.getElementById('theForm'));
						//alert("goodcode");
						return formIsValid;	
						
						
						}
						else{
						getPriPhone1.style.backgroundColor = reqColor;
						getPriPhone1.style.color = "#fff";	
						//formIsValid = "Missing or invalid primary area code \n";
						alert("Missing or invalid primary area code");
						//alert("bsdcode");
						return formIsValid;
						}
						
					}
					
				
				}); // close ajax
					
				return formIsValid;	
	} // close  checkcode


// extra benefits page INCLUDED benefits copy change
	
	$("#included_header").click(function () {
			$('#includedBen').show();
			$('#optionalBen').hide();
			$('#acctMngt').hide();									   
	});

// extra benefits page OPTIONAL benefits copy change

	$("#optional_header").click(function () {
			$('#includedBen').hide();
			$('#optionalBen').show();
			$('#acctMngt').hide();									   
	});

// extra benefits page OPTIONAL benefits copy change

	$("#account_header").click(function () {
			$('#includedBen').hide();
			$('#optionalBen').hide();
			$('#acctMngt').show();									   
	});



// PNG FIX -  Get Browser Good or Bad ;-)

		var badBrowser = (/MSIE ((5\.5)|6)/.test(navigator.userAgent) && navigator.platform == "Win32");
		if (badBrowser) {
		 // get all pngs on page
		 $('img[src$=.png]').each(function() {
		   if (!this.complete) {
			 this.onload = function() { fixPng(this) };
		   } else {
			 fixPng(this);
		   }

		 });

	   }
	

// PNG FIX -  Code to fix PNGs in IE
 
	 function fixPng(png) {
	   // get src
	   var src = png.src;
	   // set width and height
		   if (!png.style.width) { png.style.width = $(png).width(); }
		   if (!png.style.height) { png.style.height = $(png).height(); }
		   // replace by blank image
		   png.onload = function() { };
		   png.src = blank.src;
		   // set filter (display original image)
		   png.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
		 }
	 });


// DIM SCREEN EFECT -  Code to create the global Dims
 
	// DIM SCREEN EFECT - SET DIM
			jQuery.extend({
				//dims the screen
				dimScreen: function(speed, opacity, callback) {
					if(jQuery('#dimScreen').size() > 0) return;
					
					if(typeof speed == 'function') {
						callback = speed;
						speed = null;
					}
			
					if(typeof opacity == 'function') {
						callback = opacity;
						opacity = null;
					}
			
					if(speed < 1) {
						var placeholder = opacity;
						opacity = speed;
						speed = placeholder;
					}
					
					if(opacity >= 1) {
						var placeholder = speed;
						speed = opacity;
						opacity = placeholder;
					}
			
					speed = (speed > 0) ? speed : 500;
					opacity = (opacity > 0) ? opacity : 0.5;
					if ($.browser.msie) {
					return jQuery('<div></div>').attr({
							id: 'dimScreen'
							,fade_opacity: opacity
							,speed: speed
						}).css({
						background: '#555'
						,height: $(document).height() + 'px'
						,left: '0px'
						,opacity: 0
						,position: 'absolute'
						,top: '0px'
						,width:  '100%'
						,zIndex: 999
					}).appendTo(document.body).fadeTo(speed, 0.7, callback);
					
					}
					else{
							return jQuery('<div></div>').attr({
							id: 'dimScreen'
							,fade_opacity: opacity
							,speed: speed
						}).css({
						background: '#555'
						,height: $(document).height() + 'px'
						,left: '0px'
						,opacity: 0
						,position: 'absolute'
						,top: '0px'
						,width: $(document).width() + 'px'
						,zIndex: 999
					}).appendTo(document.body).fadeTo(speed, 0.7, callback);
						
					}
				},
		
		
	// DIM SCREEN EFECT - STOP DIM
				dimScreenStop: function(callback) {
					var x = jQuery('#__dimScreen');
					var opacity = x.attr('fade_opacity');
					var speed = x.attr('speed');
					
						x.fadeOut(1000, function() {
							x.remove();
							if(typeof callback == 'function') callback();
						});
				}



	
});