// THIS IS A STANDARD JAVASCRIPT FILE USED FOR CDMS inNATIONAL
// THIS DECLARES ALL THE REGULAR EXPRESSIONS THAT WE WILL BE USING
// FOR VALIDATION FUNCTIONS.

var msgWindow;

var regWhitespace = /^\s+$/;

var regInteger = /(^-?\d\d*$)/;

// =========================== 
// AJAX Calls
// ===========================

function getXMLHTTP()
{
var xmlHttp=null;
try
  {
  // Firefox, Opera 8.0+, Safari
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  // Internet Explorer
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}


function replaceDiv(strUrl,target) {		

   var req = getXMLHTTP();		
   if (req) {			
      req.onreadystatechange = function() {
         if (req.readyState == 4) {
            // only if "OK"
            if (req.status == 200) {						
               document.getElementById(target).innerHTML=req.responseText;						
            } else {
               alert("There was a problem while using XMLHTTP:\n" + req.statusText);
            }
         }				
	  }	
      req.open("GET",strUrl, true);
      req.send(null);
   }		
}

// =========================== 
//
// ===========================

function confirmLink(txt) {

	if (confirm(txt)) {
	   return true;
	}
	else {
	  return false;
	}
}

function togglex(img, node) {

    var target = document.getElementById(node);
    if (target.style.display == "none") {
       target.style.display = "block";
    } else {
      // target.style.display = "none"; - disabled it
    }
}

function toggle( node , which ) {

    var target = document.getElementById(node);

    if ( which == "block") {
    
       target.style.display = "block";
       
    } else {

       target.style.display = "none"; 
    }
}
// =========================== 
//
// ===========================

function isEmpty(s)       { return ((s == null) || (s.length == 0)) }
// =========================== 
//
// ===========================

function isWhitespace (s) { return (isEmpty(s)  || regWhitespace.test(s)); }
// =========================== 
//
// ===========================
function formattedDate(myfield, e, dec) {

var key;
var keychar;

if (window.event)
   key = window.event.keyCode;
else if (e)
   key = e.which;
else
   return true;
keychar = String.fromCharCode(key);

// control keys
if ((key==null) || (key==0) || (key==8) || 
    (key==9) || (key==13) || (key==27) )
   return true;

// numbers
else if ((("0123456789.-/").indexOf(keychar) > -1))
   return true;

// decimal point jump
else if (dec && (keychar == "."))
   {
   myfield.form.elements[dec].focus();
   return false;
   }
else
   return false;

}

function formattedNumber(myfield, e, dec) {

var key;
var keychar;

if (window.event)
   key = window.event.keyCode;
else if (e)
   key = e.which;
else
   return true;
keychar = String.fromCharCode(key);

// control keys
if ((key==null) || (key==0) || (key==8) || 
    (key==9) || (key==13) || (key==27) )
   return true;

// numbers
else if ((("0123456789.$,").indexOf(keychar) > -1))
   return true;

// decimal point jump
else if (dec && (keychar == "."))
   {
   myfield.form.elements[dec].focus();
   return false;
   }
else
   return false;

}

function altNumber(myfield, e, dec)
{
var key;
var keychar;

if (window.event)
   key = window.event.keyCode;
else if (e)
   key = e.which;
else
   return true;
keychar = String.fromCharCode(key);

// control keys
if ((key==null) || (key==0) || (key==8) || 
    (key==9) || (key==13) || (key==27) )
   return true;

// numbers
else if ((("0123456789.").indexOf(keychar) > -1))
   return true;

// decimal point jump
else if (dec && (keychar == "."))
   {
   myfield.form.elements[dec].focus();
   return false;
   }
else
   return false;
}

function phoneNumber(myfield, e, dec)
{
var key;
var keychar;

if (window.event)
   key = window.event.keyCode;
else if (e)
   key = e.which;
else
   return true;
keychar = String.fromCharCode(key);

// control keys
if ((key==null) || (key==0) || (key==8) || 
    (key==9) || (key==13) || (key==27) )
   return true;

// numbers
else if ((("0123456789()-.").indexOf(keychar) > -1))
   return true;

// decimal point jump
else if (dec && (keychar == "."))
   {
   myfield.form.elements[dec].focus();
   return false;
   }
else
   return false;
}

function wholeNumber(myfield, e, dec)
{
var key;
var keychar;

if (window.event)
   key = window.event.keyCode;
else if (e)
   key = e.which;
else
   return true;
keychar = String.fromCharCode(key);

// control keys
if ((key==null) || (key==0) || (key==8) || 
    (key==9) || (key==13) || (key==27) )
   return true;

// numbers
else if ((("0123456789").indexOf(keychar) > -1))
   return true;

// decimal point jump
else if (dec && (keychar == "."))
   {
   myfield.form.elements[dec].focus();
   return false;
   }
else
   return false;
}
// =========================== 
//
// ===========================

function closeDep() {

if (msgWindow && msgWindow.open && !msgWindow.closed) msgWindow.close();

}


// =========================== 
//
// ===========================

function file_name_only(str) {
   var slash = '/';
   if (str.match(/\\/)) {
      slash = '\\';
   }
   sURL = str.substring(str.lastIndexOf(slash) + 1);
   return sURL;
}

// =========================== 
//
// ===========================

function flip(name,src) {
    if (document.images)
    document.images[name].src = src;
}

// =========================== 
//
// ===========================

function warnEmpty (theField, s) { 
	theField.focus();
	cName = convert_name(s);
    alert('You are missing the following:\n\n'+cName);	
}
// =========================== 
//
// ===========================

function convert_name (s) {
	var v;
	if (s == 'ORGANIZATION')        { v = 'Organization'; }
	if (s == 'TAXID')        { v = 'Tax Id Number'; }
	if (s == 'P_NAME_FIRST')        { v = 'First Name'; }
	if (s == 'P_NAME_LAST')        { v = 'Last Name'; }
	if (s == 'P_EMAIL')        { v = 'Email'; }
	if (s == 'P_TITLE')        { v = 'Title'; }
	if (s == 'P_PHONE')        { v = 'Phone'; }
	if (s == 'P_ADDRESS_1')        { v = 'Address Line 1'; }
	if (s == 'P_CITY')        { v = 'City'; }
	if (s == 'P_STATE')        { v = 'State'; }
	if (s == 'P_ZIPCODE')        { v = 'ZIp Code'; }
	return v;
}
// =========================== 
//
// ===========================

function sumup(form) {

	var num = 0;

	for (i=0;i<form.length;i++) {
		var tempobj=form.elements[i]; 		
		if (tempobj.name.substring(0,9)== "subtotal_") {
			num += Math.abs( tempobj.value.toString().replace(/\$|\,/g,'') );
		}
	}

	form.amount.value = formatCurrency( num );
	
} // CLOSING THE VALIDATE FUNCTION

function validate(form) {
 var valid = true;

 for (i=0;i<form.length;i++) {
 		var tempobj=form.elements[i]; 		
		if (tempobj.name.substring(0,9)== "required_") {
			shortFieldName=tempobj.name.substring(9,100).toUpperCase();
			//alert(shortFieldName);
			// THIS IS TO CHECK TEXT BOXES AND TEXTAREA
			if (tempobj.type == 'text' || tempobj.type == 'textarea' || tempobj.type == 'hidden' || tempobj.type == 'password') {
  	            if (isWhitespace(tempobj.value)) { 
				   if (tempobj.type == 'hidden') { alert('Hidden');valid = false; break; }
  				   warnEmpty(tempobj,shortFieldName); valid = false; break; 
				
				}
			}
			//  THIS IS TO CHECK SELECT BOXES THAT ARE NOT SET TO THE FIRST OPTION
			if (tempobj.type.toString().charAt(0)=="s"&& tempobj.selectedIndex==0) { 
		     warnEmpty(tempobj,shortFieldName); valid = false; break; 
         }
		}
		
		
		if ( tempobj.name == "pict" || tempobj.name == "required_pict" ) {
		   if (!(isWhitespace(tempobj.value))) { 
        //    newWindow('/sms/modules/standard/screens/small-progress.php','progressbar','350','100');		
		   }
		}
  }

    if (!valid) { return false; } else { return true; }

} // CLOSING THE VALIDATE FUNCTION

// =========================== 
//
// ===========================

function newWindow(file,window,wid,hei) {
	var wtop  = (screen.height - hei) / 2;
    var wleft = (screen.width - wid) / 2;
    msgWindow=open(file,window,"toolbar=no,location=no,directories=no,copyhistory=no,resizable=yes,scrollbars=yes,width=" + wid + ",height=" + hei + ",top=" + wtop + ",left=" + wleft + ",screenX=" + wleft + ",screenY=" + wtop);
	msgWindow.focus();
    if (msgWindow.opener == null) msgWindow.opener = self;
}

// =========================== 
//
// ===========================

// function that displays status bar message

function dm(msgStr) {
  document.returnValue = false;
  if (document.images) { 
     window.status = msgStr;
     document.returnValue = true;
  }
}
// functions that swap images

function di(id,name){
  if (document.images) document.images[id].src=eval(name+".src");
}


if (document.images) {
 
   

}
// =========================== 
//
// ===========================
function comboItemSelected(oList1,oList2,cType) {
	if (oList2!=null){
 	    alert('oList2 Not Null and need to clear it out');
		clearComboOrList(oList2);
       if (oList1.selectedIndex==-1){
         oList2.options[oList2.options.length] = new Option('SELECT','');
       } else {
	  // fillCombobox(oList2,cType + '=' + oList1.options[oList1.selectedIndex].value);
	   fillCombobox(oList2,cType + oList1.options[oList1.selectedIndex].value);
	   }
	}
}

function clearComboOrList(oList) {
   for (var i = oList.options.length-1; i>=0 ; i--) {
	   oList.options[i]=null;
   }
   oList.selectedIndex = -1;
   if (oList.onchange) oList.onchange();
}

function fillCombobox(oList,vValue){
   if (vValue!=''){
	   if (assocArray[vValue]){
		   oList.options[0] = new Option('SELECT','');
		   var arrX = assocArray[vValue];
           for ( var i = 0; i<arrX.length ; i = i+2 ){
			   if (arrX[i]!='EOF') oList.options[oList.options.length] = new Option(arrX[i+1],arrX[i]);
           }
           if (oList.options.length == 1){
			   oList.selectedIndex=0;
			   if (oList.onchange) oList.onchange();
           }
       } else {
		 oList.options[0] = new Option('None Selected','');  
	   }
   }
}

function listboxItemSelected(oList1,oList2,cType) {
   if (oList2!=null) {
	   clearComboOrList(oList2);
      if (oList1.selectedIndex==-1) {
         oList2.options[oList2.options.length] = new Option('SELECT','');
      } else {
		 fillListbox(oList2,cType + oList1.options[oList1.selectedIndex].value)
		 // fillListbox(oList2,oList1.name + '=' + oList1.options[oList1.selectedIndex].value)
	  }
   }
   else {

   }
}


function fillListbox(oList,vValue){
   if (vValue!=''){
	   if (assocArray[vValue]){
		   var arrX = assocArray[vValue];
		   for ( var i = 0; i<arrX.length ; i = i+2 ){
			   
			   if (arrX[i]!='EOF') oList.options[oList.options.length] = new Option(arrX[i+1],arrX[i]);

           }
           if (oList.options.length == 1){
			   oList.selectedIndex=0;
			   if (oList.onchange) oList.onchange();
 
           }
       } else {
		 oList.options[0] = new Option('None Selected','');  
	   }

   }
   else {
     	
   }
 
}

//Pop-it menu- By Dynamic Drive

//
// ************************
// layer utility routines *
// ************************

function getStyleObject(objectId) {
    // cross-browser function to get an object's style object given its id
    if(document.getElementById && document.getElementById(objectId)) {
	// W3C DOM
	return document.getElementById(objectId).style;
    } else if (document.all && document.all(objectId)) {
	// MSIE 4 DOM
	return document.all(objectId).style;
    } else if (document.layers && document.layers[objectId]) {
	// NN 4 DOM.. note: this won't find nested layers
	return document.layers[objectId];
    } else {
	return false;
    }
} // getStyleObject

function changeObjectVisibility(objectId, newVisibility) {
    // get a reference to the cross-browser style object and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
	styleObject.visibility = newVisibility;
	return true;
    } else {
	// we couldn't find the object, so we can't change its visibility
	return false;
    }
} // changeObjectVisibility

function moveObject(objectId, newXCoordinate, newYCoordinate) {
    // get a reference to the cross-browser style object and make sure the object exists
    var styleObject = getStyleObject(objectId);
    if(styleObject) {
	styleObject.left = newXCoordinate;
	styleObject.top = newYCoordinate;
	return true;
    } else {
	// we couldn't find the object, so we can't very well move it
	return false;
    }
} // moveObject

// ********************************
// application-specific functions *
// ********************************

// store variables to control where the popup will appear relative to the cursor position
// positive numbers are below and to the right of the cursor, negative numbers are above and to the left
function showPopup (targetObjectId, eventObj, xOffset, yOffset ) {
    if(eventObj) {
	// hide any currently-visible popups
	hideCurrentPopup();
	// stop event from bubbling up any farther
	eventObj.cancelBubble = true;
	// move popup div to current cursor position 
	// (add scrollTop to account for scrolling for IE)
	var newXCoordinate = (eventObj.pageX)?eventObj.pageX + xOffset:eventObj.x + xOffset + ((document.body.scrollLeft)?document.body.scrollLeft:0);
	var newYCoordinate = (eventObj.pageY)?eventObj.pageY + yOffset:eventObj.y + yOffset + ((document.body.scrollTop)?document.body.scrollTop:0);
	moveObject(targetObjectId, newXCoordinate, newYCoordinate);
	// and make it visible
	if( changeObjectVisibility(targetObjectId, 'visible') ) {
	    // if we successfully showed the popup
	    // store its Id on a globally-accessible object
	    window.currentlyVisiblePopup = targetObjectId;
	    return true;
	} else {
	    // we couldn't show the popup, boo hoo!
	    return false;
	}
    } else {
	// there was no event object, so we won't be able to position anything, so give up
	return false;
    }
} // showPopup

function hideCurrentPopup() {
    // note: we've stored the currently-visible popup on the global object window.currentlyVisiblePopup
    if(window.currentlyVisiblePopup) {
	changeObjectVisibility(window.currentlyVisiblePopup, 'hidden');
	window.currentlyVisiblePopup = false;
    }
} // hideCurrentPopup
function cent(amount) {
   amount -= 0;
   amount = (Math.round(amount*100))/100;
 return (amount == Math.floor(amount)) ? amount + '.00' : (  (amount*10 == Math.floor(amount*10)) ? amount + '0' : amount);
}

function formatCurrency(num) {
num = num.toString().replace(/\$|\,/g,'');
if(isNaN(num))
num = "0";
sign = (num == (num = Math.abs(num)));
num = Math.floor(num*100+0.50000000001);
cents = num%100;
num = Math.floor(num/100).toString();
if(cents<10)
cents = "0" + cents;
for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
num = num.substring(0,num.length-(4*i+3))+','+
num.substring(num.length-(4*i+3));
return (((sign)?'':'-') + '$' + num + '.' + cents);
}

