//netscape, firefox, safari - all the normal browsers
var not_ie = document.getElementById && !document.all;

var isdrag = false;
var x, y;
var pop_window;
var leap_year = false;

function setAlpha( value, obj_id , appearing) {
        document.getElementById(obj_id).style.opacity = value / 100;
        document.getElementById(obj_id).style.filter = 'alpha(opacity=' + value + ')';
		if(document.getElementById(obj_id).style.opacity == appearing) transition_end(); 
}

function fadeIn(obj_id) {
        for( var i = 1 ; i <= 100 ; i++ ){
                setTimeout( 'setAlpha(' + (i) + ', "'+obj_id+'", 1)' , 2*i );
        }
}
function fadeOut(obj_id) {
        for( var i = 100 ; i >= 0 ; i-- ) {
                setTimeout( 'setAlpha(' + (i) + ', "'+obj_id+'",0)' , 2 * (100-i) );
        }
        setTimeout('closeWindow("'+obj_id+'")', 200 );
}
function closeWindow(obj_id) {
//    document.getElementById(obj_id).style.display = "none";
    document.getElementById(obj_id).style.visibility = "hidden";

}
function openWindow(obj_id){
        setAlpha( 0, obj_id );
	    document.getElementById(obj_id).style.visibility = "visible";
        document.getElementById(obj_id).style.display = "block";
        fadeIn(obj_id);
}
function pos_center(obj_id){
        winW = not_ie ? window.innerWidth : document.body.offsetWidth;
        winH = not_ie ? window.innerHeight : document.body.offsetHeight;
        document.getElementById(obj_id).style.top=(winH-document.getElementById(obj_id).clientHeight)/2+"px";
        document.getElementById(obj_id).style.left=(winW-document.getElementById(obj_id).clientWidth)/2+"px";
}
function transition_end(){
	//fires when fading in/out is complete
	if(document.getElementById('msg_ok_btn')!= undefined)
		document.getElementById('msg_ok_btn').focus();
}
//makes sure user doesn't enter too many characters
function imposelength(field,length){
	if (field.value.length>length){
		alert('Maximum length of this field is '+length+' symbols.');
		field.value= field.value.substr(0,length);
	}
	document.getElementById('char_counter').innerHTML="Characters: "+field.value.length+"/"+length;
}
//depending on the year determines if it's a leap year or not
function setyear(year,month_id,days_id){
	leap_year = (!(year % 4) && (!(year % 400) || (year % 100)));
	document.getElementById(month_id).selectedIndex = 0;
	document.getElementById(month_id).value=document.getElementById(month_id).options[0].value;
	setmonth(document.getElementById(month_id).value,days_id);
}
//depending on the month selected it will set number of days in the list
function setmonth(month,days_id){
	var month_days = new Array(31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); 
	if (leap_year) month_days[1]=29;
	for(i=27;i<month_days[month];i++){
		document.getElementById(days_id).options[i].style.visibility='visible';
		document.getElementById(days_id).options[i].text = i+1;
		document.getElementById(days_id).options[i].value = i+1;
	}
	for(i=month_days[month];document.getElementById(days_id).options[i]!=undefined;i++){
		document.getElementById(days_id).options[i].style.visibility='hidden';
		document.getElementById(days_id).options[i].text = '';
		document.getElementById(days_id).options[i].value = '';
	}
	document.getElementById(days_id).selectedIndex=0;
	document.getElementById(days_id).value=document.getElementById(days_id).options[0].value;
}
function set_selector(selector){
	document.getElementById('img_load').style.visibility='visible';
	var loc = document.location.toString();
	if(loc.indexOf('/index')>=0)
		document.location=loc+'/'+selector;
	else
		document.location='index/'+selector;
}
function add_link(url_id, txt_id, details_id){
	document.getElementById(details_id).value += "[link="+document.getElementById(url_id).value+"]";
	if(document.getElementById(txt_id).value.length>0)
		document.getElementById(details_id).value += document.getElementById(txt_id).value+"[/link]";
	document.getElementById(url_id).value = "";
	document.getElementById(txt_id).value = "";
}
//hides/displays details
function toggleDetail(id){
	if(not_ie) document.getElementById(id).style.display=(document.getElementById(id).style.display=='table-row')?'none':'table-row';
	else document.getElementById(id).style.display=(document.getElementById(id).style.display=='block')?'none':'block';
}
function set_provinces(name){
	document.getElementById('st_usa').style.display='none';
	document.getElementById('st_other').style.display='none';
	document.getElementById('st_can').style.display='none';
	document.getElementById('stusa').name='st1';
	document.getElementById('stcan').name='st2';
	document.getElementById('stother').name='st3';
	switch(name.toLowerCase()){
		case 'canada':
			document.getElementById('st_can').style.display='block';
			document.getElementById('stcan').name = 'state';
		break;
		case 'united states':
			document.getElementById('st_usa').style.display='block';
			document.getElementById('stusa').name = 'state';
		break;
		default:
			document.getElementById('st_other').style.display='block';
			document.getElementById('stother').name = 'state';
		break;
	}
}


//AJAX STUFF
function ajax_search(query_id,search_result){
	query = $('#'+query_id).val();
	$('#img_load > style').visibility='visible';
	$('#'+search_result).html('searching...');
	//jquery
	$.ajax({
		url: '/krypton/search', 
		type:'POST',
		data:({
			search:query,
			parent_fct:'ajax_search',
			ajax:1
		}),
		success: function(msg) {
			$('#'+search_result).html(msg || '');
			$('#img_load > style').visibility='hidden';
		}
	});		

	/*prototype
	new Ajax.Request('/krypton/search', 
	{
		method:'post',
		parameters:{
			search:query,
			parent_fct:'ajax_search',
			ajax:1
		},
		onSuccess: function(transport) {
			$(search_result).innerHTML = transport.responseText || '';
			$('img_load').style.visibility='hidden';
		}
	});		
	*/
}
function ajax_sort(field,asc){
//jquery
	$.ajax({
		url: '/krypton/ticket_all', 
		type:'POST',
		data:({
			sort_by:field,
			order:asc,
			ajax:1
		}),
		success: function(msg) {
			location.reload(true);			
		}
	});		

/* prototype
	new Ajax.Request('/krypton/ticket_all', 
	{
		method:'post',
		parameters:{
			sort_by:field,
			order:asc,
			ajax:1
		},
		onSuccess: function(transport) {
			//refresh the page once the SESSION var has been set
			location.reload(true);			
		}
	});		
	*/	
}
function process_form(form_id,result_id,caller_name){
	if(caller_name === undefined) caller_name = 0;//to make caller_name optional parameter
	$('#img_load > style').visibility='visible';
	action = $('#'+form_id).attr('action');
	fields = $('#'+form_id).serialize();
	fields +='&caller='+caller_name+'&parent_fct=process_form&ajax=1';
	$.ajax({
		url: action,
		type: 'POST',
		data:fields,
		success: function(answer){
			$('#img_load > style').visibility='hidden';
			if(answer.toString().substr(1).match(/[^0-9]/)==null){
				//response is numeric: parse it
				switch(answer.substr(0,4)){
					case 'c001': 
						document.location='/krypton';
					break;
					case 'c002':
						document.location='/krypton/profile';
					break;
					case 'c003':
						document.location='/krypton/profile/'+answer.substr(4);
					break;
					case 'c004':
						document.location='/krypton/search';
					break;
					case 'c005':
						document.location='/krypton/ticket_view/'+answer.substr(4);
					break;
					case 'c006':
						document.location='/krypton/downloads';
					break;
					default: 
						document.location='/krypton/ticket_view/'+answer;
					break;
				}
			}
			else{//response is not numeric
				$('#'+result_id).html(answer);
				openWindow(result_id);
			}
		}
	});
	//disable the submit button for a few seconds (to prevent accidental double clicks)
	$('#accept').disabled=true;
	setTimeout("$('#accept').disabled=false", 2500 );
	if(!($('#btn_rma').length<=0 || $('#btn_rma') == null)){//if there's a RMA button - disable it for a few seconds too
		$('#btn_rma').disabled=true;
		setTimeout("$('#btn_rma').disabled=false", 2500 );
	}
}
//sends ajax request to filter users based on the entered value
function filter_users(filter){
	$.ajax(
	{
		url:'/krypton/users_all', 
		type:'post',
		data:({
			user:filter,
			parent_fct:'filter_users',
			ajax:1
		}),
		success: function(ans) {
			$('#users').html(ans);
		}
	});		
}
function getmodel(sn,output){
	if(sn.length<1){ 
		$('#'+output).html('');
		return;
	}
	$.ajax(
	{
		url:'/krypton/getmodels', 
		type:'post',
		data:({
			entered_sn:sn,
			parent_fct:'getmodel',
			ajax:1
		}),
		success: function(ans) {
			$('#'+output).html(ans);
		}
	});		
}
//make ajax call to set or remove the specified flag from the ticket
function setops(num,chbox){
	$.ajax(
	{
		url:'/krypton/setops', 
		type:'post',
		data:({
			ticket:num,
			ops:chbox.name,
			flag:chbox.checked,
			parent_fct:'setops',
			ajax:1
		}),
		success: function(ans) {
			success = ans || 0;
			//checkbox should be checked if it was un-checked before and the option was set successfully 
			//or it was checked before and option was not set successfully
			chbox.checked = !(success ^ chbox.checked); 
		},
		error: function(){
			//this should never happen but if request fails for some reason
			chbox.checked = !chbox.checked;
		}
	});		
}
//hides or displays the other elements depending on whether the issue is technical or non-technical
function set_issue(issue){
	if(issue == 0){//non-technical
		visible = 'none';
	}
	else{
		visible = 'block';
	}
	$('#platform > style').display = visible;
	$('#launchpad > style').display = visible;
	$('#game > style').display = visible;
	$('#hiding_spacer > style').display=visible;
}
//displays correct list of games for the specified platform
function set_games(platforms){
	sel = new Array();
	//get all the selected platforms
	for(i = 0; i<platforms.options.length; i++){
		if(platforms.options[i].selected) sel.push(platforms.options[i].value);
	}
	$.ajax(
	{
		url:'/krypton/getgames', 
	 	type:'post',
		data:({
			'platform':sel,
			parent_fct:'set_games',
			ajax:1
		}),
		success: function(ans) {
			$('#games_list').html(ans || 'No games available for this platform.');
		},
		error: function(error_txt){
			//alert('fail'+error_txt);
		}
	});	
}

//used for main site
function m_process_form(form_id,result_id,caller_name){
	if(caller_name === undefined) caller_name = 0;//to make caller_name optional parameter
	$('img_load').style.visibility='visible';
	$(form_id).request({
		method: 'get',
		parameters:{
			caller:caller_name,
			parent_fct:'m_process_form',
			ajax: 1	
			},
		onComplete: function(transport){
			$('img_load').style.visibility='hidden';
//			alert(transport.responseText);
			if(transport.responseText.toString().substr(1).match(/[^0-9]/)==null){
				//response is numeric: success - simply refresh the page
				location.reload(true);
			}
			else{//response is not numeric
				$(result_id).innerHTML=transport.responseText;
				openWindow(result_id);
			}
		}
	});
	//disable the submit button for a few seconds (to prevent accidental double clicks)
	$('accept').disabled=true;
	setTimeout("$('accept').disabled=false", 2500 );
}