/*---------------------------------------------default.aspx-----------------------------------------------------------------*/
function PageQuery(q) {
	if(q.length > 1) 
		this.q = q.substring(1, q.length);
	else this.q = null;
		this.keyValuePairs = new Array();
	if(q) 
	{
		for(var i=0; i < this.q.split("&").length; i++) 
		{
			this.keyValuePairs[i] = this.q.split("&")[i];
		}
	}
	
	this.getKeyValuePairs = function() { return this.keyValuePairs; }
	
	this.getValue = function(s)
	{
		for(var j=0; j < this.keyValuePairs.length; j++) 
		{
			if(this.keyValuePairs[j].split("=")[0] == s)
			return this.keyValuePairs[j].split("=")[1];
		}
		return "";
	}
	
	this.getParameters = function() 
	{
		var a = new Array(this.getLength());
		for(var j=0; j < this.keyValuePairs.length; j++) 
		{
			a[j] = this.keyValuePairs[j].split("=")[0];
		}
		return a;
	}
	
	this.getLength = function() { return this.keyValuePairs.length; } 
}
function queryString(key){
	var page = new PageQuery(window.location.search); 
	return unescape(page.getValue(key)); 
}

function pageLoad()
{
	
	if(queryString("page").toLowerCase() == "search")
	{
		setYears(document.getElementById("AddForm"));
	}
}

/*---------------------------------------------Search.aspx and ArchiveSearch.aspx-----------------------------------------*/



function fieldChecker(theForm){
	var dummy = doSelecter(theForm)
	if (theForm.MC_KEYWORD_EXCLUDE.selectedIndex < 0){
		alert ("You must enter at least one keyword")
		theForm.MC_KEYWORD_EXCLUDE.focus()
		return false
	}
				
	if (window.keywordwin){// if the window exists close it
		keywordwin.close()
	}
				
	return true
			
}
			
function doKeywordPopper(type){
	var theurl = './content/opportunities/procurementOpportunities/keywordadder.html?t='+type
	keywordwin = window.open(theurl, 'newwin', 'width=500,height=250,scrollbars=yes,status=yes')
	return false
}
			
function doSelecter(theForm){
	for (i=0;i<theForm.MC_KEYWORD_EXCLUDE.length;i++){
		theForm.MC_KEYWORD_EXCLUDE.options[i].selected = true
	}
				
	return true
}

function setYears(theForm)
{
	var now = new Date();
	var len = 0;
	var year = 2003;
	theForm.lyear.length = 0;
	theForm.uyear.length = 0;
	theForm.alyear.length = 0;
	theForm.auyear.length = 0;
	theForm.lyear.options[len] = new Option("", "");
	theForm.uyear.options[len] = new Option("", "");
	theForm.alyear.options[len] = new Option("", "");
	theForm.auyear.options[len] = new Option("", "");
	len++;
	while(year <= now.getYear())
	{
		theForm.lyear.options[len] = new Option(year,year);
		theForm.uyear.options[len] = new Option(year,year);
		theForm.alyear.options[len] = new Option(year, year);
		theForm.auyear.options[len] = new Option(year, year);
		year++;
		len++;
	}
	theForm.lyear.selectedIndex = 0;
	theForm.uyear.selectedIndex = 0;
	theForm.alyear.selectedIndex = 0;
	theForm.auyear.selectedIndex = 0;
}
			
function fieldChecker2(theForm, resultsPageUrl){
	var dummy = doSelecter2(theForm);
	var unspscOK = theForm.MC_UNSPSC.length;
	var keywordsOK = theForm.MC_KEYWORD_INCLUDE.length;
	var titleOK = theForm.title.value.length;
	var lmonthOK = theForm.lmonth.value.length;
	var ldayOK = theForm.lday.value.length;
	var lyearOK = theForm.lyear.value.length;
	var umonthOK = theForm.umonth.value.length;
	var udayOK = theForm.uday.value.length;
	var uyearOK = theForm.uyear.value.length;
	var refOK = theForm.REF.value.length;
	/*var solOK = theForm.SOL.value.length;*/
	var orgOK = theForm.ORG.value.length;
				
	if (!unspscOK && !keywordsOK && !titleOK && (!lmonthOK || !ldayOK || !lyearOK || !umonthOK || !udayOK || !uyearOK) && !refOK && !orgOK){
		alert ("You must either enter a title, choose at least one UNSPSC, enter at least one Keyword, enter a valid date range, enter a reference number, enter a sol number, or entering a buying organization")
		theForm.MC_UNSPSC.focus()
		return false
	}
				
	if (window.keywordwin){// if the window exists close it
		keywordwin.close()
	}
				
	if (window.spscwin){// if the window exists close it
		spscwin.close()
	}
	theForm.action=resultsPageUrl;
	theForm.method="post";
	theForm.submit();
				
	return true
			
}
		
function doKeywordPopper2(url, type){				
	//var theurl = './content/opportunities/procurementOpportunities/keywordadder.html?t='+type
	var theurl = url + '?t=' + type;
	keywordwin = window.open(theurl, 'newwin1', 'width=500,height=250,scrollbars=yes');
	return false;
}
			
function  doHelpPop2(page){
	var theurl = './content/opportunities/procurementOpportunities/help_popup.html?page='+page;
	window.open(theurl, 'newwin', 'width=550,height=350,scrollbars=yes,status=yes');
	return false;
}
			
function doUNSPSCPopper2(url){
	//var theurl = './content/opportunities/procurementOpportunities/unspscadder.aspx?search=yes';
	spscwin = window.open(url, 'newwin2', 'width=600,height=500,scrollbars=yes,status=yes');
	return false;
}

function doUNSPSCRemoverPopper2(url){

	//var theurl = './content/opportunities/procurementOpportunities/unspscremover.htm?search=yes';
	spscwin = window.open(url, 'newwin2', 'width=600,height=500,scrollbars=yes,status=yes');
	return false;
}
		
function doSelecter2(theForm){
	// loop over the multi select boxes for keywords and spsc and select them before submitting
	for (i=0;i<theForm.MC_UNSPSC.length;i++){
		theForm.MC_UNSPSC.options[i].selected = true
	}
			
	for (i=0;i<theForm.MC_KEYWORD_INCLUDE.length;i++){
		theForm.MC_KEYWORD_INCLUDE.options[i].selected = true
	}
				
	return true		
}


/*--------------------------------------------------OpportunityDetail.ascx-----------------------------------------*/



function printit(tab,opid,ft){
		var theURL = "./content/opportunities/procurementOpportunities/browse_print.aspx?tab=" + tab + "&opid=" + opid + "&ft=" + ft
		window.open(theURL,'_newwin','width=800,height=600,scrollbars=yes')
		return false
	}
	
function emailChecker(theForm){
	if (theForm.sname.value == ''){
		alert ("You must enter your name")
		return false
	}
	var whereATis = theForm.semail.value.indexOf('@')
	var whereDOTis = theForm.semail.value.indexOf('.')
	if (whereATis==-1){
		alert ("Invalid email address format. Email must have an '@'")
		return false
	}
	if (whereDOTis==-1){
		alert ("Invalid email address format. Email must have a '.'")
		return false
	}
	var whereATis = theForm.remail.value.indexOf('@')
	var whereDOTis = theForm.remail.value.indexOf('.')
	if (whereATis==-1){
		alert ("Invalid email address format. Email must have an '@'")
		return false
	}
	if (whereDOTis==-1){
		alert ("Invalid email address format. Email must have a '.'")
		return false
	}
	return true
}


/*-------------------------------------------------KeywordAdder.html ---------------------------------------------*/
function doDeleter1(pos){
	window.opener.document.forms[0].MC_KEYWORD_INCLUDE.options[pos] = null
	history.go(0)
}

function doDeleter2(pos){
	window.opener.document.forms[0].MC_KEYWORD_EXCLUDE.options[pos] = null
	history.go(0)
}

function doAddForm(){
	if (document.keyworder.t.value == "include"){
	// if the text box isn't blank add the keyword to the field
		if (document.keyworder.keyword.value.length > 0){
			var kwordList = document.keyworder.keyword.value

			kwordList = kwordList.replace(/, /g,",");
			
			kwordList =  kwordList.split(",")
			
			for(i=0;i<kwordList.length;i++){
				window.opener.document.forms[0].MC_KEYWORD_INCLUDE.options.length = window.opener.document.forms[0].MC_KEYWORD_INCLUDE.options.length + 1
				window.opener.document.forms[0].MC_KEYWORD_INCLUDE.options[window.opener.document.forms[0].MC_KEYWORD_INCLUDE.options.length-1].value = kwordList[i]
				window.opener.document.forms[0].MC_KEYWORD_INCLUDE.options[window.opener.document.forms[0].MC_KEYWORD_INCLUDE.options.length-1].text = kwordList[i]
			}
			return true
		}else{
			return false
		}
	}else{
		// if the text box isn't blank add the keyword to the field
		if (document.keyworder.keyword.value.length > 0){
			var kwordList = document.keyworder.keyword.value
			kwordList = kwordList.replace(/, /g,",")
			kwordList =  kwordList.split(",")
			for(i=0;i<kwordList.length;i++){
				window.opener.document.forms[0].MC_KEYWORD_EXCLUDE.options.length = window.opener.document.forms[0].MC_KEYWORD_EXCLUDE.options.length + 1
				window.opener.document.forms[0].MC_KEYWORD_EXCLUDE.options[window.opener.document.forms[0].MC_KEYWORD_EXCLUDE.options.length-1].value = kwordList[i]
				window.opener.document.forms[0].MC_KEYWORD_EXCLUDE.options[window.opener.document.forms[0].MC_KEYWORD_EXCLUDE.options.length-1].text = kwordList[i]
			}
			return true
		}else{
			return false
		}
	}	
}

function focuser(){
	document.keyworder.keyword.focus()
}


/*-------------------------------------------------unspscadder.aspx--------------------------------------------------*/


function fieldChecker3(theForm){
	if (document.adder.seg.selectedIndex == -1){
		return false
	}else{
		theForm.submit()
		return true
	}
}
/*------------------------------------
check if the unspsc category has been existed		
-------------------------------------*/
function checkExists(sVal, dOpt)
{
	for(var i=0;i<dOpt.length;i++)
	{
		if(sVal == dOpt[i].value)
			return true;
	}
	return false;
	
}
function doAdder(){
	// if the box has at least one thing selected loop over it
	if (document.adder.seg.selectedIndex > -1){
		for (i=0;i<document.adder.seg.length;i++){
			if (document.adder.seg.options[i].selected == true){		
				if (window.opener.document.forms[0].MC_UNSPSC.options.length < 50){
					if(!checkExists(document.adder.seg.options[i].value,window.opener.document.forms[0].MC_UNSPSC.options))
					{
						window.opener.document.forms[0].MC_UNSPSC.options.length = window.opener.document.forms[0].MC_UNSPSC.options.length + 1
						window.opener.document.forms[0].MC_UNSPSC.options[window.opener.document.forms[0].MC_UNSPSC.options.length-1].value = document.adder.seg.options[i].value
						window.opener.document.forms[0].MC_UNSPSC.options[window.opener.document.forms[0].MC_UNSPSC.options.length-1].text = document.adder.seg.options[i].text
					}
					else
						alert(document.adder.seg.options[i].text + " has already selected.");
				}else{
					alert ("You can have a maximum of 50 UNSPSC Codes associated with a single profile.")
					i = 50000000
				}
			}
		}
			
		window.close()
	}else{
		alert ("No Segment selected")
	}
	return false
}
		
function formCheck(){
	var formCount = window.opener.document.forms[0].MC_UNSPSC.options.length
	if (formCount >= 50){
		alert ("You can have a maximum of 50 UNSPSC Codes associated with a single profile.")
		window.close()
	}
}

