﻿String.prototype.trim = function() {
	return this.replace(/^\s+|\s+$/g,"");
}

function fcnClear(strClientId, strNameId, strLocationId, strEmailId, strMessageId)
{
    document.getElementById(strClientId + '_' + strNameId).value = '';
    document.getElementById(strClientId + '_' + strLocationId).value = '';
    document.getElementById(strClientId + '_' + strEmailId).value = '';
    document.getElementById(strClientId + '_' + strMessageId).value = '';
}

function fcnValidate(strClientId, strNameId, strLocationId, strEmailId, strMessageId, Post, ctlInvoker)
{
    var strName = document.getElementById(strClientId + '_' + strNameId).value.trim();
    var strLocation = document.getElementById(strClientId + '_' + strLocationId).value.trim();
    var strEmail = document.getElementById(strClientId + '_' + strEmailId).value.trim();
    var strMessage = document.getElementById(strClientId + '_' + strMessageId).value.trim();
    var strErrors = '';
    if(strName.length == 0)
    {
        strErrors+='Please enter your name.\n';
    }
    if(strLocation.length == 0)
    {
        strErrors+='Please enter your hometown.\n';
    }
    if(strEmail.length > 0)
    {
        var strEmails = strEmail.split(',');
        for(var intCount = 0; intCount < strEmails.length; intCount++)
        {
            if(!fcnEmailValidate(strEmails[intCount].trim()))
            {
                strErrors+='Please make sure all emails entered are valid.';
                break;
            }
        }
    }
    if(strMessage.length == 0)
    {
        strErrors+='Please enter a message.\n';
    }
    else if(strMessage.length > 1000)
    {
        strErrors+='Please limit your message to 1000 characters.\n(You are ' + (strMessage.length - 1000) + ' characters over.)';
    }
    
    strName = null;
    strLocation = null;
    strEmail = null
    strMessage = null;
    
    if(strErrors.length > 0)
    {
        window.alert(strErrors);
        strErrors = null;
    }
    else
    {
        Post(ctlInvoker.id, '');
    }
}

function fcnEmailValidate(str) 
{
    var at='@';
    var dot='.';
    var lat=str.indexOf(at);
    var lstr=str.length;
    var ldot=str.indexOf(dot);
    if (str.indexOf(at)==-1){
       return false;
    }
    if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
       return false;
    }
    if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
       return false;
    }
    if (str.indexOf(at,(lat+1))!=-1){
        return false;
     }
    if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
        return false;
     }
    if (str.indexOf(dot,(lat+2))==-1){
        return false;
     }
    if (str.indexOf(' ')!=-1){
        return false;
    }
    return true;
}