﻿// JScript File
var div2print;

// Redirect a page
function getURL(strURL)
{
    try
    {
        window.location = strURL;
    }
    catch (e) {}
}

// Returns Numeric
function getNumeric(strString, allowChars)
{   
    var intRetValue = '';
    
    try
    {
        //debugger;
        var intCount = 0;
        var currentChar = '';
        
        for (intCount = 0; intCount < strString.length; intCount++)
        {
            currentChar = strString.charAt(intCount);
            if (allowChars.indexOf(currentChar) != -1)
            {
                intRetValue += currentChar;
            }
        }
    }
    catch (e) {}
    
    return intRetValue;
}

function taCount(visCnta) 
{ 
    debugger;
    var visCnt = document.getElementById(visCnta);
	var taObj=event.srcElement;
	if (taObj.value.length>taObj.maxLength*1) taObj.value=taObj.value.substring(0,taObj.maxLength*1);
	if (visCnt) visCnt.innerText=taObj.maxLength-taObj.value.length;
}

function characters_remaining(txtBox, divId, maxCount) 
{ 
    var txt = document.getElementById(txtBox);
    var txtvalue = '';
    var txtLength = 0;
    var txtRemaining = 0;
    var divbox = document.getElementById(divId);
    if (txt)
    {
        txtvalue = txt.value;
        txtLength = txtvalue.length;
        txtRemaining = maxCount - txtLength;
    }
    if (divbox)
    {
        divbox.innerText = txtRemaining;
    }
    return true;
}

function countChars(txtBox, divId, maxCount) 
{ 
    //debugger;
    var txt = document.getElementById(txtBox);
    var txtvalue = '';
    var txtLength = 0;
    var txtRemaining = 0;
    var divbox = document.getElementById(divId);
    if (txt)
    {
        txtvalue = txt.value;
        txtLength = txtvalue.length;
        txtRemaining = maxCount - txtLength;
    }
    if (divbox)
    {
        divbox.innerText = txtRemaining + ' characters remaining';
    }
    return true;
}


// Check for valid email address
function validEmail(email)
{
	var filter  = /\w+([-+.']\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*/;
	if (filter.test(email))
	{
	    return true;
	}
	else
	{
	    return false;
	}
}

// Print function for admin pages
function adminPrintMe(id)
{
    //debugger;
    var links = document.getElementById(id);
    if ((links != null) && (links != 'undefined'))
    {
        links.style.display = 'none';
    }
    window.print();
    if ((links != null) && (links != 'undefined'))
    {
        links.style.display = 'block';
    }
}

function PrintDiv(strText, relativePath){
	var a = window.open('','','width=300,height=300');
	a.document.open("text/html");
	a.document.write(strText);
	a.print();
    a.document.write('<a onclick="window.close();" style="background-image:url(' + relativePath + '_images/closeprint.jpg); display: block; width: 50px; cursor: pointer; float: right;">&nbsp;</a>');
	a.document.close();
}

function HtmlEncode(RawHTML) {
var encodedHtml;
     encodedHtml = escape(RawHTML);
     encodedHtml = encodedHtml.replace(/\//g,"%2F");
     encodedHtml = encodedHtml.replace(/\?/g,"%3F");
     encodedHtml = encodedHtml.replace(/=/g,"%3D");
     encodedHtml = encodedHtml.replace(/&/g,"%26");
     encodedHtml = encodedHtml.replace(/@/g,"%40");
     
     return encodedHtml;
   } 
   
function openPopup(url,width,height) 
{
   var width = 600;
   var height = 800;
   var LEFT = parseInt((screen.availWidth/2) - (width/2));
   var TOP = parseInt((screen.availHeight/2) - (height/2));
   var windowFeatures = "width=" + width + ",height=" + height + ",toolbar=no,status=no,location=no,resizable=no,directories=no,scrollbars=yes,left=" + LEFT + ",top=" + TOP + "screenX=" + LEFT + ",screenY=" + TOP;
   myWindow = window.open(url, "Help", windowFeatures);
   event.returnValue = false;
   return false;
}




//Use as is or customise for popup messageboxes on the client that only perform POSTBACK
//if the user responds YES
//
//Example - heres a delete example where user hits the delete button and they have to
//          confirm before the POSTBACK occurs for the delete to execute
//
// Add a button to page - which requires a messagebox for confirmation of the function...
//
//      <asp:Button runat="server" width="140px" Visible="true" ID="btnDelete" ToolTip="Delete exception messages" Text="Exeptions" />
//
// Wire up the buttons OnClick() handler to the javascript confirm dialog
//
//      Me.btnDelete.Attributes.Add("onclick", "javascript:return confirmDialog('Are you sure you want to delete the exception messages?');")
//
// Finally some code 
//        Protected Sub btnDelete_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnDelete.Click
//            Try
//                DeleteMessages()
//            Catch ex As Exception
//                // Catch error
//            End Try
//        End Sub
//
function confirmDialog(msg)
{
    exit = confirm( msg + " (OK=Yes Cancel=NO)")
 
    if (exit == true)
    {
        return true; //add order
    }
    else
    {
        return false; //dont add order
    }
}

var bSaving=false;

function checksaved(xForm, btnID)
{

	if(!bSaving)
	{
		if(IsFormDirty(xForm))
		{
            if (confirm('Save changes?'))
            {
                 __doPostBack(btnID,'');
            }
		}
	}
}

function checksaved2(xForm, btnID)
{
    debugger;
	if(!bSaving)
	{
		if(IsFormDirty(xForm))
		{
            if (confirm('Save changes?'))
            {
                 return true;
            }
            else
            {
                return false;
            }
		}
	}
}

function characters_remaining(txtBox, divId, maxCount) 
{ 
    var txt = document.getElementById(txtBox);
    var txtvalue = '';
    var txtLength = 0;
    var txtRemaining = 0;
    var divbox = document.getElementById(divId);
    if (txt)
    {
        txtvalue = txt.value;
        txtLength = txtvalue.length;
        txtRemaining = maxCount - txtLength;
    }
    if (divbox)
    {
        divbox.innerText = txtRemaining;
    }
    return true;
}

function IsFormDirty(xForm)
{
	/* Returns an indication of whether all fields are in their default state */
	/* True means it has changed */
	
	var eForm = document.getElementById(xForm);

	var iNumElems = eForm.elements.length;
	
	for (var i=0; i<iNumElems; i++)
	{
		var eElem = eForm.elements[i];
		if ("text" == eElem.type || "TEXTAREA" == eElem.tagName)
		{
			if (eElem.value != eElem.defaultValue) {
			   return true;
			}
		}
		else if ("checkbox" == eElem.type || "radio" == eElem.type)
		{
			if (eElem.checked != eElem.defaultChecked) { 
			    return true;
			}
		}
		else if ("SELECT" == eElem.tagName)
		{
			var cOpts = eElem.options;
			var iNumOpts = cOpts.length;
			for (var j=0; j<iNumOpts; j++)
			{
				var eOpt = cOpts[j];
		
				if (eOpt.selected != eOpt.defaultSelected) 
				{ 
			        // dont want to validate the drop downs on the html editor
			        if(eOpt.parentElement.id.indexOf('mce_editor_') == -1 && (eOpt.parentElement.id.indexOf('_toolbar') == -1 || eOpt.parentElement.id.indexOf('_styleSelect')))
			        {
					    return (true);
					}
				}
			}
		}
	}
	return false;
}


function trim(str)
{
    var trimmed = str.replace(/^\s+|\s+$/g, '') ;
    return trimmed;
}


//Clear control x if control y is empty
function clearControlXifControlYEmpty(ctrlX,CtrlY)
{
    //debugger;
    var currentvalue= $('.' + CtrlY).attr("value");
    if (currentvalue=='')
    {
        $('.' + ctrlX).attr("value","");
    }
    
}


function IsNumeric(strString)
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789.";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   }
   
   function IsInteger(strString)
   //  check for valid numeric strings	
   {
   var strValidChars = "0123456789";
   var strChar;
   var blnResult = true;

   if (strString.length == 0) return false;

   //  test strString consists of valid characters listed above
   for (i = 0; i < strString.length && blnResult == true; i++)
      {
      strChar = strString.charAt(i);
      if (strValidChars.indexOf(strChar) == -1)
         {
         blnResult = false;
         }
      }
   return blnResult;
   }
   
   function RadioButtonSelected(sender, args)
  {


        //2nd: ensure one of the radios is selected
        $('.rdoUserType').each(function() {
            //if its blank set warning in the warning span immediately after the textbox
            //debugger;
            if ($('.rdoUser')[0].firstChild.checked==false) 
            {
                if ($('.rdoGolfPro')[0].firstChild.checked==false) 
                {
                    //debugger;
                     args.IsValid = false;
                     return;
                    
                } 
                else
                {
                    args.IsValid = true;                
                }
            } 
            if (blnError==true)
            {
                $('.lblUserType').text("Required");
            }
            else
            {
                $('.lblUserType').text("");
            }
        })
            
  }

function URLEncode (clearString) {
  var output = '';
  var x = 0;
  clearString = clearString.toString();
  var regex = /(^[a-zA-Z0-9_.]*)/;
  while (x < clearString.length) {
    var match = regex.exec(clearString.substr(x));
    if (match != null && match.length > 1 && match[1] != '') {
    	output += match[1];
      x += match[1].length;
    } else {
      if (clearString[x] == ' ')
        output += '+';
      else {
        var charCode = clearString.charCodeAt(x);
        var hexVal = charCode.toString(16);
        output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
      }
      x++;
    }
  }
  return output;
}

function URLDecode (encodedString) {
  var output = encodedString;
  var binVal, thisString;
  var myregexp = /(%[^%]{2})/;
  while ((match = myregexp.exec(output)) != null
             && match.length > 1
             && match[1] != '') {
    binVal = parseInt(match[1].substr(1),16);
    thisString = String.fromCharCode(binVal);
    output = output.replace(match[1], thisString);
  }
  return output;
}


//Is used simply by including server side as follows, passing the id of the div to print. (what you get is a link saying 'print this' which pops up a dialog containing the
//content and the print dialog
//
//              Example include...(see golfpro job
//              Me.cMain.InnerHtml += Common.includePrint("divExtraContent") 
//
function CallPrint(strid)
{
//debugger;
var prtContent = document.getElementById(strid);
var WinPrint =
window.open('','','left=0,top=0,width=1,height=1,toolbar=0,scrollbars=0,status=0');
WinPrint.document.write(prtContent.innerHTML);
WinPrint.document.close();
WinPrint.focus();
WinPrint.print();
WinPrint.close();
//prtContent.innerHTML=strOldOne;
}
