// JavaScript Document
window.onerror = KillError

var proTimeoutID = 0;
var timeoutID = 0;

var userAgent = navigator.userAgent.toLowerCase();
var is_opera  = (userAgent.indexOf('opera') != -1);
var is_saf    = ((userAgent.indexOf('applewebkit') != -1) || (navigator.vendor == "Apple Computer, Inc."));
var is_webtv  = (userAgent.indexOf('webtv') != -1);
var is_ie     = ((userAgent.indexOf('msie') != -1) && (!is_opera) && (!is_saf) && (!is_webtv));
var is_ie4    = ((is_ie) && (userAgent.indexOf("msie 4.") != -1));
var is_moz    = ((navigator.product == 'Gecko') && (!is_saf));
var is_kon    = (userAgent.indexOf('konqueror') != -1);
var is_ns     = ((userAgent.indexOf('compatible') == -1) && (userAgent.indexOf('mozilla') != -1) && (!is_opera) && (!is_webtv) && (!is_saf));
var is_ns4    = ((is_ns) && (parseInt(navigator.appVersion) == 4));
//alert(is_saf);


function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
function openPop(theURL,winName,features) {
  	window.open(theURL,winName,features);
}
function close_win(){window.close();}

function go_back(){history.go(-1);}

function setFocus(el){
	if(document.getElementById(''+el+'') != 'undefined'){
		document.getElementById(''+el+'').focus();
	}
}
function getValue(el){
	if(document.getElementById(''+el+'') != 'undefined'){
		return document.getElementById(''+el+'').value;
	}
}
function getElement(el){
	if(document.getElementById(''+el+'') != 'undefined'){
		return document.getElementById(''+el+'');
	}
}

function noop(){return true;}

function go_back(){history.go(-1);}

function shwError(msg, typ){
	if(msg){
	 if(getElement('error_td_1')){getElement('error_td_1').style.display = 'none';}
	 if(typ == 2){
		 getElement('error_td').innerHTML = "<span>"+msg+".</span>";
	 } else {
		 getElement('error_td').innerHTML = "<span>"+msg+" cannot be blank.</span>";
	 }
	 getElement('error_td').style.display = 'block';
	 //window.scrollTo(10,0);
	}
}

function cancel(form){
	ask = confirm('Are you sure you want to cancel this order?');
	if(ask == true){getElement(form).reset();window.location = site_url;}
}

function reset_form(form){
	ask = confirm('Are you sure you want to reset this form?');
	if(ask == true){getElement(form).reset();}
}

function view_invoice(id){
	openPop(site_url+'/popups/invoice.php?id='+id,'print_invoice','toolbar=0,location=0,status=0,menubar=0,scrollbars=1,resizable=0,width=550,height=500');
}

function print_invoice(){
	$('td_btn').hide();
	window.print();
}

function js_replace(target,oldTerm,newTerm,caseSens,wordOnly){
  var work = target;
  var ind = 0;
  var next = 0;
  if (!caseSens) {
    oldTerm = oldTerm.toLowerCase();
    work = target.toLowerCase();
  }
  while ((ind = work.indexOf(oldTerm,next)) >= 0) {
    if (wordOnly) {
      var before = ind - 1;
      var after = ind + oldTerm.length;
      if (!(space(work.charAt(before)) && space(work.charAt(after)))) {
        next = ind + oldTerm.length;
        continue;
      }
    }
    target = target.substring(0,ind) + newTerm + target.substring(ind+oldTerm.length,target.length);
    work = work.substring(0,ind) + newTerm + work.substring(ind+oldTerm.length,work.length);
    next = ind + newTerm.length;
    if (next >= work.length) { break; }
  }
  return target;
}
function space(check) {
  var space = " .,/<>?!`';:@#$%^&*()=-|[]{}" + '"' + "\\\n\t";
  for (var i = 0; i < space.length; i++)
    if (check == space.charAt(i)) { return true; }
  if (check == "") { return true; }
  if (check == null) { return true; }
  return false;
}
function js_search(target,term,caseSens,wordOnly){
  var ind = 0;
  var next = 0;
  if (!caseSens) {
    term = term.toLowerCase();
    target = target.toLowerCase();
  }
  while ((ind = target.indexOf(term,next)) >= 0) {
    if (wordOnly) {
      var before = ind - 1;
      var after = ind + term.length;
      if (!(space(target.charAt(before)) && space(target.charAt(after)))) {
        next = ind + term.length;
        continue;
      }
    }
    return true;
  }
  return false;
}
function in_array(thearray, i){
	if (typeof(thearray[i]) == "undefined" || (thearray[i] == "") || (thearray[i] == null)){
		return false;
	} else {
		return true;
	}
}
function sizeof(thearray){
	for (var i = 0; i < thearray.length; i++){
		if (!in_array(thearray, i)){return i;}
	}
	return thearray.length;
}
function array_push(thearray, value){
	var thearraysize = sizeof(thearray);
	thearray[thearraysize] = value;
	return thearray[thearraysize];
}
function array_pop(thearray){
	var thearraysize = sizeof(thearray);
	var retval = thearray[thearraysize - 1];
	delete thearray[thearraysize - 1];
	return retval;
}

//Textarea Maxlength
function ismaxlength(obj){
	var mlength=getElement(obj).getAttribute? parseInt(getElement(obj).getAttribute("maxlength")) : ""
	//if(!mlength && obj.getAttribute){mlength = obj.getAttribute("maxlength");}
	//alert(mlength);
	if (getElement(obj).getAttribute && getValue(obj).length>mlength)
	getElement(obj).value=getValue(obj).substring(0,mlength)
}
function getLength(obj_txt, obj_dst){
	getElement(obj_dst).innerText = getValue(obj_txt).length;	
}

function chkHTML(obj, typ){
	if(typ == 1){
		var elms = getElement(obj).elements.length;
		var elm_val = "";
		for (var count = 0; count <= elms; ++count){
			elm_val = getElement(obj).elements[count].value;
			if ((elm_val.indexOf("<", 0) >= 0) && (elm_val.indexOf(">", 0) >= 0)){
				return true;
			}			
		}
	} else if(typ == 2){
		var elm_val = getValue(obj);
		if ((elm_val.indexOf("<", 0) >= 0) && (elm_val.indexOf(">", 0) >= 0)){
			return true;
		}			
	}
}

function count_chars(obj){
	if(getElement(obj)){
		return getValue(obj).length;	
	}
}

function getFile(url){
	if(getElement('file_win')){
		getElement('file_win').src = url;
	}	
}

function KillError(message, url, line) {
  var str = "An error has occurred in this dialog." + "\n\n"
  +"Line:        " + line + "\n\nUrl:        " + url + "\n\nError:        " + message;
  //alert(str);
  return true;
}

function getRdValue(id) {
    var value = null;
  
    var element = getElement(id);
    var radioGroupName = null;  
    // if null, then the id must be the radio group name
    if (element == null) {
        radioGroupName = id;
    } else {
        radioGroupName = element.name;  
    }
    if (radioGroupName == null)
	return null;    
    var radios = document.getElementsByTagName('input');
    for (var i=0; i<radios.length; i++) {
        var input = radios[ i ]; 
        if (input.type == 'radio' && input.name == radioGroupName && input.checked) {        
            value = input.value;
            break;
        }
    }
    return value;
}

function submit_frm(frm, mode) {
	var keyCode = event.keyCode ? event.keyCode : event.which ? event.which : event.charCode;
	if(mode == 1){
		if (keyCode == 13) {getElement(frm).submit();} 
	} else if(mode == 2) {
		getElement(frm).submit(); 
	}
}      

function trim(str) { 
       
  var str,m; 
  var rx; 

  rx = new RegExp('^(( |r|n)*)','gi'); 
  str = str.replace(rx,''); 
  rx = new RegExp('(( |r|n)*)$','gi'); 
  returnString = str.replace(rx,''); 
  return returnString; 

} 


