var panel2=null;
var panel3=null;
var panel4=null;

function openInstructionWindow(id)
{

	var strWindowFeatures = "toolbar=no,resize=no,titlebar=no,";
	strWindowFeatures = strWindowFeatures +
				"location=no,directories=no,status=no,scrollbars=yes,"
	strWindowFeatures = strWindowFeatures +
			"menubar=no,width=950,height=auto,maximize=null";
	// Open the window
	window.open('instructions.php?instr_id='+id, 'View Instruction', strWindowFeatures);
}

function setPrice(option, price0, price1, price2, shipprice, url)
{
	var p=null;
	
	if (option == 0) p=price0;
	if (option == 1) p=price1;
	if (option == 2) p=price2;
	
	$('#td_itemprice').html('$'+p);

	var tprice = p+shipprice;

	$('#td_totalprice').html('$'+tprice);

	$('#a_cart').href = url;
}
function deleteSearch(id)
{
	if (confirm('Do you really want to delete the search?'))
		xajax_xaj_deleteSearch(id);
}

function validateSubmitInstructions()
{
	if (! validateForm('instrFrmInstructions') )
		return false;
		
	var options = { 
			target:        '#instrDivContainer',   // target element(s) to be updated with server response 
			//beforeSubmit:  validateForm('instrFrmInstructions'),  // pre-submit callback 
			//success:       showResponse,  // post-submit callback 

			// other available options: 
			url:       'index.php',         // override for form's 'action' attribute 
			type:      'post',        // 'get' or 'post', override for form's 'method' attribute 
			//clearForm: true        // clear all form fields after successful submit 
			resetForm: false        // reset the form after successful submit 

			// $.ajax options can be used here too, for example: 
			//timeout:   3000 
	}; 
 
    // bind form using 'ajaxForm' 
	$('#instrFrmInstructions').ajaxForm(options);
	$('#instrFrmInstructions').submit();
		
	return true;
}

function setInstrType(type)
{
	$('#instrTxtText').css('display', 'none');	
	$('#instrInpLink').css('display', 'none');	
	$('#instrDivFile').css('display', 'none');
	$('#instrDivYoutube').css('display', 'none');
	
	if (type == 'text')
	{
		$('#instrTxtText').css('display', 'block');
	}

	if (type == 'file')
		$('#instrDivFile').css('display', 'block');

	if (type == 'link' || type == 'youtube')
	{
		$('#instrInpLink').css('display', 'block');
	}

	if (type == 'file')
		$('#instrSubmitBtn').val('Upload Instructions');
	else
		$('#instrSubmitBtn').val('Submit Instructions');

	if (type == 'youtube')
		$('#instrDivYoutube').css('display', 'block');

}

function submitInstructionsForm(string)
{

	if (panel4 == null)
	{
		var body="";
		body= body +"<div id='instrDivContainer'>";
		body= body +"<form action='#' id='instrFrmInstructions'  >";
		body= body +"Your name (required): <br/> <input type='text' maxlen=200 id='instrInpName' name='instrInpName' value=''/><br>";
		body= body +"Your E-mail (required):<br/> <input type='text' maxlen=200 id='instrInpEmail' name='instrInpEmail'  value='' /><br>";
		body= body +"<table cellpadding='5' cellspacing='0' border='0' width='300px' align='center'>";
		body= body +"	<tr align='center'>";
		body= body +"		<td> <label for='instrRdText'> Text </label> </td>";
		body= body +"		<td> <label for='instrRdLink'> Link to other site </label></td>";
		body= body +"		<td> <label for='instrRdYoutube'> Youtube video </label></td>";
		body= body +"		<td> <label for='instrRdFile'> File </label></td>";
		body= body +"	</tr>";
		body= body +"	<tr align='center'>";
		body= body +"		<td> <input type='radio' name='instrtype' value='text' checked=true id='instrRdText' onclick='setInstrType(this.value);' /></td>";
		body= body +"		<td> <input type='radio' name='instrtype' value='link' id='instrRdLink' onclick='setInstrType(this.value);' /></td>";
		body= body +"		<td> <input type='radio' name='instrtype' value='youtube' id='instrRdYoutube' onclick='setInstrType(this.value);' /></td>";
		body= body +"		<td> <input type='radio' name='instrtype' value='file' id='instrRdFile' onclick='setInstrType(this.value);' /></td>";
		body= body +"	</tr>";
		body= body +"</table>";

		body= body +"<textarea name='instrTxtText' id='instrTxtText' cols='80' rows='12'  > </textarea>";

		body= body +"<div id='instrDivYoutube' style='display: none; margin: 0 auto;'>";
		body= body +"<p id='instrTxtYoutube'> Please submit link to the video <br /> (example - 'http://www.youtube.com/watch?v=Aw3-NJHr0Ro') </p>";
		body= body +"</div>";
		
		body= body +"<input type='text' id='instrInpLink' name='instrInpLink' value='' style='display:none; margin: 0 auto;' />";
		
		body= body +"<div id='instrDivFile' style='display: none; margin: 0 auto;'>";
		body= body +"<p id='instrTxtFile'> Please use file with following extensions: " + instr_allowed_ext.join(',') + "</p>";
		body= body +"<input type='file' id='instrInpFile' name='instrInpFile' />";
		body= body +"</div>";

		
		body= body +"<input type='hidden' id='instrInpPartData' name='instrInpPartData' value='" + string +"' />";
		body= body +"<input type='hidden' id='ajax' name='ajax' value='instructions'/>";

		body= body +"<br />";
		body= body +"<input type='button' style='padding-top: 5px;margin-top: 10px;' onclick='validateSubmitInstructions();' id='instrSubmitBtn' value='Submit Instructions' />";
		body= body +"</form>";
		body= body + '<span id="instrReturn"> </span>';
		body= body + '</div>';

    body= body + "<p class='a_navigation' onclick='panel4.hide(); panel4.destroy(); panel4=null;'> Close </p>";
		

		panel4 = new YAHOO.widget.Panel("panel4", {tl:"-50,-50",width:"680px", height:"auto", background:"#ffffcc", visible:true, fixedcenter: true,  draggable:true, close:false} );
		panel4.setHeader("Suggest Repair Instructions");
		panel4.setBody(body);
		panel4.setFooter("");
		panel4.render("container");
		

	}
	else
		panel4.show();
	
	panel4.focusFirst();
}

function askQuestionForm(part)
{

	if (panel3 == null)
	{

		var body="<form action='#' id ='qstnFrmQuestion'  >";
    body= body +"Your name (required): <br/> <input type='text' id='qstnInpName' name='qstnInpName' style='margin-bottom: 5px; width: 300px; height: 25px' /><br>";
    body= body +"Your E-mail (required):<br/> <input type='text' id='qstnInpEmail' name='qstnInpEmail' style='margin-bottom: 5px; width: 200px; height: 25px' /><br>";
    body= body +"Your question: <br/> <textarea name='qstnTxtQuestion' id='qstnTxtQuestion' cols='60' rows='5' > </textarea>";
    body= body +"<input type='hidden' id='curpart' value='' />";
    body= body +"<input type='button' onclick='validateAskQuestion(\"" + part + "\");' value='Submit Question' />";
		body= body +"</form>";
		
		panel3 = new YAHOO.widget.Panel("panel3", {tl:"-50,-50",width:"520px", height:"300px", background:"#ffffcc", visible:true, fixedcenter: true,  draggable:true, close:true} );
		panel3.setHeader("Ask A Question");
		panel3.setBody(body);
		panel3.setFooter("");
		panel3.render("container");
	}
	else
		panel3.show();
	
	panel3.focusFirst();
}

function saveSearchForm()
{
	if (! validateForm('saveSearchForm') )
		return;
	
	if (panel2 == null)
	{
		var body=$('#panel2_container').html();
		
		panel2 = new YAHOO.widget.Panel("panel2", {tl:"-50,-50",width:"220px", visible:true, fixedcenter: true,  draggable:true, close:true} );
		panel2.setHeader("Set searh name");
		panel2.setBody(body);
		panel2.setFooter("");
		panel2.render("container");
	}
	else
		panel2.show();
	
	$("src_name").focus();
}

function saveSearch(name)
{
	if (trim(name) == '')
	{
		alert ('Provide search name!');
		return;
	}
	
  var syear1= $('#year1');
  var smodel= $('#model');
  var sparts= $('#parts');
	
	var subpart=$('#userInterchange_select');
	subpart_text=null;
	subpart_vals=null;
	
	if (subpart != null)
	{
		subpart_text = subpart.options[subpart.selectedIndex].text;
		subpart_vals = 	xajax.getFormValues('form2');
	}
	
	xajax_xaj_saveSearch(
			syear1.options[syear1.selectedIndex].text,
			smodel.options[smodel.selectedIndex].text,
			sparts.options[sparts.selectedIndex].text,
			name,
			subpart_text,
			subpart_vals
			);
}

function validateForm(formname)
{

	if (formname == 'instrFrmInstructions')
	{
		if (isEmpty('instrInpName') )
		{
			alert ('Please enter Your Name!');
			return false;
		}
		
		if (isEmpty('instrInpEmail') )
		{
			alert ('Please enter Your E-mail!');
			return false;
		}

		var ftype= $('input:radio[name=instrtype]:checked').val();
		
		if ( ftype == 'youtube' ) //check if video placed on youtbe site
		{
			var site = $('#instrInpLink').val();
			site = site.toLowerCase();

			if ( site.indexOf("http://") != 0)
				site = 'http://' + site;
			
			if ( site.indexOf("http://youtube.com") && site.indexOf("http://www.youtube.com") ) 
			{
				alert ('Only Youtube video is supported now.');
				return false;
			}			
			
		}

		if ( ftype == 'link' ) //check if link starts with 'http://'. if not - append it.
		{
			var site = $('#instrInpLink').val();
			site = site.toLowerCase();

			if ( site.indexOf("http://") != 0)
				site = 'http://' + site;			
		}
		
		if ( ftype == 'file' ) //check extension of the file
		{
			var fname = trim($('#instrInpFile').val());

			if (isEmpty('instrInpFile') )
			{
				alert ('Please select file!');
				return false;
			}			
			
			var ext = fname.split('.');
			ext=ext.reverse();
			
			ext=ext[0];
			if ( jQuery.inArray( ext, instr_allowed_ext )  == -1 )
			{
				alert ('Please upload allowed file!');
				return false;
			}
		}

		return true;
	}

	if (formname == 'saveSearchForm')
	{

		if (trim( $('#year1 option:selected').text()) == 'Select Year')
		{
			alert ('Select Year!');
			return false;
		}
	
		if ( trim( $('#model option:selected').text()) == 'Select Model')
		{
			alert ('Select Model!');
			return false;
		}
	
		if (trim( $('#parts option:selected').text()) == 'Select Part')
		{
			alert ('Select Part!');
			return false;
		}
		
		return true;
		
	}

	if (formname == 'askQuestionForm')
	{
		if (isEmpty('qstnInpName') )
		{
			alert ('Please enter Your Name!');
			return false;
		}
	
		if (isEmpty('qstnInpEmail') )
		{
			alert ('Please enter Your E-mail!');
			return false;
		}
	
		if (isEmpty('qstnTxtQuestion') )
		{
			alert ('Please enter Question text!');
			return false;
		}
		
		return true;
	}

	return false;
}

function validateAskQuestion(part)
{

	if (! validateForm ('askQuestionForm'))
		return false;
	
	if (panel3)
		panel3.hide();
	
	xajax_xaj_askQuestion(
			$('#qstnInpName').val(),
			$('#qstnInpEmail').val(),
			$('#qstnTxtQuestion').val(),
			part
	);
	
	getbyId('qstnFrmQuestion').reset();

	return true;
}

function trim(string)
{
	return jQuery.trim(string);
}


function getbyId(id)
{
    return document.getElementById(id);
}

function doSearch1()
{
  xajax_getResults1(
				xajax.getFormValues('form2'),
				trim( $('#year1 option:selected').text() ),
				trim( $('#model option:selected').text() ),
				trim( $('#parts option:selected').text() ),
				trim( $('#userInterchange_select option:selected').text() ));
}

function resetSearch()
{
	getbyId('search_part').reset();
	$('#search_btn0').css('display', 'inline');
	$('#a_reset_search').css('display', 'none');

	if ( $('#right_title').innerHTML != 'Saved search')
		xajax_xaj_showSavedSearches();
}

function clearSearch()
{
	$('#search_btn0').css('display', 'inline');
	$('#a_reset_search').css('display', 'none');
	
	if ( trim($('#right_title').innerHTML) != 'Saved search')
	{
		xajax_xaj_showSavedSearches();
	}
}

function doSearch()
{
	if ( !validateForm('saveSearchForm') )
		return;

  xajax_getResults(
				trim( $('#year1 option:selected').text() ),
				trim( $('#model option:selected').text() ),
				trim( $('#parts option:selected').text() ) );

}

function isEmpty(id)
{
		var txt = $('#' + id).val();

		if ( trim(txt) == '' )
				return true;
		else
				return false;
}

function sendFriend()
{
		if ( isEmpty('memail') && isEmpty('femail') )
		{
				alert ('Your email and recipeint`s email must be provided!')
				return 0;
		}
		
	xajax_sendFriend(
					$('#memail').val(),
					$('#femail').val(),
					$('#name').val(),
					$('#send_copy').checked,
					$('#message').value
	);
	
}

function checkData()
{
		if ( isEmpty('email') && isEmpty('phone') )
		{
				alert ('Email or phone must be provided!')
				return 0;
		}

		var lbl=null;
		if ($('#form2') != null)
		{
				var radio = document.getElementsByName('userInterchange');
				for (var j=0; j < radio.length; j++)
				{
					if (radio[j].checked)
					{
						lbl = $('#lbl_'+radio[j].id).html();
						break;
					}			
				}
		}
		
		xajax_subscribeNtf(trim( $('#email').val() ),
											trim( $('#phone').val() ),
											trim( $('#year1 option:selected').text() ),
											trim( $('#model option:selected').text() ),
											trim( $('#parts option:selected').text() ),
											 lbl);
}

