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

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


var regWhitespace                = /^\s+$/;

// =========================== 
// FUNCTIONS
// ===========================

function isEmpty(s)       { return ((s == null) || (s.length == 0)) }
function isWhitespace (s) { return (isEmpty(s)  || regWhitespace.test(s)); }
// =========================== 
//
// ===========================

function addtoList( oList1, oList2, cHidden ) {
var i;
var isNew = true;
var cTemp = '';

   if (oList2.length != 0) {
      for (i = 0; i < oList2.length; i++) {
         thisitem = oList2.options[i].text;
         if (thisitem == oList1.options[oList1.selectedIndex].text ) {
            isNew = false;
            break;
         }
      }
   } 
 
   if (isNew) {
      newoption = new Option( oList1.options[oList1.selectedIndex].text , oList1.options[oList1.selectedIndex].value, false, false);
      oList2.options[oList2.length] = newoption;
   }
   
  for (i = 0; i < oList2.length; i++) {
     cTemp += ','+oList2.options[i].value;
  }
  cHidden.value = cTemp;
  oList1.selectedIndex=-1;
}
// =========================== 
//
// ===========================
function removeFromList( oList, cHidden ) {
  var boxLength = oList.length;  
   arrSelected = new Array();
   var count = 0;
   for (i = 0; i < boxLength; i++) {
      if ( oList.options[i].selected) {
         arrSelected[count] = oList.options[i].value; 
      }
      count++;
   }
   var x;
   for (i = 0; i < boxLength; i++) {
      for (x = 0; x < arrSelected.length; x++) {
         if ( oList.options[i].value == arrSelected[x]) {
            oList.options[i] = null;
         }
      }
      boxLength = oList.length;
   }
   
  for (i = 0; i < oList.length; i++) {
    oList.options[i].selected = true;
  }
}
// =========================== 
//
// ===========================

function toggle(img, node) {
    var target = document.getElementById(node);
    if (target.style.display == "none") {
    target.style.display = "block";
    } else {
     target.style.display = "none";
    }
}

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

function SimpleDate(date){
	if (date == null){
		this.dateValue = new Date();
	}else if (date instanceof Date){
		this.dateValue = date;
	}else{
		var longValue = parseInt(date, 10);
	 	this.dateValue = new Date(longValue);
	}
}

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

function toggle_folder(img, node) {
    
    var cookie = readCookie( node );
    
    if ( cookie == '1' ) {
       createCookie(node , 0, 1);
    } else {
       createCookie(node , 1, 1);
    }
    
    var target = document.getElementById(node);
    if (target.style.display == "none") {
    target.style.display = "block";
    img.src = "images/openfolder.gif";
    } else {
     target.style.display = "none";
     img.src = "images/closedfolder.gif";
    }
}

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

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

function warnEmpty (theField, s) { 
	theField.focus();
 	alert('EMPTY:\n'+convert_name(s));  
}

// =========================== 
//
// ===========================
function intgerOnly(i) {
	if(i.value.length>0) {
       i.value = i.value.replace(/[^\d|\.|\,]+/g, ''); 
	}
}
// =========================== 
//
// ===========================

function convert_name (s) {
	var v;
	if (s == 'NAME')      { v = 'Name'; }
	if (s == 'TITLE')     { v = 'Title'; }
	if (s == 'FNAME')     { v = 'First Name'; }
	if (s == 'LNAME')     { v = 'Last Name'; }
	if (s == 'HANDLE')    { v = 'User ID'; }
	if (s == 'EMAIL')     { v = 'Email Address'; }
	if (s == 'LINE1')     { v = 'Address Line 1'; }
	if (s == 'LINE2')     { v = 'Address Line 2'; }
	if (s == 'CITY')      { v = 'City'; }
	if (s == 'ZIPCODE')   { v = 'Postal Code'; }
	if (s == 'AMOUNT')    { v = 'Amount'; }
	if (s == 'PASSWD')    { v = 'Password'; }
	if (s == 'PHONE')     { v = 'Phone Number'; }
	if (s == 'MESSAGE')   { v = 'Message or Note Field'; }
	if (s == 'MSG')       { v = 'Message or Description'; }
   if (s == 'PART')      { v = 'Part Inventory Number'; }
	if (s == 'ACCOUNT')   { v = 'Account/Company Name'; }
	if (s == 'COMPANY')   { v = 'Account/Company Name'; }
	if (s == 'SUBJECT')   { v = 'Subject'; }
 	if (s == 'SEARCH')    { v = 'Search Parameter'; }
 	if (s == 'REASON')    { v = 'Reason Code'; }
 	if (s == 'DATE')      { v = 'Date'; }
   if (s == 'SKU')       { v = 'SKU'; }
   if (s == 'TRACKING')  { v = 'Tracking Number'; }
   if (s == 'BIN')       { v = 'Bin Location'; }
   if (s == 'USERID')    { v = 'User'; }
   if (s == 'CODE')      { v = 'Code'; }
   if (s == 'AMOUNT')     { v = 'Amount'; }
   if (s == 'CCNAME')     { v = 'Name on Credit Card'; }
   if (s == 'CCNUM')     { v = 'Credit Card Number'; }
   if (s == 'CCLINE1')     { v = 'Credit Card Billing Address'; }
   if (s == 'CCCITY')     { v = 'Credit Card Billing City'; }
   if (s == 'CCSTATE')     { v = 'Credit Card Billing   '; }
   if (s == 'CCCOUNTRY')     { v = 'Credit Card Number'; }
   if (s == 'CCCVV2')     { v = 'Credit Card Security Code'; }
   if (s == 'CCZIPCODE')     { v = 'Zip Code'; }
   
	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('/stagedoor/modules/screens/small-progress.php','progressbar','400','75');		
		   }
		}
  }

    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");
}

// =========================== 
//
// ===========================
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 {
     	
   }
 
}

