var prepareFormFields = (function(formId) {
	var inputs=new Array();
	form=document.getElementById(formId);
   		$(':input', form).each(function() {
   			if (this.type=="radio") {
   				if ((inputs[inputs.length-1].indexOf(this.name)==-1) && (this.checked)) {
   					inputs.push(this.name + '=' + encode(this.value));
   				}
   			} else 
   				if (this.type=="checkbox") {
   					if (this.checked) {
   						inputs.push(this.name + '=' + encode(this.value));
	   				}
	   				else
	   				{
	   					inputs.push(this.name + '=');
	   				}
   				} else {
	      	   		inputs.push(this.name + '=' + encode(this.value));
   				}
   	 	});
 	return inputs.join('&');
});
// public method for URL encoding
var encode = (function (string) {
	 return escape(this._utf8_encode(string));
})

// public method for URL decoding
var decode = (function (string) {
 	return this._utf8_decode(unescape(string));
})

// private method for UTF-8 encoding
var _utf8_encode = (function (string) {
	string = string.replace(/\r\n/g,"\n");
 	var utftext = "";
	for (var n = 0; n < string.length; n++) {
		var c = string.charCodeAt(n);
		if (c < 128) {
			utftext += String.fromCharCode(c);
		} else if((c > 127) && (c < 2048)) {
			utftext += String.fromCharCode((c >> 6) | 192);
			utftext += String.fromCharCode((c & 63) | 128);
		} else {
			utftext += String.fromCharCode((c >> 12) | 224);
			utftext += String.fromCharCode(((c >> 6) & 63) | 128);
			utftext += String.fromCharCode((c & 63) | 128);
		}
	}
	return utftext;
})

// private method for UTF-8 decoding
var _utf8_decode = ( function (utftext) {
	var string = "";
 	var i = 0;
 	var c = c1 = c2 = 0;
	while ( i < utftext.length ) {
		c = utftext.charCodeAt(i);
   		if (c < 128) {
    		string += String.fromCharCode(c);
    		i++;
  		} else if((c > 191) && (c < 224)) {
 			c2 = utftext.charCodeAt(i+1);
    		string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
    		i += 2;
  		} else {
 			c2 = utftext.charCodeAt(i+1);
    		c3 = utftext.charCodeAt(i+2);
    		string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
   			i += 3;
 		}
	}
	return string;
})

function isEmail(strValue) {
  	var objRE = /^[\w-\.\']{1,}\@([\da-zA-Z-]{1,}\.){1,}[\da-zA-Z-]{2,}$/;
	return (strValue != '' && objRE.test(strValue));
}

function isData(strValue) {
	var objRE = /^((0[1-9]|[12]\d)\/(0[1-9]|1[0-2])|30\/(0[13-9]|1[0-2])|31\/(0[13578]|1[02]))\/\d{4}$/;
	return (strValue != '' && objRE.test(strValue));
}

function autoEllipseText(text, width, textend)
{
   $('<span id="ellipsisSpan" style="white-space:nowrap;">' + text + '</span>').appendTo(document.body)
   inSpan = $('#ellipsisSpan');
if(inSpan.outerWidth() > width)
   {
      var i = text.length;
      while((inSpan.outerWidth() >= width) && i > 1)
      {
         inSpan.html(text.substr(0,i) +  textend);
         i--; 
      } 
      returnText = inSpan.html();
      inSpan.remove(); 
      return returnText;
   }
   inSpan.remove(); 
   return text;
}

function autoEllipseTextHeight(element, height, textend) {
   	$('<div class="itemInnerResultsW" id="ellipsisSpan" style="height: auto!important; display:none;"><div style="font-size:90%; line-height:115%;padding:0 2px 0 2px; margin:3px 0px 0px 0px;"><div id="inSpanContent" style="margin:0; padding:0;">' + element.html() + '</div></div></div>').appendTo(document.body)
   	inSpan = $('#ellipsisSpan');
	if (inSpan.height()>height){
		i=element.html().length-1;
		
	   e = 0; d = element.html().length-1;
	   flag=true;
	   while ((e <= d) && (flag)){ 
      		m = (e + d)/2; 
			$("#inSpanContent").html(element.html().substring(0,m) + " " + textend);
			if ((inSpan.outerHeight() == height)) flag=false;
			if (inSpan.outerHeight() < height) e = m + 1;
	      		else d = m - 1;
   		}
   		while (inSpan.outerHeight() <= height) {
   			m ++;
			$("#inSpanContent").html(element.html().substring(0,m) + " " + textend);
   		}
		$("#inSpanContent").html(element.html().substring(0,m-1) + " " + textend);
		element.html($("#inSpanContent").html());
	}
    inSpan.remove(); 
}
             
