 /*
	Libraries	 
 */
 
/*************************************************************************
 Fixes IE nav under select elements on the admin.
*************************************************************************/ 
function hideSelect() {
	if (document.all) {
	for (j=0; j<document.forms.length; j++) {
		var theForm = document.forms[j];
		
		for(i=0; i<theForm.elements.length; i++) {
			var alertText = ""
			
			if(theForm.elements[i].type == "select-one") {
				theForm.elements[i].style.visibility = "hidden";
			}
		}
	}
	}
}

function unhideSelect() {
	if (document.all) {
	for (j=0; j<document.forms.length; j++) {
		var theForm = document.forms[j]
		
		for(i=0; i<theForm.elements.length; i++){
			var alertText = ""
			
			if(theForm.elements[i].type == "select-one") {
				theForm.elements[i].style.visibility = "visible";
			}
		}
	}
	}
} 
 
/*************************************************************************
 radio button easies.
*************************************************************************/ 
 function getSelectedRadio(buttonGroup) {
   // returns the array number of the selected radio button or -1 if no button is selected
   if (buttonGroup[0]) { // if the button group is an array (one button is not an array)
      for (var i=0; i<buttonGroup.length; i++) {
         if (buttonGroup[i].checked) {
            return i
         }
      }
   } else {
      if (buttonGroup.checked) { return 0; } // if the one button is checked, return zero
   }
   // if we get to this point, no radio button is selected
   return -1;
} // Ends the "getSelectedRadio" function

function getSelectedRadioValue(buttonGroup) {
   // returns the value of the selected radio button or "" if no button is selected
   var i = getSelectedRadio(buttonGroup);
   if (i == -1) {
      return "";
   } else {
      if (buttonGroup[i]) { // Make sure the button group is an array (not just one button)
         return buttonGroup[i].value;
      } else { // The button group is just the one button, and it is checked
         return buttonGroup.value;
      }
   }
} // Ends the "getSelectedRadioValue" function
 
 
/*************************************************************************
	String Manipulation
*************************************************************************/	
 
	// only allow $1,234
	function isNumeric(string) {
		var reg = /[^0-9$,.-]/i;
		if(reg.exec(string)) return false;
		return true;
	}

	function numbersOnly(string) {
		return [string.replace(/[^0-9.]/gi, "")].join("");
	}	 
	 
	function trim(str) {
		if(str)
			return str.replace(/^\s*|\s*$/g,"");
		else
			return "";
	}

 /*************************************************************************
	Generic Form Validator
 *************************************************************************/

	function validateforms(form){	
		for(i = 0; i < form.elements.length; i++){
			// first check required elements as filled in
			if (trim(form.elements[i].value) == "" && form.elements[i].getAttribute("required") == "true") {
				alert("Please enter a value in the "+ form.elements[i].getAttribute("description") +" field.");
				form.elements[i].focus();
				return false ;
			}		
			
			//Check numeric fields expected	
			if (form.elements[i].getAttribute("numeric") == "true") {
				if(trim(form.elements[i].value) != "" && !isNumeric(form.elements[i].value)){
					alert("Please enter a numeric value in the "+ form.elements[i].getAttribute("description") +" field.");
					form.elements[i].focus();
					return false ;
				}
				else if(form.elements[i].value != "" && isNumeric(form.elements[i].value)){
					form.elements[i].value = numbersOnly(form.elements[i].value);				
				}
			}
			
			/* Check Email */
			if (form.elements[i].getAttribute("email") == "true" && trim(form.elements[i].value) != "" ) {
				var filter  = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
				if (!filter.test(form.elements[i].value)){
					alert("The value in the '" +form.elements[i].getAttribute("description") +"' field is not a valid email address.");
					form.elements[i].focus();
					return false;
				}			
			}
			
			/* Check url validation */
			if (form.elements[i].getAttribute("url") == "true") {
				var filter  = /^([a-zA-Z0-9_-])+$/;
				if (!filter.test(form.elements[i].value)){
					alert("The value in the '" +form.elements[i].getAttribute("description") +"' field is not valid. Spaces and special characters are not permitted.");
					form.elements[i].focus();
					return false;
				}			
			}
			
			/* Check Date
			if (form.elements[i].getAttribute("date") == "true") {
				selecteddate = new Date(form.elements[i].value);
				mynow = new Date();		
				alert(selecteddate.getTime());
				alert(mynow.getTime());	
				if(selecteddate.getTime() <= mynow.getTime()){
					alert("Please enter a date not already passed for the date field.");
					form.elements[i].focus();
					return false ;
				}
			}
			*/
			//		 then check other things				    
		}
		return true ;  
	}

 /*************************************************************************
	Alerts, Messageboxes, Confirms
 *************************************************************************/ 
   
	function deleteconfirm(myform){
		return confirm('Are you sure you wish to delete this record? Delete actions cannot be undone.');
	}

	function cancelAction(){
		history.go(-1);
	}	
	
	function coverSetVisible(obj) {
		var divRef = document.getElementById(obj);
		var ifrRef = document.getElementById(obj+'cover');
	 
		ifrRef.style.display = "block";
		ifrRef.style.width = divRef.offsetWidth;
		ifrRef.style.height = divRef.offsetHeight;
		ifrRef.style.top = divRef.style.top;
		ifrRef.style.left = divRef.style.left;
		ifrRef.style.zIndex = divRef.style.zIndex - 1;
		//alert(IfrRef.style.zIndex);
	}


	 // Selected Tab
	 var tabSelected = "";
	 
	 var dom = document.getElementById;
	 var iex = document.all;
	 var ns4 = document.layers;

	function getElement(name, nest) {
		nest = nest ? 'document.'+nest+'.' : '';
		var el = dom ? document.getElementById(name) : iex ? document.all[name] : ns4 ? eval(nest+'document.'+name) : false;
		el.css = ns4 ? el : el.style;
		return el;
	}

/*************************************************************************
	Form Change Checking (to prevent users from navigating away from a changed form)
*************************************************************************/
		function jsxge(jsxe,jsxn) {
			jsxn = jsxn ? 'document.'+jsxn+'.' : '';
			return document.getElementById ? document.getElementById(jsxe) : document.all ? document.all[jsxe] : document.layers ? eval(jsxn+'document.'+jsxe) : false;
			}
		
		// check for change
		function nav(page) {
			var changelog = jsxge("changelog");
			var string = changelog.value;
			if (string != "" && string) {
				var input = confirm("You have made changes to the following items:\n\n"+string+"\n\nIf you navigate away from this page without saving these changes will be lost.");
			} else input = true;
			if (input) 
				{ return true; } 
			else return false;
		}
		
		//update change log
		function doChange(el) {
			var changelog = jsxge("changelog");
			if (changelog.value != "") changelog.value += "\n";
			changelog.value += this.getAttribute("description");			
		}
		
		function changecheck(form){		
			//Mark all items on the page with an onchange element
			for(i = 0; i < form.elements.length; i++){
				form.elements[i].onchange = doChange;			
			}		
			
			//Then mark all items in the adminColumn with a function to prompt confirm
			var x = document.getElementById('adminColumn');
			if (!x) return;
			var y = x.getElementsByTagName('a');
			for (var i=0;i<y.length;i++){			
				/*if(ns4)
					y[i].addEventListener("onclick", nav, false);
				else if(iex)
					y[i].attachEvent("onclick", nav);				
					*/
				y[i].onclick = nav;
			}
		}

/*
    Let's Eat Valutech parsing
    How to use: on element, do    
    onkeypress="parseGC(this);"    
    for cc scanning.
*/
function parseGC(e){
    //bad valutech characters (;?=)
    //Also note that cardswipes always send a carriage return
    e.value = e.value.replace(';', '');
    e.value = e.value.replace('?', '');
    e.value = e.value.replace('=', '');
    e.value = e.value.replace("\r", '');
}

//swiping credit cards
function parseCC(e){          
    if(window.event.keyCode == 13){        
        savethevalue = e.value;
        /*
                //bad valutech characters (;?=)
                //Also note that cardswipes always send a carriage return
                e.value = e.value.replace(';', '');
                e.value = e.value.replace('?', '');
                e.value = e.value.replace('=', '');
                e.value = e.value.replace("\r", '');
                e.value = e.value.replace("%B", '');		
        */				
        //trim off first two and everthing after pos 18
        finalccvalue = savethevalue.substr(2,16);		        
        
        cctypedd = document.getElementById('cctype');        
       
        if(finalccvalue.charAt(0) == '4'){
            cctypedd.selectedIndex = 1;        
        }
        
        if(finalccvalue.charAt(0) == '5'){
            cctypedd.selectedIndex = 2;        
        }
       
        if(finalccvalue.charAt(0) == '3'){
            cctypedd.selectedIndex = 3;       
        }
        //amex            
        if(finalccvalue.charAt(15) == '^'){
            cctypedd.selectedIndex = 3;             
            finalccvalue = finalccvalue.substr(0,15);
        }
        
        e.value = finalccvalue;	
        var temp = new Array();
        temp = savethevalue.split('^');
        
        monthdd = document.getElementById('expmonth');
        yeardd = document.getElementById('expyear');
               
        yearvalue = temp[2].substring(0,2);        
        monthvalue = temp[2].substring(2,4);
        
        for(x = 0; x < monthdd.options.length; x++)
        {     
            if(monthdd.options[x].value.length == 1)
            {               
                if(('0'+monthdd.options[x].value) == monthvalue){           
                    monthdd.selectedIndex = x;                      
                }
            }
            else
            {
                if((monthdd.options[x].value) == monthvalue){           
                    monthdd.selectedIndex = x;                      
                }
            }
        }             
       
        for(x = 0; x < yeardd .options.length; x++)
        {                               
            if(yeardd.options[x].value == ('20'+yearvalue)){           
                yeardd.selectedIndex = x;                      
            }            
        }        
    }                 
}

/* Delete confirmation function 
EG - I believe this is crap. Please make it less crap.

*/
function deleteConfirm(wheretogo) {
	var doit = confirm("Are you sure you want to delete this item?");
	if(doit)
		window.location = wheretogo;
	else
		return false;
}

String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g, "");
}
String.prototype.ltrim = function() {
    return this.replace(/^\s+/, "");
}
String.prototype.rtrim = function() {
    return this.replace(/\s+$/, "");
}