var xmlHttp;

function Clicky(status){
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  }    

  
var url="index.php?class=gallery&action=getPicture";
url=url+"&status=" + status;
url=url+"&sid="+Math.random();
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);
}

//selectCategory

function selectCategory(id){

xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
  {
  alert ("Your browser does not support AJAX!");
  return;
  }

var url = "index.php?class=kalorikalkulaator&action=selectCategoryCalculator&category="+id;
xmlHttp.onreadystatechange=stateChanged;
xmlHttp.open("GET",url,true);
xmlHttp.send(null);

}

function stateChanged() 
{ 
if (xmlHttp.readyState==4)
{ 
document.getElementById("Hint").innerHTML=xmlHttp.responseText;
}
} 

function GetXmlHttpObject()
{
var xmlHttp=null;
try
  {
  xmlHttp=new XMLHttpRequest();
  }
catch (e)
  {
  try
    {
    xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
    }
  catch (e)
    {
    xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
  }
return xmlHttp;
}
//load

function load(id,type,lang) {
var load = window.open("Gallery_ajx/contacts.php?id="+id+"&type="+type+"&lang="+lang,"","scrollbars=no,menubar=0,height=350,width=360,resizable=no,toolbar=no,location=0,status=0");
}

function textCounter(field,cntfield,maxlimit) {
if (field.value.length > maxlimit)
field.value = field.value.substring(0, maxlimit);
else
cntfield.value = maxlimit - field.value.length;
}

//popup

function popup(item_nr,item_name,lang){
var load = window.open("components/Catalog/bookmark.php?action=view&item_nr="+item_nr+"&item_name="+item_name+"&lang="+lang,"","scrollbars=no,menubar=0,height=350,width=500,resizable=no,toolbar=no,location=0,status=0");
}

//images popup


function popimages(id){
var load = window.open("components/Catalog/images.php?id=" + id,"","scrollbars=yes,menubar=0,height=750,width=500,resizable=no,toolbar=no,location=0,status=0");
}

function print(url){
  var load = window.open("index.php?" + url,"","scrollbars=no,menubar=yes,height=0,width=0,resizable=yes,toolbar=no,location=0,status=0");
}

  
function printPage() {
if (window.print)
   window.print();
}


function setCookie(c_name,value,expiredays){
var exdate=new Date();exdate.setDate(exdate.getDate()+expiredays);
document.cookie=c_name+ "=" +escape(value)+
((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

// AUTOQUOTE

function autoquote(){

x = autoquoteController();

if(x == false)
return;

switch(document.getElementById("granite_selector").value){

case "Rosa_Limbara":
 price = 2750;
break;

case "Kashmir_White":
 price = 2750;
break;

case "Nero_Africa":
 price = 2750;
break;

case "Verde_Bahia":
 price = 2750;
break;

case "Multicolor_Red":
 price = 2750;
break;

case "Balmoral_Red":
 price = 2750;
break;

case "Santa_Cecilia_Yellow":
 price = 2750;
break;

case "Baltic_Brown":
 price = 2750;
break;

case "Juparana_Colombo":
 price = 3160;
break;

case "Star_Gate":
 price = 3160;
break;

case "Angola_Black":
 price = 3160;
break;

case "Tan_Brown":
 price = 3160;
break;

case "Ivory_Brown":
 price = 3160;
break;

case "Nero_Assoluto":
 price = 3160;
break;

case "Verde_Maritaca":
 price = 3160;
break;

case "Carrara_Marble":
 price = 3160;
break;

case "Emerald_Pearl":
 price = 3560;
break;

case "Star_Galaxy":
 price = 3560;
break;

case "Blue_Pearl":
 price = 3560;
break;

case "Giallo_Veneziano":
 price = 3560;
break;

case "Black_Quartz":
 price = 3560;
break;

case "White_Quartz":
 price = 4560;
break;

case "Gray_Quartz":
 price = 3560;
break;

case "Red_Galaxy_Quartz":
 price = 3560;
break;

case "Blue_Eyes":
 price = 7400;
break;

default: //test
 price = 10000;

}//switch

size = 0;

for(i=1;i<8;i++){
 size += document.getElementById(i+"_peace_length").value * document.getElementById(i+"_peace_width").value;
} 

piecePrice = price * size / 1000000;


hobPrice = document.getElementById("number_of_cut_outs_for_hob").value * 810;//1300

cutOutsForSinkPrice = 
 document.getElementById("number_of_cut_outs_for_sink_over_mounted").value * 1141 + //1700
 document.getElementById("number_of_cut_outs_for_sink_flush_fit").value * 1517 + //2500
 document.getElementById("number_of_cut_outs_for_sink_under_mounted").value * 1517 + //2400
 document.getElementById("number_of_cut_outs_for_sink_under_up_mounted").value * 1733;//2700
 
tapHolesPrice = document.getElementById("number_of_tap_and_switch_holes").value * 72;//100

if(document.getElementById("rounded_corners_radius100").checked)
 roundedCornersPrice = document.getElementById("rounded_corners_r_100").value * 188; //500 ???
else if(document.getElementById("rounded_corners_radius300").checked)
 roundedCornersPrice = document.getElementById("rounded_corners_r_100").value * 260; //500 ???
else
 roundedCornersPrice = document.getElementById("rounded_corners_r_100").value * 500;

curvedShapedCutsPrice = (
document.getElementById("total_length_curved_square").value * 800 + 
document.getElementById("total_length_curved_halfRounded").value * 800 + 
document.getElementById("total_length_curved_rounded").value * 800
) / 1000;


upstands1 = document.getElementById("1_upstands_height").value * document.getElementById("1_upstands_length").value;
upstands2 = document.getElementById("2_upstands_height").value * document.getElementById("2_upstands_length").value;
upstands3 = document.getElementById("3_upstands_height").value * document.getElementById("3_upstands_length").value;

upstandsPrice = (upstands1 + upstands2 + upstands3) * 500 / 100000; //500 //1400

drainerGroovesPrice = document.getElementById("sets_of_drainer_grooves").value * 925; //200 ???

polishedSquareEdges = (
 document.getElementById("total_length_straight_square").value * 144 + //170
 document.getElementById("total_length_straight_halfRounded").value * 289 + //320
 document.getElementById("total_length_straight_rounded").value * 376 )/1000; //440
 
sizeCalculationsPrice = 1250;
setUpPrice = 2750;

document.getElementById("measuring_up").value = sizeCalculationsPrice + " SEK";

document.getElementById("totalPrice").value = piecePrice + hobPrice + cutOutsForSinkPrice + tapHolesPrice + roundedCornersPrice + 
drainerGroovesPrice + polishedSquareEdges + sizeCalculationsPrice + setUpPrice + upstandsPrice + curvedShapedCutsPrice + " SEK";

document.getElementById("number_of_pieces").value = x;

}


function autoquoteController(){

x = 0;

for(i=1;i<8;i++){
 
if(document.getElementById(i+"_peace_length").value == ""){
 continue;

}else if(document.getElementById(i+"_peace_length").value < 100){
 getError(i+"_peace_length","Length of piece cannot be less then 100 mm");
 return false;

}else if(document.getElementById(i+"_peace_width").value < 100){
getError(i+"_peace_width","Width of piece cannot be less then 100 mm");
return false; 

}//else if

 x++;

}//for

if(x == 0){
getError("1_peace_length","Please enter length and height atleast of 1 peace");
 return false;
}//if



//2.
if(quoteChecker("total_length_straight_square","You forgot to fill total length straight square",
"The total length straight square cannot be less then 100mm",100) == false)
return false;

if(quoteChecker("total_length_straight_halfRounded","You forgot to fill total length straight half-rounded",
"The total length straight half-rounded cannot be less then 100mm",100) == false)
return false;

if(quoteChecker("total_length_straight_rounded","You forgot to fill total length straight rounded",
"The total length straight rounded cannot be less then 100mm",100) == false)
return false;



//3.
if(quoteChecker("total_length_curved_square","You forgot to fill total length curved square",
"The total length curved square cannot be less then 100mm",100) == false)
return false;

if(quoteChecker("total_length_curved_halfRounded","You forgot to fill total length curved half-rounded",
"The total length curved half-rounded cannot be less then 100mm",100) == false)
return false;

if(quoteChecker("total_length_curved_rounded","You forgot to fill total length curved rounded",
"The total length curved rounded cannot be less then 100mm",100) == false)
return false;


//4.
if(quoteChecker("rounded_corners_r_100","You forgot to fill number of rounded corners R < 100 mm","",1) == false)
return false;


//5.
for(i=1;i<4;i++){
if(quoteChecker(i+"_upstands_height","You forgot to fill 5."+ i +" upstands height","5."+ i +
" upstands height cannot be less then 50mm",50) == false)
return false;

if(quoteChecker(i+"_upstands_length","You forgot to fill 5."+ i +" upstands length","5."+ i +
" upstands length cannot be less then 100mm",100) == false)
return false;

}//for


//6.
//if(quoteChecker("rounded_corners_r_100_300","You forgot to fill number of rounded corners 100 < R <300 mm","",1) == false)
//return false;

//7.
if(quoteChecker("sets_of_drainer_grooves","You forgot to fill number of sets (1 set = 5 grooves) of drainer grooves","",1) == false)
return false;


//8.
if(quoteChecker("number_of_cut_outs_for_sink_over_mounted","You forgot to fill umber of cut-outs for sink over-mounted","",1) == false)
return false;
if(quoteChecker("number_of_cut_outs_for_sink_flush_fit","You forgot to fill umber of cut-outs for sink flush-fit","",1) == false)
return false;
if(quoteChecker("number_of_cut_outs_for_sink_under_mounted","You forgot to fill umber of cut-outs for sink under-mounted","",1) == false)
return false;
if(quoteChecker("number_of_cut_outs_for_sink_under_up_mounted","You forgot to fill umber of cut-outs for sink under-up mounted","",1) == false)
return false;

//9.
if(quoteChecker("number_of_cut_outs_for_hob","You forgot to fill number of cut-outs for hob","",1) == false)
return false;


//10.
if(quoteChecker("number_of_tap_and_switch_holes","You forgot to fill number of tap and switch holes","",1) == false)
return false;


return x;

}

function quoteChecker(element,text1,text2,length){
  
if(document.getElementById(element).value == "" || document.getElementById(element).value == 0)
return true;  
  
if(document.getElementById(element).value < length){  
  getError(element,text2);
  return false;
}//if

return true;

}

function getError(element,text){
 document.getElementById(element).focus();
 alert(text);
 return;

}

//dynamic selector

function generateimage(which){
if (document.all){
dynamic3.innerHTML='<center>Loading image...</center>'
dynamic3.innerHTML='<a href="selector/'+which+'2.jpg" target="_blank"><img src="selector/'+which+'.jpg"></a>'

}else if(document.layers){
document.myForm.document.granite_selector.document.write('<a href="selector/'+which+'2.jpg" target="_blank"><img src="selector/'+which+'".jpg></a>')
document.myForm.document.granite_selector.document.close()

}else
dynamic3.innerHTML='<center>Loading image...</center>'
dynamic3.innerHTML='<a href="selector/'+which+'2.jpg" target="_blank"><img src="selector/'+which+'.jpg"></a>'

}

