
function refreshMain() {

   if (window.opener) {
      window.opener.location.reload();
   }
}

var newWind;

function popUp(URL,WIDTH,HEIGHT) {

   newWind = window.open(URL,"POPUP","HEIGHT="+HEIGHT+",WIDTH="+WIDTH+",scrollbars=yes,resizable=yes,alwaysraised=yes");

//   if (newWind.opener=null) {
      newWind.opener = window;
//   }

   newWind.moveTo(200,200);

}

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

var LOGINWINVAR;
var EDITWINVAR;
var WINS = new Array();
var imgid;

function getLogin(DBNAME) {
   var cookie=getCookie(DBNAME);
   if (cookie) {
      title="Account Information & Settings";
      width=600;
      height=400;
   } else {
      title="Please Login:";
      width=300;
      height=200;
   }
   //alert("starting login win with dbname=" + DBNAME);
   LOGINWINVAR = dhtmlmodal.open('LOGINWIN','iframe','/cgi-bin/login.pl?DBNAME='+DBNAME,title,'width='+width+'px,height='+height+'px,center=1,resize=0,scrolling=0','recal');
   //LOGINWINVAR.moveTo('middle','middle');
   LOGINWINVAR.onclose=function() {location.reload();return true;}

}

function editWin(URL) {
   //alert("starting login win with dbname=" + DBNAME);
   EDITWINVAR = dhtmlwindow.open('EDITWIN','iframe',URL,'WMS Editor:','width=800px,height=600px,center=1,resize=0,scrolling=0','recal');
   EDITWINVAR.moveTo('middle','middle');
   EDITWINVAR.onclose=function() {location.reload();return true;}

}

function showHide(URL,ID) {
   //alert("Showing " + URL + "in element "+ID);
   if (document.getElementById(ID).innerHTML == "") {
      document.getElementById(ID).style.display = "block";
      makeRequest(URL,ID)
   } else {
      document.getElementById(ID).innerHTML="";
      document.getElementById(ID).style.display = "none";
   }
}

function getPicID(DBNAME,ID,SHEET,FIELD,RECID) {
   //alert("DBNAME " + DBNAME + " and ID" + ID);

   imgid = dhtmlmodal.open('GetPIC','iframe','/wms/wmsimgid&COND=customer.schema='+DBNAME,'Select Image:','width=640px,height=480px,center=1,resize=1,scrolling=1','recal');
   imgid.moveTo('middle','middle');
   imgid.onclose=function(){
      var imagePath;
      imagePath=this.contentDoc.getElementById('returnHTML').innerHTML;
      alert("Selected Picture Number "+ imagePath);
      if ((DBNAME) && (SHEET) && (FIELD) && (RECID)) {
         id(ID).value = imagePath; 
         id(ID).src = "/PIC-SIZE/"+imagePath+"-90.jpg";
         alert("Updating Picid on record using AJAX");
         //alert('/cgi-bin/AJAXFORM.pl?DBNAME='+DBNAME+'&SCRIPT=true&ACTION=Update&SHEETNAME='+SHEET+'&FIELD='+FIELD+'&ID='+RECID+'&data='+imagePath);
         makeRequest('/cgi-bin/AJAXFORM.pl?DBNAME='+DBNAME+'&SCRIPT=true&ACTION=Update&SHEETNAME='+SHEET+'&FIELD='+FIELD+'&ID='+RECID+'&data='+imagePath,ID+'_upd','Silent');
      } else {
         id(ID+"_pic").value = imagePath; 
         id(ID+"_pic").src = "/PIC-SIZE/"+imagePath+"-90.jpg";
         alert("Setting Picid Field, save changes to update record");
         id(ID).value=imagePath;
      }
      return true;
   };
}

function openWin(URL,ID,NAME,SIZE,RELOAD) {

   if (SIZE > 0 && SIZE <= 100) {
      var Width = document.body.clientWidth* SIZE /100;
      var Height = document.body.clientHeight * SIZE / 100;
   } else {
      var sizes = SIZE.split("x");
      //alert("WIDTH="+sizes[0]+" HEIGHT="+sizes[1]);
      var Width = sizes[0];
      var Height = sizes[1];
   }
   //alert("Opening " + URL + "ID("+ID+") Name:"+ NAME + "Size: " + SIZE + "DIM:" + Width + "x" + Height);

   if (RELOAD == "reload") {
      WINS[ID] = dhtmlwindow.open('TOOL'+ID,'iframe',URL,NAME,'width='+Width+'px,height='+Height+'px,center=1,resize=1,scrolling=1','recal');
      WINS[ID].moveTo('middle','middle');
      WINS[ID].onclose=function(){
         //alert ("closing " + WINS[ID]);
         document.location.reload();
         return true;
      };
   } else {
      WINS[ID] = dhtmlwindow.open('TOOL'+ID,'iframe',URL,NAME,'width='+Width+'px,height='+Height+'px,center=1,resize=1,scrolling=1','recal');
      WINS[ID].moveTo('middle','middle');
   }
}

function id (id) {
   return document.getElementById(id);
}

function $ (id) {
   return document.getElementById(id);
}
/*
	parseUri 1.2.1
	(c) 2007 Steven Levithan <stevenlevithan.com>
	MIT License
*/

function parseUri (str) {
	var	o   = parseUri.options,
		m   = o.parser[o.strictMode ? "strict" : "loose"].exec(str),
		uri = {},
		i   = 14;

	while (i--) uri[o.key[i]] = m[i] || "";

	uri[o.q.name] = {};
	uri[o.key[12]].replace(o.q.parser, function ($0, $1, $2) {
		if ($1) uri[o.q.name][$1] = $2;
	});

	return uri;
};

parseUri.options = {
	strictMode: false,
	key: ["source","protocol","authority","userInfo","user","password","host","port","relative","path","directory","file","query","anchor"],
	q:   {
		name:   "queryKey",
		parser: /(?:^|&)([^&=]*)=?([^&]*)/g
	},
	parser: {
		strict: /^(?:([^:\/?#]+):)?(?:\/\/((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?))?((((?:[^?#\/]*\/)*)([^?#]*))(?:\?([^#]*))?(?:#(.*))?)/,
		loose:  /^(?:(?![^:@]+:[^:@\/]*@)([^:\/?#.]+):)?(?:\/\/)?((?:(([^:@]*):?([^:@]*))?@)?([^:\/?#]*)(?::(\d*))?)(((\/(?:[^?#](?![^?#\/]*\.[^?#\/.]+(?:[?#]|$)))*\/?)?([^?#\/]*))(?:\?([^#]*))?(?:#(.*))?)/
	}
};



// Read a page's GET URL variables and return them as an associative array.
function getUrlVars() {
   var vars = [], hash;
   var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');

   for(var i = 0; i < hashes.length; i++) {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
     
   }
   return vars;
}

function validateMatch(fld1,fld2) {
    var error = "";
    
    if ((fld1.value != fld2.value) || (fld1.value == "")) {
        fld1.style.background = 'Yellow'; 
        fld2.style.background = 'Yellow'; 
        error = fld1.name+" and "+fld2.name+ " must match.\n"
    } else {
        fld1.style.background = 'White';
        fld2.style.background = 'White';
    }
    return error;  

}

function validateEmpty(fld) {
    var error = "";
 
    if (fld.value.length == 0) {
        fld.style.background = 'Yellow'; 
        error = fld.name + " is required.\n"
    } else {
        fld.style.background = 'White';
    }
    return error;  
}

function trim(s) {
  
  return s.replace(/^\s+|\s+$/, '');

}

function validateEmail(fld) {
    var error="";
    var tfld = trim(fld.value);                        // value of field with whitespace trimmed off
    var emailFilter = /^[^@]+@[^@.]+\.[^@]*\w\w$/ ;
    var illegalChars= /[\(\)\<\>\,\;\:\\\"\[\]]/ ;
   
    if (fld.value == "") {
        fld.style.background = 'Yellow';
        error = "You didn't enter an email address.\n";
    } else if (!emailFilter.test(tfld)) {              //test email for illegal characters
        fld.style.background = 'Yellow';
        error = "Please enter a valid email address.\n";
    } else if (fld.value.match(illegalChars)) {
        fld.style.background = 'Yellow';
        error = "The email address contains illegal characters.\n";
    } else {
        fld.style.background = 'White';
    }
    return error;
}

function valiDate(fld) {
    var error="";
    var tfld = trim(fld.value);      // value of field with whitespace trimmed off
    var pattern = /(19|20)[0-9]{2}-(0|1)[0-9]-[0-3][0-9]/

    if (fld.value.length != 0) {
      if (fld.value.match(pattern)) {
        fld.style.background = 'White';
      } else {
        fld.style.background = 'Yellow';
        error= fld.name + " is not a valid Date.\n";
      }
    } else {
      fld.style.background = 'Yellow';
      error = fld.name + " is required.\n";
    }
    return error;
}

function validateTime(fld) {
    var error="";
    var tfld = trim(fld.value);      // value of field with whitespace trimmed off
    var pattern = /(^([0-9]|[0-1][0-9]|[2][0-3]):([0-5][0-9])$)|(^([0-9]|[1][0-2]):([0-5][0-9]) ([A|a|P|p][{M|m])$)/

    if (fld.value.length != 0) {
      if (fld.value.match(pattern)) {
        fld.style.background = 'White';
      } else {
        fld.style.background = 'Yellow';
        error= fld.name + " is not a valid Time Format.\n";
      }
    } else {
      fld.style.background = 'Yellow';
      error = fld.name + " is required.\n";
    }
    return error;
}

function validateNumber(fld) {
    var error="";
    var tfld = trim(fld.value);      // value of field with whitespace trimmed off

    if (fld.value.length != 0) {
      if (parseInt(fld.value)) {
        fld.style.background = 'White';
      } else {
        fld.style.background = 'Yellow';
        error= fld.name + " is not a valid number.\n";
      }
    } else {
      fld.style.background = 'Yellow';
      error = fld.name + " is required.\n";
    }
    return error;
}


