
// Forces a postback to the server for controls that are wrapped in an ajax panel
function realPostBack(eventTarget, eventArgument) {
  //alert('calling do postback');
  __doPostBack(eventTarget, eventArgument);
}
 
function closeWindow(refreshOpener) {
  //this.focus();
  //self.opener = this;
  //self.close();
  if (refreshOpener) {
    RefreshOpener();
  }
  window.open('', '_self', '');
  window.close();
}

function RefreshOpener() {
  if (window.opener != null) {
    //window.opener.location.href = window.opener.location;
    //window.opener.location.reload(true);
    window.opener.__doPostBack('ChildWindowPostBack', '');
  }
}

function BrowserHeight() {
  var theHeight;
  if (window.innerHeight) {
    theHeight = window.innerHeight;
  }
  else if (document.documentElement && document.documentElement.clientHeight) {
    theHeight = document.documentElement.clientHeight;
  }
  else if (document.body) {
    theHeight = document.body.clientHeight;
  }
  return theHeight;
}

GetWindowSize = function(w) {
  var width, height;
  w = w ? w : window;
  this.width = w.innerWidth || (w.document.documentElement.clientWidth || w.document.body.clientWidth);
  this.height = w.innerHeight || (w.document.documentElement.clientHeight || w.document.body.clientHeight);

  return this;
}

function GetDocumentScrollTop() {
  var top;
  if (document.documentElement.scrollTop) {
    top = document.documentElement.scrollTop;
  }
  else {
    top = document.body.scrollTop;
  }

  return top;
}     

function GetGeneralStatusImage(status, altIfTrue, altIfFalse, host) {
  var src;
  var alt;
  var imgTag;

  if (status) {
    src = host + "/images/yes_small.gif";
    alt = altIfTrue;
  }
  else {
    src = host + "/images/cancel_red_x_icon.gif";
    alt = altIfFalse;
  }

  imgTag = '<img src="' + src + '" alt="' + alt + '" />';
  return imgTag;
}

function GetPaymentTypeImageStringValues(paymentType, hasActiveAcceptedAuth, paidInFull, host) {
  var src;
  var alt;
  //alert("paymentType is " + paymentType);
  switch (paymentType) {
    case 1: // CheckCash
      src = host + '/images/check_cash_small.gif';
      alt = "Check/Cash";
      break;

    case 2: // CreditCard
      if (paidInFull) {
        src = host + "/images/cc_small_capture.gif";
        alt = "Captured";
      }
      else if (hasActiveAcceptedAuth) {
        src = host + "/images/cc_small_auth.gif";
        alt = "Authorized";
      }
      else {
        src = host + "/images/cc_small_noauth.gif";
        alt = "Not Authorized";
      }
      break;

    case 3: // Escrow
      src = host + "/images/bank_small.gif";
      alt = "Escrow";
      break;

    case 4: //None
    case 5: // Unsure
      src = host + "/images/question_small.gif";
      alt = "Unknown";
      break;

    case 6: //CreditCardOnSite
      src = host + "/images/cc_house_small.gif";
      alt = "Credit Card On Site";
      break;
  }

  return [src, alt];
}

function GetPaymentTypeImageString(paymentType, hasActiveAcceptedAuth, paidInFull, host) {
  var src;
  var alt;
  var imgTag
  var ary = GetPaymentTypeImageStringValues(paymentType, hasActiveAcceptedAuth, paidInFull, host);
  src = ary[0];
  alt = ary[1];
  imgTag = '<img src="' + src + '" alt="' + alt + '" />';
  return imgTag;
}

//checks all CheckBoxList with the given name with the given value
function CheckAllCheckBoxList(aspCheckBoxListUniqueID, checkVal) 
{
    for(i = 0; i < document.forms[0].elements.length; i++) 
    {
        elm = document.forms[0].elements[i]
        if (elm.type == 'checkbox') 
        {
            if (elm.name.indexOf(aspCheckBoxListUniqueID)>=0) 
            {
                elm.checked = checkVal
            }
        }
    }
}

function SetUniqueRadioButton(match, current) 
{
    for(i = 0; i < document.forms[0].elements.length; i++)
    { 
        elm = document.forms[0].elements[i]

        if (elm.type == 'radio' && elm.id.indexOf(match, 0) > -1) 
        {
           elm.checked = false; 
        }
    }
    current.checked = true; 
}


function AutoSetSelectAllCheckBoxFromCheckBoxList(aspCheckBoxListUniqueID, aspSelectAllCheckBoxUniqueID)
{
    selectAll = true;
    //re = new RegExp( aspCheckBoxListUniqueID )  // find any checkbox contains this ID
    for(i = 0; i < document.forms[0].elements.length; i++) 
    {
        elm = document.forms[0].elements[i]
        if (elm.type == 'checkbox') 
        {
            //if (elm.name.match(re))
            if (elm.name.indexOf(aspCheckBoxListUniqueID)>=0)  
            {
                selectAll = selectAll && elm.checked;
            }
        }
    }    
    //var chkSelectAll = document.getElementById(aspSelectAllCheckBoxUniqueID);
    
    var chkSelectAll = document.forms[0].elements[aspSelectAllCheckBoxUniqueID];
    chkSelectAll.checked = selectAll;
}



function SetFocus(aspNetObjectID)
{
	var object = document.getElementById(aspNetObjectID);
	if (object != null)
	{
	    object.focus();
	}
}

function RefreshPage()
{
  window.location.href = window.location;
}

var phone_field_length=0;
function TabNext(obj,event,len,next_field)
{
	if(event == "down")
	{
		phone_field_length=obj.value.length;
	}
	else if(event == "up")
	{
		if(obj.value.length != phone_field_length)
		{
			phone_field_length=obj.value.length;
			if(phone_field_length == len)
			{
				//var object = document.getElementById(next_field);
				var object = document.forms[0].elements[next_field];
				if (object != null)
				{
					object.focus();
					object.select();
				}
			}
		}
	}
}

// return the value of the radio button that is checked
// return an empty string if none are checked, or
// there are no radio buttons
function GetRadioCheckedValue(radioObj) 
{
	if(!radioObj)
		return "";
	var radioLength = radioObj.length;
	if(radioLength == undefined)
		if(radioObj.checked)
			return radioObj.value;
		else
			return "";
	for(var i = 0; i<radioLength; i++) {
		if(radioObj[i].checked) {
			return radioObj[i].value;
		}
	}
	return "";
}

// set the radio button with the given value as being checked
// do nothing if there are no radio buttons
// if the given value does not exist, all the radio buttons
// are reset to unchecked
function SetRadioCheckedValue(radioObj, newValue) 
{
	if(!radioObj)
		return;
	var radioLength = radioObj.length;
	if(radioLength == undefined) {
		radioObj.checked = (radioObj.value == newValue.toString());
		return;
	}
	for(var i = 0; i<radioLength; i++) {
		radioObj[i].checked = false;
		if(radioObj[i].value == newValue.toString()) {
			radioObj[i].checked = true;
		}
	}
}

//Format the first letter of the word to upper case
function UpperFirstLetter(obj) {
    var a = obj.value.split(" "); // split the sentence into an array of words

	var result = "";
    for (i = 0 ; i < a.length ; i ++ ) 
    {
        var firstLetter = a[i].substring(0, 1).toUpperCase();
        var restOfWord = a[i].substring(1);
        result += firstLetter + restOfWord + " "; 
    }

    //obj.value = a.join(" "); // join it back together
    result = result.replace( /\s+$/g, "" );// strip trailing
    
    //Maintain the space
    if (obj.value.substring(obj.value.length -1) == ' ')
    { result += " ";}
    obj.value = result;
}

function ScrollPage(y) 
{
    if (y >0)
   		window.scroll(0, y); // horizontal and vertical scroll targets
}

function ConfirmDelete(s)
{
    return confirm("Are you sure you want to delete " + s + "?");
}

// calling the function
// InsertAtCursor(document.formName.fieldName, ‘this value’);
function InsertAtCursor(myField, myValue) {
    //IE support
    if (document.selection) {
        myField.focus();
        sel = document.selection.createRange();
        sel.text = myValue;
    }
    //MOZILLA/NETSCAPE support
    else if (myField.selectionStart || myField.selectionStart == '0') 
    {
        var startPos = myField.selectionStart;
        var endPos = myField.selectionEnd;
        myField.value = myField.value.substring(0, startPos)
        + myValue
        + myField.value.substring(endPos, myField.value.length);
    } 
    else 
    {
        myField.value += myValue;
    }
  }

// Returns a RadWindow object from the RadWindow itself
function GetRadWindowLocal() {
  var oWindow = null;
  if (window.radWindow) oWindow = window.radWindow;
  else if (window.frameElement.radWindow) oWindow = window.frameElement.radWindow;
  return oWindow;
}

// Returns a RadWindow object from the RadWindow itself
function CloseRadWindowLocal() {
  var oWindow = GetRadWindowLocal();
  if (oWindow) oWindow.close();
}

// Returns a RadWindow object from the RadWindow itself
function CloseRadWindowLocalWithArgs(args) {
  var oWindow = GetRadWindowLocal();
  if (oWindow) oWindow.close(args);
}

