var prices=new Array(139,69,129,65,309,409,379,129,109,54,150,75,349)
var addon_prices=new Array(39,0,0,0,0,0,0,0,19,9,19,19,19,19,9,9,5,5,19,19,9)
var speed_prices=new Array(40,39,20,10,30,15)
var delivery_times=new Array(1,2,2,3,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,13,14,14,15,15)
var express_delivery_times=new Array(0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,6,6,6,6,7,7,7,7)
var bulk_discount=new Array(0,0,0,0,4,4,4,4,4,4,8,8,8,8,8,12,12,12,12,12,16,16,16,16,16,16,16,16,16)
var currency_sign="$"
var email=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
var num=/^\d{1,3}$/
var url=/^(https?|ftp):\/\/.*\..+$/i
var empty=/.+/
var W3CDOM=(document.createElement && document.getElementsByTagName);
var month=new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec");
var defnotes="Enter your notes here"

function CheckContact(){
var err=''
var erno=0
	$("email").style.border="1px solid #524C4F"
	$("msg").style.border="1px solid #524C4F"
	if(!empty.test($("msg").value))
		erno=2
	if(!email.test($("email").value))
		erno=1
	if(erno!=0){
		switch(erno){
			case 1:
				$("email").style.border="1px solid #C11B17"
				err+="Enter valid email.\r\n"
				alert(err)
				$("email").focus()
				break;
			case 2:
				$("msg").style.border="1px solid #C11B17"
				err+="Enter your message.\r\n"
				alert(err)
				$("msg").focus()
				break;
		}
		return false
	}
	return true
}
function CheckOrder(){
var err=''
var erno=0
	$("email").style.border="1px solid #524C4F"
	$("nrpage2").style.border="1px solid #524C4F"
	if(!empty.test($("nrpage2").value)){
		erno=2
	}
	if(!email.test($("email").value)){
		erno=1
	}
	if(erno!=0){
		switch(erno){
			case 1:
				$("email").style.border="1px solid #C11B17"
				err+="Enter valid email.\r\n"
				alert(err)
				$("email").focus()
				break;
			case 2:
				$("nrpage2").style.border="1px solid #C11B17"
				err+="Enter the number of pages.\r\n"
				alert(err)
				$("nrpage2").focus()
				break;
		}
		return false
	}
	if($("msg").value==defnotes)
		$("msg").value=''
	return true
}
function $(e){
	return document.getElementById(e)
}
function countLines(strtocount,cols){
	var hard_lines=1
	var last=0
	var soft_lines=Math.round(strtocount.length/(cols-1))
	do{
		last=strtocount.indexOf("\n",last+1)
		hard_lines++
	}while(last!=-1)
	if(hard_lines>soft_lines)
		return hard_lines
	return soft_lines
}
function cleanForm() {
	$("msg").rows=countLines($("msg").value,$("msg").cols)+1
	setTimeout("cleanForm();",150)
}
function getDeliveryDateObj(start,businessDaysLeftForDelivery) {
	var now = new Date();
	now.setDate(now.getDate()+start);
	var dayOfTheWeek = now.getDay();
	var calendarDays = businessDaysLeftForDelivery;
	var deliveryDay = dayOfTheWeek + businessDaysLeftForDelivery;
	if (deliveryDay >= 6) {
	businessDaysLeftForDelivery -= 6 - dayOfTheWeek;  //deduct this-week days
	calendarDays += 2;  //count this coming weekend
	deliveryWeeks = Math.floor(businessDaysLeftForDelivery / 5); //how many whole weeks?
	calendarDays += deliveryWeeks * 2;  //two days per weekend per week
	}
	now.setTime(now.getTime() + calendarDays * 24 * 60 * 60 * 1000);
	return now;
}
function calculateprice(){
	var nrpage=parseInt($("nrpage2").value)
	var fprice
	var bdis=0
	var addons=0
	var mprice=new Array(0,0)
	var start=0
	var timedays=0
	var speed=0
	var delay=0

	if((isNaN(nrpage))||(nrpage>=29)||(nrpage<1)||(!num.test($("nrpage2").value))){
		$("nrpage2").innerHTML="-"
		$("data_dead").innerHTML="-"
		$("time").innerHTML=$("data_dead").innerHTML
		$("data_dis").innerHTML="-"
		$("start").innerHTML=$("data_dis").innerHTML
		
		$("data_price").innerHTML="-"
		$("price").innerHTML="-"
		$("tprice").value="-"
		$("tdays").value="-"
		$("tdead").value="-"
		return false;
	}

	// Speed
	if($("option_speed1").checked){ speed=-2 }
	if($("option_speed4").checked){ speed=1 }
	if($("option_speed5").checked){ speed=2 }

	// Jquery
	if($("option_jquery_lightbox").checked){ addons+=19 }
	if($("option_jquery_accordion").checked){ addons+=19 }
	if($("option_jquery_slideshow").checked){	addons+=19 }
	if($("option_jquery_showhide").checked){ addons+=9 }
	if($("option_jquery_tabs").checked){ addons+=9 }
	if($("option_jquery_background").checked){ addons+=9 }
	if($("option_jquery_scroll").checked){ addons+=9 }
	if($("option_jquery_calendar").checked){ addons+=9 }

	// Addons
	if($("option_linkall").checked){ addons+=19 }
	if($("option_printstyle2").checked){ addons+=addon_prices[8] }
	if($("option_lateststandards").checked){ addons+=addon_prices[10] }
	if($("option_ie6").checked){ addons+=addon_prices[11] }
	if($("option_dw").checked){	addons+=addon_prices[12] }
	if($("option_megadd").checked){ addons+=addon_prices[13] }
	if($("option_customform").checked){ addons+=addon_prices[13] }
	if($("option_ipad").checked){ addons+=addon_prices[14] }
	if($("option_loadspeed").checked){ addons+=addon_prices[15] }
	if($("option_sifr").checked){ addons+=addon_prices[16] }
	if($("option_footerstick").checked){ addons+=addon_prices[17] }
	if($("option_liquid").checked){ addons+=addon_prices[18] }
	if($("option_ff").checked){ addons+=addon_prices[19] }
	if($("option_wcagaaa").checked){ addons+=addon_prices[20] }

	// Integration
	if($("integration_xhtml").checked){
		delay=0
		mprice[0]=prices[0]
		mprice[1]=prices[1]
	}
	if($("integration_email").checked){
		delay=1
		mprice[0]=prices[0]+10
		mprice[1]=prices[0]+10
	}
	if($("integration_wp").checked){
		delay=2
		mprice[0]=prices[4]
		mprice[1]=prices[1]
	}
	if($("integration_jl").checked){
		delay=2
		mprice[0]=prices[5]
		mprice[1]=prices[1]
	}
	if($("integration_dp").checked){
		delay=4
		mprice[0]=prices[6]
		mprice[1]=prices[1]
	}
	if($("integration_oc").checked){
		delay=2
		mprice[0]=prices[10]
		mprice[1]=prices[11]
	}
	if($("integration_cmsms").checked){
		delay=2
		mprice[0]=349
		mprice[1]=prices[1]
	}

	switch(speed){
		case -2:
			timedays=express_delivery_times[nrpage-1]+Math.round(delay/2)
			addons+=Math.round((mprice[0]+(nrpage-1)*mprice[1]+addons)*speed_prices[0]/100)
			break;
		case -1:
			timedays=delivery_times[nrpage-1]-1+delay
			addons+=speed_prices[1]
			break;
		case 0:
			timedays=delivery_times[nrpage-1]+delay
			break;
		case 1:
			timedays=delivery_times[nrpage-1]+1+delay
			mprice[0]-=speed_prices[2]
			mprice[1]-=speed_prices[3]
			break;
		case 2:
			timedays=delivery_times[nrpage-1]+2+delay
			mprice[0]-=speed_prices[4]
			mprice[1]-=speed_prices[5]
			break;
	}
	fprice=mprice[0]+(nrpage-1)*mprice[1]+addons
	/*if(bulk_discount[nrpage-1]>0){
		fprice-=fprice*bulk_discount[nrpage]/100
	}*/
	start=parseInt($("start_delay").value);
	var data_fin=getDeliveryDateObj(start,timedays)

	if(timedays>1){
		$("data_dead").innerHTML=timedays+" days"
	}else{
		if(((speed==-2)&&(timedays==1))||(timedays<=0)){
			$("data_dead").innerHTML="ASAP"
		}else{
			$("data_dead").innerHTML="8 hours"
		}
	}
	$("time").innerHTML=$("data_dead").innerHTML
	$("data_dis").innerHTML=month[data_fin.getMonth()]+" "+data_fin.getDate()
	$("start").innerHTML=$("data_dis").innerHTML
	
	$("data_price").innerHTML=currency_sign+" "+parseInt(fprice)
	$("price").innerHTML=currency_sign+parseInt(fprice)
	$("tprice").value=parseInt(fprice)
	$("tdays").value=parseInt(timedays)
	$("tdead").value=(data_fin.getMonth()+1)+"/"+data_fin.getDate()
}
function init(){
	if(document.getElementsByTagName){
		var anchors=document.getElementsByTagName("a")
		for(var i=0;i<anchors.length;i++){
			var x=anchors[i].getAttribute("rel")
			if((x=="external")||(x=="external nofollow"))
				anchors[i].target="_blank"
		}
	}
	if($("btn_send")){
		$("btn_send").onmouseout=function(){
			$("btn_send").style.backgroundPosition="0 0"
		}
		$("btn_send").onmouseover=function(){
			$("btn_send").style.backgroundPosition="0 -36px"
		}
		var rn=new Date()
		var date1=new Date(rn.getFullYear(),0,1,0,0,0,0)
		var temp=date1.toGMTString()
		var date3=new Date(temp.substring(0,temp.lastIndexOf(" ")-1))
		$("tdiff").value=(date1-date3)/(1000*60*60)
		$("frmcontact").onsubmit=function(){
			return CheckContact()
		}
	}
	if($("btn_order")){
		initFileUploads()
		if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){
			$('fakepic').style.margin="1px 0 0 0"
			$('fakefile').style.left="-186px"
			$('hinput').style.left="-16px"
		}
		$("btn_order").onmouseout=function(){
			$("btn_order").style.backgroundPosition="0 0"
		}
		$("btn_order").onmouseover=function(){
			$("btn_order").style.backgroundPosition="0 -36px"
		}
		var rn=new Date()
		var date1=new Date(rn.getFullYear(),0,1,0,0,0,0)
		var temp=date1.toGMTString()
		var date3=new Date(temp.substring(0,temp.lastIndexOf(" ")-1))
		$("tdiff").value=(date1-date3)/(1000*3600)
		$("data_price").innerHTML=currency_sign+" "+prices[0]
		$("msg").innerHTML=defnotes
		$("msg").onfocus=function(){
			if(this.value==defnotes)
				this.value=''
		}
		$("msg").onblur=function(){
			if(this.value=='')
				this.value=defnotes
		}
		calculateprice()
		$("frmorder").onsubmit=function(){
			return CheckOrder()
		}
		$("nrpage2").onkeyup=function(){
			calculateprice()
		}
		
		$alloptions=Array(
			"option_speed1","option_speed3","option_speed4","option_speed5",
			"option_megadd","option_customform","option_jquery_lightbox","option_jquery_accordion","option_jquery_slideshow","option_jquery_showhide","option_jquery_tabs","option_jquery_background","option_jquery_scroll","option_jquery_calendar",
			"option_lateststandards","option_liquid",
			"option_printstyle2","option_ie6","option_footerstick","option_dw","option_ipad","option_loadspeed","option_sifr","option_ff","option_wcagaaa","option_linkall",
			"integration_xhtml","integration_email","integration_wp","integration_jl","integration_dp","integration_oc","integration_cmsms"
		);

		for(var i=0;i<$alloptions.length;i++) {
			$($alloptions[i]).onclick=function(){
				calculateprice()
			}
		}
	}
	if($('msg'))
		cleanForm()
}
function initFileUploads(){
	if (!W3CDOM) return
	var fakeFileUpload=document.createElement('div')
	fakeFileUpload.className='fakefile'
	fakeFileUpload.setAttribute('id','fakefile')
	fakeFileUpload.appendChild(document.createElement('input'))
	var image=document.createElement('img')
	image.setAttribute('id','fakepic')
	image.src='images/button_upload.jpg'
	fakeFileUpload.appendChild(image)
	var x=document.getElementsByTagName('input')
	for(var i=0;i<x.length;i++){
		if(x[i].type!='file') continue
		if(x[i].parentNode.className != 'fileinputs')
			continue
		x[i].className='file hidden'
		x[i].setAttribute('id','hinput')
		x[i].style.opacity=0
		x[i].style.filter='alpha(opacity=0)'
		var clone=fakeFileUpload.cloneNode(true)
		x[i].parentNode.appendChild(clone)
		x[i].relatedElement = clone.getElementsByTagName('input')[0]
		x[i].onchange=x[i].onmouseout=function (){
			this.relatedElement.value=this.value
		}
	}
}

window.onload=init
