
var browser;
agt=navigator.userAgent;
uVer=navigator.appVersion.substring(0,4);

if (agt.indexOf("MSIE") == -1)

         browser="ns";

else {
         browser="ms";
}

// path to calculator images
	var calcimage = "";	
	if(browser == "ns") { calcimage = "";}
	else if(browser == "ns" && document.getElementById) { calcimage = "";} //for NS 6 

// path to calculator form	
	var calcform = "document.forms['calc'].";	
	if(browser == "ns" && document.layers) { calcform = "document.forms['calc'].";}
	else if(browser == "ns" && document.getElementById) { calcform = "document.getElementById('calc').";}
	

// image preload
/*  shandfor : deprecated
	if(document.images) {
				
		var clothes = new Image();
		clothes.src = '/images/gmcard/gmcal_silverado_en_CA.jpg';
		
		var travel = new Image();
		travel.src = '/images/gmcard/gmcal_denali_en_CA.jpg';
		
		var dine = new Image();
		dine.src = '/images/gmcard/gmcal_caddy_en_CA.jpg';
		
		var books = new Image();
		books.src = '/images/gmcard/gmcal_aztek_en_CA.jpg';
		
		var music = new Image();
		music.src = '/images/gmcard/gmcal_corvet_en_CA.jpg';
		
		var misc = new Image();
		misc.src = '/images/gmcard/gmcal_avalanche_en_CA.jpg';
	}
	
*/
	
// calc imagechanger	:: shandfor : deprecated
	function show(num) {
		return;
		/*
		if(document.images) {
			eval(calcimage + "document.c_image.src = " + num + ".src");
		}
		*/
	}
	
// Calc image randomizer
// var which = 0;
// function change() {
// var next = which;
// while ( (next == which) || (next > 3) ) {
// next = Math.ceil(3 * (Math.random() + 0.1));
// }
// eval(calcimage + "document.c_theme.src = \'/images/gmcard/gmcal_" + next + "_en_CA.jpg\'"); 
// which = next;
// }


// convert entry to valid number by removing everything but 0-9 or '.'

function MakeNum(newNumber){
	newStr=new String;
	newNumber = newNumber + "";
	if(newNumber.charAt(0) == ".") newNumber = "0" + newNumber;
	strLength=newNumber.length;
	
	for(var i=0; i<=strLength; i++) {
		tmpChar=newNumber.charAt(i)		
		//convert non-alpha to letter to ensure fail (except for space, which we'll skip)
		if (tmpChar=="-" || tmpChar=="+" || tmpChar==")" || tmpChar=="(" || tmpChar=="*" 
		  || tmpChar=="&" || tmpChar=="#" || tmpChar=="!" || tmpChar=='"' || tmpChar=="/" 
		  || tmpChar=="'" || tmpChar=="$" || tmpChar=="," || tmpChar=="%") {tmpChar="h"}
		  
		if (tmpChar==" "){tmpChar=""}
		
		// unnecessary? testChar=parseInt(tmpChar)
		if(tmpChar=="." || tmpChar<="9") {newStr=newStr+tmpChar}
	
	}	
	
	if(isNaN(parseInt(newStr))) return "0";	
	return pad(Math.round(newStr*100)/100);	
}

//pads out field to fake right-justification
//length fixed as constant
function pad(number) {
     var str = '' + number;
     while (str.length < 4) { str = ' ' + str; }
     return str;
 }

function formatMoney(Amt) {
	//mult by 100, round, drop signs
	var N=Math.abs(Math.round(Amt*100));
	//leading zero's if less than 0.10/1.00
	var S=((N<10)?"00":((N<100)?"0":""))+N;
	S=S.substring(0,(S.length-2)) + "." + S.substring((S.length-2),S.length);
	return pad(S);
}	
	
/* Calculate functions */

function calculate(field) {
	var fieldVal, newVal;
	var total = 0;
	if ((field+0) > 0) {		
		eval("fieldVal = Math.round(" + calcform + "exp_" + (field - 0) + ".value)");
		newVal = MakeNum(fieldVal);
		eval(calcform + "exp_" + (field - 0) + ".value = newVal");
	}
	
	for (var i=1;i<7;i++) {
		eval("total += " + calcform + "exp_" + i + ".value - 0");
	}
	eval(calcform + "total.value = '  ' + MakeNum(total-0)");
	eval(calcform + "bonus.value = ' --- '");
	eval(calcform + "monthly.value = ' --- '");
}

function calcSavings() {
	var total = 0;
	for (var i=1;i<7;i++) {
		eval("total += " + calcform + "exp_" + i + ".value - 0");
	}
	
	eval(calcform + "bonus.value = '  ' + formatMoney(12*total*0.03)");
	eval(calcform + "monthly.value = '  ' + formatMoney(total*0.03)");
	// change();
}

/* 11/08/2010 added by Ping Zhang for gm card ICRD0206 */
function clearForm(form) {
  // iterate over all of the inputs for the form
  // element that was passed in
  $(':input', form).each(function() {
 var type = this.type;
 var tag = this.tagName.toLowerCase(); // normalize case
 // it's ok to reset the value attr of text inputs,
 // password inputs, and textareas
 if (type == 'text' || type == 'password' || tag == 'textarea')
   this.value = "";
 // checkboxes and radios need to have their checked state cleared
 // but should *not* have their 'value' changed
 else if (type == 'checkbox' || type == 'radio')
   this.checked = false;
 // select elements need to have their 'selectedIndex' property set to -1
 // (this works for both single and multiple select elements)
 else if (tag == 'select')
   this.selectedIndex = -1;
  });
}

$(function()
  {
         $('#q').blur(function(){
			var q_val=$('#q').val();
			if(q_val=='')
			 {
				$('#q').val("search");
			 }
		 });

		 var new_src1=$('#chat_now_home').attr("longdesc");
		 var old_src1=$('#chat_now_home').attr("src");
	 
		 $('#chat_now_home').hover(
		 function(){
			$(this).attr("src",new_src1);
			$(this).css("cursor","pointer");
		 },
 		 function(){
			$(this).attr("src",old_src1);
		 }
		 );
		 
		 var new_src2=$('#chat_now_contact').attr("longdesc");
		 var old_src2=$('#chat_now_contact').attr("src");
	 
		 $('#chat_now_contact').hover(
		 function(){
			$(this).attr("src",new_src2);
			$(this).css("cursor","pointer");
		 },
 		 function(){
			$(this).attr("src",old_src2);
		 }
		 );
		
		 
		 var new_src3=$('#signup_home').attr("longdesc");
		 var old_src3=$('#signup_home').attr("src");
		 $('#signup_home,#offer_home,#dealer_home').hover(
		 function(){
			$(this).attr("src",new_src3);
			$(this).css("cursor","pointer");
		 },
 		 function(){
			$(this).attr("src",old_src3);
		 }
		 );
		 
		 var new_src4=$('.my_earnings_home').attr("longdesc");
		 var old_src4=$('.my_earnings_home').attr("src");
		 $('.my_earnings_home').hover(
		 function(){
			$(this).attr("src",new_src4);
			$(this).css("cursor","pointer");
		 },
 		 function(){
			$(this).attr("src",old_src4);
		 }
		 );
		 
		var earnings_url=window.location.href.split('/');
		earnings_url=earnings_url[earnings_url.length-1];
		
		if(earnings_url==='myearnings')
		{
			$('.my_earnings_home').attr("src",new_src4);
			
		}else
		{
			$('.my_earnings_home').attr("src",old_src4);
		}
		
		 
		 var new_src5=$('#dealer_contact').attr("longdesc");
		 var old_src5=$('#dealer_contact').attr("src");
		 $('#dealer_contact,#signup_contact').hover(
		 function(){
			$(this).attr("src",new_src5);
			$(this).css("cursor","pointer");
		 },
 		 function(){
			$(this).attr("src",old_src5);
		 }
		 );
		 
		 var new_src6=$('#rules_on').attr("longdesc");
		 var old_src6=$('#rules_on').attr("src");
		 $('#rules_on').hover(
		 function(){
			$(this).attr("src",new_src6);
			$(this).css("cursor","pointer");
		 },
 		 function(){
			$(this).attr("src",old_src6);
		 }
		 );
		 
		 var new_src7=$('#rules_off').attr("longdesc");
		 var old_src7=$('#rules_off').attr("src");
		 $('#rules_off').hover(
		 function(){
			$(this).attr("src",new_src7);
			$(this).css("cursor","pointer");
		 },
 		 function(){
			$(this).attr("src",old_src7);
		 }
		 );
		 
		 var new_src8=$('#check_earnings').attr("longdesc");
		 var old_src8=$('#check_earnings').attr("src");
		 $('#check_earnings').hover(
		 function(){
			$(this).attr("src",new_src8);
			$(this).css("cursor","pointer");
		 },
 		 function(){
			$(this).attr("src",old_src8);
		 }
		 );
		 
		 
		 $('.my_earnings_home').click(function(){
			window.location="/gm/" + language +"/services/gm-card/myearnings";
			
		 });
		 
		 
		 $('#offer_home,#offer_home,#map_offer_home,#dealer_home,#dealer_contact,#map_dealer_home').hover(
		 
	 		 function(){
				$(this).css("cursor","pointer");
			 }
		 );
		 
		 
		 $('#offer_home,#offer_home,#map_offer_home').click(function(){
			openCurrentOffers(document.location.href);
			
		 });
		 $('#dealer_home,#dealer_contact,#map_dealer_home').click(function(){
			window.location="/gm/" + language +"/hostredirect?host=http://apps.gm.ca/app/GMCanada/DealerLocatorSearch.do&nav=0&cta=blank&lang=" + lang;
			
		 });					
		 
		
		 /* add style to form input	 */
		 $("#form_a input:text,#form_b input:text,#form_c input:text,#form_d input:text").addClass("form_input");
         /* add style to form input ends */
		 /* light box for my earnings*/
		 
		 /* light box for my earnings ends*/
		 $('#myearnings_form input:text:lt(2)').addClass('input_bg');
		 $('#birth_mm').addClass('birth_month');
		 $('#birth_yy').addClass('birth_year');
		 $('#last_name').addClass('last_name');

		 for(var i=1;i<13;i++){
			if(i<10)
			{$('#birth_mm').append("<option value='0"+i+"'>0"+i+"</option>");}else
			{
			$('#birth_mm').append("<option value='"+i+"'>"+i+"</option>");
			}
			}
			
		for(var j=2010;j>=1900;j--){
			$('#birth_yy').append("<option value='"+j+"'>"+j+"</option>");
			}	
		 
		 /* auto tab for my earnings form*/
	/*	  $('#myearnings_form input[name*="account_number"]:first').focus();
		 $('#myearnings_form input:text').autotab_magic();
		 $('#myearnings_form input[name*="account_number"]').autotab_filter({
				format: 'numeric',
				nospace: true
		});   
		
	*/
		 /* auto tab for my earnings form ends*/
		 
		 
		 
});

$(function(){
	$('.fix-z-index').bgiframe();
	
	$('#faq_first').click();
	$('#allow_first').click();
	
	
	$('#print_this_page').click(function(){
			window.print();
		});
		$('#chat_now_btn').click(function(){

		if (language == "french") {
			window.open('https://www.chat.gm.ca/netagent/gm_cimlogin.aspx?questid=995A6FEE-0743-49C8-A2BE-D711E94ECD73&portid=99F7DAD1-1FE2-4A36-AB98-FD27C9BDA71F&nareferer='+escape(document.location),'_blank','resizable= yes,width=581,height=437,scrollbars=yes');
			} else {
			window.open('https://www.chat.gm.ca/netagent/gm_cimlogin.aspx?questid=374502E7-C2BE-4A92-8769-01F6CA1241BB&portid=BE838826-FC4A-4C98-8D6C-505D4E9CED5D&nareferer='+escape(document.location),'_blank','resizable= yes,width=581,height=437,scrollbars=yes');
		}
		return false;
		}); 
	
		$('#chat_now,#chat_now_home,#chat_now_contact').click(function(){

		if (language == "french") {
			window.open('https://www.chat.gm.ca/netagent/gm_cimlogin.aspx?questid=995A6FEE-0743-49C8-A2BE-D711E94ECD73&portid=99F7DAD1-1FE2-4A36-AB98-FD27C9BDA71F&nareferer='+escape(document.location),'_blank','resizable= yes,width=581,height=437,scrollbars=yes');
			} else {
			window.open('https://www.chat.gm.ca/netagent/gm_cimlogin.aspx?questid=374502E7-C2BE-4A92-8769-01F6CA1241BB&portid=BE838826-FC4A-4C98-8D6C-505D4E9CED5D&nareferer='+escape(document.location),'_blank','resizable= yes,width=581,height=437,scrollbars=yes');
		}
		return false;
		}); 
	

	/* form validation */
	
	//$('#myearnings_form').validate();
	/* form validation ends*/
	
	$('#auth_user_last4').focus();
	

	var response_text=$('#response_content').html();
	$("#myearnings_form").submit(function(event) {
		event.preventDefault();
	
	/* get today's date */
		var now=new Date();
		var m_names = new Array("January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December");
		var m=now.getMonth();
		var d=now.getDate();
		var y=now.getFullYear();
		var current_date=m_names[m]+" "+d+", "+y;
		var url = "/gm/" + language + "/getgmcarddata";

		// submit the data to the server with AJAX
		$.getJSON(url + "?" + $('#myearnings_form').serialize(), function(response) {
			// everything is ok. (the server returned true)
		
			if ( response.statusCode == 0 ) {
				$('#response_content').html(response_text);
				if($('#lightbox_header_headline').hasClass("hideme"))
				{
					$('#lightbox_header_headline').removeClass("hideme");
				}
				
				$('#step1_msg,#step2_msg,#step3_msg,#step4_msg').text('');
				$('#firstName').text(response.firstName);
				$('#lastName').text(response.lastName);
				$('#middleName').text(response.middleName);
				$('#current_date').text(response.statusDate);
				$('#currentEarnings').text(response.currentEarnings);
				
				clearForm($('#myearnings_form'));
				$('#auth_user_last4,#postal_code,#last_name,#birth_mm,#birth_yy').removeClass('red_border');
				
				$('#response_content').dialog({
					modal:true, 
					width: 890,
					height:730,
					minWidth:800
				});
				/* fix ie6 z-index issue */
				if(typeof document.body.style.maxHeight === "undefined") {
						$('#step3').addClass('hideme');
				} else {
					   $('#step3').removeClass('hideme');
				}

				$('#lightbox_icons1').click(function(){window.print();});
				$('#lightbox_icons2').click(function(){
					$('#response_content').dialog( "destroy" );
					if(typeof document.body.style.maxHeight === "undefined") {
							if($('#step3').hasClass('hideme'))
							{
								$('#step3').removeClass('hideme');
							}
							
					}

					});
					
		
		$('#go_dealer,#go_redeem,#go_offer').hover(function(){
			$(this).css("cursor","pointer");
			//alert ("woo!");
		 });
		 
		 $('#go_redeem').click(function(){
			window.location="/gm/" + language +"/services/gm-card/redemption/allowances";
			
		 });

		
		$('#go_offer').click(function(){
			openCurrentOffers(document.location.href);
		 });		 
		 
		 $('#go_dealer').click(function(){
			window.location="/gm/" + language +"/hostredirect?host=http://apps.gm.ca/app/GMCanada/DealerLocatorSearch.do&nav=0&cta=blank&lang=" + lang;
		 });
		 
		 

			}
			
			// server-side validation failed. use invalidate() to show errors
			else if(response.statusCode==200) {
					$('#step1_msg,#step2_msg,#step3_msg,#step4_msg').text('');
					$('#lightbox_header_headline').addClass("hideme");
					
					if(language=="french")
					{
						$('#lightbox_content1').html("<br/><br/><br/><h2 class='highlight_blue_18'>D&#233;sol&#233;s !</h2><p>Nous sommes dans l'impossibilit&#233; de r&#233;cup&#233;rer votre dossier pour le moment et nous nous excusons de ce contretemps. Veuillez essayer de nouveau un peu plus tard ou appelez au Centre de primes de La Carte GM<br/>au <span class='phone'>1 888 446-6232</span> pour de plus amples renseignements.</p>");
					}else
					{
						$('#lightbox_content1').html("<br/><br/><br/><h2 class='highlight_blue_18'>Sorry!</h2><p>We are unable to retrieve your record at this time. We sincerely apologize for the inconvenience.<br/>Please try again later, or call the GM Card Redemption Centre at <span class='phone'>1-888-446-6232</span> for more information.</p>");
					}
					$('#auth_user_last4,#postal_code,#last_name,#birth_mm,#birth_yy').removeClass('red_border');
					$('#response_content').dialog({
						modal:true, 
						width: 890,
						height:730,
						minWidth:800
					});
					if(typeof document.body.style.maxHeight === "undefined") {
						$('#step3').addClass('hideme');
		
						} else {
							   $('#step3').removeClass('hideme');
						}

		$('#go_dealer,#go_redeem,#go_offer').hover(function(){
			$(this).css("cursor","pointer");
			//alert ("woo!");
		 });
		 
		 $('#go_redeem').click(function(){
			window.location="/gm/" + language +"/services/gm-card/redemption/allowances";
			
		 });

		
		$('#go_offer').click(function(){
			openCurrentOffers(document.location.href);
		 });		 
		 
		 $('#go_dealer').click(function(){
			window.location="/gm/" + language +"/hostredirect?host=http://apps.gm.ca/app/GMCanada/DealerLocatorSearch.do&nav=0&cta=blank&lang=" + lang;
		 });
			
			}
			
			else if(response.statusCode==100) {
					$('#step1_msg,#step2_msg,#step3_msg,#step4_msg').text('');
					$('#lightbox_header_headline').addClass("hideme");
					
					if(language=="french")
					{
						$('#lightbox_content1').html("<br/><br/><br/><h2 class='highlight_blue_18'>D&#233;sol&#233;s !</h2><p>Nous n'avons aucun dossier li&#233; aux renseignements que vous avez fournis. Veuillez v&#233;rifier l'information entr&#233;e et essayer de nouveau ou appelez au Centre de primes de La Carte GM au <span class='phone'>1 888 446-6232</span> et nous serons ravis de vous aider.</p>");
					}else
					{
						$('#lightbox_content1').html("<br/><br/><br/><h2 class='highlight_blue_18'>Sorry!</h2><p>We cannot find any records with the information you entered. Please verify your information and try again,<br/>or call the GM Card Redemption Centre at <span class='phone'>1-888-446-6232</span> and we will be happy to assist you.</p>");
					}
					$('#auth_user_last4,#postal_code,#last_name,#birth_mm,#birth_yy').removeClass('red_border');
					$('#response_content').dialog({
						modal:true, 
						width: 890,
						height:730,
						minWidth:800
						}
					);					
					if(typeof document.body.style.maxHeight === "undefined") {
							$('#step3').addClass('hideme');
					} else {
						   $('#step3').removeClass('hideme');
					}

		$('#go_dealer,#go_redeem,#go_offer').hover(function(){
			$(this).css("cursor","pointer");
			//alert ("woo!");
		 });
		 
		 $('#go_redeem').click(function(){
			window.location="/gm/" + language +"/services/gm-card/redemption/allowances";
			
		 });

		
		$('#go_offer').click(function(){
			openCurrentOffers(document.location.href);
		 });		 
		 
		 $('#go_dealer').click(function(){
			window.location="/gm/" + language +"/hostredirect?host=http://apps.gm.ca/app/GMCanada/DealerLocatorSearch.do&nav=0&cta=blank&lang=" + lang;
		 });
			}
			
			else if (response.statusCode == -100) {
				if(response.auth_user_last4)	
				{
					$('#step1_msg').text(response.auth_user_last4);
					$('#auth_user_last4').addClass('red_border');
				}else
				{
					$('#step1_msg').text('');
					$('#auth_user_last4').removeClass('red_border');
				}
				
				if(response.postal_code)
				{
					$('#step2_msg').text(response.postal_code);
					$('#postal_code').addClass('red_border');
				}else
				{
					$('#step2_msg').text('');
					$('#postal_code').removeClass('red_border');
				}
				
				if(response.birth_mm||response.birth_yy)
				{
					$('#step3_msg').text(response.birth_mm);
					if(response.birth_mm){$('#birth_mm').addClass('red_border');}
					if(response.birth_yy){$('#birth_yy').addClass('red_border');}
				}else
				{
					$('#step3_msg').text('');
					if(!response.birth_mm){$('#birth_mm').removeClass('red_border');}
					if(!response.birth_yy){$('#birth_yy').removeClass('red_border');}
				}
				
				if(response.last_name)
				{
					$('#step4_msg').text(response.last_name);
					$('#last_name').addClass('red_border');
				}else
				{
					$('#step4_msg').text('');
					$('#last_name').removeClass('red_border');
				}
			}
				
			
		});
		
		// prevent default form submission logic
		
		
});

		 
$('#lightbox_icons1').click(function(){window.print();});
$('#lightbox_icons2').click(function(){
	$('#response_content').dialog( "destroy" );
	if(typeof document.body.style.maxHeight === "undefined") {
			if($('#step3').hasClass('hideme'))
			{
				$('#step3').removeClass('hideme');
			}
			
	}

	});

});

// TRACKING CHAT BUTTON SC - Michael M
// ---------------------------------------------------------------------------------------------------
function trackButton(Obj, Type){
	
	switch(Type){
		case 1:
			var s=s_gi('s_account'); s.tl(Obj,'o','The GM Card - CHAT NOW BUBBLE'); 
			break;
		case 2:
			var s=s_gi('s_account'); s.tl(Obj,'o','The GM Card - HOME - CHAT NOW Btn');
			break;
		case 3:
			var s=s_gi('s_account'); s.tl(Obj,'o','The GM Card - CONTACT US - CHAT NOW Btn');
			break;
		case 4:
			var s=s_gi('s_account'); s.tl(Obj,'o','The GM Card - MY EARNINGS - CHAT NOW Btn');
			break;
	}
	
	return false;
	
}


