function submitForm(event) {
    var formObj = getDynamicSearchFormObj();
    if(validateSearchForm(event, formObj)) {
        formObj.submit();
    }
    return false;
}

function validateSearchForm(event, formObj) {
	var validForm = true;
	var inputFieldValue = "inputFieldValue(0)";
	var searchTermObj = formObj.elements[inputFieldValue];
    if(searchTermObj!=null && trim(searchTermObj.value)==""){
		alert(SEARCH_TERMS_EMPTY);
		searchTermObj.focus();
	    cancelEvent(event);
	    return false;
	} else if (isValidObject(searchTermObj) && !isValidWildCardSearch(searchTermObj.value) ) {
        alert(WILDCARD_SEARCH_TERMS);
        searchTermObj.focus();
	    cancelEvent(event);
	    return false;
    }
	return validForm;
}

function isValidWildCardSearch(searchTerm){
    var validSearch = true;
    var nonWildCardChars = searchTerm.replace(/\?|!|\*/g, "");
    var nonWildCardCharsLen = trim(nonWildCardChars).length;
    if( ((searchTerm.indexOf('*') > -1) || (searchTerm.indexOf('?') > -1)  || (searchTerm.indexOf('!') > -1))
        && (nonWildCardCharsLen < 3) ){
            validSearch = false;
    }
    return validSearch;
}

function getDynamicSearchFormObj() {
    return getFormById("dynamicSearchForm");
}

function getWorldNewsDynamicSearchFormObj() {
	return getFormById("worldNewsDynamicSearchForm");
}

function getVediosDynamicSearchFormObj() {
	return getFormById("videosDynamicSearchForm");
}

function getFormById(formId) {
    var e=document.getElementsByTagName("form");
	for(var i=0;i<e.length;i++) {
		if(e[i].id == formId) {
			return e[i];
		}
	}
	return document.forms[0];
}

function submitWorldNewsForm(event) {
    var formObj = getWorldNewsDynamicSearchFormObj();
    if(validateSearchForm(event, formObj)) {
        formObj.submit();
    }
    return false;
}

function submitVediosForm(event) {
    var formObj = getVediosDynamicSearchFormObj();
    if(validateSearchForm(event, formObj)) {
        formObj.submit();
    }
    return false;
}

function submitVideoGallery() {
   var formObj = getVediosDynamicSearchFormObj();
   getElementById("limiterFieldValue(CT)").value = "Videos";
   var inputFieldValue = "inputFieldValue(0)";
   var searchTermObj = formObj.elements[inputFieldValue];
   if(isValidObject(searchTermObj)) {
       searchTermObj.value = "";
   }
   formObj.submit();
}

function readmore(){
 	getElementById("text250").style.display = "none";
	getElementById("fulloverviewlink").style.display = "none";
	getElementById("textfull").style.display = "block";
	getElementById("hide-link").style.display = "inline";
 }
 function hidemore(){
 	getElementById("text250").style.display = "block";
	getElementById("fulloverviewlink").style.display = "inline";
	getElementById("textfull").style.display = "none";
	getElementById("hide-link").style.display = "none";
 }

function attachPlayer() {
    var videos = document.getElementsBySelector("div.portal_videoimg");
    if(isValidObject(videos) && videos.length >0) {
        var anchor = getElementById("video-url-0");
        var href = anchor.href;
        var fileName = href.substring(href.indexOf(".com") + 4);
        var script = "<object classid=\"CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95\" codebase=\"http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701\" id=\"MoviePlayer\" standby=\"Loading Media Player components...\" type=\"application/x-oleobject\" height=\"250\" width=\"200\">"
		+ "<param name=\"TransparentAtStart\" value=\"false\">"
		+ "<param name=\"AutoStart\" value=\"false\">"
		+ "<param name=\"AnimationatStart\" value=\"false\">"
		+ "<param name=\"ShowControls\" value=\"true\">"
		+ "<param name=\"ShowStatusBar\" value=\"false\">"
		+ "<param name=\"autoSize\" value=\"false\">"
		+ "<param name=\"displaySize\" value=\"2\">"
		+ "<param name=\"EnableContextMenu\" value=\"false\">"
		+ "<param name=\"filename\" value=\"" + fileName + "\">"
		+ "<embed showstatusbar=\"0\" animationatstart=\"0\" autosize=\"0\" autostart=\"0\" displaysize=\"1\" enablecontextmenu=\"0\" name=\"MoviePlayer\" pluginspage=\"http://www.microsoft.com/Windows/Downloads/Contents/Products/MediaPlayer/\" showcontrols=\"1\" "
        + "src=\"" + href.replace("player", "playback") + "\" type=\"application/x-mplayer2\" windowlessvideo=\"1\" height=\"200\" width=\"200\">"
	+ "</object>"
    getElementById("video-0").innerHTML = script;
    }
}

function attachSearchAlertScripts() {
    var searchAlerts = document.getElementsBySelector("a.searchAlert");
    for(i=0; i<searchAlerts.length; i++) {
        searchAlerts[i].onclick = openGaleChildWindow;
    }
}

/*function setSelectedPublicationInCookie() {
    var preSelectedPupNames = document.getElementsBySelector("input.preSelectedPupName");
    var preSelectedMcodes = document.getElementsBySelector("input.preSelectedMcode");
    var slctd = "";
    var value = "";
    for(var i=0; i<preSelectedPupNames.length; i++) {
        var pubName = preSelectedPupNames[i].value;
        var mcode = preSelectedMcodes[i].value;
        var replaceVal = pubName.replace(/\+/g," ");
        replaceVal=unescape(replaceVal);
        var finalValue=replaceAll( replaceVal,'"','""') + " ";
        slctd = slctd + "\""+ finalValue + "\"" + " OR ";
        value = value + mcode + " OR ";
    }
    setCookie("selectRslts_0", slctd);
    setCookie("selectValue_0", value);
}*/

/*function replaceAll( str, look,replace ) {
    var idx = str.indexOf( look );
    var beforeString="";
    var j=0;
    while ( idx > -1) {
          str = str.replace(look, replace );
          beforeString=beforeString+str.substring(0, (idx+replace.length));
          if(idx==0)
              str=str.substring(beforeString.length-j,str.length);
          else
                str=str.substring(beforeString.length-(j+1),str.length);

          idx = str.indexOf( look );
          j=j+1;

   }
   str=beforeString+str;
   return str;
}*/

function attachBasicSearchEvents() {
    addClickEvent(getElementById("basic-search"), submitForm);
    addClickEvent(getElementById("news-search"), submitWorldNewsForm);
    addClickEvent(getElementById("vedios-search"), submitVediosForm);
    addClickEvent(getElementById("video-gallery"), submitVideoGallery);
    attachPopup("browseField_JX", openGaleChildWindow);
    attachPopup("customize-news", openGaleChildWindow);
    //addClickEvent(getElementById("customize-news"), setSelectedPublicationInCookie);
    //attachPopup("searchAlert", openGaleChildWindow);
    attachSearchAlertScripts();
    attachPopup("preferences", openGaleChildWindow);
    attachPopup("dictionaryTool", openDictionary);
    attachPopup("infomarkTool", openGaleChildWindow);
    attachPopup("infomark-portal", openGaleChildWindow);
    attachPopupForSelector("a.podcast-play", openGaleChildWindow);
    //document page
    attachPopupForSelector("a.videos-external", openVideoWindow);
    attachPopupForSelector("a.websiteLink", openGaleChildWindow);
    // resultlist page
    attachPopupForSelector("a.videos-external-result", openVideoResult);
    attachPopupForSelector("a.websiteLink-result", openWebsiteVideoResult);
    //featured video
    attachPopupForSelector("a.videos-external-fresult", openFeaturedVideoResult);
    attachPopupForSelector("a.websiteLink-fresult", openWebsiteFeaturedVideoResult);
    attachPopup("globalTools-help", openGaleChildWindow);
    attachPopup("why-reg-help", openGaleChildWindow);
    attachPopupForSelector("a.embed-feature-video", embedFeaturedVideo);
    attachPopupForSelector("a.embed-feature-nbc-video", embedFeaturedNBCVideo);
    attachPopupForSelector("a.embed-feature-pbs-video", openFeaturePBSVideoWindow);
    addSubmitEvent(getDynamicSearchFormObj(), submitForm);
    attachPlayer();
    attachPopupForSelector("a.tool-box", openToolBox);
    attachPopupForSelector("a.pub_logo", openToolBox);
}

function openFeaturePBSVideoWindow(){
   window.open(this.href, "galeChildWindow","toolbar=no, directories=no, status=no, location=no, resizable=yes, menubar=no, scrollbars=yes,screenX=50,screenY=50,top=50,left=50,width=750,height=550");
   return false;
}

function embedFeaturedNBCVideo() {
   var id = "video-container";
   var width = "216";
   var height = "175";
   var videoUrl = this.href;
   var elem = getElementById(id);
   var fileName = videoUrl.substring(videoUrl.indexOf(".com") + 4);
   var xhtmlString = "<object classid=\"CLSID:22d6f312-b0f6-11d0-94ab-0080c74c7e95\" codebase=\"http://activex.microsoft.com/activex/controls/mplayer/en/nsmp2inf.cab#Version=5,1,52,701\" id=\"MoviePlayer\" standby=\"Loading Media Player components...\" type=\"application/x-oleobject\" height=\"200\" width=\"200\">"
		+ "<param name=\"TransparentAtStart\" value=\"false\">"
		+ "<param name=\"AutoStart\" value=\"true\">"
		+ "<param name=\"AnimationatStart\" value=\"false\">"
		+ "<param name=\"ShowControls\" value=\"true\">"
		+ "<param name=\"ShowStatusBar\" value=\"false\">"
		+ "<param name=\"autoSize\" value=\"false\">"
		+ "<param name=\"displaySize\" value=\"2\">"
		+ "<param name=\"EnableContextMenu\" value=\"false\">"
        + "<param name=\"filename\" value=\"" + videoUrl.replace("player", "playback") +  "\">"
        + "<embed ShowStatusBar=\"0\" animationatstart=\"0\" autosize=\"0\" autostart=\"1\" displaysize=\"1\" enablecontextmenu=\"0\" name=\"MoviePlayer\" pluginspage=\"http://www.microsoft.com/Windows/Downloads/Contents/Products/MediaPlayer/\" showcontrols=\"1\" "
        + "src=\"" + videoUrl.replace("player", "playback") + "\" type=\"application/x-ms-wmp\" windowlessvideo=\"1\" height=\"200\" width=\"200\">"
	+ "</object>"
    elem.innerHTML = xhtmlString;
    return false;
}

function openToolBox() {
    toolBoxPopupWindow(this.href);
    return false;
}

function openVideoWindowWithAudit(url, id) {
/*    alert(id);
    alert(getElementById(id).href);*/
    postRequest(getElementById(id).href + "&isAudit=true");
    window.open(url,"galeChildWindow","toolbar=no, directories=no, status=no, location=no, resizable=no, menubar=no, scrollbars=no,screenX=50,screenY=50,top=50,left=50,width=320,height=340");
    return false;
}

function openWebsiteWindowWithAudit(url, id) {
    postRequest(getElementById(id).href + "&isAudit=true");
    window.open(url,"galeChildWindow","toolbar=no, directories=no, status=no, location=no, resizable=yes, menubar=no, scrollbars=yes,screenX=50,screenY=50,top=50,left=50,width=750,height=550");
    return false;
}

function openVideoResult() {
    var url = this.href;
    var id = "video_details_" + this.id.split("_")[1];
    return openVideoWindowWithAudit(url, id);
}

function openWebsiteVideoResult() {
    var url = this.href;
    var id = "video_details_" + this.id.split("_")[1];
    return openWebsiteWindowWithAudit(url, id);
}

function openFeaturedVideoResult() {
   var url = this.href;
   var id = "fvideo_details_" + this.id.split("_")[1];
   return openVideoWindowWithAudit(url, id);
}

function openWebsiteFeaturedVideoResult() {
   var url = this.href;
   var id = "fvideo_details_" + this.id.split("_")[1];
   return openWebsiteWindowWithAudit(url, id);
}

function embedFeaturedVideo() {
    InsertRtspMovie('video-container', '216', '175', this.href);
    return false;
}

addWindowLoadEvent(attachBasicSearchEvents);