var defaultEmptyOK = false


// name - name of the cookie
// value - value of the cookie
// [expires] - expiration date of the cookie (defaults to end of current session)
// [path] - path for which the cookie is valid (defaults to path of calling document)
// [domain] - domain for which the cookie is valid (defaults to domain of calling document)
// [secure] - Boolean value indicating if the cookie transmission requires a secure transmission
// * an argument defaults when it is assigned null as a placeholder
// * a null placeholder is not required for trailing omitted arguments
function setCookie(name, value, expires, path, domain, secure) {
  var curCookie = name + "=" + escape(value) +
      ((expires) ? "; expires=" + expires.toGMTString() : "") +
      ((path) ? "; path=" + path : "") +
      ((domain) ? "; domain=" + domain : "") +
      ((secure) ? "; secure" : "");
  document.cookie = curCookie;
}

// name - name of the desired cookie
// * return string containing value of specified cookie or null if cookie does not exist
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));
}

// name - name of the cookie
// [path] - path of the cookie (must be same as path used to create cookie)
// [domain] - domain of the cookie (must be same as domain used to create cookie)
// * path and domain default if assigned null or omitted if no explicit argument proceeds
function deleteCookie(name, path, domain) {
  if (getCookie(name)) {
    document.cookie = name + "=" + 
    ((path) ? "; path=" + path : "") +
    ((domain) ? "; domain=" + domain : "") +
    "; expires=Thu, 01-Jan-70 00:00:01 GMT";
  }
}

// date - any instance of the Date object
// * hand all instances of the Date object to this function for "repairs"
function fixDate(date) {
  var base = new Date(0);
  var skew = base.getTime();
  if (skew > 0)
    date.setTime(date.getTime() - skew);
}


function getQueryVariable(variable) {
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {
	var pair = vars[i].split("=");
	if (pair && pair[0] && pair[1] && pair[0] == variable) {
	  return unescape(pair[1]);
	}
  }
}

function getValues(objForm){
	 for(i=0;objForm && objForm.elements && i<objForm.elements.length;i++){

		alert(document.write(objForm.elements[i].type + " : " + objForm.elements[i].name + " = " + objForm.elements[i].value));


	 }
}

function getCheckedValues (select) {
  var r = new Array();
  for (var i = 0; i < select.length; i++)
	if (select[i].checked)
	  r[r.length] = select[i].value;
  return r;
}

function getSelectedValues (select) {
  var r = new Array();
  for (var i = 0; i < select.length; i++)
	if (select[i].selected)
	  r[r.length] = select[i].value;
  return r;
}

function onSubmit(me, pollUrl, siteID, pollID, catID, pID, type) {
	me.action=pollUrl+"&answer="+getSelectedValues(me.answer);
	getValues(me);

	return false;
	//alert(me.answer.selectedIndex);
	//eval("submitIT_"+pollID+"()");
}

function show(strURL, width, height) {
    	if (document.all)
    	    var xMax = screen.width, yMax = screen.height;
    	else
    	    if (document.layers)
    	        var xMax = window.outerWidth, yMax = window.outerHeight;
    	    else
    	        var xMax = 640, yMax=480;

    	var xOffset = (xMax - 200)/2, yOffset = (yMax - 200)/2;
    
	vWin = window.open(strURL,'popup',
		'width='+width+',height='+height+',screenX='+xOffset+',screenY='+yOffset+',top='+yOffset+',left='+xOffset+',status=0,resizable=0,scrollbars=1');

}

function checkTextFieldLimit(field, maxlimit, msg) {
	if (field.value.length > maxlimit) { // if too long...trim it!
		field.value = field.value.substring(0, maxlimit);
		if (msg.length>0) {
			var var_pos = msg.indexOf("$var");
			if (var_pos>0)
				msg = msg.substring(0,var_pos) + maxlimit + msg.substring(var_pos+"$var".length,msg.length);
			//alert(msg);
			field.focus();
		}
	}
}

function isValidEmail(emailStr) {

	if (isEmpty(emailStr)) 
       		if (isValidEmail.arguments.length == 1) return defaultEmptyOK;
        else return (isValidEmail.arguments[1] == true);

	var emailPat=/^(.+)@(.+)$/
	
	var specialChars="\\(\\)<>@,;:\\\\\\\"\\.\\[\\]"
	
	var validChars="\[^\\s" + specialChars + "\]"
	
	var quotedUser="(\"[^\"]*\")"
	
	var ipDomainPat=/^\[(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})\]$/
	
	var atom=validChars + '+'
	
	var word="(" + atom + "|" + quotedUser + ")"
	
	var userPat=new RegExp("^" + word + "(\\." + word + ")*$")
	
	var domainPat=new RegExp("^" + atom + "(\\." + atom +")*$")
	
	var matchArray=emailStr.match(emailPat)

	if (matchArray==null) {

		alert("E-mail address is incorrect (check @ and .'s).");

		return false;
	
	}

	var user=matchArray[1]
	
	var domain=matchArray[2]

	if (user.match(userPat)==" ") {

		alert("The username is not valid.");

		return false;
    	
	}

	var IPArray=domain.match(ipDomainPat)

	if (IPArray!=null) {

		for (var i = 1; i <= 4; i++) {

		if (IPArray[i] > 255) {

			alert("Destination IP address is not valid.");

			return false;

			}
		}

		return true;

	}

	var domainArray=domain.match(domainPat)

	if (domainArray==null) {

		alert("The domain name is not valid.");

		return false;
	
	}

	var atomPat=new RegExp(atom,"g")
	
	var domArr=domain.match(atomPat)
	
	var len=domArr.length

	if (domArr[domArr.length-1].length < 2 || domArr[domArr.length-1].length > 4) {

		alert("The address must end in a two letter country, or four letter domain.");

		return false;
   	
	}

	return true;

}

function isEmpty(inputStr) {
	
	if (inputStr == "" || inputStr == null) {

	return true;

	}

	return false;
			
}
	
