String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function() {
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function() {
	return this.replace(/\s+$/,"");
}

		function getElement(ele)
		{
			var theobj = false;
			if(typeof ele == 'string')
				theobj = (document.getElementById)?document.getElementById(ele):document.all[ele];
			else
				theobj = ele;
		
			return theobj;
		}
		
		function test(src)
		{
     		var emailReg = "^[\\w-_\.+]*[\\w-_\.]\@([\\w]+\\.)+[\\w]+[\\w]$";
     		var regex = new RegExp(emailReg);
     		return regex.test(src);
  		}
  		
		function checkEmail(inputvalue){
    		var pattern=/^([a-zA-Z0-9_.-])+@([a-zA-Z0-9_.-])+\.([a-zA-Z])+([a-zA-Z])+/;
    		var chararray = [',', ';', ' ', '/'];
    		
    		inputvalue = inputvalue.trim();
     		if(pattern.test(inputvalue)){      
			 	for (var i=0; i<chararray.length; i++)
				 {
				  	if (inputvalue.indexOf(chararray[i]) != -1)
				  		return false;
				 }   
				return true;   
    		}else{   
				return false; 
   		 }
		}

		function check_changes(form1, applyelement, statusmessageelement)
		{
		 	if (form1.FIRSTNAME.value == '')
		 	{
				statusmessageelement.innerHTML = 'Enter your first name<br>';
				applyelement.style.display = 'none';
				return false;
			}
		 	if (form1.LASTNAME.value == '')
		 	{
				statusmessageelement.innerHTML = 'Enter your last name<br>';
				applyelement.style.display = 'none';
				return false;
			}
		 	if (form1.ADDRESS1.value == '')
		 	{
				statusmessageelement.innerHTML = 'Enter your address<br>';
				applyelement.style.display = 'none';
				return false;
			}
		 	if (form1.CITY.value == '')
		 	{
				statusmessageelement.innerHTML = 'Enter your city<br>';
				applyelement.style.display = 'none';
				return false;
			}
		 	if ((form1.STATE.value == '') || (form1.STATE.value.length != 2))
		 	{
				statusmessageelement.innerHTML = 'Enter your 2-character state abbreviation<br>';
				applyelement.style.display = 'none';
				return false;
			}
		 	if (form1.ZIP.value == '')
		 	{
				statusmessageelement.innerHTML = 'Enter your zip-code<br>';
				applyelement.style.display = 'none';
				return false;
			}
		 	if ((form1.EMAIL.value == '') || (!checkEmail(form1.EMAIL.value)))
		 	{
				statusmessageelement.innerHTML = 'Enter your valid email address<br>';
				applyelement.style.display = 'none';
				return false;
			}
		 	if (form1.MESSAGE.value == '')
		 	{
				statusmessageelement.innerHTML = 'Enter your message<br>';
				applyelement.style.display = 'none';
				return false;
			}

		 	if ((form1.captcha_code.value == '') || (form1.captcha_code.value.length != 5))
		 	{
				statusmessageelement.innerHTML = 'Enter the text from the image<br>';
				applyelement.style.display = 'none';
				return false;
			}
			
			statusmessageelement.innerHTML = 'Click Submit to send the message<br>';
			applyelement.style.display = '';
			return true;
		}
		
		function apply_changes(url, postdata, statusmessageelement)
		{
		 	var response = false;
		 	var retval = false;
//		 	statusmessageelement.innerHTML = url + postdata;
//		 	return updated;
		 	//alert([xmlhttp, url, postdata, statusmessageelement]);
			doRequest(xmlhttp, "POST", url, false, null, postdata);
			//alert(xmlhttp.status);
			if (xmlhttp.status == 200)
			{
	         	response = xmlhttp.responseText;
			}
			//alert(response);
			if(response)
			{
				var result = getTextBetweenTags(response, 'STATUSMESSAGE');
				var error = getTextBetweenTags(response, 'ERRORMESSAGE');
			 	var updated = getTextBetweenTags(response, 'UPDATED');
			 	
			 	if (updated != "1")
			 	{
//				 	statusmessageelement.innerHTML = result;
					if (updated != "2")
						statusmessageelement.innerHTML = "Error saving Email...";
					else
						statusmessageelement.innerHTML = error;
				}
				else
				{
					var previous = getTextBetweenTags(response, 'PREVIOUS');
					retval = [updated,previous];
				}
			}
			return retval;
		}
		
		function build_link(formobj, pagetocall, statusmessageelement)
		{
			var submitstring = build_submit_string(formobj);
			var temp_link = '';

			submitstring = submitstring.replace("%u2010","-");
			submitstring = submitstring.replace("%u2011","-");
			submitstring = submitstring.replace("%u2012","-");
			submitstring = submitstring.replace("%u2013","-");
			submitstring = submitstring.replace("%u2014","-");
			submitstring = submitstring.replace("%u2015","-");
			submitstring = submitstring.replace("%u2016","|");
			submitstring = submitstring.replace("%u2017","_");
			submitstring = submitstring.replace("%u2018","'");
			submitstring = submitstring.replace("%u2019","'");
			submitstring = submitstring.replace("%u201A","'");
			submitstring = submitstring.replace("%u201B","'");
			submitstring = submitstring.replace("%u201C",'"');
			submitstring = submitstring.replace("%u201D",'"');
			submitstring = submitstring.replace("%u201E",'"');
			submitstring = submitstring.replace("%u201F",'"');
			submitstring = submitstring.replace("%u2026","...");

		//	PROCESS USING POST
			temp_link += 'refreshop=APPLY&'+submitstring;
			var updated = apply_changes(pagetocall, temp_link, statusmessageelement);
			
			if (updated[0] == "1")
			{	
				statusmessageelement.innerHTML = "Message Sent!";
				alert('Message Sent!');
			 	document.getElementById('captcha').src = '/securimage/securimage_show.php?' + Math.random();
			 	document.location.replace(updated[1]);
			 	//alert(pagetocall);
			 	//echo "<PREVIOUS>".$_SESSION['prev_page']."</PREVIOUS>";
				//document.location.replace(pagetocall);		
			}
		}

		var submitstring = false;
		var applyelement = false;
		
		function toggleSelectBox(show, posx, posy, html)
		{
		 	//alert('toggling select box from '+arguments.caller);
			var el = null;
			if(document.all)
				el = document.all['selectbox'];
			else if(document.getElementById)
				el = document.getElementById('selectbox');
		
			if(show)
			{
				el.innerHTML = html;
				el.style.left = posx;
				el.style.top = posy;
				el.style.display = 'inline';
			}
			else
			{
				el.style.display = 'none';
			}
		}
		
		function buildpage(attachmentkey)
		{
			var updated = false, response = false, postdata = null;
		 	var url = "/uploadfile.php?ATTACHMENTKEY="+attachmentkey;
//		 	statusmessageelement.innerHTML = url + postdata;
//		 	return updated;
		 	
			doRequest(xmlhttp, "POST", url, false, null, postdata);
			if (xmlhttp.status == 200)
			{
	         	response = xmlhttp.responseText;
			}
	/*		if(response)
			{
				var result = getTextBetweenTags(response, 'STATUSMESSAGE');
				var error = getTextBetweenTags(response, 'ERRORMESSAGE');
			 	updated = getTextBetweenTags(response, 'UPDATED');

			 	if (updated != "1")
			 	{
				 	statusmessageelement.innerHTML = result;
				}
			}*/
			return response;		 	
		}

		function removefile(attachmentkey, filename)
		{
			var updated = false, response = false, postdata = null;
		 	var url = "/uploadfile.php?ATTACHMENTKEY="+attachmentkey+"&RemoveFile="+filename;
		 	
//		 	statusmessageelement.innerHTML = url + postdata;
//		 	return updated;
		 	
			doRequest(xmlhttp, "POST", url, false, null, postdata);
			if (xmlhttp.status == 200)
			{
	         	response = xmlhttp.responseText;
			}		 	
		}
		
		function combine_filenames(formobj)
		{
		 	var string = "";

		 	for(var j=0;j<formobj.options.length;j++)
			{
				if(formobj.options[j].value == null || formobj.options[j].value == "undefined" || formobj.options[j].value == "")
					string += formobj.options[j].text + ";";
				else
					string += formobj.options[j].value + ";";
			}
			return string;
		}
		
		function check_extension(obj)
		{
		 	var filename = document.getElementById("filename");
			var ext=filename.value.slice(-4).toUpperCase();
			if ((ext != ".PDF") && (ext != ".RTF") && (ext != ".XLS") && (ext != "XLSX") && (ext != ".DOC") && (ext != "DOCX"))
			{
			 	alert("Invalid document type:\nAllowed filetypes are .pdf, .rtf, excel and word documents ONLY");
				obj.reset();
			}
		}

		function check_filename(obj)
		{
		 	var filename = document.getElementById("filename");

		 	if (filename.value != "")
		 	{
		 	 	var lastperiod = filename.value.lastIndexOf(".");
		 	 	if (lastperiod != -1)
		 	 	{
					var lastindex = filename.value.lastIndexOf("\\");
					
					var rootname = '';
					if(lastindex > -1 && lastindex < lastperiod)
						rootname=filename.value.slice(lastindex+1,lastperiod);
					else
						rootname=filename.value.slice(0,lastperiod);

					rootname = rootname.replace("C:\\fakepath\\", "");  //IE-8 implementation to HTML5 specifications
					var result=rootname.search(/[^A-Z^a-z^_^0-9^ ]/);

					if (result != -1)
					{
					 	alert("Invalid file name:\nPlease use a file with only alphanumeric characters in the filename");
						obj.reset();
					}
				}
				else
				{
				 	alert("Invalid file name:\nThere is no extension in the filename");
					obj.reset();
				}				
			}
		}
