//Primrix Common JavaScript Library
//Ver 1.3
//Author James Dalgarno
//Copyright © Primrix 2010 all rights reserved
//Use of any part of this document outside the Primrix framework is prohibited.
//See Primrix licence agreement for more information (http://www.primrix.com)

//Index of functions
// 1. getInt(strInt)
// 2. checkbrowser()
// 3. pageFeeler()
// 4. measureScroller(axis)
// 5. select_checkboxes(form_id, parentelement, select_ids)
// 6. confirmDelete()
// 7. confirmGeneric()
// 8. expand_tr(element)
// 9. checkDialogue(id)
//10. open_menu(menu_id)
//11. open_tab(click_element,tab_id)
//12. showhide(obj,id,hidebytick)
//13. showhide_icons(state)
//14. pass_value(form_box_id,form_box_value)
//15. TextLimit(limitField, limitCount, limitNum)
//16. lightup_progression(state)
//17. popup(id,url)


//declare common variables & objects
var px = 'px';
var page = new Object();
var browser = null;


//function getInt ver 2.0
//function accepts string number and returns integer
//example [getInt('200px')] ~ (returns 200)
function getInt(strInt)
{
	strInt = parseInt(strInt,10);
	if(isNaN(strInt))strInt = 0;
	return(strInt);
}

//Check Browser ver 2.0
function checkbrowser()
{
	var navinfo = navigator.userAgent;
	if(navinfo.search(/MSIE/i) != -1) browser = "IE";
	else if(navinfo.search(/Opera/i) != -1) browser = "OP";
	else if(navinfo.search(/Navigator/i) != -1) browser = "NN";
	else if(navinfo.search(/Firefox/i) != -1) browser = "FF";
	else if(navinfo.search(/Chrome/i) != -1) browser = "GC";
	else  browser = "NK"; //not known
	return(browser);	
}

//Page Feeler ver 2.0
function pageFeeler()
{
	page.leftMargin = getInt(document.documentElement.offsetLeft); 
	page.topMargin = getInt(document.documentElement.offsetTop);
    if(browser == "IE") page.height = getInt(document.documentElement.clientHeight);
    else page.height = getInt(window.innerHeight);
    if(browser == "IE") page.width = getInt(document.documentElement.clientWidth);
    else page.width = getInt(window.innerWidth);	
}

//Measure Scroller ver 2.0
function measureScroller(axis)
{
	if(browser != "IE"){
		if(axis == "x") return(window.pageXOffset-5);
		if(axis == "y") return(window.pageYOffset-5);
	}else{
		if(axis == "x") return(document.documentElement.scrollLeft-5);
		if(axis == "y") return(document.documentElement.scrollTop-5);
	}	
}

//Select Checkboxes ver 1.0
//Used to select all checkboxes within a group. param 1 is used to id the form, param 2 is the master element
//and param 3 is a string matching the static part of the naming of the group, for example mygroup_xyz and mygroup_123
//are the same group if the match 'mygroup' is used.
//example [<input type="checkbox" name="select_id" onclick="select_checkboxes('form_1', this, 'select_id');">]	
function select_checkboxes(form_id, parentelement, select_ids)
{
	var thisform = document.getElementById(form_id);
	for(i = 0; i < thisform.elements.length; i++){
		if(thisform.elements[i].type == 'checkbox'){
			if(thisform.elements[i].id.substr(0,select_ids.length) == select_ids){
				 thisform.elements[i].checked = parentelement.checked;
			}
		}
	}
}

//Confirm Delete ver 1.0
//Used as a generic confirmation for a delete button, no arguments passed.
//example [onclick="return confirmDelete();"]
function confirmDelete()
{
	var user_confirmation = confirm('Are you sure you wish to delete?');
	if(user_confirmation) return true;
	else return false;
}

function confirmGeneric()
{
	var user_confirmation = confirm('Are you sure you?');
	if(user_confirmation) return true;
	else return false;
}

//Expand Table Row ver 1.0
//Uses the element as a reference point to a match for all table rows.
//example <img src="" id="trid_1" onclick="expand_tr(this);"> expands/collapses all <tr> elements with an id begining with "trid_1"*
function expand_tr(element)
{
	var rid = element.id;
	var n = 1;
	while(document.getElementById(rid + '_' + n)){
		if(document.getElementById(rid + '_' + n).style.display == ''){
			document.getElementById(rid + '_' + n).style.display = 'none';
			element.src = '/main/assets/graphix/system/buttons/o_open.jpg';
		}
		else{
			document.getElementById(rid + '_' + n).style.display = '';
			element.src = '/main/assets/graphix/system/buttons/o_close.jpg';
		}
		n++;
	}
}

function checkDialogue(id)
{
	if(document.getElementById('dialogue_message').innerHTML != ""){
		document.getElementById(id).style.left = ((page.width - 380) / 2) + px;
		document.getElementById(id).style.top = (page.height / 4) + px;
		document.getElementById(id).style.display = 'block';	
	}
	else document.getElementById(id).style.display = 'none';
}

function open_menu(menu_id)
{
	if(open_menu_last_opened != null){
		document.getElementById(open_menu_last_opened).style.display = 'none';
	}
	document.getElementById(menu_id).style.display = 'block';
	open_menu_last_opened = menu_id;
}

function open_tab(click_element,tab_id)
{
	if(open_tab_last_opened != null){
		document.getElementById(open_tab_last_opened).style.display = 'none';
		last_this_tab.style.backgroundColor = tab_unselect_colour;
		last_this_tab.style.backgroundImage = tab_bg_image;
	}
	click_element.style.backgroundColor = tab_highlight_colour;
	click_element.style.backgroundImage = 'none';
	document.getElementById(tab_id).style.display = 'block';
	open_tab_last_opened = tab_id;
	last_this_tab = click_element;
}

function showhide(obj,id,hidebytick)
{
	if(hidebytick){			
		if(obj.checked) document.getElementById(id).style.display = 'block';
		else document.getElementById(id).style.display = 'none';
	}
	else{
		if(obj.checked) document.getElementById(id).style.display = 'none';
		else document.getElementById(id).style.display = 'block';			
	}
}

function showhide_icons(state)
{
	if(state == 'show') document.getElementById('icon_browse').style.display='block';
	else document.getElementById('icon_browse').style.display='none';
}

function pass_value(form_box_id,form_box_value)
{
	document.getElementById(form_box_id).value = form_box_value;
}

function TextLimit(limitField, limitCount, limitNum) 
{
	if (limitField.value.length > limitNum){
	limitField.value = limitField.value.substring(0, limitNum);
	} 
	else{
	limitCount.value = limitNum - limitField.value.length;
	}	
}

var lightup_opacity = 0;
var lightup_timer;
var lightup_element;
var last_lightup_element = null;

function lightup(element,state,set_opacity)
{
		if(last_lightup_element != null && element != last_lightup_element){
			lightup_element = last_lightup_element;
			lightup_progression('reset',set_opacity);
		}
		else if(last_lightup_element == null) lightup_opacity = set_opacity;
		lightup_element = element;
		last_lightup_element = element;
		if(last_lightup_element != null) clearInterval(lightup_timer);

		lightup_timer = setInterval("lightup_progression('" + state + "','" + set_opacity + "')", 20); //the show must go on
		
}

function lightup_progression(state,set_opacity)
{
	if(state != 'reset'){
		if(state == 'on') lightup_opacity = Number(lightup_opacity) + 5;
		else lightup_opacity = Number(lightup_opacity) - 5;	

		if(lightup_opacity > 100){
			clearInterval(lightup_timer);
			lightup_opacity = 100;
		}
		else if(lightup_opacity < set_opacity){
			clearInterval(lightup_timer);
			lightup_opacity = set_opacity;
		}		
	}
	else lightup_opacity = set_opacity;
	
	lightup_element.style.opacity = (lightup_opacity/100);
	lightup_element.style.filter = "alpha(opacity=" + lightup_opacity + ")";	
}

function popup(id,url,pWidth,pHeight)
{
	eval("page" + id + " = window.open('" + url + "', '" + id + "', 'toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=1,resizable=1,width=" + pWidth + ",height=" + pHeight + ",left=50,top=50');");
}


