﻿// Add the helpful trim method to string
String.prototype.trim = function() { return this.replace(/^\s+|\s+$/, ''); };

// Verify the event was an input number 
function isNumberKey(evt) {
    var charCode = (evt.which)? evt.which : evt.keyCode;
    if (charCode > 31 && (charCode < 48 || charCode > 57))
    {
        return false;
    }
    return true;
}

var prefix = 'ctl00_ctl00_Main_SubMain_';

// Display the institutions as a popup 
function popup()
{
    var newwindow = window.open('AddInstitution.aspx','name','height=768,width=768');
    if (window.focus)
    {
        newwindow.focus();
    }
    return true;
}

// If enter is pressed in the called textbox, call GetDocumentNumbers
function KeyPressed(caller)
{
    if(window.event && window.event.keyCode == 13)
    {
        GetDocumentNumbers(caller);
    }
}

function dropdown(mySel)
{
    var myWin, myVal;
    myVal = mySel.options[mySel.selectedIndex].value;
    if(myVal)
    {
        if(mySel.form.target)
            myWin = parent[mySel.form.target];
        else
            myWin = window;
        if (! myWin)
            return true;
        myWin.location = myVal;
    }
    return false;
}

function putUniversity()
{   
    var id = getCheckedValue(document.forms[0]['university']);
    var institutionValue;
    var hiddenElement;
    
    // temporary variables
    var type, name, value, iid;
    
    var el = opener.document.getElementById(prefix + 'universityDisplay');

    if (id === "")
    {
        if (!valInst())
        {
            //alert('The Institution is invalid');
            return false;
        }
        
        var nm = document.getElementById('institutionName').value;
        var add1 = document.getElementById('institutionAddress1').value;
        var add2 = document.getElementById('institutionAddress2').value;
        var city = document.getElementById('institutionCity').value;
        var st  = document.getElementById('institutionState').value;
        var region = document.getElementById('institutionRegion').value;
        var zip = document.getElementById('institutionZip').value;
        var cnty = document.getElementById('institutionCountry').value;
        
        if ( cnty != 'USA' ) {
			st = region;
        }
		

        institutionValue = nm.trim() + '<br />' +  city.trim() + ', ' + st.trim();
        type='type=\"hidden\"';
        name='name=\"institutionId\"';
        value='value=\"' + nm.trim() + '\n' + add1.trim() + '\n' + add2.trim() + '\n' + city.trim() + '\n' + st.trim() + '\n' + zip.trim() + '\n' + cnty.trim() + '\"';
        iid='id=\"' + -1 + '\"';
        hiddenElement = '<input ' + type + ' ' + name + ' ' + iid + ' ' + value + '/>';
    }
    else
    {
        institutionValue = document.getElementById(id).value;
        type='type=\"hidden\"';
        name='name=\"institutionId\"';
        value='value=\"' + id + '\"';
        iid='id=\"' + id + '\"';
        hiddenElement = '<input ' + type + ' ' + name + ' ' + iid + ' ' + value + '/>';
    }
    //alert("H = " + hiddenElement);
    //alert(el);
    //alert(institutionValue + hiddenElement);
    
    el.innerHTML = institutionValue + hiddenElement;

    window.close();
    return true;
}



// Called by the callback function
// Update the literal myMessage to display the results
function GetDocumentNumbersFromServer(documentNumbers, content)
{
    var msg = null;
    if (document.getElementById)
    {
        msg = document.getElementById("myMessage");
    }
    else if (document.all)
    {
        msg = document.all["myMessage"];
    }
    if (msg)
    {
        msg.innerHTML = '<b>' + documentNumbers + '</b>';
    }
}

function getCheckedValue(radioObj) {
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	
	if(radioLength == undefined)
	{
		if(radioObj.checked)
		{
			return radioObj.id;
		}
		else
		{
			return "";
	    }
	}
	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].id;
		}
	}
	return "";
}

function AddInstitution () {
    UseCallBack('add', "");
}

function Redirect(page)
{
    parent.location=page;
}

function valInst()
{
    var submit = true;
    var institutionName = document.getElementById('institutionName').value;
    var institutionAddress1 = document.getElementById('institutionAddress1').value;
    var institutionCity = document.getElementById('institutionCity').value;
    var institutionState = document.getElementById('institutionState').value;
    var institutionZip = document.getElementById('institutionZip').value;
    var institutionCountry = document.getElementById('institutionCountry');
    var institutionRegion = document.getElementById('institutionRegion').value;

    var results = new Array();

    institutionName.length > 0 ? results['institutionName'] = '' : results['institutionName'] = 'Must not be blank';
    institutionAddress1.length > 0 ? results['institutionAddress1'] = '' : results['institutionAddress1'] = 'Must not be blank';
    institutionCity.length > 0 ? results['institutionCity'] = '' : results['institutionCity'] = 'Must not be blank';
    
    if ( institutionCountry.value == 'USA' ) {
		institutionState.length > 0 ? results['institutionState'] = '' : results['institutionState'] = 'Must not be blank';
	} else {
		institutionRegion.length > 0 ? results['institutionState'] = '' : results['institutionState'] = 'Must not be blank';
	}

    var regex = new RegExp(/(^\d{5}-\d{4}$)|(^\d{5}$)/);

    var isZipCode = regex.test(institutionZip);

    !(institutionCountry.value === 'USA' && !isZipCode) ? results['institutionZip'] = '' : results['institutionZip'] = 'Must be a valid zip code';
    var tab = '&nbsp;&nbsp;&nbsp;&nbsp;';

    for (var i in results) {
        // get the text between the anchors
        var text = document.getElementById(i + 'Div').innerHTML;
        var start = text.search(/<a>/i);
        var end = text.search(/<\/a>/i);
        text = text.substring(start + 3, end);

        // if there's an error, set the error and set text red 
        if (results[i].length > 0)
        {
            document.getElementById(i + 'Div').innerHTML = '<font color="#FF0000"><a>' + text +'</a></font>';
            document.getElementById(i + 'Error').innerHTML = '<font color="#FF0000">' + tab + tab + results[i] +'</font>';
            submit = false;
        }
        // otherwise, clear the error and set text black
        else
        {
            document.getElementById(i + 'Div').innerHTML = '<a>' + text +'</a>';
            document.getElementById(i + 'Error').innerHTML = '';
        }
    }
    
    return submit;
}

function Search()
{
    var text = document.getElementById('searchTextBox').value;
    //alert(text);
    window.location='AddInstitution.aspx?search=' + text + '&page=0';
}
