// JavaScript Document

function initialize() {
    externalLinks();
	popupWindows();	
}
window.onload = initialize;

function popupWindows() {	
    if(!document.getElementsByTagName) {
         return;
    }
    var scrW = screen.availWidth;
    var scrH = screen.availHeight;
    var anchors = document.getElementsByTagName("a");
    for (var i = 0; i < anchors.length; i++) {
			//alert('here');
         var anchor = anchors[i];
         var linkDest = anchor.getAttribute("href");
         var relIndex = anchor.getAttribute("rel");
         var relSplit = relIndex.split("|");
         var windowAttributes = "";
         if(relSplit[0] == "popup") {
              if (relSplit[1] > scrW) {
                 pW = scrW - 10;
              }
              else {
                 pW = relSplit[1];
              }
              if (relSplit[2] > scrH) {
                 pH = scrH - 40;
              }
              else {
                 pH = relSplit[2];
              }
              scrX = (scrW - pW - 10) * .5;
              scrY = (scrH - pH - 30) * .5;
              var windowAttributes = "width=" + pW + ",height=" + pH + ",left=" + scrX + ",top=" + scrY + ",screenX=" + scrX + ",screenY=" + scrY;
              windowAttributes += ",location=" + relSplit[4] + ",resizable=" + relSplit[4] + ",scrollbars=" + relSplit[4];
              anchor.setAttribute("href", "javascript:popupWin('" + linkDest + "','" + windowAttributes + "')");
         }
    }
}
function popupWin(link,attribs) {
    var popupWin = null;
    popupWin = window.open(link,'winPopup',attribs);  
}
function externalLinks() {
 if (!document.getElementsByTagName) return;
 var anchors = document.getElementsByTagName("a");
 for (var i=0; i<anchors.length; i++) {
   var anchor = anchors[i];
   if (anchor.getAttribute("href") &&
       anchor.getAttribute("rel") == "external")
     anchor.target = "_blank";
 }
}
function textCounter(field, countfield, maxlimit) {	
	if (field.value.length > maxlimit) // if too long...trim it!
	field.value = field.value.substring(0, maxlimit);
	 //otherwise, update 'characters left' counter
	else 
	countfield.value = maxlimit - field.value.length;
}
function acc_close() {
	document.getElementById('accolade_more').style.display = 'none';
}
function acc_open() {
	document.getElementById('accolade_more').style.display = 'block';
}
//email validate
function echeck(str) {
	var at="@"
	var dot="."
	var lat=str.indexOf(at)
	var lstr=str.length
	var ldot=str.indexOf(dot)
	if (str.indexOf(at)==-1){
	   alert("Invalid E-mail")
	   return false
	}

	if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
	   alert("Invalid E-mail")
	   return false
	}

	if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		alert("Invalid E-mail")
		return false
	}

	 if (str.indexOf(at,(lat+1))!=-1){
		alert("Invalid E-mail")
		return false
	 }

	 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		alert("Invalid E-mail")
		return false
	 }

	 if (str.indexOf(dot,(lat+2))==-1){
		alert("Invalid E-mail")
		return false
	 }
	
	 if (str.indexOf(" ")!=-1){
		alert("Invalid E-mail")
		return false
	 }

	 return true					
}
function validate_contact(){
	var email=document.forms['contact'].email	
	if ((email.value==null)||(email.value=="")){
		alert("Please Enter your Email")
		email.focus()
		return false
	}
	if (echeck(email.value)==false){
		email.value=""
		email.focus()
		return false
	}
	return true
 }
 //end email validate
// validate order
function validate_order() {	
	if (document.forms[0].font_type.value=="0"){
	alert("Please Select a Font Type")
	document.forms[0].font_type.focus()
	return false
	}
	if (document.forms[0].wood_type.value=="0"){
	alert("Please Select a Wood Species")
	document.forms[0].wood_type.focus()
	return false
	}
	if (document.forms[0].finish.value=="0"){
	alert("Please Select a Finish")
	document.forms[0].finish.focus()
	return false
	}
	if (document.forms[0].quantity.value=="0"){
	alert("Please Select a Quantity")
	document.forms[0].quantity.focus()
	return false
	}	
}
// validate order
function validate_order_non_engraved() {	
	if (document.forms[0].wood_type.value=="0"){
	alert("Please Select a Wood Species")
	document.forms[0].wood_type.focus()
	return false
	}
	if (document.forms[0].finish.value=="0"){
	alert("Please Select a Finish")
	document.forms[0].finish.focus()
	return false
	}
	if (document.forms[0].quantity.value=="0"){
	alert("Please Select a Quantity")
	document.forms[0].quantity.focus()
	return false
	}	
}
