	function replaceAll(string, token, newtoken) {
		while (string.indexOf(token) != -1) {
	 		string = string.replace(token, newtoken);
		}
		return string;
	}
	
	function limitCharOfTextarea(areaName, limit)
	{
		if (areaName.value.length>limit)
			areaName.value = areaName.value.substring(0,limit);
	}
	
	function getXMLHTTP(){
		try{
			obj = new XMLHttpRequest() //for FireFox
		}catch(e){
			obj = new ActiveXObject("Microsoft.XMLHTTP"); //for IE
		}
		
		return obj;
	}
	
	function save(form){
		var http_request;
		document.getElementById('bntSubmit').href = "#";
		document.getElementById('prodBtnCancel').href = "#";
		http_request = getXMLHTTP();
		
		if (validateForm(form))
		{	
			http_request.onreadystatechange = function()
			{
               if (http_request.readyState == 4 ) 
			   {
                   if ( http_request.status == 200 || http_request.status == 301 )
				   {
						closeModalDiv("submit-questions");
						openModalDiv("submit-questions-thanks",-200);
						document.getElementById('prodBtnCancel').href = "javascript:closeProductComment();";
	                    document.getElementById('bntSubmit').href = "javascript: save(document.commentForm);";
                        form.reset();    
                   }
				   else
				   {
				   		 document.getElementById('prodBtnCancel').href = "javascript:closeProductComment();";
				   		 document.getElementById('bntSubmit').href = "javascript: save(document.commentForm);";
                         document.location.href = "/error.jsp";
                   }   
			   }
      
			};
			
			name = 'nameOptional='+form.nameOptional.value;
			comment = 'comments='+ replaceAll( form.comments.value, '\n', ' ');
			segment = 'segment='+form.segment.value;
			showToOthers = 'showToOthers=' + (form.showToOthers.checked == true ? 'Y' : 'N');
			
			http_request.open('GET','/product.do?method=postComment&' + name+'&'+comment+'&'+segment+'&'+showToOthers , true);
			http_request.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
			
			http_request.send(null);
			
		} else{
			document.getElementById('bntSubmit').href = "javascript: save(document.commentForm);";
			document.getElementById('prodBtnCancel').href = "javascript:closeProductComment();";
		}
	}
	
	function validateForm(frm){
	
		var els = frm.elements;
		for (var i = 0; i < els.length; i++) if (els[i].type == 'text' || els[i].type == 'textarea') els[i].value = els[i].value.replace(/^\s\s*/, '').replace(/\s\s*$/, '');
		
		if ( frm.segment.value == "" ){
			alert ("Please select the product that you wish to comment.")
			frm.segment.focus()
			return false
		} else if ( frm.comments.value == "" ){
			alert ("Please enter with your comments.")
			frm.comments.focus()
			return false
		}
		
		return true;
	}
