// general functions
function setCookie(name, value, expires, path, domain, secure) {
    document.cookie= name + "=" + escape(value) +
        ((expires) ? "; expires=" + expires.toGMTString() : "") +
        ((path) ? "; path=" + path : "") +
        ((domain) ? "; domain=" + domain : "") +
        ((secure) ? "; secure" : "");
}

function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1)
    {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    }
    else
    {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookie(name, path, domain) {
    if (getCookie(name))
    {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}
function showHelp (location) {
	var w = 480, h = 340;

	if (document.all) {
		/* the following is only available after onLoad */
		w = document.body.clientWidth;
		h = document.body.clientHeight;
	} else if (document.layers) {
   		w = window.innerWidth;
   		h = window.innerHeight;
	}

	var popW = 450, popH = 340;

	var leftPos = (w-popW)/2, topPos = (h-popH)/2;

	window.open (location,"help_window", "width=" + popW + ",height=" + popH + ",top=" + topPos + ",left=" + leftPos + ",scrollbars=yes");
	return false;
}

// trim leading and trailing spaces from value
function trim ( s ) {
	s = s.replace (/^\s*|\s*$/g, "");
	return s;
}

function empty ( x ) {
	return (x.length > 0) ? false : true;
}

function is_numeric ( n ) {
	var v = 1.0 * n ;

//	if ( v == 0.0 || isNaN(v) )
	if ( isNaN(v) )
		return false ;
	else
		return true ;
}

function unformat_number (value, decimals) {
	var x = new String (value);
	x = x.replace(/,/g,"");
	x = x.replace(/%/g,"");

	return x;

//	decimals = (decimals == null) ? 2 : decimals;

//	var num = new NumberFormat();
//	num.setInputDecimal('.');
//	num.setNumber(value);
//	num.setPlaces(decimals, false);
//	num.setCurrency(false);
//	num.setNegativeFormat(num.LEFT_DASH);
//	num.setSeparators(false);
//	return num.toFormatted();
}

function format_number (value, decimals, postfix) {
	decimals = (decimals == null) ? 2 : decimals;

	var num = new NumberFormat();
	num.setInputDecimal('.');
	num.setNumber(value);
	num.setPlaces(decimals, false);
	num.setCurrency(false);
	num.setNegativeFormat(num.LEFT_DASH);
	num.setSeparators(true, ',', ',');
	return num.toFormatted() + postfix;
}
function format_float (value, decimals) {
	decimals = (decimals == null) ? 2 : decimals;

	var num = new NumberFormat();
	num.setInputDecimal('.');
	num.setNumber(value);
	num.setPlaces(decimals, false);
	num.setCurrency(false);
	num.setNegativeFormat(num.LEFT_DASH);
	num.setSeparators(true, ',', ',');
	return num.toFormatted();
}
function format_perc (value, decimals) {
	decimals = (decimals == null) ? 2 : decimals;

	var num = new NumberFormat();
	num.setInputDecimal('.');
	num.setNumber(value);
	num.setPlaces(decimals, false);
	num.setCurrency(false);
	num.setNegativeFormat(num.LEFT_DASH);
	num.setSeparators(true, ',', ',');
	return num.toFormatted() + '%';
}

function validateDate( strValue ) {
  var objRegExp = /^\d{4}(\-|\/|\.)\d{1,2}\1\d{1,2}$/

  //check to see if in correct format
  if (!objRegExp.test(strValue)) {
    return false; //doesn't match pattern, bad date
  } else {
    var strSeparator = strValue.substring(4,5) //find date separator
    var arrayDate = strValue.split(strSeparator); //split date into month, day, year
    //create a lookup for months not equal to Feb.
    var arrayLookup = { '01' : 31,'03' : 31, '04' : 30,'05' : 31,'06' : 30,'07' : 31,
                        '08' : 31,'09' : 30,'10' : 31,'11' : 30,'12' : 31}
    var intDay = parseInt(arrayDate[2], 10);

    //check if month value and day value agree
    if(arrayLookup[arrayDate[1]] != null) {
      if(intDay <= arrayLookup[arrayDate[1]] && intDay != 0) {
        return true; //found in lookup table, good date
	  }
    }

    //check for February (bugfix 20050322)
    var intMonth = parseInt(arrayDate[1], 10);
    if (intMonth == 2) {
       var intYear = parseInt(arrayDate[0], 10);
       if( ((intYear % 4 == 0 && intDay <= 29) || (intYear % 4 != 0 && intDay <=28)) && intDay !=0)
          return true; //Feb. had valid number of days
       }
  }
  return false; //any other values, bad date
}
function isValidEmail(emailAddress) {
    var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((([0-1]?[0-9]{1,2}\.)|(2[0-4][0-9]\.)|(25[0-5]\.)){3}(([0-1]?[0-9]{1,2})|(2[0-4][0-9])|(25[0-5]))|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
    if (!re.test(emailAddress)) {
    	return false;
    }
    return true;
}
function makeRemote (fileName) {
alert (fileName);
	var remote = fileName.replace (/\\/g, '/');
alert (remote);
alert ("file://" + remote);
//	return "file://" + remote;
	return remote;
}
function confirmDelete(what) {
	if (confirm ("Are you sure you want to delete this " + what)) {
		return true;
	} else {
		return false;
	}
}

function GetRadioGroupValue (cForm, cControl) {
	for (var counter = 0; counter < document.forms[cForm].elements.length; counter++) {
		if (document.forms[cForm].elements[cControl][counter].checked) {
			return document.forms[cForm].elements[cControl][counter].value;
		}
	}
}

function get_radio_value()
{
for (var i=0; i < document.orderform.music.length; i++)
   {
   if (document.orderform.music[i].checked)
      {
      var rad_val = document.orderform.music[i].value;
      }
   }
}

function ClearDropDown (cForm, cControl) {
	var control = document.forms[cForm].elements[cControl];

	while (control.firstChild) {
		control.removeChild(control.firstChild);
	}
}

function LoadDropDown (cForm, cControl, aData) {
	ClearDropDown (cForm, cControl);
	cmb = document.forms[cForm].elements[cControl];
	for (key in aData) {
		cmb.options.add(new Option (aData[key] ,key));
	}
}

function LoadParentDropDown (cForm, cControl, aData) {
	ClearDropDown (cForm, cControl);
	cmb = document.forms[cForm].elements[cControl];
	for (key in aData) {
		cmb.options.add(new Option (aData[key] ,key));
	}
}

function LoadChildDropDown (cForm, cChildCtrl, cParentCtrl, aData) {
	ClearDropDown (cForm, cChildCtrl);
	parent_cmb = document.forms[cForm].elements[cParentCtrl];
	parent_id = parent_cmb[parent_cmb.selectedIndex].value;
	child_cmb = document.forms[cForm].elements[cChildCtrl];
	for (key in aData[parent_id]) {
		child_cmb.options.add(new Option (aData[parent_id][key] ,key));
	}
}

function SetDropDown (cForm, cCtrl, nId, aData) {
	cmb = document.forms[cForm].elements[cCtrl];
	for (counter = 0; counter < cmb.options.length; counter++) {
		if (cmb.options[counter].value == nId) {
			cmb.options[counter].selected = true;
		}
	}
}

function SetParentDropDown (cForm, cParentCtrl, cChildCtrl, nParentId, aData) {
	parent_cmb = document.forms[cForm].elements[cParentCtrl];
	for (counter = 0; counter < parent_cmb.options.length; counter++) {
		if (parent_cmb.options[counter].value == nParentId) {
			parent_cmb.options[counter].selected = true;
		}
	}
	LoadChildDropDown (cForm, cChildCtrl, cParentCtrl, aData);
}

function SetChildDropDown (cForm, cChildCtrl, nChildId) {
	child_cmb = document.forms[cForm].elements[cChildCtrl];
	for (counter = 0; counter < child_cmb.options.length; counter++) {
		if (child_cmb.options[counter].value == nChildId) {
			child_cmb.options[counter].selected = true;
		}
	}
}

function ExposeMandatoryFields (cForm, aFields, cBackgroundColor) {
	for (counter = 0; counter < aFields.length; counter++) {
		switch (document.forms[cForm].elements[aFields[counter][0]].type) {
			case "textarea":
				document.forms[cForm].elements[aFields[counter][0]].style.background = cBackgroundColor;
				break;
			case "text":
				document.forms[cForm].elements[aFields[counter][0]].style.background = cBackgroundColor;
				break;
			case "select-one":
				document.forms[cForm].elements[aFields[counter][0]].style.background = cBackgroundColor;
				break;
			default:
				document.forms[cForm].elements[aFields[counter][0]].type
				break;
		}
	}
}

function doHideShow ( ohide, oshow, etype ) {
	var display_type;
	switch (etype) {
		case "td":
			display_type = "table-cell";
			break;
		case "tr":
			if (document.all) {
				display_type = "block";
			} else {
				display_type = "table-row";
			}
			break;
		case "div":
		default:
			display_type = "block";
			break;
	}
	if (ohide != "") {
		if (ohide instanceof Array) {
			for (var iHide = 0; iHide < ohide.length; iHide++) {
				document.getElementById (ohide[iHide]).style.display = "none";
			}
		} else {
			document.getElementById (ohide).style.display = "none";
		}
	}
	if (oshow != "") {
		if (oshow instanceof Array) {
			for (var iShow = 0; iShow < oshow.length; iShow++) {
				document.getElementById (oshow[iShow]).style.display = display_type;
			}
		} else {
			document.getElementById (oshow).style.display = display_type;
		}
	}
}
function write_object(embedded_object) {
	document.write(embedded_object);
	document.write('\n');
}
var hintcontainer = null;
function showhint(obj, txt) {
	if (hintcontainer==null) {
		hintcontainer = document.createElement("div");
		hintcontainer.className="hintstyle";
		document.body.appendChild(hintcontainer);
	}
	obj.onmouseout = hidehint;
	obj.onmousemove=movehint;
	hintcontainer.innerHTML=txt;
}
function movehint(e) {
	if (!e) e = event; //line for IE compatibility
	hintcontainer.style.top =  (e.clientY+document.documentElement.scrollTop+2)+"px";
	hintcontainer.style.left = (e.clientX+document.documentElement.scrollLeft+10)+"px";
	hintcontainer.style.display="";
}
function hidehint() {
	hintcontainer.style.display="none";
}
