﻿
// Global Variable Declarations
var isLoaded = false;


// Removes leading whitespaces
function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function trim( value ) {
	
	return LTrim(RTrim(value));
	
}

function TextCounter(fieldId, countfieldId) {
	var field=document.getElementById(fieldId);
	var countfield=document.getElementById(countfieldId);
	
	countfield.innerHTML = field.value.length;
}

// Validates, if a source field has a value. 
// Returns TRUE on success, FALSE otherwise.
// If supplied and result is FALSE, an error message target is set to visible.
function validateRequiredField( fieldId, errorMessageId )
{
    var field = document.getElementById(fieldId);
    var errorMessage = document.getElementById(errorMessageId);
    
    if (field == null)
    {
        return false;
    }
    
    if (field.value == null || 
        field.value == '' ||
        trim(field.value) == null || 
        trim(field.value) == '')
    {
        if (errorMessage != null)
        {
            errorMessage.style.display = "block";
        }
        return false;
    }
    
    
    if (errorMessage != null)
    {
       errorMessage.style.display = "none";
    }
    
    return true;
}


// Validates, if a source field has a valid URL value. 
// Returns TRUE on success, FALSE otherwise.
// If supplied and result is FALSE, an error message target is set to visible.
function validateURLField( fieldId, errorMessageId )
{
    var field = document.getElementById(fieldId);
    var errorMessage = document.getElementById(errorMessageId);
    
    if (field == null)
    {
        return true;
    }
    
    if (field.value != null && 
        field.value != '')
    {
			var urlRegexp = /^(http:|https:)\/\/(\w+\.)+.*$/
			
			if (urlRegexp.test(field.value) != true)
			{
				if (errorMessage != null)
				{
						errorMessage.style.display = "block";
				}
				return false;
      }
    }
    
    
    if (errorMessage != null)
    {
       errorMessage.style.display = "none";
    }
    
    return true;
}

// Global Functions

// Adds appropriate targets to anchor tags.
function processAnchorTargets()
{
    if (!document.getElementsByTagName) return;

    var anchors = document.getElementsByTagName("a");
    
    for (var i=0; i < anchors.length; i++)
    {
        var anchor = anchors[i];
        
        if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "popup")
        {
            anchor.target = "_blank";
        }
    }
}

// Submits form[0]
function submitForm()
{
    document.forms[0].submit();
    return true;
}

// Submit Target Form
function submitTargetForm(in_formIndex)
{
    document.forms[in_formIndex].submit();
    return true;
}


// Image Swaps
function imageSwap(in_Name, in_State)
{
    if (!isLoaded)
    {
        return true;
    }
    document.images[in_Name].src = eval(in_Name + "_" + in_State + ".src");
}


// Window Openers
function popWindow(in_target, in_WindowParameters)
{
    //windowParameters are very browser dependent.  Be sure to test in all browswers when
    //changing window parameters.

    //For instance for Both IE and Netscape browsers to absolutely position a window properly 2 sets
    // of dimensions should be included example: screenX=0,screenY=0,left=0,top=0
    //To set the height and width of the windows example: width=625,height=400
    //A Good PopupWindow in all browsers with no toolbar, no scrollbar, and not resizable.
    //windowParameters = "scrollbars=no,width=625,height=400";
    
    //alert('here 2 param: ' + in_target + ' ' + in_WindowParameters);
    //return false;


    if ( in_WindowParameters.length == 0 )
    {
        in_WindowParameters = "resizable,dependent,toolbar,scrollbars,location,status,menubar";
    }
    
    popupWin = window.open(in_target,'PopupWindow',in_WindowParameters);
    if (window.focus) {popupWin.focus()}
    
    return false;
}

function popWindowWithQStringParameters(in_target, qStringParams, in_WindowParameters)
{
    //alert('here 3 param: ' + in_target + ' ' + qStringParams + ' ' + in_WindowParameters);
    //return false;
    //windowParameters are very browser dependent.  Be sure to test in all browsers when
    //changing window parameters.

    //For instance for Both IE and Netscape browsers to absolutely position a window properly 2 sets
    // of dimensions should be included example: screenX=0,screenY=0,left=0,top=0
    //To set the height and width of the windows example: width=625,height=400
    //A Good PopupWindow in all browsers with no toolbar, no scrollbar, and not resizable.
    //windowParameters = "scrollbars=no,width=625,height=400";
    
    if ( in_WindowParameters.length == 0 )
    {
        in_WindowParameters = "resizable,dependent,toolbar,scrollbars,location,status,menubar";
    }
    
    var urlTarget=in_target+"?param="+escape(Sys.Serialization.JavaScriptSerializer.serialize(qStringParams));
    popupWin = window.open(urlTarget,'PopupWindow',in_WindowParameters);

    if (window.focus) {popupWin.focus()}
    
    
    return false;
}



// Global Initialization

function blurInputs()
{
    if (!document.getElementsByTagName) return;

    var inputs = document.getElementsByTagName("input");
    
    for (var i=0; i < inputs.length; i++)
    {
        var inp = inputs[i];
        inp.blur();
    }
    
}

function globalInit()
{
    processAnchorTargets();
    
    //var loadingIndicator = document.getElementById("ProgressMessagePageLoading");
	

	//if (loadingIndicator != null)
	//{
	//	//loadingIndicator.style.display = 'none';
	//	alert(loadingIndicator.id)
	//}
    
    
    isLoaded = true;
    
} 


// Additional: binary data

function RefreshBinaryDataFileDataUploader()
{
	//alert('RefreshBinaryDataFileDataUploaderClicked');
	return false;
}

function RefreshBinaryImageDataUploader()
{
	//alert('RefreshBinaryImageDataUploaderClicked');
	return false;
}


function setBinaryDataUploaderKeyValue(binaryDataKeyControlClientId, binaryDataKeyControlValue, binaryDataOnClickActionControlClientId)
{
	document.getElementById(binaryDataKeyControlClientId).value = binaryDataKeyControlValue;
	document.getElementById(binaryDataOnClickActionControlClientId).click();
	return false;
}


function updateParentBinaryDataUploaderAndClose(binaryDataKeyControlClientId,binaryDataKeyControlValue,binaryDataOnClickActionControlClientId)
{
	// note forms[0].elements does not work for input type="image" for some scary reason.
	window.opener.document.getElementById(binaryDataKeyControlClientId).value = binaryDataKeyControlValue;
	window.opener.document.getElementById(binaryDataOnClickActionControlClientId).click();

	closeSelfWindow();
	return false;
}



function updateParentAuthorNetworkItemAndClose(hcRolodexIdentificatorId, firstNameId, lastNameId, displayNameId, externalUrlId, hcRolodexIdentificator, firstName, lastName, displayName, externalUrl, actionControlId)
{
	window.opener.document.getElementById(hcRolodexIdentificatorId).value = hcRolodexIdentificator;
	window.opener.document.getElementById(firstNameId).value = firstName;	
	window.opener.document.getElementById(lastNameId).value = lastName;
	window.opener.document.getElementById(displayNameId).value = displayName;
	if (externalUrlId != null && externalUrlId != '')
	{
		window.opener.document.getElementById(externalUrlId).value = externalUrl;
	}

//alert("actionControlId.href = " + window.opener.document.getElementById(actionControlId).href);
	window.opener.location = window.opener.document.getElementById(actionControlId).href;

	//closeSelfWindow();
	window.close();
	return false;
}
          
function updateParentWhatAmIReadingAndClose(params)
{
	
	
	window.opener.document.getElementById(params.Title.Id).value = params.Title.Value;
	window.opener.document.getElementById(params.ISBN13.Id).value = params.ISBN13.Value;
	window.opener.document.getElementById(params.Dsc.Id).value = params.Dsc.Value;
	window.opener.document.getElementById(params.ExtUri.Id).value = params.ExtUri.Value;
	window.opener.document.getElementById(params.IsHCB.Id).value = params.IsHCB.Value;
	window.opener.document.getElementById(params.AFN1.Id).value = params.AFN1.Value;
	window.opener.document.getElementById(params.AFN2.Id).value = params.AFN2.Value;
	window.opener.document.getElementById(params.AFN3.Id).value = params.AFN3.Value;
	window.opener.document.getElementById(params.AFN4.Id).value = params.AFN4.Value;
	window.opener.document.getElementById(params.AFN5.Id).value = params.AFN5.Value;
	window.opener.document.getElementById(params.ALN1.Id).value = params.ALN1.Value;
	window.opener.document.getElementById(params.ALN2.Id).value = params.ALN2.Value;
	window.opener.document.getElementById(params.ALN3.Id).value = params.ALN3.Value;
	window.opener.document.getElementById(params.ALN4.Id).value = params.ALN4.Value;
	window.opener.document.getElementById(params.ALN5.Id).value = params.ALN5.Value;
	
	window.opener.location = window.opener.document.getElementById(params.ActionId).href;
	window.close();
	return false;
}

                






function closeSelfWindow()
{
	window.self.close();
    return false;
}

function openChildWindow(targetUrl,windowName,windowParameters)
{

    childWindow = window.open(targetUrl,windowName,windowParameters);

    if (childWindow.opener == null)
    {
    	childWindow.opener = self;
    }
}