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><br>';
				applyelement.style.display = 'none';
				return false;
			}
		 	if (form1.LASTNAME.value == '')
		 	{
				statusmessageelement.innerHTML = 'Enter your last name<br><br>';
				applyelement.style.display = 'none';
				return false;
			}
		 	if (form1.ADDRESS1.value == '')
		 	{
				statusmessageelement.innerHTML = 'Enter your address<br><br>';
				applyelement.style.display = 'none';
				return false;
			}
		 	if (form1.CITY.value == '')
		 	{
				statusmessageelement.innerHTML = 'Enter your city<br><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><br>';
				applyelement.style.display = 'none';
				return false;
			}
		 	if (form1.ZIP.value == '')
		 	{
				statusmessageelement.innerHTML = 'Enter your zip-code<br><br>';
				applyelement.style.display = 'none';
				return false;
			}
		 	if ((form1.EMAIL.value == '') || (!checkEmail(form1.EMAIL.value)))
		 	{
				statusmessageelement.innerHTML = 'Enter your valid email address<br><br>';
				applyelement.style.display = 'none';
				return false;
			}
		 	if (form1.MESSAGE.value == '')
		 	{
				statusmessageelement.innerHTML = 'Enter your message<br><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><br>';
				applyelement.style.display = 'none';
				return false;
			}
			
			statusmessageelement.innerHTML = 'Click Submit to send the message<br><br>';
			applyelement.style.display = '';
			return true;
		}
		
		function apply_changes(url, postdata, statusmessageelement)
		{
		 	var updated = false, response = false;
		 	
//		 	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;
					if (updated != "2")
						statusmessageelement.innerHTML = "Error saving Email...";
					else
						statusmessageelement.innerHTML = error;
				}
			}
			return updated;
		}
		
		function build_link(formobj, pagetocall, statusmessageelement)
		{
			var submitstring = build_submit_string(formobj);
			var temp_link = '';


		//	PROCESS USING POST
			temp_link += 'refreshop=APPLY&'+submitstring;
			var updated = apply_changes(pagetocall, temp_link, statusmessageelement);

			if (updated == "1")
			{	
			 	var contactmessageelement=getElement('contactmessage');
			 	contactmessageelement.innerHTML = 'Message Sent';
			 	document.getElementById('captcha').src = '/securimage/securimage_show.php?' + Math.random();
			 	hideLightbox();		
			}
		}

		var submitstring = false;
		var applyelement = false;
		
		function toggleSelectBox(show, posx, posy, html)
		{
			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 = "/php/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 = "/php/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();
			}
		}