var	main_form = null //pointer will be changed in application
var pop_win = null;
var focus_field = "";
var	warning = "";
function formSetmain(thisform) {
	if (thisform != null)
		main_form = thisform;
	else
		main_form = document.forms[0];
}
function formPostback() {
	main_form.action = document.location;
	main_form.Reload.value = "Y";
	main_form.submit();
}
function formSubmitto(url) {
	main_form.action = url;
	main_form.submit();
}
function fileOpen(url) {
	pop_win = window.open(url,'file','scrollbars=yes,resizable=yes,width=640,height=480,left=100,top=100');	
}
function fileUpload(id) {
	pop_win = window.open('/CommonLib/Field/Attachment.aspx?fid=' + id,'file','width=350,height=50,left=100,top=100');
	pop_win.focus();
}
function fileRemove(id) {
	pop_win = window.open('/CommonLib/Field/Attachment.aspx?fid=' + id + '&del=1','file','width=10,height=10,left=1100,top=800');
}
function fileClose() {
	if (pop_win != null)
		if (!pop_win.closed)
			pop_win.close();
	pop_win = null;
}
function checkField(pointer, msg, warn) {
	pointer.value = jsDV_strTrim(pointer.value);
	if (pointer.value == "") {
    	if (warn != "")
      		warn = warn+"\n- ";
    	warn = warn+msg;
		if (focus_field == "")
			focus_field = pointer;
  	}
	return warn;
}
function checkFile(pointer, msg, warn) {
	if (pointer[0].value == "" || pointer[1].value == "") {
    	if (warn != "")
      		warn = warn+"\n- ";
    	warn = warn+msg;
  	}
	return warn;
}
function checkSingleCheckbox(pointer, msg, warn) {
	var c = "";
	if (pointer.checked == true)
			c = "checked";
	if (c == "") {
    	if (warn != "")
    		warn = warn+"\n- ";
    	warn = warn+msg;
  	}
  	return warn;
}
function checkRadioCheckbox(pointer, msg, warn){
	var c = "";
	if (pointer.length!=null){
		if(pointer.length>1){
			for (var i=0; i<pointer.length; i++)
				if (pointer[i].checked == true)
					c = "checked";
		}else{
		if (pointer.checked == true)
			c = "checked";
		}
	}
	else
		if (pointer.checked == true)
			c = "checked";
	if (c == "") {
    	if (warn != "")
    		warn = warn+"\n- ";
    	warn = warn+msg;
  	}
  	return warn;
}
function checkSelect(pointer, msg, warn) {
	var s = "";
	if (pointer.type=="select-one") {
		s = "selected";
		if ((pointer[0].selected == true) && (pointer.options[0].value == ""))
			s = "";
	}
	else {
		if (pointer.length!=null) {
			for (var i=0; i<pointer.length; i++) {
				if ((pointer[i].selected == true) && (pointer.options[i].value != ""))
					s = "selected";
			}
		}
	}
	if (s == "") {
    	if (warn != "")
      		warn = warn + "\n- ";
    	warn = warn + msg;
		if (focus_field == "")
			focus_field = pointer;
	}
	return warn;
}
function checkSegment(pointer, msg, warn, ids, num) {
	var c = 0;
	var arrid = ids.split(",");
	var idlen = arrid.length; 
	if (num > idlen)
		num = idlen;
	if (pointer.length!=null && pointer.length>1) {
		for(var i=0; i<=idlen-1; i++)
			for (var j=0; j<pointer.length; j++)
				if ((arrid[i] == pointer[j].value) && (pointer[j].checked == true))
					c++;
	}
	else {
		if (pointer.checked == true)
			c++;
	}
	if (c < num) {
    	if (warn != "")
    		warn = warn+"\n- ";
    	warn = warn + num + " item(s) from " + msg;
  	}
  	return warn;
}
function setSelectValue(pointer, value) {
	for (var i=0; i<pointer.length; i++) {
		if (pointer.options[i].value == value)
			pointer[i].selected = true;
	}	
}
function setDynamicSelectValue(ptr, value){
	var o, l;
	for (var i=0; i<ptr.length; i++){
		o = ptr.options[i].value;
		l = o.split("~");
		if (l[1] == value) ptr[i].selected = true;
	}
}
function changeSelect(ptr1, ptr2, arr){
	var i, j, val;
	i = ptr1.selectedIndex;
	if(i>0){
		val = ptr1[i].value;
		for(i = 1; i < arr.length; i++)
			if(arr[i][0] == val) j = i;
		fillSelect(ptr2, arr[j]);
	}
	else clearSelect(ptr2);
}
function fillSelect(ptr, arr){
	var o, l;
	clearSelect(ptr);
	var len = arr.length;
	for(var i = 1; i < len; i++){
		o = arr[i];
		l = o.split("~");
		ptr.options[i] = new Option(l[1], o);
	}
}
function clearSelect(ptr){
	var len = ptr.options.length;
	for(var i=(len-1); i >= 1; i--) ptr.options[i] = null;
	ptr.selectedIndex = 0;
}
function checkLength(pointer, maxlength) {
	if(pointer.value.length > maxlength) {		
    	alert("Please enter a note equal to or less than "+maxlength+" characters");
		pointer.focus();
    	return false;
  	}
}
function displayLength(pointer, tagid) {
	if(document.all) {
		document.all[tagid].innerHTML = pointer.value.length;
	}
}
function checkNumber(pointer, msg) {
	pointer.value = jsDV_strTrim(pointer.value);
	if(pointer.value != "") {		
    	if(!numericCheck(pointer.value,0,'nomsg')) { 
        	if (msg != null && msg != "")
        		alert(msg);
        	else
        		alert("Please enter a valid number");
			pointer.select();
        	pointer.focus();
        	return false;
		}
  	}
}
function checkFloat(pointer, msg) {
	pointer.value = jsDV_strTrim(pointer.value);
	if(pointer.value != "") {		
    	if(!numericCheck(pointer.value,2,'nomsg')) { 
        	if (msg != null && msg != "")
        		alert(msg);
        	else
        		alert("Please enter a valid number");
			pointer.select();
        	pointer.focus();
        	return false;
		}
  	}
}
function checkPointNumber(pointer, msg) {
	pointer.value = jsDV_strTrim(pointer.value);
	if(pointer.value != "") {		
    	if(!numericCheck(pointer.value,4,'nomsg')) { 
        	if (msg != null && msg != "")
        		alert(msg);
        	else
        		alert("Please enter a valid number");
			pointer.select();
        	pointer.focus();
        	return false;
		}
  	}
}
function checkCurrency(pointer, msg) {
	var amountValue = jsDV_strTrim(pointer.value.replace(/,|\$/g,""));
	if(amountValue != ""){
		returnValue = jsDV_isValidMoney(amountValue)
		if(returnValue == "-1") { 
        	if (msg != null && msg != "")
        		alert(msg);
        	else
        		alert("Please enter a valid currency");
			pointer.select();
        	pointer.focus();
        	return false;
		} 
		else {
			pointer.value = returnValue;
		}
	}
}
function checkPercentage(pointer, msg) {
	pointer.value = jsDV_strTrim(pointer.value);
	if(pointer.value != "") {		
    	if(!numericCheck(pointer.value,6,'nomsg')) { 
        	if (msg != null && msg != "")
        		alert(msg);
        	else
        		alert("Please enter a valid percent number.");
			pointer.select();
        	pointer.focus();
        	return false;
		}
  	}
}
function checkAge(pointer, msg) {
	pointer.value = jsDV_strTrim(pointer.value);
	if(pointer.value != "") {		
		if(!numericCheck(pointer.value,0,'nomsg')) { 
        	if (msg != null && msg != "")
        		alert(msg);
        	else
				alert("Please enter a valid age");
			pointer.select();
			pointer.focus();
			return false; 
		}   
	}
}
function checkZip(pointer, msg) {
	pointer.value = jsDV_strTrim(pointer.value);
	if(pointer.value != "") {
		var flag = 0;
		strchar = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 -";
		for (var i=0; i<pointer.value.length; i++) {
			tst = pointer.value.substring(i,i+1);
    		if (strchar.indexOf(tst)<0)
      			flag++;
		}
		if (flag != 0) {
        	if (msg != null && msg != "")
        		alert(msg);
        	else
        		alert("Please enter a valid zip/postal code");
			pointer.select();
        	pointer.focus();
        	return false; 
		}
	}
}
function checkCountryZip(zipptr, ctrptr, msg) {
	var zip = "";
	var formatzip = "";
	var country = "";
	var flag = 0;
	var i;
	var err;
	zip = zipptr.value;
	
	for (i=0; i<ctrptr.length; i++) {
		if (ctrptr[i].selected == true)
			country = ctrptr[i].value;
	}
	if ((zip != "") && (country == "CA")) {
		var strchar = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
		var strint = "0123456789";
		for (i=0; i<zip.length; i++) {
			tst = zip.substring(i,i+1);
			switch(i) {
				case 0:
				case 2:
				case 5:
					if (strchar.indexOf(tst)<0)
						flag++;
					break;
				case 1:
				case 4:
				case 6:
					if (strint.indexOf(tst)<0)
						flag++;	
					break;
				case 3:
					if (tst == "-")
						tst = " ";
					if (tst != " ")
						flag++;
					break;
				default:
					if (i>6)
						flag++;
					break;
			}
			formatzip = formatzip + tst;
			err = "Canadian postal code format is incorrect. Correct format is\nLDL DLD\nwhere L=letter, D=digit, and the space in the middle is required.";
		}
	}
	else {
		formatzip = zip;
		if (msg != null && msg != "")
			err = msg;
		else
			err = "Please enter a correct format zip/postal code";
	}
	if (flag != 0) {
        alert(err);
        return false; 
	}
	else {
		zipptr.value = formatzip.toUpperCase();
		return true;
	}
}
function checkFormLoginName(thisform, msg1, msg2, msg3) {
	if (thisform.LoginName != null && thisform.Password != null && thisform.VerifyPassword != null) {
		if (thisform.Password.value.length < 5) {
        	if (msg1 != null && msg1 != "")
        		alert(msg1);
        	else
				alert ("Your password is less than 5 characters - please reenter");
			return false;
		}
		if (thisform.Password.value != thisform.VerifyPassword.value) {
        	if (msg2 != null && msg2 != "")
        		alert(msg2);
        	else
				alert ("Your passwords do not match - please reenter");
			return false;
		}
		if (thisform.Password.value == thisform.LoginName.value) {
        	if (msg3 != null && msg3 != "")
        		alert(msg3);
        	else
				alert ("Your password cannot be the same as your login name");
			return false;
		}
	}
	return true;
}
