function pop_up(url, width, height){
	var halfwinwidth = width / 2;
	var halfwinheight = height / 2;
	leftpos = screen.width / 2 - halfwinwidth;
	toppos = screen.height / 2 - halfwinheight;
	window.open(url,'popwin','width=' + width +',height=' + height + ',left=' + leftpos + ',top=' + toppos + ', scrollbars=yes');
}


//  ********************  Modal Decoration Script  *********************//
/* Modal usage 
			<div id='modalwindow'>
				<div class='container' style=''>
					<a href='#' id='closebutton'><img align='right' src='your_close_button' border='0' /></a>
					<br clear='all' />
					<div class='content'>
					</div>
					<div class='footer'>
					</div>
				</div>
			</div>
			
			// to make the close button work. Keep this one in document load
			$('#closebutton').click(function(e){
				e.preventDefault();
				$('#modalwindow').hide();
				$('.transparency').remove();

			});
			
			// on window resize, we need to spread the modal semi-transparent background
			$(window).resize(function() {
				var height = ($(window).height()>$(document).height())?$(window).height():$(document).height();
				var width = ($(window).width()>$(document).width())?$(window).width():$(document).width();
						$('.transparency').css('height',height);
						$('.transparency').css('width',width);
			});	
			
			// then you can call onclick='openModal'
			
			

*/

			var overlays = new Array();
			overlays[2]='contest/june10/images/overlay_atlantic.png';
			//overlays[3]='';
			//overlays[4]='';
			overlays[5]='contest/june10/images/overlay_dominion.png';
			overlays[6]='contest/june10/images/overlay_extra_foods.png';
			//overlays[7]='';
			overlays[8]='contest/june10/images/overlay_fortinos.png';
			//overlays[9]='';
			overlays[10]='contest/june10/images/overlay_loblaws.png';
			//overlays[11]='';
			//overlays[12]='';
			overlays[13]='contest/june10/images/overlay_maxi.png';
			overlays[14]='contest/june10/images/overlay_metro.png';
			overlays[15]='contest/june10/images/overlay_nofrills.png';
			overlays[16]='contest/june10/images/overlay_provigo.png';
			overlays[17]='contest/june10/images/overlay_realcanadian.png';
			//overlays[18]='';
			overlays[19]='contest/june10/images/overlay_save_easy.png';
			//overlays[20]='';
			overlays[21]='contest/june10/images/overlay_sobeys.png';
			overlays[22]='contest/june10/images/overlay_independent.png';
			overlays[23]='contest/june10/images/overlay_valu-mart.png';
			//overlays[24]='';
			overlays[25]='contest/june10/images/overlay_zehrs.png';
			//overlays[26]='';
		function openModal(content,footercontent){
			var storeSelect = document.getElementById('store_code');
			var store_code = storeSelect.options[storeSelect.selectedIndex].value;
			//alert(store_code );
			// show nothing if there's nothing selected as store code
			if(store_code=='') return;
			
			if(!overlays[store_code]) return;
			
			var province = document.getElementById('province');
			var province_code = province.options[province.selectedIndex].value;
			
			// for Metro, we only do it for Ontario
			if(store_code=='14' && province_code!='ON') return;
			
			// for sobeys, we do it for NS, NB, NFLD and PEI
			if(store_code=='21' && province_code!='NS' && province_code!='NB' && province_code!='NL' && province_code!='PE' ) return;
			
			$("#modalwindow .container").css('background-image','url('+overlays[store_code]+')');
			
			$('body').append('<div class="transparency">');
			$('#modalwindow .content').html(content);
			$('#modalwindow .footer').html(footercontent);
			
			if(!ie) $('#modalwindow').CenterIt({ignorechildren:false});
			else {
					$('#modalwindow').css('display','block');
					$('#modalwindow').css('position','absolute');
					$('#modalwindow').css('top','200');
					$('#modalwindow').css('left','50%');
					var off = $('#modalwindow').offset();
					$('#modalwindow').css('left',(off.left-370)+'px');
					$('#modalwindow').css('z-index','20000');
					
			}
			$('.transparency').css('height',$(document).height());
			$('.transparency').css('width',$(document).width());
			$('#modalwindow').css('display','block');
		}
//  ********************  CheckBox Decoration Script  *********************//

//global variables that can be used by ALL the function son this page. 
var inputs; 
var imgFalse = '../contest/june10/images/checkbox.png'; 
var imgTrue = '../contest/june10/images/checkbox-selected.png'; 
this.label2value = function(){	
	var inactive = "inactive";
	var active = "active";
	var focused = "focused";
	
	$("label").each(function(){		
		obj = document.getElementById($(this).attr("for"));
		if(($(obj).attr("type") == "text") || (obj.tagName.toLowerCase() == "textarea")){			
			$(obj).addClass(inactive);			
			var text = $(this).text();
			$(this).css("display","none");
			
			// Added custom code not to override pre-populated user infos
			if(!doNotOverrideTextValues){
				$(obj).val(text); // skip if doNotOverride is true
			}
			
			$(obj).focus(function(){	
				$(this).addClass(focused);
				$(this).removeClass(inactive);
				$(this).removeClass(active);								  
				if($(this).val() == text) $(this).val("");
			});	
			$(obj).blur(function(){	
				$(this).removeClass(focused);													 
				if($(this).val() == "") {
					$(this).val(text);
					$(this).addClass(inactive);
				} else {
					$(this).addClass(active);		
				};				
			});				
		};	
	});		
};

function stopRKey(evt) {
  var evt = (evt) ? evt : ((event) ? event : null);
  var node = (evt.target) ? evt.target : ((evt.srcElement) ? evt.srcElement : null);
  if ((evt.keyCode == 13) && (node.type=="text"))  {
	submitpromoform();
	return false;
  }
}


document.onkeypress = stopRKey; 

$(document).ready(function(){	
	
	// for label to value functions
	label2value();	
	
	// For modal close button
	$('#closebutton').click(function(e){
		e.preventDefault();
		$('#modalwindow').hide();
		$('.transparency').remove();

	});
	
	//
	$('.hoverimg').hover(function(){
		var temp = $(this).attr('src');
		$(this).attr('src',$(this).attr('hsrc'));
		$(this).attr('hsrc',temp);
	}, function(){
		var temp = $(this).attr('hsrc');
		$(this).attr('hsrc',$(this).attr('src'));
		$(this).attr('src',temp);
	});

	// For PIC code auto tab features
	$('input.pinautotab').autotab();
	
	// this is for pre-loading the overlay images.
	for(i=0; i<overlays.length; i++){
		if(overlays[i]){
		var img = new Image();
			$(img).attr('src', overlays[i]);
		}
	}
});
	var inprogress=false;
	function submitpromoform(){
	
		// we do not want to do anything if user already hit enter
		if(inprogress) {
			alert("Processing");
		return false;
		}

		// flagging that we are in progress to avoid double click		
		inprogress=true;
		
			var objectArray =  $("#promoform").serializeArray();

			var data = "";
			for(var i = 0; i < objectArray.length; i++) {
				data += "&" + objectArray[i]["name"] + "=" + objectArray[i]["value"];
			}
		  $.ajax({ url: "contest/may10/includes/process.php",async: false, cache:false, data:data, success: function(data){
			
			
			var success=false;
			if(data.length>=10 && data.substring(0,9)=='Thank you') success=true;
			
			
			if(!success){
				Recaptcha.reload(); // Reload recaptcha image
				alert(data);
			} else {
				$('#msg').html(data);
				$('#thankyoubox').css('display','block');
				$('#promoform').css('display','none');
				$('#mainheader').html('');
				
			}
			}});
		
		// unflagging so that user can submit again
		  inprogress=false;
		  return false;
	}
	
//this function runs when the page is loaded, put all your other onload stuff in here too. 
function init() { 
    replaceChecks(); 
} 
function replaceChecks() { 
     
    //get all the input fields on the page 
    inputs = document.getElementsByTagName('input'); 

    //cycle trough the input fields 
    for(var i=0; i < inputs.length; i++) { 

        //check if the input is a checkbox 
        if(inputs[i].getAttribute('type') == 'checkbox') { 
             
            //create a new image 
            var img = document.createElement('img'); 
             
            //check if the checkbox is checked 
            if(inputs[i].checked) { 
                img.src = imgTrue; 
            } else { 
                img.src = imgFalse; 
            } 
            //set image CLASS reference 
            img.className = 'ImageCheckbox'; 
            //set image ID and onclick action 
            img.id = 'checkImage'+i; 
            //set image 
            img.onclick = new Function('checkChange('+i+')'); 
            //place image in front of the checkbox 
            inputs[i].parentNode.insertBefore(img, inputs[i]); 
            //hide the checkbox 
            inputs[i].style.display='none'; 
        } 
    } 
} 

//change the checkbox status and the replacement image 
function checkChange(i) { 
	
    if(inputs[i].checked) { 
		if(inputs[i].name=='opt_in') $('.bonuspin').attr('value','');
			
        inputs[i].checked = ''; 
        document.getElementById('checkImage'+i).src=imgFalse; 
    } else { 
		if(inputs[i].name=='opt_in'){
			$('.bonuspin').each(function(){
				$(this).attr('value',$(this).attr('rvalue'));
			});
		}
        inputs[i].checked = 'checked'; 
        document.getElementById('checkImage'+i).src=imgTrue; 
    } 
} 

window.onload = init;
$(window).resize(function() {
	var height = ($(window).height()>$(document).height())?$(window).height():$(document).height();
	var width = ($(window).width()>$(document).width())?$(window).width():$(document).width();
	$('.transparency').css('height',height);
	$('.transparency').css('width',width);
});	


