/*-------------------------------------------------------------------------------------------
getFormDataAsString
	- single paramter is a FORM object or name attribute or a regular JS object
	- return the getFormElementsByName data in a URI encoded string
	- saves time for ajax component. Instead of adding fields to an HTML FORM, then having 
	ckAjax extract it, we can just send what it needs
-------------------------------------------------------------------------------------------*/
function getFormDataAsString(variant) {
	var value;
	var aMsgBody = Array();
	
	var bFormObj = true;
	if (typeof(variant)=="object") bFormObj = (variant.tagName=="FORM");
	
	var oData = (bFormObj ? formObjectsArray(variant) : variant);

	for (var sFieldName in oData)  {
		if (sFieldName != "") { //appears to be blank for fieldset
			value = (bFormObj ? oData[sFieldName].value : oData[sFieldName]);
			if (typeof(value)=="boolean") value=(value ? 1 : 0); //boolean to numeric so can be converted to string
			aMsgBody[aMsgBody.length]= (sFieldName + "=" + ckEncodeURI(value));
		}
	}

	return aMsgBody.join("&");
}

function ckEncodeURI(sValue) {
	sValue += "" //convert numbers to string
	if (sValue != "") {
		//replace non-standard formatting
		sValue = sValue.replace(/\u2018|\u2019/g,"'");
		sValue = sValue.replace(/\u201C|\u201D/g,"\"");
		sValue = sValue.replace(/\u2026/g,"...")
		sValue = sValue.replace(/\u2013|\u2014|\u2015/g,"--")
		sValue = sValue.replace(/\u2022|\u204C|\u204D|\u2043|\u2023/g,"*") //bullet
		
		//replace html entities. Even in going into textarea they are entities, textarea converts to character so we need to convert back before saving
		sValue = sValue.replace(/\u00AE/g,"&reg;");
		sValue = sValue.replace(/\u00A9/g,"&copy;");
		sValue = sValue.replace(/\u2122/g,"&trade;");
		sValue = sValue.replace(/\u00A0/g,"&nbsp;");        

		//do not encode uniqueidentifier
		if (!isGUID(sValue)) sValue = encodeURI(sValue);

		sValue = sValue.replace(/\+/g,encodeURIComponent('+')); //must be done AFTER encodeURI
		sValue = sValue.replace(/&/g,escape('&')); //must be done AFTER encodeURI
	}
	return sValue;

}
function getURIEncoded(msgBody) {
	var aParam;
	
	if (msgBody == "") return ""
	else {
		var aParams = msgBody.split("&");
		for (var index=0; index < aParams.length; index++) {
			aParam = aParams[index].split("=");
			if (aParam[1] != "") {
				aParam[1] = ckEncodeURI(aParam[1]);
			}
			aParams[index] = aParam.join("=");
		}
	
		return aParams.join("&");
	}
}

/*-------------------------------------------------------------------------------------------
getFormDataAsForm
	- single paramter is a FORM object or name attribute
	- return the getFormElementsByName data in a FORM object
-------------------------------------------------------------------------------------------*/
function getFormDataAsForm(variant) {	
	var sActionURL = (arguments.length==2 ? arguments[1] : "");
	var oForm = createForm(sActionURL);	
	
	var aFormObjects = formObjectsArray(variant);
	for (var sFieldName in aFormObjects) addToForm(oForm,sFieldName,aFormObjects[sFieldName].value);
			
	return oForm;	
}

