include ("ajax");
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

function systemRequirements() {
   var popup = new NeoPopup(
        'mypop',
        {
            'title': sysRequireTitle, 'class': 'KQPopup', 'content': document.getElementById('system-requirements').innerHTML,
            '_textValues': stdPopupTranslations,
            'mode': 'modal'
        }
    );
    popup.alert();
    return false;
}

addLoadEvent(init_page);

addLoadEvent(function() {
  /* more code to run on page load */
});

function init_page() {
	//document.getElementById('whatis_div').style.height = (document.getElementById('redeem_div').offsetHeight - 4)  + 'px';
			}

function check_code() {
	var code = document.getElementById('code_1').value + document.getElementById('code_2').value + document.getElementById('code_3').value;
	get_ajax_data('check_prizecode.phtml', 'code='+code);
}

function show_result(result) {
	var tmp_arr = result.split("|");
	var result_arr = new Array();
	for (var key = 0; key < tmp_arr.length; ++key) {
		var split_arr = tmp_arr[key].split('=');
		var elementName = split_arr.shift();
		result_arr[elementName] = split_arr.join('=');
	}
	alert_str = '';
	for (key in result_arr) {
		if (result_arr.hasOwnProperty(key)) {
			alert_str += key + ": " + result_arr[key] + "\n";
		}
	}
	//alert(alert_str);
	if (result_arr['error'] == 0) {
		document.getElementById('token_img').src = 'http://images.neopets.com/keyquest/tokens/' + result_arr['token_src'] + '_80.gif';
		document.getElementById('item_img').src = 'http://images.neopets.com/items/' + result_arr['prize_src'] + '.gif';
		document.getElementById('token_name').innerHTML = result_arr['token_name'];
		document.getElementById('item_name').innerHTML = result_arr['prize_name'];
		
		if (result_arr['prize_np'] ) {
			document.getElementById('prize_np').style.display='block';
			document.getElementById('np_prize_msg').innerHTML = result_arr['np_prize_msg'];
		}
		if (result_arr['bonus_item_extra_html']) {
			document.getElementById('bonus_item_extra_html').style.display='block';
			document.getElementById('bonus_item_extra_html_inner').innerHTML = result_arr['bonus_item_extra_html'];
		} else {
			document.getElementById('bonus_item_extra_html').style.display='none';
		}

		document.getElementById('cc_link').href += '?catToLoad=' + result_arr['cat_id'] + '&colToLoad=' + result_arr['col_id'];
		body_html = document.getElementById('secret_hidden_div').innerHTML;
		make_rollover('result', body_html, 'Woohoo!!', 380);
		clear_code();
	} else {
		document.getElementById('msg_div').innerHTML = transData.redeemFail;
		//document.getElementById('msg_div').innerHTML = "Having trouble getting your code to work? If so, <a href='/contact.phtml'><b>contact us</b></a> with the code you are having problems with or visit our <a href='/s/index.phtml?cat_id=53'><b>faq's</b></a>.";
	}
}

function get_ajax_data(script,args){
	document.getElementById('msg_div').innerHTML = "&nbsp;<br>&nbsp;";
	var request = new Object;
	request.url = script;
	request.method = "POST";
	request.args = '';
	for( var i=0; i<args.length; i++ ){
		if( args.charAt(i) == '|' ) request.args += '&';
		else request.args += args.charAt(i);
	}
	request.args += '&user=644255';
	request.onSuccess = function (response){
		response_str = response;
		if (response == 1) {
			window.location.href = '/space/warehouse/prizecodes.phtml';
			return;
		}
		show_result(response);
	}
	request.onFailure = function (response) {
		//document.getElementById('msg_div').innerHTML = "There was a technical problem!<br>This would be a good time to panic!!";
		document.getElementById('msg_div').innerHTML = transData.ajaxFail;
	}
	ajaxRequest(request);
}

function show_help() {
	make_rollover('help', '', strHelpMsgTitle, 250);
	document.getElementById('help_html').innerHTML = document.getElementById('secret_hidden_evil_div').innerHTML;
	/*if (browser.isIE) {
		document.getElementById('iframe_hack').style.height = '468px';
		document.getElementById('iframe_hack').style.width = '255px';
	}
	document.getElementById('shadow_img').style.height = '475px';
	document.getElementById('shadow_img').style.width = '260px';*/
}
function show_help2() {
	var popup = new NeoPopup(
		'help',
		{
			'title': 'Key Quest Help', 'class': 'KQPopup', 'content': $('secret_hidden_evil_div').innerHTML,
			'state': 'modal'
		}
	);
	popup.alert();
}

function clear_code() {
	document.getElementById('code_1').value = '';
	document.getElementById('code_2').value = '';
	document.getElementById('code_3').value = '';
}

function tab_next(obj) {
	if (obj.value.length == 5) {
		switch (obj.id) {
			case 'code_1':
				document.getElementById('code_2').focus();
				break;
			case 'code_2':
				document.getElementById('code_3').focus();
				break;
			case 'code_3':
				document.getElementById('redeem_btn').focus();
				break;
		}
	}
}
