﻿function updateCharacterCount(o,t)
{
    document.getElementById(o).innerHTML = t.value.length;
}

function closeFlagModal(o)
{
    document.forms[0].reset();
    document.getElementById(o).innerHTML = "0";
}

/* BEGIN COMMENT CHARACTER COUNT CODE */

var CommentTextareaControl = null;
var CommentCountFeedbackControl = null;
function initCommentCharacterCounter(t_id, c_id)
{
	try{
		CommentTextareaControl = document.getElementById(t_id);
		CommentCountFeedbackControl = document.getElementById(c_id);
		
		if(document.addEventListener)
		{
			CommentTextareaControl.addEventListener('keyup',function(){CommentCountFeedbackControl.innerHTML = CommentTextareaControl.value.length},false);
		}
		else if(document.attachEvent)
		{
			CommentTextareaControl.attachEvent('onkeyup',function(){CommentCountFeedbackControl.innerHTML = CommentTextareaControl.value.length});
		}
	}
	catch(e){}
}

/* END COMMENT CHARACTER COUNT CODE */


/* BEGIN COMMENT SUBMISSION CODE */
var LCICommentSubmit_TrueSubmitButton = null;
var LCICommentSubmit_TrueSubmitButton_ValidationGroup = null;
function initTrueSubmitButton(id, vg)
{
	LCICommentSubmit_TrueSubmitButton = document.getElementById(id);
	LCICommentSubmit_TrueSubmitButton_ValidationGroup = vg;
}

function CallTrueSubmitButton()
{
	var controlOnClick = LCICommentSubmit_TrueSubmitButton.onclick.toString();
	var controlID = new RegExp(/new\sWebForm_PostBackOptions\("(.*?)",/);
	var m = controlOnClick.match(controlID);
	var ControlIDToCall = m[1];
	
	WebForm_DoPostBackWithOptions(new WebForm_PostBackOptions(ControlIDToCall, "", true, LCICommentSubmit_TrueSubmitButton_ValidationGroup, "", false, true))
}
/* END COMMENT SUBMISSION CODE */


/* BEGIN COMMENT VALIDATION LOGIC */

var LCICommentValidator;
var LCICommentValidator_RegEx_Comment = null;
var LCICommentValidator_RegEx_Email = null;

function initLCICommentValidator(n,e,cm,ca,ch,pr)
{
	try{
	LCICommentValidator = new LCICommentsFormValidation(n,e,cm,ca,ch,pr);
	LCICommentValidator.previewImageOnSrc = LCICommentValidator.previewControl.src;
	LCICommentValidator.previewImageOffSrc = LCICommentValidator.previewControl.src.replace("_on.gif","_off.gif");
	LCICommentValidator.formChange();
}
	catch(e)
	{
		
	}
}


function LCICommentsFormValidation(n,e,cm,ca,ch,pr)
{
	this.objArr = new Array();
	
	this.nameControl = this.addObjectById(n);
	this.emailControl = this.addObjectById(e);
    this.commentControl = this.addObjectById(cm);
    this.captchaControl = this.addObjectById(ca);
    this.checkboxControl = this.addObjectById(ch);
    this.previewControl = this.addObjectById(pr);
	
	this.previewImageOffSrc;
	this.previewImageOnSrc;
	
	this.objArr[this.objArr.length] = this.nameControl;
	this.objArr[this.objArr.length] = this.emailControl;
	this.objArr[this.objArr.length] = this.commentControl;
	this.objArr[this.objArr.length] = this.captchaControl;
	this.objArr[this.objArr.length] = this.checkboxControl;
	this.objArr[this.objArr.length] = this.previewControl;
	
	if(document.addEventListener)
	{
		this.nameControl.addEventListener('keyup',function(){LCICommentValidator.formChange();},false);
		this.emailControl.addEventListener('keyup',function(){LCICommentValidator.formChange();},false);
		this.commentControl.addEventListener('keyup',function(){LCICommentValidator.formChange();},false);
		this.captchaControl.addEventListener('keyup',function(){LCICommentValidator.formChange();},false);
		this.nameControl.addEventListener('blur',function(){LCICommentValidator.formChange();},false);
		this.emailControl.addEventListener('blur',function(){LCICommentValidator.formChange();},false);
		this.commentControl.addEventListener('blur',function(){LCICommentValidator.formChange();},false);
		this.captchaControl.addEventListener('blur',function(){LCICommentValidator.formChange();},false);
		this.checkboxControl.addEventListener('click',function(){LCICommentValidator.formChange();},false);
	}
	else if(document.attachEvent)
	{
		this.nameControl.attachEvent('onkeyup',function(){LCICommentValidator.formChange();});
		this.emailControl.attachEvent('onkeyup',function(){LCICommentValidator.formChange();});
		this.commentControl.attachEvent('onkeyup',function(){LCICommentValidator.formChange();});
		this.captchaControl.attachEvent('onkeyup',function(){LCICommentValidator.formChange();});
		this.nameControl.attachEvent('onblur',function(){LCICommentValidator.formChange();});
		this.emailControl.attachEvent('onblur',function(){LCICommentValidator.formChange();});
		this.commentControl.attachEvent('onblur',function(){LCICommentValidator.formChange();});
		this.captchaControl.attachEvent('onblur',function(){LCICommentValidator.formChange();});
		this.checkboxControl.attachEvent('onclick',function(){LCICommentValidator.formChange();});
	}
	
	this.emailPattern = "\\w+([-+.']\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*";
	this.commentPattern = "[\\n|\\D|\\d]{1,500}";
}

function addObjectById(c_id){return document.getElementById(c_id);}
LCICommentsFormValidation.prototype.addObjectById = addObjectById;

function formChange()
{
	var formValid = this.checkIsValid()
	if(formValid){this.enablePreview();}
	else{this.disablePreview();}
}
LCICommentsFormValidation.prototype.formChange = formChange;


function disablePreview()
{
	LCICommentValidator.previewControl.src = LCICommentValidator.previewImageOffSrc;
	LCICommentValidator.previewControl.disabled = true;
	LCICommentValidator.previewControl.style.cursor = 'default';
}
LCICommentsFormValidation.prototype.disablePreview = disablePreview;

function enablePreview()
{
	LCICommentValidator.previewControl.src = LCICommentValidator.previewImageOnSrc;
	LCICommentValidator.previewControl.disabled = false;
	LCICommentValidator.previewControl.style.cursor = 'pointer';
}
LCICommentsFormValidation.prototype.enablePreview = enablePreview;

LCICommentsFormValidation.prototype.checkIsValid = function()
{
    var isValid = true;
    if(this.nameControl.value.trim().length == 0){isValid = false;}
    if(this.emailControl.value.trim().length == 0){isValid = false;}
	
    var emailRegEx = new RegExp(this.emailPattern);
	if(!this.emailControl.value.match(emailRegEx)){isValid = false;}
	
	if(this.commentControl.value.trim().length == 0){isValid = false;}
	
	var commentRegEx = new RegExp(this.commentPattern);
	if(!this.commentControl.value.match(commentRegEx)){isValid = false;}
	
	if(this.captchaControl.value.trim().length == 0){isValid = false;}
	if(this.checkboxControl.checked == false){isValid = false;}
	
    return isValid;
}
/* END COMMENT VALIDATION LOGIC */

/* BEGIN COMMENT HELPER FUNCTIONS */
function updateCommentsPreviewModal(name, name_src, loc, loc_src, date, comment, comment_src, image, img_src)
{
	document.getElementById(name).innerHTML = document.getElementById(name_src).value;
	document.getElementById(loc).innerHTML = document.getElementById(loc_src).value;
	document.getElementById(date).innerHTML = getCommentDateStamp();
	document.getElementById(comment).innerHTML = document.getElementById(comment_src).value.replace(/\n/gi,"<br />");
	
	try
	{
		document.getElementById(image).src = document.getElementById(img_src).src;
	}
	catch(e)
	{
		document.getElementById(image).style.display = "none";
	}
}


function getCommentDateStamp()
{
	var s = "";
	var d = new Date();
	var monthNames = new Array("Jan","Feb","Mar","Apr","May","June","July","Aug","Sept","Oct","Nov","Dec");
	var h,m,ampm;
	
	var h = (d.getHours() > 12) ? (d.getHours()-12) : ( (d.getHours() == 0) ? '12' : d.getHours() ) ; 
	var m = (d.getMinutes() < 10) ? ( "0" + d.getMinutes() ) : d.getMinutes();
	var ampm = (d.getHours() > 11) ? "PM" : "AM";
	
	s = d.getDate() + " ";
	s += monthNames[d.getMonth()] + ", ";
	s += h + ":";
	s += m + " ";
	s += ampm;
	
	return s.toUpperCase();
}
/* END COMMENT HELPER FUNCTIONS */


/*
	BEGIN UTILITY FUNCTIONS
*/

String.prototype.trim = function()
{
	a = this.replace(/^\s+/, '');
	return a.replace(/\s+$/, '');
};