// 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*$)/;

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

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 intgerOnly(i) {
	if(i.value.length>0) {
       i.value = i.value.replace(/[^\d|\.|\,]+/g, ''); 
	}
}
// =========================== 
//
// ===========================

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 == 'NAME')        { v = 'Name'; }
	if (s == 'TITLE')       { v = 'Title'; }
	if (s == 'NAME_FIRST')  { v = 'First Name'; }
	if (s == 'NAME_LAST')   { v = 'Last Name'; }
	if (s == 'HANDLE')      { v = 'Username'; }
	if (s == 'PASSWD')      { v = 'Password'; }
	if (s == 'EMAIL')       { v = 'Email Address'; }
	if (s == 'LINE1')       { v = 'Address Line 1'; }
	if (s == 'LINE2')       { v = 'Address Line 2'; }
	if (s == 'LINE3')       { v = 'Address Line 3'; }
	if (s == 'CITY')        { v = 'City'; }
	if (s == 'ZIPCODE')     { v = 'Postal Code'; }
	if (s == 'PHONE')       { v = 'Phone Number'; }
	if (s == 'MSG')         { v = 'Message or Description'; }

	return v;
}
// =========================== 
//
// ===========================

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,30).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,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



// ***********************
// hacks and workarounds *
// ***********************

function explorerMacResizeFix () {
    location.reload(false);
}

// initialize hacks whenever the page loads
window.onload = initializeHacks;

// setup an event handler to hide popups for generic clicks on the document
//document.onclick = hideCurrentPopup;

function initializeHacks() {
    // this ugly little hack resizes a blank div to make sure you can click
    // anywhere in the window for Mac MSIE 5
    if ((navigator.appVersion.indexOf('MSIE 5') != -1) 
	&& (navigator.platform.indexOf('Mac') != -1)
	&& getStyleObject('blankDiv')) {
	window.onresize = explorerMacResizeFix;
    }
    resizeBlankDiv();
    // this next function creates a placeholder object for older browsers
    createFakeEventObj();
}

function createFakeEventObj() {
    // create a fake event object for older browsers to avoid errors in function call
    // when we need to pass the event object to functions
    if (!window.event) {
	window.event = false;
    }
} // createFakeEventObj

function resizeBlankDiv() {
    // resize blank placeholder div so IE 5 on mac will get all clicks in window
    if ((navigator.appVersion.indexOf('MSIE 5') != -1) 
	&& (navigator.platform.indexOf('Mac') != -1)
	&& getStyleObject('blankDiv')) {
	getStyleObject('blankDiv').width = document.body.clientWidth - 20;
	getStyleObject('blankDiv').height = document.body.clientHeight - 20;
    }
}


function correctPNG() // correctly handle PNG transparency in Win IE 5.5 & 6.
{
   var arVersion = navigator.appVersion.split("MSIE")
   var version = parseFloat(arVersion[1])
   if ((version >= 5.5) && (document.body.filters)) 
   {
      for(var i=0; i<document.images.length; i++)
      {
         var img = document.images[i]
         var imgName = img.src.toUpperCase()
         if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
         {
            var imgID = (img.id) ? "id='" + img.id + "' " : ""
            var imgClass = (img.className) ? "class='" + img.className + "' " : ""
            var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
            var imgStyle = "display:inline-block;" + img.style.cssText 
            if (img.align == "left") imgStyle = "float:left;" + imgStyle
            if (img.align == "right") imgStyle = "float:right;" + imgStyle
            if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
            var strNewHTML = "<span " + imgID + imgClass + imgTitle
            + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
            + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
            + "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
            img.outerHTML = strNewHTML
            i = i-1
         }
      }
   }    
}

if (window.attachEvent)
{
   window.attachEvent("onload", correctPNG);
}

function getState(countryId)
{
   var strURL=ÓfindState.php?country=Ó+countryId;
   var req = getXMLHTTP();
   if (req)
   {
     req.onreadystatechange = function()
     {
      if (req.readyState == 4)
      {
	 // only if ÒOKÓ
	 if (req.status == 200)
         {
	    document.getElementById(ÕstatedivÕ).innerHTML=req.responseText;
	 } else {
   	   alert(ÓThere was a problem while using XMLHTTP:\nÓ + req.statusText);
	 }
       }
      }
   req.open(ÓGETÓ, strURL, true);
   req.send(null);
   }
}

function getCity(countryId,stateId)
{
  var strURL=ÓfindCity.php?country=Ó+countryId+Ó&state=Ó+stateId;
  var req = getXMLHTTP();
  if (req)
  {
    req.onreadystatechange = function()
    {
      if (req.readyState == 4) // only if ÒOKÓ
      {
        if (req.status == 200)
        {
          document.getElementById(ÕcitydivÕ).innerHTML=req.responseText;
        } else {
          alert(ÓThere was a problem while using XMLHTTP:\nÓ + req.statusText);
        }
      }
    }
    req.open(ÓGETÓ, strURL, true);
    req.send(null);
  }
}