var util={

	/////////////////////////
	//toggles
	/////////////////////////

	//selected id is toggled to show or hide
	display_toggle:function(id){
		if(document.getElementById(id)){
			if(document.getElementById(id).style.display=='block'){
				document.getElementById(id).style.display=='none'
			}else{
				document.getElementById(id).style.display=='block'
			}
		}
	},
	//selected id is toggled to enabled or disabled
	disable_toggle:function(id){
		if(document.getElementById(id)){
			if(document.getElementById(id).disabled='false'){
				document.getElementById(id).disabled='true';
			}else{
				document.getElementById(id).disabled='false';
			}
		}
 	},

 	/////////////////////////
 	//validations
 	/////////////////////////

 	//output object information
	var_dump:function (obj){
		var str='';
		for(prop in obj){
			str+=prop + ' Value :'+ obj[prop]+'<br>';
		}
		document.write(str);
	},

	//validate email
	email_validation:function (str,msg){
		msg = msg || 'true';
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	    if(reg.test(str) == false) {
	      if (msg!='true')
	     	ss.displayMessage('Invalid Email Address');
	      return false;
	    }
	    return true;
 	},

 	//validate money value
 	//str->string value, msg->alternate/custom message, neg->allow negative values
 	MoneyFormat:function (str,msg,neg){
 		//default values
		msg = msg || 'true';
 		neg = neg || 'false';

 		if(neg=='false'){
 			//allow negative values
			var reg = /(^-*\d+$)|(^-*\d+\.\d+$)/;
 		}else{
 			//positive values only - not working
 			var reg = /(^-*\d+$)|(^-*\d+\.\d+$)/;
 		}
	    if(str!=''&&reg.test(str) == false) {
	      if (msg=='true'){
	      	//default message
	     	alert('Invalid Dollar Amount Format.  (Numeric 0-9 and . are allowed)');
	      }else{
	      	alert(msg);
	      }
	      return false;
	    }
	    return true;
	},

	/////////////////////////
 	//single keypress
 	/////////////////////////

 	//validate each key press to be a numeric key
	isNumberKey:function (evt){
		var charCode = (evt.which) ? evt.which : evt.keyCode;
//		alert(charCode);
		if (charCode > 31 && (charCode < 48 || charCode > 57)){
			ss.displayMessage('Please enter numeric numbers only (0-9)','Error');
			return false;
		}
		return true;
	},

	//single keypress - not working yet
 	ssn:function (evt,obj,hidden){
 		hidden = hidden || 5;
		var charCode = (evt.which) ? evt.which : evt.keyCode;
		if (charCode > 31 && (charCode < 48 || charCode > 57)){
			ss.displayMessage('Please enter numeric numbers only (0-9)','Error');
			return false;
		}
		obj.hidVal+=charCode;
		if(obj.value.length<hidden){
			obj.value=obj.value+'X';
		}else{
			obj.value=obj.value+charCode;
		}
		return true;
	},

	/////////////////////////
	//selecting
	/////////////////////////

	//select and focus on id
 	focus_and_highlight:function (id){
 		if($('#'+id)){
 			$('#'+id).focus();
 			$('#'+id).select();
 		}
	},

	//select and foucs on id and alert a message
 	focus_and_highlight_if_empty:function (id,msg,title){
 		if($('#'+id)&&$('#'+id).val()==''){
 			this.focus_and_highlight(id);
 			if(msg!='')
				button = {
					"OK":function(){
						$(this).dialog('close');
					}
				}
	 			ss.displayMessage(msg,title,button);
 			return false;
 		}
 		return true;
	},

	//ajax version of getting check box values that are true
 	ajax_radio_checkbox_checked:function (obj){
 		var checkItems=new Array();
		for(i=0;i<(obj.length);i++){
			if(obj[i].checked){
				checkItems.push(obj[i].value);
			}
		}
		return checkItems;
	},

	//ajax version of clearing
 	ajax_radio_checkbox_clear:function (obj){
 		var checkItems=new Array();
		for(i=0;i<(obj.length);i++){
			obj[i].checked="";
		}
		return checkItems;
	},

	//strip string to numberic numbers and validate it
	validatePhone:function (fld) {
	    var error = "";
	    var stripped = fld.value.replace(/[\(\)\.\-\ ]/g, '');

	   	if (isNaN(parseInt(stripped))) {
	    	ss.displayMessage("The phone number contains illegal characters.\n");
	    	return false;
	    //7->xxx xxxx, 10-> xxx xxx xxxx
	  	} else if (!(stripped.length == 7)&&!(stripped.length == 10)) {
	        ss.displayMessage("The phone number is the wrong length. Make sure you included an area code.\n");
	        return false;
	  	}
	   	return true;
	},

	//ajax callback function (actions are seperated by |||)->
		//return content and replace innerhtml of element id
		//key::id (parse by |||) element id::id (parse by |||) content ((parse by |||) element id name (parse by |||) message (parse by |||) callback function) default is undefine
		//ex. echo "id|||display|||content_value" -> find element id=display and replace innerhtml with content_value
		//ex. echo "id|||display|||content_value|||home|||invalid phone number|||format()" -> find element id=display and replace innerhtml with content_value, highight and focus phone, alert message and execute function format()

		//redirect to application location and alert message
		//key::location (parse by |||) application location (parse by |||) message
		//ex. echo "location|||index.html|||You have successful loged in." -> redirect to index.html and alert the message

		//error return and highlight and select element id and alert message
		//element id:: id (parse by |||) error message
		//ex. echo "phone|||Incorrect phone format." -> highlight and focus element id=phone and alert message
		//(note)->if id is empty it will alert only a message

	application_doAlert:function (content) {
		//removes carriage returns and newlines
		content=content.replace(/\r|\n|\r\n/g, '');
		loff();
		//alert(content);
	    var code=content.split('|||');

	    if(code[0]=='location'){
	    	if(code[2].length!=0){
				ss.displayMessage(code[2],'Info:');		//alert a message
			}
			window.location=code[1];
		}else if(code[0]=='id'){
			document.getElementById(code[1]).innerHTML=code[2];
			if(code[4]&&code[4]!='undefined'){
				ss.displayMessage(code[4]);
			}
			if(code[3]&&code[3]!='undefined'){
				util.focus_and_highlight(code[3]);
			}
			if(code[5]&&code[5]!='undefined'){
				eval(code[5]);
			}
		}else if(code[0]=='eval'){
			eval(code[1]);
		}else{
			ss.displayMessage(code[1],'Error');
			if(code[0]!=''){
				util.focus_and_highlight(code[0]);
			}
			return false;
		}
	},
	alert_value:function(e){
		alert(e.target.id);
	},
	alert_content:function(e){
		alert(e);
	},

	stripHTML:function(oldString) {
		var newString = "";
		var inTag = false;
		for(var i = 0; i < oldString.length; i++) {
			if(oldString.charAt(i) == '<') inTag = true;
			if(oldString.charAt(i) == '>') {
				inTag = false;
				i++;
			}
			if(!inTag) newString += oldString.charAt(i);
		}

		return newString;
	}
};