/*---------------------------------------------------------*/
function getID(idname){
	return document.getElementById(idname);
}
function elem(name){
	return document.createElement(name);
}
function tagName(name){
	return document.getElementsByTagName(name);
}
/*---------------------------------------------------------*/
/*---------------------------------------------------------*/
function goto(url_query){
	window.location.href=url_query;
}
function submits(form_id){
	getID(form_id).submit();
}
function confirms(url_query, msgs){
	if( confirm(msgs) ){
		goto(url_query);
	} else {
		return void(0);
	}
}
/*---------------------------------------------------------*/
/*---------------------------------------------------------*/
function checkAllBox(num){
	var j, n=1;
	boxes = tagName('input'); 
	
	for(j=0; j<boxes.length; j++) {
		var box = boxes[j];
		if(box.type=='checkbox'){
			if(box.checked == true){
				box.checked = false;
				box.value = null;
				document.myForm.boxchecked.value = 0;
			} else {
				box.checked = true;
				box.value = 1;
				document.myForm.boxchecked.value = n;
			}
			n++;
		}
	}
}
/*---------------------------------------------------------*/
/*---------------------------------------------------------*/

/**
* Toggles the check state of a group of boxes
*
* Checkboxes must have an id attribute in the form cb0, cb1...
* @param The number of box to 'check'
* @param An alternative field name
*/
function checkAll( n, fldName ) {
  if (!fldName) {
     fldName = 'id_array';
  }
	var f = document.myForm;
	var c = f.toggle.checked;
	var n2 = 0;
	for (i=0; i < n; i++) {
		cb = eval( 'f.' + fldName + '' + i );
		if (cb) {
			if(cb.disabled == false){
				cb.checked = c;
				n2++;
			}
		}
	}
	if (c) {
		document.myForm.boxchecked.value = n2;
	} else {
		document.myForm.boxchecked.value = 0;
	}
}

function isChecked(isitchecked){
	if (isitchecked == true){
		document.myForm.boxchecked.value++;
	} else {
		document.myForm.boxchecked.value--;
	}
}

/**
* Default function.  Usually would be overriden by the component
*/
function submitbutton(pressbutton) {
	submitform(pressbutton);
}

/**
* Submit the admin form
*/
function submitform(pressbutton){
	document.myForm.task.value=pressbutton;
	
	try {
		document.myForm.onsubmit();
	}
	catch(e){}
	document.myForm.submit();
}

/**
* Submit the control panel admin form
*/
function submitcpform(sectionid, id){
	document.myForm.sectionid.value=sectionid;
	document.myForm.id.value=id;
	submitbutton("edit");
}

/**
* Submit the control panel admin form and Delete selected Items
*/
function deleteSelected(events){
	if(document.myForm.boxchecked.value == 0){
		alert('Please make a selection from the list to delete');
		return false;
	} else if(confirm('Are you sure you want to delete selected items?')){
		submitbutton(events);
	}
}

function selects(events){
	if(document.myForm.boxchecked.value == 0){
		alert('Please make a selection from the list to delete');
		return false;
	} else {
		submitbutton(events);
	}
}

/**
* Getting radio button that is selected.
*/
function getSelected(allbuttons){
	for (i=0;i<allbuttons.length;i++) {
		if (allbuttons[i].checked) {
			return allbuttons[i].value
		}
	}
}


// This function gets called when an end-user clicks on some date
function selected(cal, date) {
	cal.sel.value = date; // just update the value of the input field
}

// LTrim(string) : Returns a copy of a string without leading spaces.
function ltrim(str)
{
   var whitespace = new String(" \t\n\r");
   var s = new String(str);
   if (whitespace.indexOf(s.charAt(0)) != -1) {
      var j=0, i = s.length;
      while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
         j++;
      s = s.substring(j, i);
   }
   return s;
}

//RTrim(string) : Returns a copy of a string without trailing spaces.
function rtrim(str)
{
   var whitespace = new String(" \t\n\r");
   var s = new String(str);
   if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
      var i = s.length - 1;       // Get length of string
      while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
         i--;
      s = s.substring(0, i+1);
   }
   return s;
}

// Trim(string) : Returns a copy of a string without leading or trailing spaces
function trim(str) {
   return rtrim(ltrim(str));
}

//needed by saveorder function
function checkAll_button( n ) {
	for ( var j = 0; j <= n; j++ ) {
		box = eval( "document.myForm.cb" + j );
		if ( box ) {
			if ( box.checked == false ) {
				box.checked = true;
			}
		} else {
			alert("You cannot change the order of items, as an item in the list is `Checked Out`");
			return;
		}
	}
	submitform('saveorder');
}
/**
* @param object A form element
* @param string The name of the element to find
*/
function getElementByName( f, name ) {
	if (f.elements) {
		for (i=0, n=f.elements.length; i < n; i++) {
			if (f.elements[i].name == name) {
				return f.elements[i];
			}
		}
	}
	return null;
}

function logout(){
	userPrompt = confirm("Are you sure you want to Logout?");
	if(userPrompt){
		window.location.href="logout.php";
	}
}

function textCounter(field, countfield, maxlimit) {
	var reg = /\n/;
	var temp = new Array();
	temp = field.value.split('');
	var lengthChar = eval(field.value.length);
	var n=0;
	for(i=0; i<=lengthChar; i++){
		if(reg.test(temp[i])){
			n++;
		}
	}
	maxlimit-=n;
	if (field.value.length > maxlimit){ // if too long...trim it!
		field.value = field.value.substring(0, maxlimit);
		countfield.value = 0;
	} else  {
		// otherwise, update 'characters left' counter
		countfield.value = maxlimit-lengthChar;
	}
}

function countWords(obj,resInput){
	var regExp = /\w+/g;
	var num = obj.value.match(regExp);
	
	document.getElementById(resInput).value = num.length;
}

function validateURL(form) {
    var v = new RegExp();
    v.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");
    if (!v.test(form["URL"].value)) {
        alert("You must supply a valid URL.");
        return false;
    } else {
		return true;	
	}
} 

// check for valid numeric strings
function isNumeric(TextBox,strType){
	if (strType=="Decimal"){
		var strValidChars = "0123456789.";
	} else if(strType=="Integer"){
		var strValidChars = "0123456789";
	}
	
	var strString = TextBox.value;
	
	for (i = 0; i < strString.length; i++){
		var strChar = strString.charAt(i);
		if (strValidChars.indexOf(strChar) == -1){
			TextBox.value = TextBox.value.substring(0, i);
			TextBox.value.length = i
			event.returnValue = false;
                        return false;
		}
	}
        /*
        if (strType="Decimal"){
		var ValidChars = "0123456789.";
	} else if(strType="Integer"){
		var ValidChars = "0123456789";
	}
	
        var IsNumber=true;
        var Char;

        for (i = 0; i < TextBox.length && IsNumber == true; i++){ 
                Char = TextBox.charAt(i); 
                if (ValidChars.indexOf(Char) == -1){
                      IsNumber = false;
                }
        }
        
        return IsNumber;
    
        */
} 

/*********************************************************************************
  cookies.js - cookie functions for www.dyn-web.com
  Recycled from various sources 
**********************************************************************************/

// Modified from Bill Dortch's Cookie Functions (hidaho.com) 
// (found in JavaScript Bible)
function setCookie(name, value, days, path, domain, secure) {
  var expires, date;
  if (typeof days == "number") {
    date = new Date();
    date.setTime( date.getTime() + (days*24*60*60*1000) );
		expires = date.toGMTString();
  }
  document.cookie = name + "=" + escape(value) +
    ((expires) ? "; expires=" + expires : "") +
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    ((secure) ? "; secure" : "");
}

// Modified from Jesse Chisholm or Scott Andrew Lepera ?
// (found at both www.dansteinman.com/dynapi/ and www.scottandrew.com/junkyard/js/)
function getCookie(name) {
  var nameq = name + "=";
  var c_ar = document.cookie.split(';');
  for (var i=0; i<c_ar.length; i++) {
    var c = c_ar[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameq) == 0) return unescape( c.substring(nameq.length, c.length) );
  }
  return null;
}

// from Bill Dortch's Cookie Functions (hidaho.com) 
function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}
/**********************************************************************************/

function importCSS(file_path){
	//<![CDATA[
	if( document.createStyleSheet ) {
		document.createStyleSheet(file_path);
	} else {
		var styles = "@import url('"+file_path+"');";
		var css=document.createElement("link");
		css.rel="stylesheet";
		css.href="data:text/css,"+escape(styles);
		document.getElementsByTagName("head")[0].appendChild(css);
	}
	//]]>	
}
function importJS(file_path){
	//<![CDATA[
	var script=document.createElement("script");
	script.type="text/javascript";
	script.src=file_path;
	document.getElementsByTagName("head")[0].appendChild(script);
	//]]>
}

/*
function onLoad(){
	var div = document.createElement('div');
	div.style.width = '100px';
	div.style.height = '100px';
	div.style.backgroundColor = 'white';++
	document.body.appendChild( div );
}

gEi( 'my_textarea' ).onkeyup = function() {
		var my_split = this.value.split('\n').length;
		if( this.p == undefined ) {
			this.p = create.create( 'p' );
			this.p.innerHTML = my_split;
			document.body.appendChild( this.p );
		} else {
			this.p.innerHTML = my_split;
		}
	}
*/

function get_scroll_xy() {
	var scrOfX = 0, scrOfY = 0;
	if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	} else if( typeof( window.pageYOffset ) == 'number' ) {
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	}
	return [ scrOfX, scrOfY ];
}
/*---------------------------------------------------------*/
/*---------------------------------------------------------*/
function show_msg(classname){
	
	var msgs = getID('msg');
	
	msgs.style.display = '';
	msgs.className = classname;
}
function close_msg(){
	
	var tags = tagName('div');
	for(var i=0; i<tags.length; i++){
		var tag = tags[i];
		if(tag.className == 'msg_result'){
			tag.style.display = 'none';
		}
	}
	
	/*var msgs = getID('msg_result');
	
	msgs.innerHTML = '';
	msgs.className = '';
	msgs.style.display = 'none';*/
}
function close_msg_ajax(){
	
	var tags = tagName('div');
	for(var i=0; i<tags.length; i++){
		var tag = tags[i];
		if(tag.className == 'warn_msg'){
			tag.style.display = 'none';
		}
		if(tag.className == 'error_msg'){
			tag.style.display = 'none';
		}
		if(tag.className == 'info_msg'){
			tag.style.display = 'none';
		}
	}
	
	/*var msgs_result = getID('msg_result_ajax');
	msgs_result.innerHTML = '';
	msgs_result.style.display = 'none';*/
}
//window.setInterval('close_msg()',20000);
//window.setInterval('close_msg_ajax()',20000);
/*---------------------------------------------------------*/
/*---------------------------------------------------------*/
