// Author: Al Sierra
// Version: 6.30.2008
// Description: AJAX for contact screen
var contact = {
	_dataURL: 'scripts/php/contact.php',
	_docRef: false,
	_divShutter: false,
	_divWindow: false,
	_loc:false,
	_id: false,
	_shutter_h:false,
	_shutter_w:false,
	
	Init: function(inDocumentRef,loc)
	{
		//alert(loc);
		
		this._docRef = inDocumentRef;
		this._dataURL = loc+this._dataURL;
		this._loc = loc;
		// find our shutter DIV
		this._divShutter = $('shutter_con');
		if(!this._divShutter) {
			// shutter not found, create
			this._divShutter = this._docRef.createElement('div');
			this._divShutter.id = 'shutter_con';
			this._divShutter.style.display = 'none';
			this._divShutter.style.background = '#000000';
			this._divShutter.style.top = '0px';
			this._divShutter.style.left = '0px';
			this._divShutter.style.position = 'absolute';
			contact._getPageDimensions();
			this._divShutter.style.height = contact._shutter_h+"px";
			this._divShutter.style.width = contact._shutter_w+"px";
			//this._divShutter.style.height = '100%';
			//this._divShutter.style.width = '100%';
			this._docRef.body.appendChild(this._divShutter);
		}
		// find our window DIV
		this._divWindow = $('window_con');
		if(!this._divWindow) {
			// popup DIV not found, create
			this._divWindow = this._docRef.createElement('div');
			this._divWindow.id = 'window_con';
			this._divWindow.style.position = 'fixed';
			this._divWindow.style.top = '0px';
			this._divWindow.style.left = '0px';
			this._divWindow.style.display = 'none';
			this._docRef.body.appendChild(this._divWindow);
			// load the dispay HTML if not already loaded
			new Ajax.Updater('window_con', loc+'scripts/php/get_screen.php?s=contact', {evalScripts: true});
		}
	},
	DisplayContact: function() 
	{	
	 	$('topic').value="";
		$('name').value="";
		$('emails').value="";
		$('company').value="";
		$('telephone').value="";
		$('message').value="";
		//$('contact_table_thanks').hide();
		this._divWindow.show();
		new Effect.Appear(contact._divWindow,{duration: .1, to:1.0 });
		this._divWindow.style.zIndex = 9500;		
		this._divShutter.style.zIndex = 8000;
		new Effect.Appear(contact._divShutter,{duration: 0, from: 0.0, to:0.45 });
		
	},
	DisplayForm: function() 
	{	
	 	$('topic').value="";
		$('name').value="";
		$('emails').value="";
		$('company').value="";
		$('telephone').value="";
		$('message').value="";
		$('contact_table_thanks').style.display = 'none';
		$('contact_email_form').style.display = 'block';
	},
	DisplayQuestion: function() 
	{	
		$('search_working_cs').style.display = 'none';
		$('faqs_detail').style.display = 'none';
		$('faqs_list').style.display = 'block';
	},
	GetAnswer: function(id) 
	{	
	 	//alert($('faqs_list').style.display);
		$('faqs_list').style.display = 'none';
		$('search_working_cs').style.display = 'block';
		
		//alert($('faqs_list').style.display);
		var msg_data = {};
		msg_data.faq_id = id;	 	
		// fire an AJAX update
		var ajaxtags = new Ajax.Request(this._loc+'scripts/php/faq.php?cmd=showJ',{
			parameters: $H(msg_data).toQueryString(),
			asynchronous: false
		});
		if (ajaxtags.transport.statusText != "OK") {
			alert("A problem was encountered when trying to contact the server.\nPlease try again.");
			return false;
		}
		if (ajaxtags.transport.responseText.length > 0 ) {
			//alert(ajaxtags.transport.responseText);
			try {
				var JSON = eval("("+ajaxtags.transport.responseText+")");
				if (undefined!==JSON.ehandler) {
					alert('The following error has occured:\n'+JSON.emsg);
					return false;
				}
			} catch(e) {
				alert("The server's response could not be understood.\nPlease try again.");
				return false;
			}
 		}
		$('cq').innerHTML=JSON.question;
		$('ca').innerHTML=JSON.answer;
		$('search_working_cs').style.display = 'none';
		$('faqs_detail').style.display = 'block';
	},
	FAQSearch: function(id) 
	{
	 	contact.GetAnswer(id);
	 	contact.DisplayContact();
	},
	Submit: function() 
	{
		// make sure user selected a folder
		var emsg='';
		if (!this._ValidateString($('topic').value)) {
			emsg+="Please enter a topic. \n";
		}
		if (!this._ValidateString($('name').value)) {
			emsg+="Please enter your name. \n";
		}
		if (!this._ValidateString($('emails').value)) {
			emsg+="Please enter your email. \n";
		}
		if (!this._ValidateString($('message').value)) {
			emsg+="Please enter a message. \n";
		}
		if (emsg!='') {
			alert(emsg);
			return false;
		}
		var msg_data = {};
		msg_data.topic = $('topic').value;
		msg_data.name = $('name').value;
		msg_data.emails = $('emails').value;
		msg_data.company = $('company').value;
		msg_data.telephone = $('telephone').value;
		msg_data.message = $('message').value;
		
		// fire an AJAX update
		var ajaxtags = new Ajax.Request(this._dataURL+'?',{
			parameters: $H(msg_data).toQueryString(),
			asynchronous: false
		});
		if (ajaxtags.transport.statusText != "OK") {
			alert("A problem was encountered when trying to contact the server.\nPlease try again.");
			return false;
		}
		if (ajaxtags.transport.responseText.length > 0 ) {
			try {
				var JSON = eval("("+ajaxtags.transport.responseText+")");
				if (undefined!==JSON.ehandler) {
					alert('The following error has occured:\n'+JSON.emsg);
					return false;
				}
			} catch(e) {
				alert("The server's response could not be understood.\nPlease try again.");
				return false;
			}
 		}
	 	$('topic').value="";
		$('name').value="";
		$('emails').value="";
		$('company').value="";
		$('telephone').value="";
		$('message').value="";
		$('contact_email_form').style.display = 'none';
		$('contact_table_thanks').style.display = 'block';
	},
	
	Submit_btn: function(state) 
	{
		
		if (state=="over") {
			//alert(document.getElementById('go_login').src);
			document.getElementById('go_login').src = "/images/buttons/submit_over.gif";
		} else {
			document.getElementById('go_login').src = "/images/buttons/submit.gif";
		}
	},
	
	Cancel: function() 
	{
		new Effect.Appear(contact._divWindow,{duration: .5, to:0.0 });
		new Effect.Appear(contact._divShutter,{duration: 0, to:0.0, afterFinish: contact._CancelHide })
	},
	_CancelHide: function() 
	{
		contact._divWindow.hide();
		contact._divShutter.hide();
	},
	
	_getPageDimensions : function() 
	{
		var xScroll, yScroll;
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = document.body.scrollWidth;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ 
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { 
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}

		var windowWidth, windowHeight;
		if (self.innerHeight) {	
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { 
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { 
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	

		if(yScroll < windowHeight){
			contact._shutter_h = windowHeight;
		} else { 
			contact._shutter_h = yScroll;
		}

		if(xScroll < windowWidth){	
			contact._shutter_w = windowWidth;
		} else {
			contact._shutter_w = xScroll;
		}
	},
	
	_ValidateString: function (s){
        if(s==undefined || s==null || s.length==0) {
            return false;
        }
        
        // Check for white space
        reWhiteSpace = new RegExp(/^\s+$/);
        if (reWhiteSpace.test(s)) {
            return false;
        }
     
        //Check for html tags
        reHtmlTags = new RegExp(/<(.|\n)/);
        if (reHtmlTags.test(s)) {
            return false;
        }
        return true;
    }
}