function Trim(str) { if(str) return( str.replace(/^\s*([\s\S]*\S+)\s*$|^\s*$/,'$1') ); else return ""; }
function RetFalse(obj,msg) { alert(msg);obj.focus();obj.select();return false; }

function ChangeMenuPic(num,admin,color) {
  if (admin==1) eval("document.all.AdminMenuBar"+num).src="images/"+color+"Btn.gif";
  else eval("document.all.MenuBar"+num).src="images/"+color+"Btn.gif";
}

function Go2Link(theLink) {
  window.location.href = theLink;
}

function Go2LinkNP(theLink,winName,prop) {
  var win = window.open(theLink,winName,prop);
  win.scrollBy(140,250);
}

function FixNumberDisplay(num) {
//  if ((num>0) && (num<10)) return "0"+num;
//  else return (new String(num));
  return num;
}

function ShortDate(pStartDate,pEndDate) {
  var shortDateString = "";
  pStartDate = pStartDate.replace("-","/").replace("-","/");
  pEndDate = pEndDate.replace("-","/").replace("-","/");
  startDate = new Date(pStartDate);
  endDate = new Date(pEndDate);
  if (startDate.getFullYear() != endDate.getFullYear()) {
    shortDateString = FixNumberDisplay(startDate.getDate())+"/"+FixNumberDisplay((startDate.getMonth()+1))+"/"+startDate.getFullYear()+"-"+FixNumberDisplay(endDate.getDate())+"/"+FixNumberDisplay((endDate.getMonth()+1))+"/"+endDate.getFullYear();
  } else if (startDate.getMonth() != endDate.getMonth()) {
    shortDateString = FixNumberDisplay(startDate.getDate())+"/"+FixNumberDisplay((startDate.getMonth()+1))+"-"+FixNumberDisplay(endDate.getDate())+"/"+FixNumberDisplay((endDate.getMonth())+1)+"/"+endDate.getFullYear();
  } else if (startDate.getDate() != endDate.getDate()) {
    shortDateString = FixNumberDisplay(startDate.getDate())+"-"+FixNumberDisplay(endDate.getDate())+"/"+FixNumberDisplay((endDate.getMonth()+1))+"/"+endDate.getFullYear();
  } else {
    shortDateString = FixNumberDisplay(endDate.getDate())+"/"+FixNumberDisplay((endDate.getMonth()+1))+"/"+endDate.getFullYear();
  }
  return shortDateString;
}

function WeekPart(pStartDate,pEndDate) {
  pStartDate = pStartDate.replace("-","/").replace("-","/");
  pEndDate = pEndDate.replace("-","/").replace("-","/");
  startDate = new Date(pStartDate);
  endDate = new Date(pEndDate);
  if (endDate.getDay() > 4 || endDate.getDay() < 2) return "סופ\"ש";
  else return "אמצ\"ש";
}

function GetDayName(pDate) {
  pDate = pDate.replace("-","/").replace("-","/");
  vdate = new Date(pDate);
  if (vdate.getDay() == 0) return "ראשון";
  else if (vdate.getDay() == 1) return "שני";
  else if (vdate.getDay() == 2) return "שלישי";
  else if (vdate.getDay() == 3) return "רביעי";
  else if (vdate.getDay() == 4) return "חמישי";
  else if (vdate.getDay() == 5) return "שישי";
  else return "שבת";
}

function CreateFlashObject(source,width,height) {
  document.writeln("<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0' width='"+width+"' height='"+height+"' align='middle'>");
//  document.writeln("  <param name='allowScriptAccess' value='sameDomain' /><param NAME='wmode' VALUE='transparent' />");
  document.writeln("  <param name='allowScriptAccess' value='sameDomain' />");
  document.writeln("  <param name='movie' value='"+source+"' /><param name='quality' value='high' /><param name='bgcolor' value='#ffffff' />");
  document.writeln("  <embed src='"+source+"' quality='high' bgcolor='#ffffff' width='"+width+"' height='"+height+"' align='middle' allowScriptAccess='sameDomain' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' />");
  document.writeln("</object>");
}

function LoadMainFlash() {
  var str="";
  var source="flash_main.swf";
  var width=682;
  var height=200;
  str+="<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0' width='"+width+"' height='"+height+"' align='middle'>";
  str+="  <param name='allowScriptAccess' value='sameDomain' />";
  str+="  <param name='movie' value='"+source+"' /><param name='quality' value='high' /><param name='bgcolor' value='#ffffff' />";
  str+="  <embed src='"+source+"' quality='high' bgcolor='#ffffff' width='"+width+"' height='"+height+"' align='middle' allowScriptAccess='sameDomain' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' />";
  str+="</object>";
  document.getElementById("FlashDiv").innerHTML=str;
}

function CreateFlashObjectWO(source) {
  document.writeln("<object classid='clsid:d27cdb6e-ae6d-11cf-96b8-444553540000' codebase='http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0' align='middle'>");
  document.writeln("  <param name='allowScriptAccess' value='sameDomain' /><param NAME='wmode' VALUE='transparent' />");
  document.writeln("  <param name='movie' value='"+source+"' /><param name='quality' value='high' /><param name='bgcolor' value='#ffffff' />");
  document.writeln("  <embed src='"+source+"' quality='high' bgcolor='#ffffff' align='middle' allowScriptAccess='sameDomain' type='application/x-shockwave-flash' pluginspage='http://www.macromedia.com/go/getflashplayer' />");
  document.writeln("</object>");
}

function del(index) {
document.getElementById("Del"+index).value = "on";
}
function ShowCart(CartItems,isCheckout) {
//Item Line: RowID | ProductID | Details | Child Price | Childs num | Adult Price | Adult num | Att Type
  var cells = 5;
  if (isCheckout) cells--;
  //else cells--;
  if (CartItems.indexOf("|h")==-1) cells--;
  if (CartItems=="") {
    document.write("<tr><td colspan='"+cells+"' align='center'>הסל ריק</td></tr>");
  } else {
    CartItems=CartItems.split("|^^|");
    var Sum=0;

    for (i in CartItems) {
      SpecItem=CartItems[i].split("|");
      document.write("<tr>");
       if (!isCheckout) {
         if (CartItems.length==1) document.write("<td align='center'><input type='hidden' value='' name='Del"+i+"' id='Del"+i+"'></td>");
         else document.write("<td title='סמן על מנת למחוק פריט זה' align='center'><input type='image' src='images/trash.gif' onclick='del("+i+");this.form.submit();'><input type='hidden' value='' name='Del"+i+"' id='Del"+i+"'></td>");
}
      document.write("<td>"+SpecItem[2]+"<br><a href='Attrcts.asp?Action=Page&AttID="+SpecItem[1]+"' style='font-size:xx-small;'>[לפרטים נוספים]</a><input type='hidden' name='Name"+i+"' id='Name"+i+"' value='"+SpecItem[1]+"|"+SpecItem[2]+"'></td>");
      if (isCheckout) {
        if (SpecItem[7]=="h") document.write("<td align='center' dir='ltr'>₪ "+SpecItem[3]+" x "+SpecItem[4]+"<input type='hidden' name='ChildPrice"+i+"' id='ChildPrice"+i+"' value='"+SpecItem[3]+"'><input type='hidden' name='ChildAmount"+i+"' id='ChildAmount"+i+"' value='"+SpecItem[4]+"' style='width:13px;' maxlength='1'></td>");
        else document.write("<input type='hidden' name='ChildPrice"+i+"' id='ChildPrice"+i+"' value='"+SpecItem[3]+"'><input type='hidden' name='ChildAmount"+i+"' id='ChildAmount"+i+"' value='"+SpecItem[4]+"' style='width:13px;' maxlength='1'>");
        document.write("<td align='center' dir='ltr'>₪ "+SpecItem[5]+" x "+SpecItem[6]+" <input type='hidden' name='AdultPrice"+i+"' id='AdultPrice"+i+"' value='"+SpecItem[5]+"'><input type='hidden' name='AdultAmount"+i+"' id='AdultAmount"+i+"' value='"+SpecItem[6]+"' style='width:13px;' maxlength='1'></td>");
      } else {
        if (SpecItem[7]=="h") document.write("<td align='center' dir='ltr'>₪ "+SpecItem[3]+" x <input type='text' name='ChildAmount"+i+"' id='ChildAmount"+i+"' onkeyup='SumUp(this.form);' value='"+SpecItem[4]+"' style='width:13px;' maxlength='1'><input type='hidden' name='ChildPrice"+i+"' id='ChildPrice"+i+"' value='"+SpecItem[3]+"'></td>");
        else document.write("<input type='hidden' name='ChildPrice"+i+"' id='ChildPrice"+i+"' value='"+SpecItem[3]+"'><input type='hidden' name='ChildAmount"+i+"' id='ChildAmount"+i+"' value='"+SpecItem[4]+"' style='width:13px;' maxlength='1'>");
        document.write("<td align='center' dir='ltr'>₪ "+SpecItem[5]+" x <input type='text' name='AdultAmount"+i+"' id='AdultAmount"+i+"' onkeyup='SumUp(this.form);' value='"+SpecItem[6]+"' style='width:13px;' maxlength='1'><input type='hidden' name='AdultPrice"+i+"' id='AdultPrice"+i+"' value='"+SpecItem[5]+"'></td>");
      }
      document.write("<td align='center'><span id='sum"+i+"'>"+parseFloat(parseFloat(SpecItem[4])*parseInt(SpecItem[3])+parseFloat(SpecItem[6])*parseInt(SpecItem[5]))+" ₪</span></td>");
      document.write("</tr>");
      document.write("<input type='hidden' name='AttType"+i+"' id='AttType"+i+"' value='"+SpecItem[7]+"'>");
      document.write("<input type='hidden' name='RowID"+i+"' id='RowID"+i+"' value='"+SpecItem[0]+"'>");
      Sum=Sum+(parseFloat(SpecItem[4])*parseInt(SpecItem[3])+parseFloat(SpecItem[6])*parseInt(SpecItem[5]));
    }
    document.write("<tr bgcolor='#E2E2E2'><td colspan='"+(--cells)+"' align='left'><b><font color='red'>סה\"כ</font></b>&nbsp;<input type='hidden' name='NumberOfItems' id='NumberOfItems' value='"+i+"'></td><td align='center'><font color='red'><b><span id='Sum'>"+Sum+" ₪</span></b></font></td></tr>");

// הערה: בטיולים וימי כיף חד יומיים, המחיר למבוגר / ילד, זהה.
  }
}

function WriteSum(CartItems) {
  CartItems=CartItems.split("|^^|");
//Item Line: RowID | ProductID | Details | Child Price | Childs num | Adult Price | Adult num | Att Type
  var Sum=0;
  for (i in CartItems) {
    SpecItem=CartItems[i].split("|");
    if (SpecItem[0].indexOf("CP")!=-1) Sum=Sum-parseFloat(Sum*SpecItem[4]/100);
    else if (SpecItem[0].indexOf("CM")!=-1) Sum=Sum-SpecItem[4];
    else Sum=Sum+(parseFloat(SpecItem[4])*parseInt(SpecItem[3]));
  }
  document.write(Sum+" ₪");
}

var cartEmpty=false;
function CheckAmount(TheForm,Action) {
  cartEmpty=false;
/*
  for (i=0;i<=parseInt(TheForm.NumberOfItems.value);i++) {
    document.getElementById("ChildAmount"+i).value = Trim(document.getElementById("ChildAmount"+i).value);
    document.getElementById("AdultAmount"+i).value = Trim(document.getElementById("AdultAmount"+i).value);
    if (!(/^[0-9]+$/.test(document.getElementById("ChildAmount"+i).value))) return RetFalse(document.getElementById("ChildAmount"+i), "שגיאה: כמות לא תקינה.");
    if (!(/^[0-9]+$/.test(document.getElementById("AdultAmount"+i).value))) return RetFalse(document.getElementById("AdultAmount"+i), "שגיאה: כמות לא תקינה.");
    if (parseInt(document.getElementById("AdultAmount"+i).value)==2 && parseInt(document.getElementById("ChildAmount"+i).value)>2) return RetFalse(document.getElementById("AdultAmount"+i), "שגיאה: כמות לא תקינה.\nבחדר יכולים להיות עד 3 מבוגרים וילד יחיד או עד 2 מבוגרים ו2 ילדים.");
    if (parseInt(document.getElementById("AdultAmount"+i).value)==3 && parseInt(document.getElementById("ChildAmount"+i).value)>1) return RetFalse(document.getElementById("AdultAmount"+i), "שגיאה: כמות לא תקינה.\nבחדר יכולים להיות עד 3 מבוגרים וילד יחיד או עד 2 מבוגרים ו2 ילדים.");
    if (parseInt(document.getElementById("AdultAmount"+i).value)<2) return RetFalse(document.getElementById("AdultAmount"+i), "שגיאה: כמות לא תקינה.\nמינימום מבוגרים בחדר הינו 2.");
  }
  if (Action=="Checkout") {
    for (i=0;i<=parseInt(TheForm.NumberOfItems.value);i++) if ((parseInt(eval("TheForm.AdultAmount"+i).value)>0)) cartEmpty=false;
    if (cartEmpty) {
      alert("פריטי הנופש שלך סומנו למחיקה.\nבשל כך אינך יכול לגשת לקופה.");
      return false;
    }
  }
*/
  if (!SumUp(TheForm)) return false;
  TheForm.SubmitButton.disabled=true;
  return true;
}

function SumUp(TheForm) {
var sum = 0;
var sumin = 0;
  for (i=0;i<=parseInt(TheForm.NumberOfItems.value);i++) {
    sumin=0;
    document.getElementById("ChildAmount"+i).value = Trim(document.getElementById("ChildAmount"+i).value);
    document.getElementById("AdultAmount"+i).value = Trim(document.getElementById("AdultAmount"+i).value);
    if (!(/^[0-9]+$/.test(document.getElementById("ChildAmount"+i).value))) return RetFalse(document.getElementById("ChildAmount"+i), "שגיאה: כמות לא תקינה.");
    if (!(/^[0-9]+$/.test(document.getElementById("AdultAmount"+i).value))) return RetFalse(document.getElementById("AdultAmount"+i), "שגיאה: כמות לא תקינה.");
    if (document.getElementById("AttType"+i).value == 'h' && parseInt(document.getElementById("AdultAmount"+i).value)==2 && parseInt(document.getElementById("ChildAmount"+i).value)>2) return RetFalse(document.getElementById("AdultAmount"+i), "שגיאה: כמות לא תקינה.\nבחדר יכולים להיות עד 3 מבוגרים וילד יחיד או עד 2 מבוגרים ו2 ילדים.");
    if (document.getElementById("AttType"+i).value == 'h' && parseInt(document.getElementById("AdultAmount"+i).value)==3 && parseInt(document.getElementById("ChildAmount"+i).value)>1) return RetFalse(document.getElementById("AdultAmount"+i), "שגיאה: כמות לא תקינה.\nבחדר יכולים להיות עד 3 מבוגרים וילד יחיד או עד 2 מבוגרים ו2 ילדים.");
    if (document.getElementById("AttType"+i).value == 'h' && parseInt(document.getElementById("AdultAmount"+i).value)<2) return RetFalse(document.getElementById("AdultAmount"+i), "שגיאה: כמות לא תקינה.\nמינימום מבוגרים בחדר הינו 2.");
    if (document.getElementById("AttType"+i).value == 'h' && parseInt(document.getElementById("AdultAmount"+i).value)>3) return RetFalse(document.getElementById("AdultAmount"+i), "שגיאה: כמות לא תקינה.\nמקסימום מבוגרים בחדר הינו 3.");
    if (parseInt(document.getElementById("AdultAmount"+i).value)<=0) return RetFalse(document.getElementById("AdultAmount"+i), "שגיאה: כמות לא תקינה.");
    sumin += parseInt(document.getElementById("AdultAmount"+i).value)*parseInt(document.getElementById("AdultPrice"+i).value);
    sumin += parseInt(document.getElementById("ChildAmount"+i).value)*parseInt(document.getElementById("ChildPrice"+i).value);
    document.getElementById("sum"+i).innerText = sumin + " ₪";
    sum+=sumin;
  }
  document.getElementById("Sum").innerText = sum + " ₪";
  return true;
}

function CheckSearch(TheForm) {
//var fromDate = new Date(TheForm.FromDate.value);
//alert(TheForm.FromDate.value+"\t"+fromDate+"\t"+Date()+"\t"+(fromDate<Date()));
	if (Date(TheForm.FromDate.value)<Date() && Date(TheForm.ToDate.value)<Date(TheForm.FromDate.value)) return RetFalse(TheForm.FromDate, "שגיאה: התאריכים שהוזנו לא תקינים.");
}

function updateCC(obj) {
	if (obj.value.substring(obj.value.length-1) < '0' || obj.value.substring(obj.value.length-1) > '9') {
		obj.value = obj.value.substring(0,obj.value.length-1);
		return false;
	}
  	if (obj.value.length == 4 || obj.value.length == 9 || obj.value.length == 14) obj.value += "-";
}
