// JavaScript Document

//modal dialogue vis functions
function closeDialogue(shroud,content_field){
	shroud.style.display="none";
	content_field.innerHTML="";
}

function showDialogue(w,h,title,shroud,dialogue_window,dialogue_title){	
	//Get Dialogue Components
	shroud = (shroud) ? shroud : document.getElementById("modal_dialogue") ;
	dialogue_window = (dialogue_window) ? dialogue_window : document.getElementById("md_window") ;
	dialogue_title = (dialogue_title) ? dialogue_title : document.getElementById("md_title") ;
	//Show Dialogue	
	window.scrollTo(0,0);
	dialogue_title.innerHTML = (title) ? title : "Website Dialogue" ;
	shroud.style.display="block";
	shroud.style.width="100%";
	dialogue_window.style.height = (h) ? h+"px" : "500px" ;
	dialogue_window.style.width = (w) ? w+"px" : "500px" ;	
	shroud.style.height = (document.body.clientHeight) ? document.body.clientHeight+"px" : shroud.style.height=document.body.offsetHeight ;
}

function show_membership_manager(pc2_id){
	var content = document.createElement("iframe");
	content.src = "membership_manager.php?pc2="+pc2_id;
	content.style.width = "500px";
	content.style.height = "500px";	
	content.style.margin = "auto";
	content.style.border = "0px";	
	showDialogue(520,500,"Membership Manager");
	document.getElementById("md_content_field").appendChild(content);
}

function show_admin_manager(){
	var content = document.createElement("iframe");
	content.src = "admin/?v=1";
	content.style.width = "500px";
	content.style.height = "500px";	
	content.style.margin = "auto";
	content.style.border = "0px";	
	showDialogue(520,500,"Administration Control Panel");
	document.getElementById("md_content_field").appendChild(content);
}

function select_group(mode){
	var inputs = document.getElementsByTagName('input');
	var re = /batch_([0-9a-z_]+)/;
	var i = 0;
	if(mode=='all'){
		while(i < inputs.length){
			if(re.test(inputs[i].name)){
				inputs[i].checked = true;
			}
			i++;
		}
	}else if(mode=='none'){
		while(i < inputs.length){
			if(re.test(inputs[i].name)){
				inputs[i].checked = false;
			}
			i++;
		}
	}
}

function mouse_pos(evt){
	var img_x;
	var img_y;
	var img_x_raw;
	var img_y_raw;
	var return_array = Array();
	if (!evt.pageX) { // MSIE
		img_x = evt.offsetX;
		img_y = evt.offsetY;
		img_x_raw = evt.clientX
		img_y_raw = evt.clientY	+ document.documentElement.scrollTop			
	}else{ // Netscape, etc.
		img_x = evt.pageX;	
		img_y = evt.pageY;
		img_x_raw = evt.pageX;	
		img_y_raw = evt.pageY;			
		for (var offMark = evt.target; offMark; offMark = offMark.offsetParent) {
			img_x -= offMark.offsetLeft;
		}
		for (var offMark = evt.target; offMark; offMark = offMark.offsetParent) {
			img_y -= offMark.offsetTop;
		}
	}
	return_array['x'] = img_x;
	return_array['y'] = img_y;
	return_array['x_raw'] = img_x_raw;
	return_array['y_raw'] = img_y_raw;	
	return return_array;
}

function sec_to_time(secs) {
	
	var time_ref = Array();
	var time = Array();
	var remaining_time = null;
	
	time_ref['years'] = (365.25*24*60*60);
	time_ref['days'] = (24*60*60);
	time_ref['hours'] = (60*60);
	time_ref['minutes'] = 60;
	time_ref['seconds'] = 1;
	
	//years
	time['years'] = Math.floor(secs / time_ref['years']);
	
	//days
	remaining_time = secs % time_ref['years'];
	time['days'] = Math.floor(remaining_time / time_ref['days']);
	
	//hours
	remaining_time = remaining_time % time_ref['days'];
	time['hours'] = Math.floor(remaining_time / time_ref['hours']);
	
	//Minutes
	remaining_time = remaining_time % time_ref['hours'];
	time['minutes'] = Math.floor(remaining_time / time_ref['minutes']);
	
	//Seconds
	remaining_time = remaining_time % time_ref['minutes'];
	time['seconds'] = Math.floor(remaining_time / time_ref['seconds']);
	
	return time;
	
}


function pop_up(url,w,h,name) {
	if(!w){w='640'}
	if(!h){h='800'}
	if(!name){name='popup'}	
	var params = 'scrollbars=1, height=' + w + ',width=' + h;
	newwindow=window.open(url,name,params);
	newwindow.resizeTo(w,h)
	if (window.focus) {newwindow.focus()}
	return false;
}

function change_q_var(uri){
	//Sort arguments
	var i = 1;
	var keys = Array();
	var vals = Array();
	while(i < change_q_var.arguments.length){
		if(i%2==0){ // arg is value
			vals.push(change_q_var.arguments[i]);
		}else{
			keys.push(change_q_var.arguments[i]);			
		}
		i++;
	}
	//prepare uri
	uri.replace(/&amp;/,"&");
	//Change variables in current uri
	i = 0;
	while(i < keys.length){
		var re = new RegExp("(.*)(\\?|&)("+keys[i]+")=([^&]*)(.*)","i");
		var key_is_already_present = re.test(uri);
		if(key_is_already_present){
			uri = uri.replace(re,"$1$2$3="+vals[i]+"$5");
		}else{
			if(/&/.test(uri)){
				uri = uri+"&"+keys[i]+"="+vals[i];				
			}else{
				if(/\\?/.test(uri)){
					if(/=/.test(uri)){
						uri = uri+"&"+keys[i]+"="+vals[i];
					}else{
						uri = uri+keys[i]+"="+vals[i];
					}
				}else{
					uri = uri+"?"+keys[i]+"="+vals[i];
				}
			}
		}
		i++;
	}
	return uri;
}

function count_down(timeout,load_action,readout,window_reference,uri){
	//count_down(10,'load',document.getElementById('txt'),window,'http://onzamap.aotea-breaks.co.nz');
	if(window_reference==null){ window_reference = window; }
	if(cur_func==null){ var cur_func; }
	readout.innerHTML=timeout;
	timeout=timeout-1;
	curfunc = curry(count_down,timeout,load_action,readout,window_reference,uri);	
	if(timeout==0){
		switch(load_action){
			case 'reload' :
				window_reference.location.reload();
			break;
			case 'load' :
				window_reference.location = uri;
			break;
			default:
				load_action();
			break;
		}
	}else{
		//t=setTimeout(count_down,1000,timeout,load_action,readout,window_reference,uri);
		t=setTimeout(curfunc,1000);		
	}
}

function curry(method){
	var curried = [];
	for (var i = 1; i < arguments.length; i++) {
		curried.push(arguments[i]);
	}
	return function() {
		var args = [];
		for (var i = 0; i < curried.length; i++) {
			args.push(curried[i]);
		}
		for (var i = 0; i < arguments.length; i++) {
			args.push(arguments[i]);
		}
		return method.apply(null, args);
	}
}

//modal dialogue vis functions
function close_dialogue(shroud,content_field){
	shroud.style.display="none";
	content_field.innerHTML="";
}

function show_dialogue(shroud){
	window.scrollTo(0,0);
	shroud.style.display="block";
	shroud.style.width="100%";		
	if(document.body.clientHeight){
		shroud.style.height=document.body.clientHeight+"px";		
	}else{
		shroud.style.height=document.body.offsetHeight;
	}
}

function clear_child_nodes(element){
	if ( element.hasChildNodes() ){
		while ( element.childNodes.length >= 1 ){
			element.removeChild( element.firstChild );       
		} 
	}
}

function add_e_handler(obj, e, func){
	//test if func exists - prevents problems in IE
	if(typeof func != "undefined"){		
		if(obj.attachEvent){
			obj.attachEvent('on' + e, func);
		}else if(obj.addEventListener){
			obj.addEventListener(e, func, false);
		}else{
			obj['on' + e] = func;
		}
	}
}

function remove_e_handler(obj, e, func){
	//test if func exists - prevents problems in IE		
	if(typeof func != "undefined"){
		if (obj.detachEvent){
			obj.detachEvent('on' + e, func);
		}else if(obj.removeEventListener){
			obj.removeEventListener(e, func, false);
		}else{
			obj['on' + e] = null;
		}
	}
}

function dumpObj(obj, name, indent, depth){

	depth = (depth) ? depth : 10 ;
	name = (name) ? name : "unknown" ;
	indent = (indent) ? indent : " " ;

	var MAX_DUMP_DEPTH = 10;
	if(depth > MAX_DUMP_DEPTH){
		return indent + name + ": <Maximum Depth Reached>\n";
	}
	if (typeof obj == "object"){
		var child = null;
		var output = indent + name;
		var total = 0;
		if(obj instanceof Array){
			total = obj.length;
			output += " (Array)\n";
		}else{
			for(var item in obj){
			   total++;
			}
			output += " (Object)\n";
		}
		output += indent + "Total item: " + total + "\n";
		indent += "\t";
		if(obj instanceof Array){
			for(var i = 0; i < obj.length; i++){
			   child = obj[i];
			   output += dumpObj(child, i, indent, depth + 1);
			}
		}else{
			for(var item in obj){
			   try{
				   child = obj[item];
			   }catch(e){
				   child = "<Unable to Evaluate>";
			   }
			   if(typeof child == "object"){
				   output += dumpObj(child, item, indent, depth + 1);
			   }else{
				   output += indent + item + ": " + child + "\n";
			   }
			}
		}
		return output;
	}else{
		return obj + " is not an object.";
	}
}
