function ClearIf(control, value)
{
    if (control.value == value || control.value == 'Required')
    {
        control.value = "";
        control.style.color = "Black";
    }
}

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 previewPage(page_id)
{
    window.open('/preview.aspx?page=' + page_id + '&type=preview', 'page_preview', 'toolbar=no, menubar=no, location=yes, resizable=yes, scrollbars=yes');
}
*/

function moveCursorToEnd(element) {
    element.value = element.value;
}

function hoverBox(box_id)
{
    for (index = 1; index < 10; index++)
    {
        switch (index)
        {
            case 1:
            case 3:
            case 7:
            case 9:
                if (document.getElementById('box' + box_id + 'shade' + index) != null)
                { document.getElementById('box' + box_id + 'shade' + index).src = '/images/borders/border-' + index + '-line.jpg'; }
                break;
            case 2:
            case 4:
            case 6:
            case 8:                
                var imageLoc = '/images/borders/border-' + index + '-line.jpg';
                if (document.getElementById('box' + box_id + 'shade' + index) != null)
                { document.getElementById('box' + box_id + 'shade' + index).style.backgroundImage = "url('" + imageLoc + "')"; }
                break;
            default:
                break;
        }
    }
}

function exitBox(box_id)
{
    for (index = 1; index < 10; index++)
    {
        switch (index)
        {
            case 1:
            case 3:
            case 7:
            case 9:
                if (document.getElementById('box' + box_id + 'shade' + index) != null)
                { document.getElementById('box' + box_id + 'shade' + index).src = '/images/borders/shade-' + index + '.jpg'; }
                break;
            case 2:
            case 4:
            case 6:
            case 8:
                var imageLoc = '/images/borders/shade-' + index + '.jpg';
                if (document.getElementById('box' + box_id + 'shade' + index) != null)
                { document.getElementById('box' + box_id + 'shade' + index).style.backgroundImage = "url('" + imageLoc + "')"; }
                break;
            default:
                break;
        }
    }
}

function ToggleTabs(tab_id)
{
    for(index = 1; index < 11; index++)
    {
        var tab = document.getElementById('Tab' + index);
        var value = document.getElementById('Tab' + index + '_Value');
        
        if (tab == null || value == null) break;
        
        if (tab.id == tab_id)
        {
            value.style.display = '';
            tab.style.backgroundColor = '#f5e1d1';
        }
        else
        {
            value.style.display = 'none';
            tab.style.backgroundColor = '#efefef';
        }
    }
}

function ToggleView(element_id)
{
    var e = document.getElementById(element_id);
    if (e != null)
    {
        if (e.style.display == '')
            e.style.display = 'none';
        else
            e.style.display = '';
    }
}

function SwapView(element_id, id2)
{
    var x = document.getElementById(element_id);
    var y = document.getElementById(id2);
    
    if (x.style.display == '')
    {
        x.style.display = 'none';
        y.style.display = '';
    }
    else
    {
        x.style.display = '';
        y.style.display = 'none';
    }
}

function ToggleButton(button, element_id)
{
    var e = document.getElementById(element_id);
    if (e.style.display == '')
    {
        button.src = '/images/expand-button.gif';
        e.style.display = 'none';
    }
    else
    {
        button.src = '/images/contract-button.gif';
        e.style.display = '';
    }
}

function ToggleButton2(button, element_id)
{
    var e = document.getElementById(element_id);
    if (e.style.display == '')
    {
        button.src = '/images/arrow-hidden.gif';
        e.style.display = 'none';
    }
    else
    {
        button.src = '/images/arrow-view.gif';
        e.style.display = '';
    }
}

function ImageHover(e, hover)
{
    var src = e.src;
    if (hover)
        src = src.replace(".png", "-hover.png");
    else
        src = src.replace("-hover.png", ".png");
        
    e.src = src;
}

function AddItem(control_id, values_id, item_value, item_text)
{
    var control_values = document.getElementById(values_id).value;
    var item_list = document.getElementById(control_id).innerHTML;
    item_list = item_list.replace(/&amp;/g,'&');
    
    var locations = document.getElementById('locations');
    
    if (item_list.match(item_text))
    {
        item_list = item_list.replace('<A href="javascript:RemoveItem(\'' + control_id + '\',\'' + values_id + '\',' + item_value + ',\'' + item_text + '\')">' + item_text + '</A>, ','');
        control_values = control_values.replace(','+item_value+',',',');
        if (locations != null && control_id != 'tags') locations.value = locations.value.replace(item_text + '/','');
    }
    else
    {
        item_list += '<a href="javascript:RemoveItem(\'' + control_id + '\',\'' + values_id + '\',' + item_value + ',\'' + item_text + '\')">' + item_text + '</a>, ';
        control_values += item_value+',';
        if (locations != null && control_id != 'tags') locations.value += item_text + '/';
    }

    document.getElementById(control_id).innerHTML = item_list;
    document.getElementById(values_id).value = control_values;
}

function RemoveItem(control_id, values_id, item_value, item_text)
{
    var control_values = document.getElementById(values_id).value;
    var item_list = document.getElementById(control_id).innerHTML;
    item_list = item_list.replace(/&amp;/g,'&');
    item_list = item_list.replace('<A href="javascript:RemoveItem(\'' + control_id + '\',\'' + values_id + '\',' + item_value + ',\'' + item_text + '\')">' + item_text + '</A>, ','');
    document.getElementById(control_id).innerHTML = item_list;
    control_values = control_values.replace(','+item_value+',',',');
    document.getElementById(values_id).value = control_values;
}

function SetUpForgotEmailMessage()
{
    document.getElementById('Subject').value = 'What is my password?';
    document.getElementById('Message').value = '***DO NOT CHANGE THE SUBJECT OF THIS MESSAGE***\n\nTo retrieve your password, please complete the following two steps\n\n1) Enter your email address below.\n2) Press "Submit"';
    document.getElementById('Your Email').focus();
}

function findPos(obj) {
	var curleft = curtop = 0;

	if (obj.offsetParent) {
	    do {
	        curleft += obj.offsetLeft;
	        curtop += obj.offsetTop;
	    } while (obj = obj.offsetParent);
	}

    return [curleft,curtop];
}

function GetLeftPos(obj) {
	var curleft = 0;

    if (obj.offsetParent) {
        do {
			    curleft += obj.offsetLeft;
        } while (obj = obj.offsetParent);
    }

    return curleft;
}


function ScreenSizes()
{
  var sw = window.screen.width;
  var sh = window.screen.height;
  var cw = document.body.offsetWidth;
  var ch = document.body.offsetHeight;
}

var mhResizeTimeout

function ClientScreenResize()
{
    if (mhResizeTimeout) window.clearTimeout(mhResizeTimeout);
    mhResizeTimeout = 0;
    mhResizeTimeout = window.setTimeout(ResizeScreen, 500);
}

function ResizeScreen()
{
    mhResizeTimeout = 0
    // your code
    document.getElementById('screenWidth').value=window.screen.width;
    document.getElementById('screenHeight').value=window.screen.height;
    document.getElementById('clientWidth').value=document.body.offsetWidth;
    document.getElementById('clientHeight').value=document.body.offsetHeight;
    document.getElementById('screenWidthSetup').submit();
}

function validateEmailAddress(e)
{
    if (e.value == '') return true;
    
    var email = e.value;
    
	var http="@";
	var dot=".";
	var lat=email.indexOf(http);
	var lstr=email.length;
	var ldot=email.indexOf(dot);
	
	if (email.indexOf(http)==-1) return false;
	
	if (email.indexOf(http)==0 || email.indexOf(http)==lstr) return false;
	
	if (email.indexOf(dot)==-1 || email.indexOf(dot)==0 || email.indexOf(dot)==lstr) return false;
	
	if (email.indexOf(http,(lat+1))!=-1) return false;

	if (email.substring(lat-1,lat)==dot || email.substring(lat+1,lat+2)==dot) return false;

	if (email.indexOf(dot,(lat+2))==-1) return false;
	
	if (email.indexOf(" ")!=-1) return false;

    e.style.borderColor = '#cccccc';
    
	return true;
}

function validateURL(e)
{
    if (e.value == '') return true;
    
    var url = e.value;
    
	var http="http";
	var dot=".";
	var httpIndex=url.indexOf(http);
	var dotIndex=url.indexOf(dot);
	
	if (httpIndex < 1 || httpIndex == url.length) return false;
	
	if (dotIndex < 8 || dotIndex == url.length) return false;
	
	e.style.borderColor = '#cccccc';
    
	return true;
}

function AddDays(date, days)
{
    var day = 86400000;
    var diff = days * day;
    var newDate = new Date(date.valueOf() + diff);
    return (newDate.getMonth() + 1) + '/' + newDate.getDate() + '/' + newDate.getFullYear();
}

function ShowPageDescription(page) {
    document.getElementById('page-description-panel').style.display = '';
    
    var pdesc = document.getElementById('page-description');
    switch (page) {
        case "activism":
            pdesc.innerHTML = '<h3>Online Activism</h3>This page lists the latest happenings on the network. Want to know what\'s going on, check out this page often.';
            break;
        case "calendar":
            pdesc.innerHTML = '<h3>Calendar of Events</h3>This page contains a visual calendar that includes event information, holidays, and birthdays.';
            break;
        case "campaigns":
            pdesc.innerHTML = '<h3>Republican Campaigns</h3>This page lists partisan, non-partisan, and club races throughout the country.';
            break;
        case "coalitions":
            pdesc.innerHTML = '<h3>Center-Right Coalitions</h3>This page lists recognized, national coalitions that promote Conservative values.';
            break;
        case "donate":
            pdesc.innerHTML = '<h3>Make a Donation</h3>Thank you for your continued support of the network.';
            break;
        case "friends":
            pdesc.innerHTML = '<h3>Friends &amp; Contacts</h3>Connect with friends but more importantly, build your Republican network.';
            break;
        case "map":
            pdesc.innerHTML = '<h3>"R" Country Map</h3>This page contains an interactive map of the United States as well as a map of counties in each state. Further information includes coalitions chairmen and state contact information.';
            break;
        case "search":
            pdesc.innerHTML = '<h3>Network Search Page</h3>This page contains a comprehensive search form that allows people to search based a number of given criteria.';
            break;
        case "techsupport":
            pdesc.innerHTML = '<h3>Technical Support</h3>Found a bug, problem, or general issue with the site?  Let us know and we\'ll take care of it.';
            break;
        case "websites":
            pdesc.innerHTML = '<h3>Websites &amp; Social Networks</h3>This page contains a list of websites on the network; also called Groups on other networks but our websites offer more features and help promote Republicans throughout the country.';
            break;
        default:
            document.getElementById('page-description-panel').style.display = 'none';
            break;
    }
}