//This function creates the correct url for previewing the graph

function createPreviewUrl() {
			
	//Return the preview url
	var strUrl;
	strUrl = '/search-index/preview-chart.asp?' + createIndexQueryString();
	return strUrl;
	
}

function createCodeUrl() {

	//Return the get code url
	var strUrl;
	strUrl = '/search-index/get-chart-code.asp?' + createIndexQueryString();
	return strUrl;
	
}

function createChartUrl() {

	//Return the get code url
	var strUrl;
	strUrl = '/search-index/create-chart.asp?' + createIndexQueryString();
	return strUrl;
	
}

function getPopupWidth(){
	
	//Get the width
	var strWidth = MM_findObj('SI_Graph_Width').value;
	
	//Check if they are set - width first
	if( strWidth != '' ){
		
		//Check it's a number
		if( ! isNaN( strWidth ) ){
			
			//Convert to an integer
			strWidth = parseInt(strWidth);
			
			if( strWidth < 450 ){
				
				strWidth = 550;
				
			}else{
			
				if( strWidth > 850 ){
				
					strWidth = 900;
					
				}else{
					
					strWidth = strWidth + 100;
					
				}
			}
		}
	}
	else{
		
		//Default value
		strWidth = 550;
		
	}
	
	//Return the width
	return strWidth;
	
}

function getPopupHeight(){
	
	//Get the height
	strHeight = MM_findObj('SI_Graph_Height').value;
	
	//Check if they are set - width first
	if( strHeight != '' ){
		
		//Check it's a number
		if( ! isNaN( strHeight ) ){
			
			//Convert to an integer
			strHeight = parseInt(strHeight);
			
			if( strHeight < 450 ){
				
				strHeight = 650;
				
			}else{
			
				if( strHeight > 600 ){
				
					strHeight = 800;
					
				}else{
					
					strHeight = strHeight + 200;
				}
			}
		}
	}
	else{
		
		//Default value
		strHeight = 650;
		
	}
	
	//Return the height
	return strHeight;
	
}

function checkScrollbars(){

	//Get the width and height
	var strHeight = MM_findObj('SI_Graph_Height').value;
	var strWidth = MM_findObj('SI_Graph_Width').value;
	
	//Check if they are set - width first
	if( strHeight != '' && strWidth != '' ){
		
		//Check it's a number
		if( ! isNaN( strHeight ) && ! isNaN( strWidth ) ){
			
			//Convert to an integer
			strHeight = parseInt(strHeight);
			strWidth = parseInt(strWidth);
			
			if( strHeight > 500 || strWidth > 680 ){
				
				return 'yes';
				
			}else{
				
				return 'no';
				
			}
			
		}else{
		
			return 'yes';
		
		}
		
	}else{
		
		return 'yes';
		
	}
			
}

function createIndexQueryString(){

	//Declare vars needed
	var strPreviewUrl, strStartDate, strEndDate, strWidth, strHeight;

	//Build the url querystring
	strPreviewUrl = 'il=1&sc=1&ssc=1&ct=0&sit=' + MM_findObj('SI_Live_Search_Index_Type').options[MM_findObj('SI_Live_Search_Index_Type').selectedIndex].value;
	strPreviewUrl += '&nor=' + MM_findObj('SI_No_Of_Rows').options[MM_findObj('SI_No_Of_Rows').selectedIndex].value;
	strPreviewUrl += '&cid=' + MM_findObj('PAF_Country_Of_Preference').options[MM_findObj('PAF_Country_Of_Preference').selectedIndex].value;
	strPreviewUrl += '&rid=' + MM_findObj('PAF_Region_Of_Preference').options[MM_findObj('PAF_Region_Of_Preference').selectedIndex].value;
	
	//alert('index: ' + );
	//strPreviewUrl += '&tid=' + document.IndexSubscriberForm.PAF_Town_Of_Preference.options[document.IndexSubscriberForm.PAF_Town_Of_Preference.selectedIndex].value;
	strPreviewUrl += '&tid=-1';
	strPreviewUrl += '&gct=' + MM_findObj('SI_Graph_Type').options[MM_findObj('SI_Graph_Type').selectedIndex].value;
	
	//Check to see if we are using a predetermined date period or bespoke dates
	strStartDate = MM_findObj('SI_Start_Date').value;
	strEndDate = MM_findObj('SI_End_Date').value;
	
	//Unless both are set we are using a predetermined date/time period
	if( strStartDate != '' && strEndDate != '' ){
	
		//Format the date/time values
		strStartDate = strStartDate.replace( '/', '_' );
		strStartDate = strStartDate.replace( '/', '_' );
		strStartDate = strStartDate.replace( ':', '.' );
		strStartDate = strStartDate.replace( ':', '.' );
		strStartDate = strStartDate.replace( ' ', '-' );
		strEndDate = strEndDate.replace( '/', '_' );
		strEndDate = strEndDate.replace( '/', '_' );
		strEndDate = strEndDate.replace( ':', '.' );
		strEndDate = strEndDate.replace( ':', '.' );
		strStartDate = strStartDate.replace( ' ', '-' );
		
		strPreviewUrl += '&sd=' + strStartDate + '&ed=' + strEndDate;
		
	}else{
	
		strPreviewUrl += '&pdp=' + MM_findObj('SI_Date_Period').options[MM_findObj('SI_Date_Period').selectedIndex].value;
		
	}
	
	//Get the height and width
	strWidth = MM_findObj('SI_Graph_Width').value;
	strHeight = MM_findObj('SI_Graph_Height').value;
	
	//Unless both are set we are using default values
	if( strWidth != '' && strHeight != '' ){
		
		//Check its a number
		if( ! isNaN(strWidth )){
					
			//Check the size
			if( parseInt(strWidth) > 850 ){
				
				//Set to the max of 850
				strWidth = '850';
				
			}
		}
	
		//Format the date/time values
		strPreviewUrl += '&gw=' + strWidth + '&gh=' + strHeight;
		
	}else{
	
		strPreviewUrl += '&gw=500&gh=400';
		
	}

	//Return the string
	return strPreviewUrl;
	
}
//This function checks the value of the search index selected and shows or hides area selection boxes
function checkSelectedIndexType(){

	//Declare any variables to be used
	var intSearchIndexType = parseInt(MM_findObj('SI_Live_Search_Index_Type').options[MM_findObj('SI_Live_Search_Index_Type').selectedIndex].value);

	
	switch(intSearchIndexType){
	
		case 3: case 6: case 8: case 12: //All need the country drop down
		
			MM_findObj('country_of_preference_div').style.display = 'block';
			MM_findObj('region_of_preference_div').style.display = 'none';
			//MM_findObj('town_of_preference_div').style.display = 'none';
			break;
			
		case 5: case 9: //All need the region and country drop downs
		
			MM_findObj('country_of_preference_div').style.display = 'block';
			MM_findObj('region_of_preference_div').style.display = 'block';
			//MM_findObj('town_of_preference_div').style.display = 'none';
			break;
			
		case 10: //Required the town, country and region drop downs
		
			MM_findObj('country_of_preference_div').style.display = 'block';
			MM_findObj('region_of_preference_div').style.display = 'block';
			//MM_findObj('town_of_preference_div').style.display = 'block';
			break;
		
		default: //No need for any drop downs so hide them all
		
			MM_findObj('country_of_preference_div').style.display = 'none';
			MM_findObj('region_of_preference_div').style.display = 'none';
			//MM_findObj('town_of_preference_div').style.display = 'none';
			break;
			
	}
	
}

//Copies the code to the clipboard
function copyCodeToClipboard( strCode ){
	
	document.copyToClipboardForm.LPSI_CODE.createTextRange().execCommand('Copy');
	alert('Installation code copied to clipboard!');
	
}
