//alert("Test");
var volume;
var max_fps=30;
var fps_limit=30;
var FPSchecked=-1;
var data = new Array();
var data_240 = new Array(60,85,150,450,600,1000);
var data_480 = new Array(75,100,200,650,800,1400);
var data_480_2card = new Array(60,80,160,500,640,1100);
var data_NV7000 = new Array(120,150,200,330,1000,1500);

function checkProduct(){

	document.getElementById("size_div").innerHTML="<select name=\"size\" size=\"1\" onchange=\"checkFPS()\"><option value=\"-1\"> </option><option value=\"0\">320x240</option><option value=\"1\">640x480</option><option value=\"2\">640x480(enhanced)</option></select>";

    if (document.form1.product.value == 8){            // PC Digital Guardian
        document.getElementById("card_txt_div").innerHTML="Capture Cards:";
		document.getElementById("card_select_div").innerHTML="<select name=\"card\" size=\"1\" onchange=\"checkCard()\"><option value=\"1\">1 card</option><option value=\"2\">2 cards</option><option value=\"3\">3 cards</option><option value=\"4\">4 cards</option></select>";
		/*document.form1.card.options[1].text="1 card";
        document.form1.card.options[2].text="2 cards";
        document.form1.card.options[3].text="";
        document.form1.card.options[4].text="";*/
    }
    /*else if (document.form1.product.value == 8){       // PC Digital Guardian
        document.form1.card.options[1].text="1 card";
        document.form1.card.options[2].text="";
        document.form1.card.options[3].text="";
        document.form1.card.options[4].text="";
    }*/
    else {
		document.getElementById("card_txt_div").innerHTML="Capture Cards:";
		document.getElementById("card_select_div").innerHTML="NA";
    }
    //document.form1.card.value = 0;
    //document.form1.channel.value = 0;
    
    if (document.form1.product.value == 1) {           // DVR4-Alert
        document.getElementById("chan_div").innerHTML="<select name=\"channel\" size=\"1\" onchange=\"checkChannel()\"><option value=\"4\">4</option></select>";
		
		/*document.form1.channel.options[1].text="4";
        document.form1.channel.options[2].text="";
        document.form1.channel.options[3].text="";
        document.form1.channel.options[4].text="";
        document.form1.channel.value = 16;
        document.form1.card.value = 1;*/
    }
	if (document.form1.product.value == 2) {           // DVR4 Advantage
        document.getElementById("chan_div").innerHTML="<select name=\"channel\" size=\"1\" onchange=\"checkChannel()\"><option value=\"4\">4</option></select>";
	}
	
	if (document.form1.product.value == 3) {           // DVR4 Elite
        document.getElementById("chan_div").innerHTML="<select name=\"channel\" size=\"1\" onchange=\"checkChannel()\"><option value=\"4\">4</option></select>";
	}
	
	if (document.form1.product.value == 4) {           // DVR4 Net Plus
        document.getElementById("chan_div").innerHTML="<select name=\"channel\" size=\"1\" onchange=\"checkChannel()\"><option value=\"4\">4</option></select>";
	}
	
	if (document.form1.product.value == 5) {           // DVR8 Net Plus
        document.getElementById("chan_div").innerHTML="<select name=\"channel\" size=\"1\" onchange=\"checkChannel()\"><option value=\"4\">4</option><option value=\"8\">8</option></select>";
	}
	
	if (document.form1.product.value == 6) {           // DVR16 Net Plus
        document.getElementById("chan_div").innerHTML="<select name=\"channel\" size=\"1\" onchange=\"checkChannel()\"><option value=\"4\">4</option><option value=\"8\">8</option><option value=\"12\">12</option><option value=\"16\">16</option></select>";
		
		
	}
	
	if (document.form1.product.value == 7) {           // DVR4 Motion
        document.getElementById("chan_div").innerHTML="<select name=\"channel\" size=\"1\" onchange=\"checkChannel()\"><option value=\"4\">4</option></select>";
	}
	
	if (document.form1.product.value == 8) {           // PC Guardian
        document.getElementById("chan_div").innerHTML="<select name=\"channel\" size=\"1\" onchange=\"checkChannel()\"><option value=\"4\">4</option><option value=\"8\">8</option><option value=\"12\">12</option><option value=\"16\">16</option></select>";
		
		document.getElementById("size_div").innerHTML="<select name=\"size\" size=\"1\" onchange=\"checkFPS()\"><option value=\"-1\"> </option><option value=\"0\">320x240</option><option value=\"1\">720x480</option></select>";
	}
    
    /*if (document.form1.product.value == 6) {           // DVR16-Net-Plus
        alert("pc DVR");
		document.form1.size.options[2].text="720x480";
        document.form1.size.options[3].text="";
    }
    else {
        document.form1.size.options[2].text="640x480";
        document.form1.size.options[3].text="640x480(enhanced)";
    }*/
    
}

function checkCard()
{
    if (document.form1.product.value == 8){  // PC DVR
    	if (document.form1.card.value == 1){ //
			document.getElementById("chan_div").innerHTML="<select name=\"channel\" size=\"1\" onchange=\"checkChannel()\"><option value=\"4\">4</option></select>";
		}
		
		if (document.form1.card.value == 2){ //
			document.getElementById("chan_div").innerHTML="<select name=\"channel\" size=\"1\" onchange=\"checkChannel()\"><option value=\"4\">4</option><option value=\"8\">8</option></select>";
		}
		
		if (document.form1.card.value == 3){ //
			document.getElementById("chan_div").innerHTML="<select name=\"channel\" size=\"1\" onchange=\"checkChannel()\"><option value=\"4\">4</option><option value=\"8\">8</option><option value=\"12\">12</option></select>";
		}
		
		if (document.form1.card.value == 4){ //
			document.getElementById("chan_div").innerHTML="<select name=\"channel\" size=\"1\" onchange=\"checkChannel()\"><option value=\"4\">4</option><option value=\"8\">8</option><option value=\"12\">12</option><option value=\"16\">16</option></select>";
		}
	}
        
    checkFPS();
}

function checkChannel(){
    checkFPS();
}

function checkFPS()
{
//	alert("Checking FPS");
    FPSchecked = -1;

    var product = document.form1.product.value;
	//if(document.form1.card.value){
	//    var card    = document.form1.card.value;
	//}
    var size    = document.form1.size.value;
    var ch      = document.form1.channel.value;
  //  alert("after vars");
   /* if (product==0 || card==0 || size==-1 || ch==0) return;
    if (product==3 && card!=1 && card!=2) return;
    else if (product==1 && card!=1) return;
    if (product==3 && card==2 && ch!=8 && ch!=16) return;
    else if (product==1 && card==1 && ch!=16) return;*/
    
    
    if (document.form1.product.value == 4)   //DVR4-Elite
    {
        data = data_NV7000;
        if (size==2) {                       //&ordf;&Aring;&ordf;&ordm;&iquest;&iuml;&para;&micro;
            return;
        }
        else if (size==1) {                  //720x480
            max_fps = 7;
        }
        else {                               //320x240
            max_fps = 30;
        }
        fps_limit = max_fps;
    }
    /*else if (document.form1.card.value == 2) //DVR Adv
    {
        if (size == 2){                      //640x480 enhanced
            data = data_480_2card;
            max_fps = 12;
        }
        else if (size == 1){                 //640x480
            data = data_240;
            max_fps = 15;
        }
        else {                               //320x240
            data = data_240;
            max_fps = 30;
        }
        fps_limit = Math.floor( max_fps/(ch/8) );
    }*/
    else                                     //&curren;@&curren;T&yen;|&plusmn;i&yen;d
    {
//		alert("card value not 2");
        if (size == 2){                      //640x480 enhanced
            data = data_480;
            max_fps = 15;
        }
        else if (size == 1){                 //640x480
            data = data_240;
            max_fps = 15;
        }
        else {                               //320x240
            data = data_240;
            max_fps = 30;
        }
        if (size != 0 && ch==4 ){
            fps_limit = max_fps;
        }
        else { 
            fps_limit = Math.floor( 30 /(ch/4) );
        }
    }
//	alert("Past some stuff");
    if (document.form1.product.value == 1)   //DVR4-Alert
        fps_limit = 2;
    if (document.form1.product.value == 2)   //DVR4-Advantage
        fps_limit = 7;

    document.form1.fps_range.value = " 1~" + fps_limit + " ";
    
    if (document.form1.fps.value <= fps_limit)
        FPSchecked = 1;
    else{
        FPSchecked = 0;
	}
//	alert(FPSchecked);
}
function checkAll()
{
    if (document.form1.product.value==0){
        alert("Please select the product type!!");
        return;
    }
    //if (document.form1.card.value==0){
    //    alert("Please select the number of cards!!");
    //    return;
    //}
    if (document.form1.channel.value==0){
        alert("Please select the number of channels!!");
        return;
    }
    if (document.form1.size.value==-1){
        alert("Please select the Video Size!!");
        return;
    }
    if (document.form1.quality.value<1 || document.form1.quality.value>100){
        alert("Please input Video Quality between 1~100.");
        return;
    }
    if (document.form1.fps.value<1 || document.form1.fps.value>30){
        alert("Please input Recording FrameRate between " + document.form1.fps_range.value + " fps.");
        return;
    }
    if (document.form1.hour.value<1 || document.form1.hour.value>24){
        alert("Please input Recording Hour(s) between 1~24.");
        return;
    }

    //------------------------------------------------------------------------//
    /*var product = document.form1.product.value;
    var card    = document.form1.card.value;
    var size    = document.form1.size.value;
    var ch      = document.form1.channel.value;
    
    if (product==3 && card!=1 && card!=2){
        alert("Please select the number of cards!!");
        return;
    }
    else if (product==1 && card!=1){
        alert("Please select the number of cards!!");
        return;
    }
    if (product==3 && card==2 && ch!=8 && ch!=16){
        alert("Please select the number of channels!!");
        return;
    }
    else if (product==1 && card==1 && ch!=16){
        alert("Please select the number of channels!!");
        return;
    }*/
    //------------------------------------------------------------------------//

    checkFPS();
}

function preCalculate()
{   
    checkAll();
    if (FPSchecked == 0) {
        alert("FPS cannot exceed " + fps_limit + " !");
        volume = 0;
        return;
    }
    else if (FPSchecked == -1){
        volume = 0;
        return;
    }
    
    step    = 20;
    quality = document.form1.quality.value;
    quant   = Math.floor(quality/step);
    if (quality>=100)
    {
        volume = data[5];
    }
    else if (quality>=80) 
    {
        diff     = quality - quant * step;
        interval = (data[5]/1.5) - data[quant];
        volume   = data[quant] + interval * diff / step;
    }
    else
    {
        diff     = quality - quant * step;
        interval = data[quant+1] - data[quant];
        volume   = data[quant] + interval * diff / step;
    }
    volume = volume * document.form1.fps.value / max_fps;
    volume = volume * document.form1.channel.value * document.form1.hour.value;
}

function calculateHDD()
{
    preCalculate();
    document.form1.HDD_result.value = Math.round(volume * document.form1.day.value / 1000 * 10) / 10;
}

function calculateDAY()
{
    preCalculate();
    document.form1.day_result.value = Math.round(document.form1.hdd.value * 1000 / volume * 10) / 10;
}
