/*
 * @author: Dang Khoa
 * This function is used to get the dom element using elementId
 */
function $tn ( elementId ) {
	return document.getElementById( elementId );
}//end $tn

function getYCoordinate ( y ) {

	if (jQuery.browser.msie) {
		return y + 25;
	}//end if
	else {
		return y + 10;
	}//end else
}//end getYCoordinate

Utils = {
	
	emptyMessageForShouldiBubble: "",
	emptyMessageForMiniShouldi: "",
	emptyMessageForAdvice: "click here to answer",
	url_reserved_characters: "%'\".$&+,/:;=?@",
	
	alert: function( message ) {
		
		alert(message);
			
	},//end alert
	
	/*
	 * @author Dang Khoa
	 * This function is used to restrict the number of
	 * character input into a text field or textarea
	 */
	limitSizedFormField: function() {
		
		jQuery("textarea[@class*=limit-size-field]").each(function() {
			
			var maxLength = jQuery(this).attr("maxsize");
			
			
			jQuery(this).keydown(function( e ) {
				if ( this.value.length > maxLength ) {
					this.value = this.value.substring(0, maxLength);
				}//end if
			});
			
			jQuery(this).keyup(function( e ) {
				if ( this.value.length > maxLength ) {
					this.value = this.value.substring(0, maxLength);
				}//end if
			});
			
			jQuery(this).focus(function( e ) {
				if ( this.value.length > maxLength ) {
					this.value = this.value.substring(0, maxLength);
				}//end if
			});
		});

	},
	
	limitSizedFormFieldJumpTo: function() {
		
		jQuery("input[@class*=limit-size-jump-field]").each(function() {
			
			var maxLength = jQuery(this).attr("maxsize");
			var jumpTo = jQuery(this).attr("jump_to_field");
			
			jQuery(this).keydown(function( e ) {
				if ( this.value.length > maxLength ) {
	
					var sentences = Utils.splitSentence( this.value, maxLength);
					var first_sentence = sentences[0];
					var second_sentence = sentences[1];
					
					if( jQuery("#" + jumpTo).parent().css("display") == "none") {
						jQuery("#" + jQuery(this).attr('click-to-open')).click();
					}//end if 
					//alert(first_sentence + " | " + second_sentence);
					//var temp = this.value.substring(0, maxLength);
					//var redundant = this.value.substring( maxLength );
					
					this.value = first_sentence;
					jQuery(this).blur();
					jQuery("#" + jumpTo).focus();
					if ( second_sentence.length > 0) {
						var jumpToValue = jQuery("#" + jumpTo).val();
						jQuery("#" + jumpTo).val("..." + second_sentence + " " + jumpToValue);
					}
				}//end if
			});
			
			jQuery(this).keyup(function( e ) {
				if ( this.value.length > maxLength ) {
					
					var sentences = Utils.splitSentence( this.value, maxLength);
					var first_sentence = sentences[0];
					var second_sentence = sentences[1];
					
					if( jQuery("#" + jumpTo).parent().css("display") == "none") {
						jQuery("#" + jQuery(this).attr('click-to-open')).click();
					}//end if 
					
					//alert(first_sentence + " | " + second_sentence);
					//var temp = this.value.substring(0, maxLength);
					//var redundant = this.value.substring( maxLength );
					this.value = first_sentence;
					jQuery(this).blur();
					jQuery("#" + jumpTo).focus();
					if ( second_sentence.length > 0) {
						var jumpToValue = jQuery("#" + jumpTo).val();
						jQuery("#" + jumpTo).val("..." + second_sentence + " " + jumpToValue);
					}
				}//end if
			});
			
			jQuery(this).focus(function( e ) {
				if ( this.value.length > maxLength ) {
					var sentences = Utils.splitSentence( this.value, maxLength);
					var first_sentence = sentences[0];
					var second_sentence = sentences[1];
					
					if( jQuery("#" + jumpTo).parent().css("display") == "none") {
						alert(jQuery(this).attr('click-to-open'));
					}//end if 
					
					//alert(first_sentence + " | " + second_sentence);
					//var temp = this.value.substring(0, maxLength);
					//var redundant = this.value.substring( maxLength );
					this.value =  first_sentence;
					
					jQuery(this).blur();
					jQuery("#" + jumpTo).focus();
					if ( second_sentence.length > 0) {
						var jumpToValue = jQuery("#" + jumpTo).val();
						jQuery("#" + jumpTo).val("..." + second_sentence + " " + jumpToValue);
					}
				}//end if
			});
			
			
		});

	},
	
	splitSentence : function( sentence, length ) {
		if ( sentence.length <= length ) {
			return sentence;
		}//end if
		var words = sentence.split(" ");
		var first_sentence = "";
		var second_sentence = "";
		var total_length = 0;
		var counter = 0;
		for( var i = 0; i < words.length; i++) {
			
			total_length += words[i].length + 1
			if (total_length > length) {
				break;
			}//end if
		      
		    first_sentence += words[i] + " "
		    counter = counter + 1
		}//end for
		
		second_sentence = words.splice(counter, words.length - 1).join(" ");
		return [first_sentence, second_sentence];
	},
	
	isSpecialKeyCode: function(code) {
		var specialKeyCode = [8, 37, 38, 39, 40];
		for ( var i = 0; i < specialKeyCode.length; i++) {
			if (code == specialKeyCode[i]) {
				return true;
			}//end if
		}//end for
		
		return false;		
	},
	
	isNumeric: function( str ) {
		
		if (str === "") {
			return false;
		}//end if 
		var validChars = "0123456789";
   		var aChar;
 
   		for (i = 0; i < str.length; i++) { 
      		aChar = str.charAt(i); 
      		if (validChars.indexOf(aChar) == -1) {
         		return false;
         	}//end if
      	}//end for
   		
		return true;
   
	},//end isNumeric
	
	 /**
	  *
	  * DHTML email validation script. Courtesy of SmartWebby.com (http://www.smartwebby.com/dhtml/)
	  */
	isEmail: function( str ) {

		var at="@";
		var dot=".";
		var lat = str.indexOf(at);
		var lstr = str.length;
		var ldot = str.indexOf(dot);
	
		if (str.indexOf(at) == -1){
			return false;
		}
	
		if (str.indexOf(at) == -1 || str.indexOf(at) === 0 || str.indexOf(at) == lstr){
			return false;
		}
		
		if (str.indexOf(dot)==-1 || str.indexOf(dot) === 0 || str.indexOf(dot)==lstr){
			return false;
		}
		
		if (str.indexOf(at,(lat+1))!=-1){
			return false;
		}
		
		if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
			return false;
		}
		
		if (str.indexOf(dot,(lat+2))==-1){
			return false;
		}
		
		if (str.indexOf(" ")!=-1){
			return false;
		}
		
		return true;
		
	},
	
	isUrl : function(s) {
		var regexp = /^((http|https):\/\/)?[a-z0-9]+([\-\.]{1}[a-z0-9]+)*\.[a-z]{2,5}(([0-9]{1,5})?\/.*)?$/i;
		return regexp.test(s);
	},
	
	isAValidField: function( value, conditions, fieldName ) {
		var condObj = eval("(" + conditions + ")");
		var constraints = condObj.conditions;
		
		for ( var i = 0; i < constraints.length; i++ ) {
			if (constraints[i] == "not-blank" && jQuery.trim(value) === "") {
				return fieldName + " cannot be empty.";	
			}//end if
			
			if (constraints[i].indexOf("size") >= 0) {
				var requiredSize = constraints[i].split("@")[2];
				var operator = constraints[i].split("@")[1];
		
				if (operator == "=" && value.length != requiredSize) {
					return fieldName + " must have size of " + requiredSize + "."; 
				}
				else if (operator == "<=" && value.length > requiredSize) {
					return fieldName + " must have size of at most " + requiredSize + "."; 
				}
				else if (operator == ">=" && value.length < requiredSize) {
					return fieldName + " must have size of at least " + requiredSize + "."; 
				}//end else
				
				
			}//end if
			
			if (constraints[i].indexOf("unique") >= 0) {

			  	className = constraints[i].split("@")[1];
		      	//console.log(condition, className, value);
		    	var text;
				var msg = "";
		    	jQuery("." + className).each( function ( i, e ) {
			      	text = jQuery.trim( jQuery(e).text() );
			      	if ( text === value) {
			        	msg = fieldName + " must be unique.";
		      		}//end if
		    	});
				if ( msg !== "") {
					return msg;
				}//end if
			}//end if
			

			if (constraints[i] == "url" && this.isUrl(value) === false && jQuery.trim(value) != "") {
				return fieldName + " must be a valid url";
			}//end if
			
			if (constraints[i] == "email" && this.isEmail(value) === false && jQuery.trim(value) != "") {
				return fieldName + " must be a valid email address";
			}//end if
		}//end for	
		return "";
	},
	
	getWindowWidth : function() {
		
		var myWidth = 0, myHeight = 0;
	  	if( typeof( window.innerWidth ) == 'number' ) {
		    //Non-IE
		    myWidth = window.innerWidth;
		    myHeight = window.innerHeight;
	  	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		    //IE 6+ in 'standards compliant mode'
		    myWidth = document.documentElement.clientWidth;
		    myHeight = document.documentElement.clientHeight;
	  	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		    //IE 4 compatible
		    myWidth = document.body.clientWidth;
		    myHeight = document.body.clientHeight;
	  	}
 	
		return myWidth;
	},
	
	hitEnterToTriggerSubmit: function( fieldHitEnterDomId, buttonDomId ) {
		
		jQuery("#" + fieldHitEnterDomId).keydown(function(e) {
			if (e.keyCode == 13) {
				jQuery("#" + buttonDomId).click();
				if ( jQuery.browser.msie) {
					return false;
				}//end if
			}//end if
		});	
	},
	
	getExtensionByProtocolId: function( protocolId ) {
		if(protocolId == "prpl-yahoo") {
			return "@yahoo.com";
		}//end if
		
		if (protocolId == "prpl-msn") {
			return "@msn.com,@hotmail.com,@live.com";
		}//end if
		
		if (protocolId == "prpl-jabber") {
			return "@gmail.com";
		}//end if
		
		if (protocolId == "prpl-aim") {
			return "@aol.com";
		}//end if
		
		return "";
	},//end getExtensionByProtocolId
	
	stripHTML: function( s ) {
		return s.replace(/(<([^>]+)>)/ig,""); 
	},//end stripHTML
	
	stripAll: function( s ) {
		var newVal = jQuery.trim(s);
		newVal = this.stripHTML(newVal);
		return newVal;
	},
	
	allocateVal: function( domId ) {
		var value = this.stripAll(jQuery("#" + domId).val());
		jQuery("#" + domId).val(value);
	},//end
	
	pressTab: function( fromDomId, toDomId ) {
		jQuery("#" + fromDomId).unbind("keydown");
		jQuery("#" + toDomId).unbind("keydown");
		jQuery("#" + fromDomId).keydown(function(e) {
		
			if( e.keyCode == 9 && e.target.id == fromDomId) {
			
				jQuery("#" + fromDomId).blur();
				window.toDomId = toDomId;
				setTimeout("jQuery('#' + window.toDomId).focus();", 0);
				return false;
			}//end if
		});	
	}//end pressTab
};
