/* These are the generic scripting aids for navigation and such. */

function winPop(page,height,width,windowname,scrollbars)
{
    /*
     * page = 'URL of file to popup' (ex. '/gsol/pex/en/warat/jsp/test.jsp?param=value')
     * height = 'height in pixels' (ex. '640')
     * width = 'width in pixels' (ex. '480')
     * windowname = 'name of target window' (ex. 'HelpWindow')
     * scrollbars = 'yes' or 'no'
     */

    if (height==null) height = 400;
    if (width==null) width = 600;
    if (windowname==null) windowname = "PopUpWindow";
    if (scrollbars==null) scrollbars = "yes";
    params = "height=" + height + ",width=" + width + ",status=yes,toolbar=no,directories=no,menubar=no,location=no,scrollbars=" + scrollbars + ",resizable=yes,top=50,left=50,screenX=50,screenY=50"
	popWindow = open(page,windowname,params);
}


function winPop2(page,height,width,windowname,scrollbars,status,toolbar,directories,menubar,location,resizable,top,left,screenX,screenY)
{
    /*
     * page = 'URL of file to popup' (ex. '/gsol/pex/en/warat/jsp/test.jsp?param=value')
     * height = 'height in pixels' (ex. '640')
     * width = 'width in pixels' (ex. '480')
     * windowname = 'name of target window' (ex. 'HelpWindow')
     * scrollbars = 'yes' (default) 'no'
     * status     = 'yes' (default) or 'no'
     * toolbar = 'yes' or 'no' (default)
     * directories = 'yes' or 'no'(default)
     * menubar = 'yes' or 'no' (default)
     * location = 'yes' or 'no' (default)
     * resizable = 'yes'(default) or 'no'
     * top =  'in pixels'
     * left =  'in pixels'
     * screenX = 'in pixels'
     * screenY = 'in pixels'
     */

    if (height==null) height = 400;
    if (width==null) width = 600;
    if (windowname==null) windowname = "PopUpWindow";
    if (scrollbars==null) scrollbars = "yes";
    if (status==null) status = "yes";
    if (toolbar==null) toolbar = "no";
    if (directories==null) directories = "no";
    if (menubar==null) menubar = "no";
    if (location==null) location = "no";
    if (resizable==null) resizable = "yes";
    if (top==null) top=50;
    if (left==null) left=50;
    if (screenX==null) screenX=50;
    if (screenY==null) screenY=50;

    params = "height=" + height + ",width=" + width + ",status="+status+",toolbar="+toolbar+",directories="+directories+",menubar="+menubar+",location="+location+",scrollbars=" + scrollbars + ",resizable="+resizable+",top="+top+",left="+left+",screenX="+screenX+",screenY="+screenY;

    popWindow = open(page,windowname,params);
}

function spellCheck(fieldId) {
    /*
     * Apparently used for spellchecking textarea fields.
     */
    winPop("/gsol/pex/en/warat/jsp/spelling/spell.jsp?word=" + escape(document.getElementById(fieldId).value) + "&input=" + fieldId, 400, 600, "spellCheck", "yes");
}

function tickAll(list,status) {
    if (list.length!=null) {
	for (var i=0; i<list.length; i++) {
	    list[i].checked = status;
	}
    } else {
	list.checked = status;
    }
}

function validateSearch(form) {
    if (form.query.value=="") {
	alert("Please enter a keyword to start your search.");
	form.query.focus();
    } else {
	if (form.type.selectedIndex == 0) {
	    form.entity_type.value = "Product";
	} else if (document.freetextsearch.type.selectedIndex == 1) {
	    form.entity_type.value = "Point";
	} else if (document.freetextsearch.type.selectedIndex == 2) {
	    form.entity_type.value = "Organization";
	} else if (document.freetextsearch.type.selectedIndex == 3) {
	    form.entity_type.value = "Point";
	}
	form.submit()
	    }
}

function uncheckCountries(obj)
{
    if (obj.checked==true)
	{
	    for (var i=0; i<document.CountrySearch.fld_Country.length;i++)
		{
		    document.CountrySearch.fld_Country[i].checked = false;
		}
	}
}

/**** tansl: to fix pre-production bug 79 - comment out this function as there is another function of the same name in tsevtscripts.js which will conflict. the one in tsevetscripts is currently being used.
function sortThis(myForm,initSorting,baseURL) {
    reverseSortCheckedValue = (myForm.reverse_sort[0].checked==false)?"true":"false";
    myURL = baseURL
	+ "&reverse_sort=" + reverseSortCheckedValue
	+ "&sorter=" + myForm.sorter[myForm.sorter.selectedIndex].value;
    if (initSorting) {
	myURL = myURL + "&initSorting=true";
    }
    window.location = myURL;
}
***/

function submitList(form,action,target) {
    form.action = action;
    hasChecked = false;
    if (form.length>0) {
	for (var i=0;i<form.length;i++) {
	    curElement = form.elements[i];
	    if(curElement.type=="checkbox" && curElement.checked == true) {
		hasChecked = true;
	    }
	}
    }

    if (hasChecked)	{
	if (target!=null) {
	    winPop('','160','350',target,'no');
	    form.target = target;
	    form.submit();
	} else {
	    form.submit();
	}
    } else {
	alert("Please select an item.");
    }
}

function toggleTrueFalse(curForm,curField,hidden) { 	
    if (curField.checked) { 
	curForm[hidden].value =='true';
    } else {
	curForm[hidden].value == 'false';
    }
}
function syncCheckboxToHidden(chkbox,hidden) {
    if (chkbox.checked == true) {
	hidden.value = "true";
    } else {
	hidden.value = "false"
	    }
}
function openInOpener(url) {
    window.opener.location.href=url;
    setTimeout('window.close()',2000);
}

function fillHolder(chkboxes,holder) {
    holder.value = "";
    if (chkboxes!=null) {
	if ((chkboxes.length == null) || (chkboxes.length == "undefined")) {
	    if (chkboxes.checked) {
		holder.value = chkboxes.value;
	    }
	} else {
	    for (var i=0;i<chkboxes.length;i++) {
		if (chkboxes[i].checked) {
		    if (holder.value == "") {
			holder.value = chkboxes[i].value;
		    } else {
			holder.value = holder.value + "|" + chkboxes[i].value;
		    }
		}
	    }
	}
    }
}

//--------- return the appURL to normal

function decodeappURL() {
    if (document.form.appURL!=null){
	var  val=document.form.appURL.value;
	if (val !="")
	    document.form.appURL.value=unescape(val);
    } 
}

// checks that field doesn not contain specified invalid characters 
// as well as check fld has at least one alpha character
function checkValidID(fld) {
    var fldVal = fld.value;
    // alert(fld.vlaue);
    var inThere = fldVal.match(/\*|\'|\+|\;|\,|\&/g);
    var isAlpha = fldVal.match(/[A-Z]/gi);
    if (inThere) {
	// invalid characters found.
	alert("Login Name should not contain any of these characters * ' + ; , & and .");
	return false;
    }        else {
	if (!isAlpha) {
	    //no alpha character found
	    alert("Login Name should have at least one alphabetical character.");
	    return false;
	}
	else {
	    // all valid characters and has at least one alpha.
	    return true;
	}
    }
}
