// functions to handle client-side interaction
function changeHTML(id,strContent){
	if(document.getElementById(id)){
		document.getElementById(id).innerHTML = strContent;	
	}
}
function changeValue(id,strValue){
	if(document.getElementById(id)){
		document.getElementById(id).value = strValue;		
	}
}
function getValue(id){
	if(document.getElementById(id)){
		return document.getElementById(id).value;		
	}else{
		return '';	
	}
}
function show(id){
	if(document.getElementById(id)){
		document.getElementById(id).style.display = "block";
	}
}

function showinline(id){
	if(document.getElementById(id)){
		document.getElementById(id).style.display = "inline";
	}
}

function hide(id){
	if(document.getElementById(id)){
		document.getElementById(id).style.display = "none";
	}
}

function fnSetVal(id,strVal){
	// function to check if element exists
	// if element exists, then determine what type of element it is
	// set value of element depending on type
	objInput = document.getElementById(id);
	if(objInput){
		// element exists	
		if(objInput.type){
			// text boxes/button
			if(objInput.type == "text" || objInput.type == "textarea" || objInput.type == "button"){
				objInput.value = strVal;
			}
			// checkbox
			else if(objInput.type == "checkbox"){
				objInput.checked = strVal;
			}
			// select
			else if(objInput.type == "select-one"){
				fnSetSel(objInput,strVal);
			}
		}
	}
}
	

function fnSwitch(id){
	if(document.getElementById(id)){
		if(document.getElementById(id).style.display == "block"){
			document.getElementById(id).style.display = "none";
		}else{
			document.getElementById(id).style.display = "block";
		}
	}
}

function fnSetSel(sel,val){
	if(sel){
		//DEBUG CODE
		//==========
		//alert("setting " + sel + " to " + val)
		//==========			
		for(var i = 0; i < sel.options.length; i++){
		   if (sel.options[i].value == val){
			  sel.selectedIndex = i;
				//DEBUG CODE
				//==========
				//alert(sel + " set to " + val)
				//==========			
			  return;
		   }
		}
	}
}

function SetSel(sel,val){
	fnSetSel(sel,val);	
}

var popUpWin=0;
function popUpWindow(URLStr, width, height){
  var left = (screen.width/2) - width/2;
  var top = (screen.height/2) - height/2;
  if(popUpWin){
		if(!popUpWin.closed) popUpWin.close();
  }
  popUpWin = open(URLStr, 'popUpWin', 'toolbar=no,location=no,directories=no,status=no,menub ar=no,scrollbar=no,resizable=no,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');
}

function uploadFile(currentFile,bizcardName,bizcardID,bizcardSize){
	popUpWindow("uploadFile.asp?utype=bizcard&clientid=<%=qsClientID%>&bizcardid=" + bizcardID + "&name=" + bizcardName + "&size=" + bizcardSize + "&file=" + currentFile ,400,250);
}
function viewFile(filename){
	popUpWindow(filename,400,400);	
}

// email validation procedure
function emailCheck (emailStr) {

var checkempty=emailStr;

if (checkempty!="") {

/* The following variable tells the rest of the function whether or not
to verify that the address ends in a two-letter country or well-known
TLD.  1 means check it, 0 means don't. */

var checkTLD=1;

/* The following is the list of known TLDs that an e-mail address must end with. */

var knownDomsPat=/^(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum)$/;

/* The following pattern is used to check if the entered e-mail address
fits the user@domain format.  It also is used to separate the username
from the domain. */

var emailPat=/^(.+)@(.+)$/;

/* The following string represents the pattern for matching all special
characters.  We don't want to allow special characters in the address. 
These characters include ( ) < > @ , ; : \ " . [ ] */

var specialChars="\\(\\)><@,;:\\\\\\\"\\.\\[\\]";

/* The following string represents the range of characters allowed in a 
username or domainname.  It really states which chars aren't allowed.*/

var validChars="\[^\\s" + specialChars + "\]";

/* The following pattern applies if the "user" is a quoted string (in
which case, there are no rules about which characters are allowed
and which aren't; anything goes).  E.g. "jiminy cricket"@disney.com
is a legal e-mail address. */

var quotedUser="(\"[^\"]*\")";

/* The following pattern applies for domains that are IP addresses,
rather than symbolic names.  E.g. joe@[123.124.233.4] is a legal
e-mail address. NOTE: The square brackets are required. */

var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/;

/* The following string represents an atom (basically a series of non-special characters.) */

var atom=validChars + '+';

/* The following string represents one word in the typical username.
For example, in john.doe@somewhere.com, john and doe are words.
Basically, a word is either an atom or quoted string. */

var word="(" + atom + "|" + quotedUser + ")";

// The following pattern describes the structure of the user

var userPat=new RegExp("^" + word + "(\\." + word + ")*$");

/* The following pattern describes the structure of a normal symbolic
domain, as opposed to ipDomainPat, shown above. */

var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$");

/* Finally, let's start trying to figure out if the supplied address is valid. */

/* Begin with the coarse pattern to simply break up user@domain into
different pieces that are easy to analyze. */

var matchArray=emailStr.match(emailPat);

if (matchArray==null) {

/* Too many/few @'s or something; basically, this address doesn't
even fit the general mould of a valid e-mail address. */

alert("Email address seems incorrect (check @ and .'s)");
form1.emailaddress.focus();
return false;
}
var user=matchArray[1];
var domain=matchArray[2];

// Start by checking that only basic ASCII characters are in the strings (0-127).

for (i=0; i<user.length; i++) {
if (user.charCodeAt(i)>127) {
alert("Email address contains invalid characters.");
form1.emailaddress.focus();
return false;
   }
}
for (i=0; i<domain.length; i++) {
if (domain.charCodeAt(i)>127) {
alert("Ths domain name of the email address contains invalid characters.");
form1.emailaddress.focus();
return false;
   }
}

// See if "user" is valid 

if (user.match(userPat)==null) {

// user is not valid

alert("The email address is incomplete.");
form1.emailaddress.focus();
return false;
}

/* if the e-mail address is at an IP address (as opposed to a symbolic
host name) make sure the IP address is valid. */

var IPArray=domain.match(ipDomainPat);
if (IPArray!=null) {

// this is an IP address

for (var i=1;i<=4;i++) {
if (IPArray[i]>255) {
alert("Email address is invalid!");
form1.emailaddress.focus();
return false;
   }
}
return true;
}

// Domain is symbolic name.  Check if it's valid.
 
var atomPat=new RegExp("^" + atom + "$");
var domArr=domain.split(".");
var len=domArr.length;
for (i=0;i<len;i++) {
if (domArr[i].search(atomPat)==-1) {
alert("The email address domain name does not seem to be valid.");
form1.emailaddress.focus();
return false;
   }
}

/* domain name seems valid, but now make sure that it ends in a
known top-level domain (like com, edu, gov) or a two-letter word,
representing country (uk, nl), and that there's a hostname preceding 
the domain or country. */

if (checkTLD && domArr[domArr.length-1].length!=2 && 
domArr[domArr.length-1].search(knownDomsPat)==-1) {
alert("The email address must end in a well-known domain or two letter " + "country.");
form1.emailaddress.focus();
return false;
}

// Make sure there's a host name preceding the domain.

if (len<2) {
alert("This email address address is missing a hostname!");
form1.emailaddress.focus();
return false;
}

// If we've gotten this far, everything's valid!
return true;
}
return true;
}

function printview(orderid,strType){
	var width="700", height="500";
	var left = (screen.width/2) - width/2;
	var top = (screen.height/2) - height/2;
	var styleStr = 'toolbar=no,location=no,directories=no,status=no,menubar=yes,scrollbars=yes,resizable=yes,copyhistory=yes,width='+width+',height='+height+',left='+left+',top='+top+',screenX='+left+',screenY='+top;
	var printview_window = window.open(strType + "orderdetailsprintview.asp?reordernbr=" + orderid,"printview_window", styleStr);
}

function statusprintview(strType){
	var width="750", height="500";
	var left = (screen.width/2) - width/2;
	var top = (screen.height/2) - height/2;
	var styleStr = 'toolbar=no,location=no,directories=no,status=no,menubar=yes,scrollbars=yes,resizable=yes,copyhistory=yes,width='+width+',height='+height+',left='+left+',top='+top+',screenX='+left+',screenY='+top;
	var printview_window = window.open(strType + "_statusprintview.asp","printview_window", styleStr);
}

function buildselect(id, selectclass, optionlist, selecteditem, onclickaction){

	field = '';
	
	field += '<select id="' + id + '_edit" class="'
		+ selectclass + '" name="' + id + '_edit" onChange="' + onclickaction + '">\n';

	for(var i in optionlist) {
		field += '<option id="' + i + '" value="' + i + '"';

		if(optionlist[i] == selecteditem) {
			field += ' selected="selected"';
		}

		field += '>' + optionlist[i] + '</option>\n';
	}

	field += '</select>\n';

	return(field);	
}


function EnterPressed(e) {
	// Code adapted from Jennifer Madden
	// http://jennifermadden.com/162/examples/stringEnterKeyDetector.html

	var characterCode
	if(e && e.which){           // NN4 specific code
		e = e
		characterCode = e.which
	}
	else {
		e = event
		characterCode = e.keyCode // IE specific code
	}
	if (characterCode == 13) return true   // Enter key is 13
	else return false
}