/*	
	#$Id: foundation.js,v 1.4 2003/09/12 17:23:48 elia Exp $
	________________________________________________________________________________	
	Copyright © 2000-2003 Casale Media													
	All Rights Reserved.																
	________________________________________________________________________________	
	Legal Notice																		
	The information contained within this document is confidential, copyrighted			
	and or trade secret. No part of this document may be reproduced or distributed		
	in any form or by any means, in whole or in part, without the prior written			
	permission of Casale Media															
	________________________________________________________________________________	
	Version:		1.0																	
	File:			foundation.js														
	Description:	Web client functionality											
	Author:			Jourdain Casale, Comspec Communications								
																				   	  */
function setCookie(value) {
	var exp = new Date();
	var nowPlusOneWeek = exp.getTime() + (7 * 24 * 60 * 60 * 1000);
	exp.setTime(nowPlusOneWeek);
	document.cookie = value + ";expires=" + exp.toGMTString();
}
function confirmGo (warning, url) {
	var agree=confirm(warning);
	if (agree) {
		self.location=url;
		return true;
	} else {
		return false;
	}
}
function uncheckAll (form, field) {
	var field		= eval('document.' + form + '.' + field);
	var boxCount	= 0;
	isError			= 1;
	while (boxCount < field.length) {
		field[boxCount].checked = false;
		boxCount++;
	}
}

function checkAll (form, field) {
	var field		= eval('document.' + form + '.' + field);
	var boxCount	= 0;
	isError			= 1;
	while (boxCount < field.length) {
		field[boxCount].checked = true;
		boxCount++;
	}
}



function help(feature){
	window.open('/help/' + feature + '.html',feature,"height=390,width=430,status=no,toolbar=no,menubar=no,location=no,scrollbars=yes,top=80");
}


// --------------------------------------------
// to be tested : fieldCheckEnhanced
// it replaces former fieldCheck()
// --------------------------------------------
// standard field check method
function fieldCheck(form, fields, error) {
	
	// make fields data accessible
	var fieldAry	= fields.split(',');
	var formObj	= eval('document.' + form);
	var count	= 0;
	var output	= error;

	// check each field
	while(fieldAry[count]) {

		// give vars friendly names
		var field	= eval('document.' + form + '.' + fieldAry[count]);	
		count++;
		var type	= fieldAry[count];		
		count++;
		var max		= fieldAry[count];
		count++;
		var name	= fieldAry[count];
		count++;
		var required	= fieldAry[count];
		count++;

		// based on the field definition, figure out if there is an error present
		var errString = "";
		
		errString = checkFieldItem( field, type, max, name, required );
		
		if ( errString != "" ) {
			output = output + " - " + name + " " + errString + "\n";
		}
	}

	// figure out whether there was an error or not && submit
	if(output != error) {
		alert(output);
		return false;
	} else {
		return true;
	}
}

function checkFieldItem(field, type, max, name, required) {

	// check required fields
	if ( required == "1" && field.value == "" ) { 
		return " "; 
	}

	// if this field is not required, && the value is "", then just return "", not need to do the field check
	if ( required == "0" && field.value== "" ) {
		return "";
	}

	// check text filed's length
	if ( (type  == "text" || type == "password" || type == "select" || type == "file" )
		&& field.value.length > max && max != 0 ) {
		return "exceeds length"; 
	} 	
	
	// check email field format
	if ( type == "email" && (!field.value.match(/^([a-zA-Z0-9_\.\-\+\&\=])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,9})$/))) {
		return "format invalid";
	}

	// check url field format
	if ( type == "url" && (!field.value.match(/^http:\/\/.*\.([a-zA-Z0-9]{2,9})([\/]{0,1}).*$/)) ) {
		return "format invalid";
	}
		
	// check int field format 
	if ( type == "int" ) {
		var intValue = field.value.replace(/,/g, "");
		if (!intValue.match(/^\d+$/) ) {
			return "invalid integer value";
		}
		// check if the value of int field exceed max
		if ( parseInt(intValue) > max && max !=0 ) {
			return "exceeds maximum";		
		}
	}
	
	// all the integers must be possitive integers
	if ( type == "int" && field.value< 0 ) {
		return "needs possitive integer";
	}
		
	// check rate(field type: text) format
	//if ( $field = "rate" && !(field.value =~ m/^\d+\.\d*$|^\d*\.\d+$|^\d+$/) ) {
	//	return "viper_fieldcheck_bad_rate";
	//}
					
	if ( type == "radio" ) {
		// handler for multiple radio boxes or check boxes
		var boxCount	= 0;
		var isError	= 1;
		while (boxCount < field.length) {
			if (field[boxCount].checked == true) {
				isError = 0;
			}
			boxCount++;
		}
		if ( isError == 1 ) return " ";
	} 
	
	if	(type == "checkbox") {
		// handler for single check box
		if (!field.checked) {
			return "check at least one box";
		}
	}

	return "";
}
