//-----------------------------------------------------------------------------------------
//
//  Copyright (c) 2003 Novedge LLC
// 
//-----------------------------------------------------------------------------------------

//-----------------------------------------------------------------------------------------
// Preload the minu sign for expandable tables
//
var MinusIcon = new Image(); MinusIcon.src = "images/minus.gif";

var Browser =  (window.navigator.appName == "Netscape" ? "NE" : "IE");
var Version = (Browser == "NE") ? 	window.navigator.appVersion.substr(0,1) : window.navigator.appVersion.substr(window.navigator.appVersion.indexOf('MSIE ')+5,1); 

//-----------------------------------------------------------------------------------------
//if (window != top && location.href.indexOf('Generic_') == -1 && location.href.indexOf('User_') == -1 ) top.location.href = location.href;

//-----------------------------------------------------------------------------------------
// To preserve Compatibility between IE and Mozilla
//
if(document.all && !document.getElementById) 
{
    document.getElementById = function(id) 
    {
         return document.all[id];
    }
}

//-----------------------------------------------------------------------------------------
// String class extensions
//
function _EXT_String_trim() {
    var s = this;
    if (s.length > 0) while (s.indexOf(' ') == 0) s = s.substr(1);
    if (s.length > 0) while(s.lastIndexOf(' ') == s.length - 1) s = s.substr(0, s.length - 1);
    return s;
}

String.prototype.trim = _EXT_String_trim;

function _EXT_String_isNumeric() 
{
    var s = this;
    return (("" + parseInt(s)) == s.trim());
}

String.prototype.isNumeric = _EXT_String_isNumeric;


//-----------------------------------------------------------------------------------------
function setCookieCID( value ) 
{
	var Expires = new Date(); Expires.setFullYear( 2010 );
	document.cookie = "CID="  + escape(value) + ";path=/;expires=" + Expires.toGMTString();
}

//-----------------------------------------------------------------------------------------
function IsNumeric( sText )
{
	var ValidChars = '0123456789.';

	for(var i = 0; i < sText.length; i++ ) 
		if( ValidChars.indexOf(sText.charAt(i)) == -1)
			return false;

	return true;
}

//-----------------------------------------------------------------------------------------
// CHECK Fields functions
//-----------------------------------------------------------------------------------------
function checkEmail(original){
    var format = /[\w|_|-]+@[\w|-]+\.[\w|-|\.]+/ig;

    if (!original.match(format)){
        return 'Email Address is invalid or missing.\n';
    }
    return '';
 }

 function checkFname(original){
    var format = /\w\w+/g;
 
    if (!original.match(format)){
        return 'First name is invalid or missing.\n';
    }
    return '';
 }

 function checkLname(original){
    var format = /\w\w+/g;

    if (!original.match(format)){
        return 'Last name is invalid or missing.\n';
    }
    return '';
 }
 
 function checkAddress(original){
   if( original.length < 4 ) 
      return 'Address is invalid or missing.\n';
   return '';
 }
 
 function checkPhone(original){
   if( original.length < 6 ) 
      return 'Phone number is invalid or missing.\n';
   return '';
 }
 
 function checkState(original){
    var format = /\w\w+/g;

    if (!original.match(format)){
         return 'State is invalid or missing.\n';
    }
    return '';
 }
 
 function checkCity(original){
    var format = /\w\w\w+/g;

    if (!original.match(format)){
        return 'City is invalid or missing.\n';
    }
    return '';
 }
 
 function checkCountry(original){
    var format = /^[\w]+/g;

    if (!original.match(format)){
         return 'Country is invalid or missing.\n';
    }
	
    return '';
 }
 
 function checkCcNum(original){
    var format = /^[\d]+/g;

	if (original.charAt(0) == 'c') {
		return '';
	}
    if (!original.match(format)){
         return 'Credit Card Number is invalid or missing.\n';
    }
    return '';
 }

 function checkCcCode(original){
    var format = /^[\d]+/g;

    if (!original.match(format)){
         return 'CVV2/CID code is invalid or missing.\n';
    }
    return '';
 }

 function checkZip(original){
    var format = /\w+/g;

    if (!original.match(format)){
         return 'Zip/Postal code is invalid or missing.\n';
    }
    return '';
 }
 
  function checkDescription(original){
     if( original.length < 5 ) 
		return 'Description is invalid or missing.\n';
	return '';
  }

  function checkTitle(original){
     if( original.length < 3 ) 
		return 'Title is invalid or missing.\n';
	return '';
  }

  function checkCompany(original){
     if( original.length < 2 ) 
		return 'The company name is invalid or missing.\n';
	return '';
  }

  function checkName(original){
     if( original.length < 2 ) 
		return 'The name is invalid or missing.\n';
	return '';
  }

  function checkFullName(original){
     var format = /[\w]+\ +[\w]/ig;

     if (!original.match(format) || original.length < 5 ){
        return 'Full Name is invalid or missing.\n';
     }
     return '';
  }

//-----------------------------------------------------------------------------------------
// Business Rule 
function BR_OnNCopiesChange( nCopies, maintFlag )
{
	if( !nCopies.value.isNumeric() )
		nCopies.value = 0;

	if( nCopies.value == 0 || nCopies.value == '0')
		maintFlag.checked = false;

	if( nCopies.value != 0 )
		maintFlag.checked = true;

	maintFlag.value = nCopies.value;
}

//-----------------------------------------------------------------------------------------
// Business Rule 
function BR_OnMaintenanceFlagChange( maintFlag, nCopies )
{
	if( nCopies.value == 0 || nCopies.value == '0')
	{
		maintFlag.checked = false;
		alert('You can buy maintenance only with new licenses.\n' +
		'If you want to buy maintenance for existing licenses \nplease call Novedge.');
	}
}

//-----------------------------------------------------------------------------------------
function ToggleTable( pTable )
{
	var ShowTable = (pTable.className != 'FormatTable' );
	
	// When DHML is not working reload the page with some parametrs to get the same results
	if( Browser != "IE" || Version < 5 ) {
		var loc = window.location.href;
		// Cleanup the url string
		if( loc.indexOf(pTable.id) > 0 ) {
			var re1 = pTable.id + '=block';
			var re2 = re1 + '&';
			var re3 = '&' + re1;
			loc = loc.replace( re2, '' ); 
			loc = loc.replace( re3, '' ); 
			loc = loc.replace( re1, '' ); 
			if( loc.charAt(loc.length-1) == '?' ) loc = loc.substr(0,loc.length-1);
		}
		// Prepare the new parameter
		if ( ShowTable )
			loc = loc + ((loc.indexOf('?')>0) ? '&' : '?') + pTable.id + '=block';
		// set the new url
		window.location = loc;
		return;
	}
		
	var pTbody = pTable.getElementsByTagName("tbody").item(0);
	var pRows  = pTbody.getElementsByTagName("tr");
	var pFirstTd = pRows.item(0).getElementsByTagName("td").item(0);
	var pImg = pFirstTd.getElementsByTagName("img").item(0);

	if (pTable.className == 'FormatTable' ) {
	
		pTable.className = 'FormatTableClose';

		for( var i=1; i < pRows.length; i++)
			pRows.item(i).style.display = 'none';

		pFirstTd.className = 'FormatCategoryClose';
		
		if( pImg != null ) pImg.src = 'images/plus.gif';

	} else {
	
		pTable.className = 'FormatTable';

		for( var i=1; i < pRows.length; i++) {
			pRows.item(i).style.display = 'block';
		}

		pFirstTd.className = 'FormatCategory';
		
		if( pImg != null ) pImg.src = 'images/minus.gif';

	}
}
	
// -----------------------------------------------------------------------------------------------
function TabHi( t )
{
	t.className = 'tabON';
}

// -----------------------------------------------------------------------------------------------
function TabLo( t)
{
	t.className = 'tabOFF';
}

// -----------------------------------------------------------------------------------------------
function MenuHi( t)
{
	t.className = 'mnuON';
}

// -----------------------------------------------------------------------------------------------
function MenuLo( t)
{
	t.className = 'mnuOFF';
}

//-----------------------------------------------------------------------------------------
function ResizeWindow(  targetX, targetY )
{
	if( Browser == "NE" ) return;
	
	if( document.documentElement == null || document.documentElement.clientWidth == 0)
	{
		window.resizeBy( targetX - document.body.clientWidth, targetY - document.body.clientHeight);
	} else {
		window.resizeBy( targetX - document.documentElement.clientWidth, targetY - document.documentElement.clientHeight );
	}
}

//-----------------------------------------------------------------------------------------
function HiLightRow( p_row )
{
	for( var i=0; i<p_row.childNodes.length; i++)
		ChangeBrightness( p_row.childNodes.item(i), -10 );	
}

//-----------------------------------------------------------------------------------------
function LoLightRow( p_row )
{
	for( var i=0; i<p_row.childNodes.length; i++)
		ChangeBrightness( p_row.childNodes.item(i), 10 );	
}

//-----------------------------------------------------------------------------------------
String.prototype.RGBsplit = function() 
{ 
	var vals = new Array(3);
	if( this.length == 0) {
		vals = [255,255,255];
	} else if( this.substr(0,4) == 'rgb(' ) {
		var decValues = this.substr(4,this.length-5);
		decValues = decValues.replace(/\ /g,"");
		vals = decValues.split(",");
		for( var i=0; i<3; i++ )
			vals[i] = parseInt(vals[i]);
	} else if( this.length == 7) {
		for( var i=0; i<3; i++ )
			eval( "vals[" + i + "] = 0x" + this.substr(1+2*i,2) + ";" );
	} else { // len = 3
		for( var i=0; i<3; i++ )
			eval( "vals[" + i + "] = 0x" + this.substr(1+i,1) + this.substr(1+i,1) + ";" );
	}
	
	return vals; 
} 

//-----------------------------------------------------------------------------------------
function ChangeBrightness( item, change )
{
	var RGB = item.style.backgroundColor.RGBsplit();
	RGB[0] += change;
	RGB[1] += change;
	RGB[2] += change;
	item.style.backgroundColor = 'rgb(' + RGB[0] + ',' + RGB[1] + ',' + RGB[2] + ')'; 
}

//-----------------------------------------------------------------------------------------

