var newO=document.createElement('input');

var _onclick = "";

function initInputs()
{
	var inputs = document.getElementsByTagName("input");
	for (var i=0; i<inputs.length; i++)
	{
		if (inputs[i].type == "text" && (inputs[i].name == "login" || inputs[i].name == "pass"))
		{
			var form = getAncestor(inputs[i], "form");
			if (form)
				form.onsubmit = handleSubmit;
			inputs[i].onfocus = function ()
			{
					if ((this.value == "Username:") || (this.value == "Password:"))
						this.value = "";
						
					
					if(this.name=='pass') {
											
						newO.setAttribute('type','password');
						newO.setAttribute('name',this.getAttribute('name'));
						newO.setAttribute('class',"contactfield");
						this.parentNode.replaceChild(newO,this);
						newO.focus();
						setTimeout("newO.focus();", 10);
					
					}						
						
			}
			inputs[i].onblur = function ()
			{
					if (this.value == "" && this.name == "login") this.value = "Username:";
					if (this.value == "" && this.name == "pass") this.value = "Password:";
			}
		}
	}
}

function handleSubmit()
{
	var re = new RegExp('^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$');
	var element = this.elements["email"];
	if (element)
	{
		if (element.value == "")
		{
			alert("Email address is empty!");
			return false;
		}
		else
			return element.value.match(re)
				|| (alert("You have entered incorrect email address!"), false);
	}
	return true;
}

function getAncestor(element, tagName)
{
	var node = element;
	while (node.parentNode && (!node.tagName ||
			(node.tagName.toUpperCase() != tagName.toUpperCase())))
		node = node.parentNode;
	return node;
}

if (window.addEventListener)
{
	window.addEventListener("load", initInputs, false);
	window.onload = initInputs();
}
else if (window.attachEvent){
	window.attachEvent("onload", initInputs);
}
else if (document.getElementById) {
window.onload=initInputs;
}

function sendContactEmail()
{
	
//	onclick = document.getElementById("submit").onclick;
	document.getElementById("submit").onclick= function() {};
// validate existence of fields
	
	var saveFields = ['fname','lname','phone','email','subject','message'];
	
	var isValid = true;
	
	//inital loop to clean up last run

	saveFields.each(function(field) {
	
		if($(field+'_lb')!=null) {
	
			$(field+'_lb').removeClassName('error_orgtxt');
			
		}
		
	});
	
	// hide boxes from last run
	
	var errBoxen = ['email','phone','fname','lname','message'];
	
	errBoxen.each(function(box) {
	
		$(box+'_ebox').hide();
		$(box+'_ebox').innerHTML='';
		
	});	
	
	// check email validity
	
	if (!GFGcheckMail($('email_fld').value)) {
		document.getElementById("submit").onclick = function onclick(event) { sendContactEmail() };
		new Insertion.Bottom('email_ebox','Please enter a valid email address.');
		new Effect.Appear('email_ebox'), {duration: 0.12};
		$('email_lb').addClassName('error_orgtxt');
		isValid = false;
		
	}
	if($('fname_fld').value == '')
	{
		document.getElementById("submit").onclick = function onclick(event) { sendContactEmail() };
		new Insertion.Bottom('fname_ebox','Please enter a first name.');
		new Effect.Appear('fname_ebox'), {duration: 0.12};
		$('fname_lb').addClassName('error_orgtxt');
		isValid = false;
	}
	if($('lname_fld').value == '')
	{
		document.getElementById("submit").onclick = function onclick(event) { sendContactEmail() };
		new Insertion.Bottom('lname_ebox','Please enter a last name.');
		new Effect.Appear('lname_ebox'), {duration: 0.12};
		$('lname_lb').addClassName('error_orgtxt');
		isValid = false;
	}
	if($('phone_fld').value == '')
	{
		document.getElementById("submit").onclick = function onclick(event) { sendContactEmail() };
		new Insertion.Bottom('phone_ebox','Please enter a phone number.');
		new Effect.Appear('phone_ebox'), {duration: 0.12};
		$('phone_lb').addClassName('error_orgtxt');
		isValid = false;
	}
	if($('message_fld').value == '')
	{
		document.getElementById("submit").onclick = function onclick(event) { sendContactEmail() };
		new Insertion.Bottom('message_ebox','Please enter a message.');
		new Effect.Appear('message_ebox'), {duration: 0.12};
		$('message_lb').addClassName('error_orgtxt');
		isValid = false;
	}
	
	// password match
	
	// make sure how'd ya hear isn't left blank
	
	/*if($('howhear_fld').selectedIndex == 0) {
	
		new Insertion.Bottom('howhear_ebox','Please select an option from the list.');
		new Effect.Appear('howhear_ebox'), {duration: 0.12};
		$('howhear_lb').addClassName('error_orgtxt');
		isValid = false;
	
	}*/
	
	if(isValid) {
	
		// serialize and send, listen for image validation issues
		
		form_vals = Form.serialize('contact_form');
		
		// post
	
		function finishSave(originalRequest) {
	
			var reqStatus = originalRequest.responseText.evalJSON();
					
			if(reqStatus.status == 'ok') {
			
				new Draggable('save_tip');
				$('save_tip').show();
			//	return(onclick);
				
			} else {
				document.getElementById("submit").onclick = function onclick(event) { sendContactEmail() };
			}	
	
		}
	
		var ajaxsave = new Ajax.Request(base_url + 'ajax/send_contact_email/',{
	
			parameters: form_vals,
			method: 'post',
			onComplete: finishSave 
		});
			
	} 
	
}

function sendSupportEmail()
{
	
// validate existence of fields

//	onclick = document.getElementById("submit").onclick;
	document.getElementById("submit").onclick= function() {};
	
	var saveFields = ['name','email','contact_num','details'];
	
	var isValid = true;
	
	if($('name').value == '')
	{
		document.getElementById("submit").onclick = function onclick(event) { sendSupportEmail() };
	//	new Insertion.Bottom('fname_ebox','Please enter a first name.');
	//	new Effect.Appear('fname_ebox'), {duration: 0.12};
	//	$('name_lb').addClassName('error_orgtxt');
		isValid = false;
	}
	if($('email').value == '')
	{
		document.getElementById("submit").onclick = function onclick(event) { sendSupportEmail() };
	//	new Insertion.Bottom('fname_ebox','Please enter a first name.');
	//	new Effect.Appear('fname_ebox'), {duration: 0.12};
	//	$('name_lb').addClassName('error_orgtxt');
		isValid = false;
	}
	if($('details').value == '')
	{
		document.getElementById("submit").onclick = function onclick(event) { sendSupportEmail() };
	//	new Insertion.Bottom('fname_ebox','Please enter a first name.');
	//	new Effect.Appear('fname_ebox'), {duration: 0.12};
	//	$('name_lb').addClassName('error_orgtxt');
		isValid = false;
	}
	
	
	if(isValid) {
	
		// serialize and send, listen for image validation issues
		
		var params = Object.toQueryString({name: $F('name'), email: $F('email'), contact_num: $F('contact_num'), details: $F('details')});
				
		// post
	
		function finishSave(originalRequest) {
	
			var reqStatus = originalRequest.responseText.evalJSON();
					
			if(reqStatus.status == 'ok') {
			
					new Draggable('save_tip');
					$('save_tip').show();
					//return(onclick);

			} else {
				document.getElementById("submit").onclick = function onclick(event) { sendSupportEmail() };
			}	
	
		}
	
		var ajaxsave = new Ajax.Request(base_url + 'process_ticket/',{
	
			parameters: params,
			method: 'post',
			onComplete: finishSave 
		});
			
	} 
	
}

function sendEmail()
{
	
//	onclick = document.getElementById("submit").onclick;
	document.getElementById("submit").onclick= function() {};
// validate existence of fields
	
	var saveFields = ['name','phone','email','desc','subject'];
	
	var isValid = true;
	
	//inital loop to clean up last run

	saveFields.each(function(field) {
	
		if($(field+'_lb')!=null) {
	
			$(field+'_lb').removeClassName('error_orgtxt');
			
		}
		
	});
	
	// check email validity
	
	if (!GFGcheckMail($('email_fld').value)) {
		document.getElementById("submit").onclick = function onclick(event) { sendEmail() };
	//	new Insertion.Bottom('email_ebox','Please enter a valid email address.');
	//	new Effect.Appear('email_ebox'), {duration: 0.12};
		$('email_lb').addClassName('error_orgtxt');
		isValid = false;
		
	}
	if($('name_fld').value == '')
	{
		document.getElementById("submit").onclick = function onclick(event) { sendEmail() };
	//	new Insertion.Bottom('fname_ebox','Please enter a first name.');
	//	new Effect.Appear('fname_ebox'), {duration: 0.12};
		$('name_lb').addClassName('error_orgtxt');
		isValid = false;
	}
	
	if(isValid) {
	
		// serialize and send, listen for image validation issues
		
		form_vals = Form.serialize('contact_form');
		
		// post
	
		function finishSave(originalRequest) {
	
			var reqStatus = originalRequest.responseText.evalJSON();
					
			if(reqStatus.status == 'ok') {
			
				new Draggable('save_tip');
				$('save_tip').show();
				
			} else {
				document.getElementById("submit").onclick = function onclick(event) { sendEmail() };
			}	
	
		}
	
		var ajaxsave = new Ajax.Request(base_url + 'ajax/send_email/',{
	
			parameters: form_vals,
			method: 'post',
			onComplete: finishSave 
		});
			
	} 
	
}

function hideEmailDiv(div)
{
	$('name_fld').value = "";
	$('email_fld').value = "";
	$('phone_fld').value = "";
	$('subject_fld').value = "";
	$('desc_fld').value = "";
	$(div).hide();
	document.getElementById("submit").onclick = function onclick(event) { sendEmail() };
}

function hideContactDiv(div)
{
	$('fname_fld').value = "";
	$('lname_fld').value = "";
	$('email_fld').value = "";
	$('phone_fld').value = "";
	$('subject_fld').value = "";
	$('message_fld').value = "";
	$(div).hide();
	document.getElementById("submit").onclick = function onclick(event) { sendContactEmail() };
}

function hideSupportDiv(div)
{
	$('name').value = '';
	$('email').value = '';
	$('contact_num').value = '';
	$('details').value = '';
	$(div).hide();
	document.getElementById("submit").onclick = function onclick(event) { sendSupportEmail() };
}

function hideDiv(div)
{
	$(div).hide();
	$(div).update('');
}

function playVideo(vid)
{	
	$('overlay').update('<div style="z-index:1;overflow-x:hidden;margin-top:40px;margin-left:auto;margin-right:auto;height:670px;width:850px;border: 1px solid #4C4C4C;background-color:#252424;"><div style="margin-right:35px;margin-left:auto;margin-top:10px;width:20px;"><a href="javascript:hideDiv('+"'overlay'"+')" style="text-decoration:none;color:white;">Close</a></div><div id="flashcontent" style="margin-left:25px;margin-top:10px;"><OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,115,0" WIDTH="800" HEIGHT="626" id="videoPlayer"><PARAM NAME=quality VALUE=high><PARAM NAME=bgcolor VALUE=#FFFFFF><embed width="800" height="626" flashvars="xmlPath=http://www.giantfin.com/giantfin/res/video/'+vid+'" quality="high" bgcolor="#111111" name="videoPlayer" id="videoPlayer" style="" src="http://www.giantfin.com/giantfin/res/swf/tutorial_player.swf" type="application/x-shockwave-flash"/></embed></object></div></div>');
	$('overlay').show();
}
