// --------------------------------------------------------------------------
// All rights are reserved. Reproduction or transmission in whole or in part,
// in any form or by any, electronic mechanical or otherwise, is prohibited
// without the prior written permission of the copyright owner
//
// Filename ............: common.js
// Author ..............: Bokkie / Gowi Group Limited
// Project .............: Hipag
// Created .............: 07.04.2006
// Description .........: Hipag common javascript library
// Calling Samples .....:
// Parameter List ......:
// Major change list ...: 
// Notes ...............: 
// --------------------------------------------------------------------------

// This function collapses or expands specified div
function ShowHide (expander, link, divname)
{
	// If there is no div, show runtime js alert
	if (divname == "")
	{
		alert("Gowi Expander control '" + expander + "' has no DIV name specified.");
		return;
	}
	
	var expander = document.getElementById(expander);
	// expanded, needs to be collapsed
	if (link.innerHTML == "Collapse this section")
	{
		expander.className = "expand";
		link.innerHTML = "Expand this section";
		document.getElementById(divname).style.display = "none";
	}
	// colapsed, needs to be expanded
	else
	{
		expander.className = "collapse";
		link.innerHTML = "Collapse this section";
		document.getElementById(divname).style.display = "block";										
	}
	// Added by Ermin 09.04.2008.
	// BEGIN
	try
	{
		SetExpandingCookie();
	}
	catch(err)
	{}
	// END
}

// This function initializes selected DIV with given display style
function InitDiv (divname, divstyle)
{
	document.getElementById(divname).style.display = divstyle;
}

// Loops through all expandable divs and sets the style for each div
function CheckExpandableDivs (divArray)
{
	var totalDivs = divArray.split("|");
	if (totalDivs.length > 1)
	{
		var divData;
		for (var i = 0; i<totalDivs.length - 1; i++)
		{
			divData = totalDivs[i].split("$");
			InitDiv(divData[0], divData[1]);
		}
	}
}


// Checks an e-mail
function CheckEmail(val)
{
	var ri = /^[\'\w\-\.]+@[\w\-\.]+\.[\w]{2,}$/;
	return (ri.test(val))	 
} 


// Trims the string on the left side
function LTrim(str)
{
	var whitespace = new String(" \t\n\r "); 
	var s = new String(str);
	if (whitespace.indexOf(s.charAt(0)) != -1) {
		var j=0, i = s.length;
		while (j < i && whitespace.indexOf(s.charAt(j)) != -1)
			j++;
		s = s.substring(j, i);
	}
	return s;
}


// Trims the string on the right side
function RTrim(str)
{
	var whitespace = new String(" \t\n\r "); 
	var s = new String(str);
	if (whitespace.indexOf(s.charAt(s.length-1)) != -1) {
		var i = s.length - 1;       // Get length of string
		while (i >= 0 && whitespace.indexOf(s.charAt(i)) != -1)
			i--;
		s = s.substring(0, i+1);
	}
	return s;
}


// Trims complete string
function Trim(str)
{
	return RTrim(LTrim(str));
}

// Erases given cooke name
function EraseCookie(name) 
{ 
	var days = -1; // yesterday, so the browser may delete it 
	var value = "D"; // marked for deletion 
	var date = new Date(); 
	date.setTime(date.getTime()+(days*24*60*60*1000)); 
	var expires = "; expires="+date.toGMTString(); 
	document.cookie  = name + "=" + value + expires + "; path=/";    
}  

// Erases .CUSTOM_AUTH cookie used for authentification
function logMeOff(url)
{
	EraseCookie(".CUSTOM_AUTH");
	document.location.href = url;
}

// Get the control name by the row template
function RowTemplateControlName(FullName)
{
	var ElementID = FullName.split("_");
	if (ElementID.length>3)
	{
	return ElementID[3];
	} 
	else
	{
	return ElementID[1];
	}
}

// Checks if the given field has numbers only
function IsNumber(formField)
{
	var result = "";
	var field = document.getElementById(formField);	
	
	inputVal = field.value;
	oneDecimal = false
	inputStr = inputVal.toString()
	for (var i = 0; i < inputStr.length; i++) {
		var oneChar = inputStr.charAt(i)
		if (i == 0 && oneChar == "-") {
			continue
		}
		if (oneChar == "." && !oneDecimal) {
			oneDecimal = true
			continue
		}
		if (oneChar < "0" || oneChar > "9") {
			result = "You must enter a number in " + SentenceParse(RowTemplateControlName(formField)) + " field.";
		}
	}
	return result
}

//added by Sajber on 1.12.2006.
//check if elements value is of decimal type
function IsDecimal(element)
{
	
    var decimalRE = "^(\\+|-)?[0-9][0-9]*(\\.[0-9]*)?$";
	return RegExTest(element,decimalRE);
}

//added by sajber mile on 1.12.2006.
//check if elements value is of deciamal type and within specified number of whole and decimal places
function IsDecimalExtended(element, wholePlaces, DecPlaces)
{
	
	var decimalRE = "^[-+]?[0-9]\\d{0,"+(wholePlaces-1)+"}(\\.\\d{1,"+DecPlaces+"})?%?$";
	return RegExTest(element,decimalRE);
}

//added by sajber mile on 1.12.2006.
//test regular expression against element's value
function RegExTest(element,expression)
{
	return element.value.match(expression) != null;
}


// Parses the sentence
function SentenceParse(inputText)
{
	if (inputText)
	{
		var text = inputText;
		var newText = "";
		for (var i=0; i< text.length; i++)
		{
			var character = text.charAt(i);
			if (character == character.toUpperCase())
			{
				newText += " " + character;
			}
			else
			{
				newText += character;
			}
		}
		return newText
	}
	else
	{
		return inputText;
	}
}

//Confirmation Javascript Box
function confirm_delete(mess){
	if (confirm(mess)==true)
		return true;
	else
		return false;
}


function ValidStringLength(fld, minL, maxL)
{

	var a = false;
	if (minL<=Trim(fld).length)
	{
		if (fld.length<=maxL)
		{
			a = true;
		}
	}
	return a;
	
}
function ShowHelpWindow(helpIndex)
{
	window.open("http://www.hipag.co.uk/help/help.aspx?h=" + helpIndex, "", "toolbar=no,height=330,width=450,scrollbars=yes,status=no,resizable=no"); 
	// Put next line into HTML code
	// <a href="javascript:ShowHelpWindow(5);" class="helpicon"><span>Help</span></a>
}
function EmailThem(helpIndex)
{
	helpContent = document.getElementById(helpIndex).value;
	window.location = "SendEmail.aspx?emailTo=" + helpContent; 
}
