/************************************************************************
ugh_ SubmitSearch - Generic Gartner.com Search, all rights reserved.

1) include g_popup.js used to pop-open the new window.
2) Name the form - frmSearch
3) Name the text field - txtSearch
4) The form tag should look like <form method="post" name="frmSearch" onsubmit="submitSearch();return false;">

*************************************************************************/

var winCTR = 0 ;
var childWindow = new Array(20); 

function ugh_validSearch(keywords){

	if (keywords.match(/[A-Z]+/g) ||
		keywords.match(/[a-z]+/g) ||
		keywords.match(/[0-9]+/g)) {
		return true;
	}

	if (keywords.match(/^ *$/g)) {
		alert('Please provide keywords for your search.');
		return false;
	}
	alert('Your search is too general.  Please provide keywords for your search.');
	return false;
}


function ugh_submitSearch(searchKey, formObj, element) {
	
	var objSearchForm = (formObj) ? document[formObj] : document.frmSearch;  
	var elementValue = (element) ? objSearchForm.elements[element.name].value : objSearchForm.txtSearch.value;
    
	if (ugh_validSearch(elementValue)) {
	
		if(searchKey) {
			mystrng = elementValue + searchKey;
		} else {
        	mystrng = elementValue;
		}
		
		mystrng = mystrng.replace(/\%/g,"%25");
		mystrng = mystrng.replace(/\"/g,"%22");
		mystrng = mystrng.replace(/\#/g,"%23");
		mystrng = mystrng.replace(/\&/g,"%26");
		mystrng = mystrng.replace(/\+/g,"%2B");
		mystrng = mystrng.replace(/\,/g,"%2C");
		mystrng = mystrng.replace(/\./g,"%2E");
		mystrng = mystrng.replace(/\//g,"%2F");
		mystrng = mystrng.replace(/\:/g,"%3A");
		mystrng = mystrng.replace(/\;/g,"%3B");
		mystrng = mystrng.replace(/\</g,"%3C");
		mystrng = mystrng.replace(/\=/g,"%3D");
		mystrng = mystrng.replace(/\>/g,"%3E");
		mystrng = mystrng.replace(/\?/g,"%3F");
		mystrng = mystrng.replace(/\@/g,"%40");
		mystrng = mystrng.replace(/\[/g,"%5B");
		mystrng = mystrng.replace(/\]/g,"%5D");
		mystrng = mystrng.replace(/\^/g,"%5E");
		mystrng = mystrng.replace(/\'/g,"%60");
		mystrng = mystrng.replace(/\{/g,"%7B");
		mystrng = mystrng.replace(/\|/g,"%7C");
		mystrng = mystrng.replace(/\}/g,"%7D");
		mystrng = mystrng.replace(/\~/g,"%7E");
		mystrng = mystrng.replace(/ /g,"+");

        for (x = 0; x < mystrng.length; x++) {
            d = mystrng.charCodeAt(x);
            if (d == 8216 || d == 8217 || d == 8220 || d == 8221) {
                if (x == 0) {
                    if (d == 8216 || d == 8217) {
                        mystrng = "%27"
                            + mystrng.substring(1, mystrng.length);
                    } else {
                        mystrng = "%22"
                            + mystrng.substring(1, mystrng.length);
                    }
                } else if (x == mystrng.length) {
                    if (d == 8216 || d == 8217) {
                        mystrng = mystrng.substring(0, x) + "%27";
                    } else {
                        mystrng = mystrng.substring(0, x) + "%22";
                    }
                } else {
                    if (d == 8216 || d == 8217) {
                        mystrng = mystrng.substring(0, x)
                            + "%27"
                            + mystrng.substring(x + 1, mystrng.length);
                    } else {
                        mystrng = mystrng.substring(0, x)
                            + "%22"
                            + mystrng.substring(x + 1, mystrng.length);
                    }
                }
            }
        }
        ugh_g_openSearch('/7_search/Search2Frame.jsp?keywords='+ mystrng);
		
    }
}

function ugh_g_openSearch(url){
	var w=750;
	var h=540;
	var features="location=no,scrollbars=yes,status=no,toolbar=no,resizable=yes";
	gPopUp(url, w, h, '', features, null);
}
