//Refreshing the screen
var bolRefresh = false;

//onLoad method for this page
function page_onLoad(requestID)
{
	//checkBrowser();
}

function findObj(n, d) { //object ID function for multiple browsers
	var p,i,x;  
	if(!d) d=document; 
	if((p=n.indexOf("?"))>0&&parent.frames.length) 
	{
		d=parent.frames[n.substring(p+1)].document; 
		n=n.substring(0,p);
	}
	if(!(x=d[n])&&d.all) 
		x=d.all[n]; 
	for (i=0;!x&&i<d.forms.length;i++) 
		x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++) 
		x=findObj(n,d.layers[i].document);
	if(!x && d.getElementById) 
		x=d.getElementById(n); 
	return x;
}

function checkBrowser()
{
	var app = navigator.appName;

	/*Disable Butterfly for Netscape browsers*/
	if(app != "Microsoft Internet Explorer")
	{
		//alert(app);
		findObj("EDVDesign").style.display = 'none';
		findObj("Season").style.display = 'none';	
	}
}

function ScanForPDFHyperlinks()
{
    var aTags = document.getElementsByTagName("A");
    for(var i = 0; i < aTags.length; i++)
    {
        try
        {
            if(aTags[i] != null && aTags[i].href != null && aTags[i].href.length > 0 &&
               (aTags[i].href.toLowerCase().indexOf(".pdf") != -1 ||
               aTags[i].href.toLowerCase().indexOf(".pdf") != -1))
            {
                aTags[i].outerHTML += "<br/><span style=\"color:black;font-size:11px;\">(Adobe Reader 5.0 or higher required! <a href=\"javascript:openWindow('http://www.adobe.com/products/acrobat/readstep2.html','800','600');\" style=\"font-size:11px;\">Click here</a> for a free download)</span>";
            }
        }
        catch(exception)
        {
            //don't do anything. anchors might have the wrong syntax for a valid html included
        }
    }
}


function ExpandLevel(levelID)
{

	if(document.getElementById(levelID).style.display == 'none')
	{
		document.getElementById(levelID).style.display = 'block';
	}
	else
	{
		document.getElementById(levelID).style.display = 'none';
	}
	document.body.style.visibility = "hidden";
	document.body.style.visibility = "visible";
}
function SwapFolderImage(objID)
{
    
    var obj = document.getElementById(objID);
    
    if(obj == null) return;
    
    if(obj.src.indexOf('assets/images/menu_folder.gif') != -1)
        obj.src = 'assets/images/menu_folder_down.gif';
    else
        obj.src = 'assets/images/menu_folder.gif';
}

function OpenPage(requestID,sCriteria)
{
	if(sCriteria != null)
	{
		location = 'gateway.aspx?page=' + requestID + '&sCriteria=' + sCriteria;
		return;
	}
	location = 'gateway.aspx?page=' + requestID; 
}
function OpenPageHTML(requestID,sCriteria)
{
	if(sCriteria != null)
	{
		location = 'http://www.armcordesign.com/gateway.aspx?page=' + requestID + '&sCriteria=' + sCriteria;
		return;
	}
	location = 'http://www.armcordesign.com/gateway.aspx?page=' + requestID; 
}

var enlargeWin=0;
function Magnify(image, subtitle, left, top, width, height)
{
  if(enlargeWin)
  {
    if(!enlargeWin.closed) enlargeWin.close();
  }
 
  var URLStr = 'enlargement.asp?image=' + image + '&height=' + height + '&width=' + width + '&subtitle=' + subtitle;
  width += 20;
  height += 129;
  enlargeWin = open(URLStr, 'enlargeWin', 'toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbar=yes,resizable=yes,copyhistory=yes,width='+width+',height='+height+',left='+left+', top='+top+',screenX='+left+',screenY='+top+'');

}

function isValidPhone(phoneStr,phoneType) {
// Checks for the following valid phone formats:
//Regular Expression for whitespace characters
	var phoneEmptyPat = /^\s*$/;
	var matchEmptyArray = phoneStr.match(phoneEmptyPat);
	
	if (matchEmptyArray != null){
		document.getElementById(phoneType).value = '';
		phoneStr = ''; //reset the phone number to an empty string
	}
	
	//Regular Expression for phone string pattern (999)999-9999 
	var phonePat = /^\(\d{3}\)\d{3}-\d{4}/;
	var matchArray = phoneStr.match(phonePat); // is the format ok?

	if (matchArray == null && (phoneStr.length != 0 || phoneStr != '')) {
		//alert('The telephone format has to be (999)999-9999!');
		var alertBox = new EDVAlert(document.getElementById('myAlert'));
		alertBox.addMessage('The telephone format has to be <br/><b>(999)999-9999<b/>!');
		alertBox.size(250,300);
		alertBox.show(400,250);
		} 
	return true;
}

function clearPhoneMask(maskfield){
	if (maskfield.value.substring(0,13) == '(___)___-____') {
		maskfield.value = "";
		}
	}
// Set phone Mask ------------------------------
function setPhoneMask(maskfield){
	maskfield.value = '(___)___-____';
	maskfield.focus();
} 

function checkPhoneOnBlur(maskfield){
	if(maskfield.value == null || maskfield.value == '' || maskfield.value == '(___)___-____'){
		//Reinsert mask as a visual aid for the user
		maskfield.value = "(___)___-____";
	}
	else {
		//validate the entry
		//var validphone = isValidphone(maskfield.value);
		//if (validphone != true) maskfield.focus();
	}
}

// Automatic phoneMask filler to type phone without needing to type "( ) - "
function autoPhoneMask(maskfield,e){
	var vCurrentValue = maskfield.value;
	var vInputKey;
	vInputKey = String.fromCharCode(e.keyCode);
	if (maskfield.value.length == 0){
		if (String.fromCharCode(e.keyCode) != "("){
		//maskfield.value= maskfield.value + "(";
		}
	} else if (maskfield.value.length == 1) {
		if (maskfield.value.substring(0,1) != "("){
		maskfield.value= "(" + maskfield.value;
		}
	} else if (maskfield.value.length == 4) {
		if (String.fromCharCode(e.keyCode) != ")"){
		maskfield.value= maskfield.value + ")";
		}
	} else if (maskfield.value.length == 8) {
		if (String.fromCharCode(e.keyCode) != "-"){
		maskfield.value= maskfield.value + "-";
		}
	} else if (maskfield.value.length == 13) {
		if (String.fromCharCode(e.keyCode) != " "){
		maskfield.value= maskfield.value + " ";
		}
	}
}
function isValidEMail(obj){

	var emailStr = obj.value;
	//Regular Expression for whitespace characters
	var emailEmptyPat = /^\s*$/;
	var matchEmptyArray = emailStr.match(emailEmptyPat);
	
	if (matchEmptyArray != null){
		obj.value = '';
		emailStr = ''; //reset the email address to an empty string
	}

	//Regular Expression for string pattern including the "@" symbol and one "." after 
	var emailPat = /[\w\S]{2,}[@]{1}[\w\S]{2,}[\.]{1}[a-zA-Z]{2,}$/;
	var matchArray = emailStr.match(emailPat);

	if (matchArray == null && (emailStr.length != 0 || emailStr != '')) {
		var alertBox = new EDVAlert(document.getElementById('myAlert'));
		alertBox.addMessage('The Email is not formatted correctly! Expected format: <br/><b>abc@company.com</b>');
		alertBox.size(250,300);
		alertBox.show(400,250);
		return false;
	}
	
	return true;
}

function requiredFields()
{
	var alertText = '';
	var inputElements = document.getElementsByTagName("input");
	for(var ii=0;ii<inputElements.length;++ii)
	{
		if(inputElements[ii].id.substring(0,3) == "req" && (inputElements[ii].value.length == 0 || inputElements[ii].value == '(___)___-____'))
		{
			alertText += '<strong>' + inputElements[ii].name + '</strong> is required!<br/>';
		}
	}
	if(alertText != '')
	{
		//alert(alertText);
		var alertBox = new EDVAlert(document.getElementById('myAlert'));
		alertBox.addMessage(alertText);
		alertBox.size(250,300);
		alertBox.show(580,330);
		return false;
	}
	return true;
}

function submitForm()
{
	if(!requiredFields()) return;
	document.getElementById('CatalogRequest').submit();
}

function submitAppForm()
{
	if(!requiredFields()) return;
}


//General functions
function EDVAlert(myDiv) {
	
	//Set HTML popup window (DIV)
	this.popup = myDiv;
	
	//Entries (HTML)
	this.messages = "";
	
	//Number of messages
	this.numMessages = 0;

	//Writing the required message box in html
	this.div = '<div style="position: absolute; background-color:#DFF293; top:0; left:0; width:300; height:300; border:1px solid #303A07;">';
	this.header = '<table cellpadding="5" cellspacing="0" border="0" width="100%" style="border:1px solid #303A07; background-color:#DFF293; filter:progid:DXImageTransform.Microsoft.Gradient(GradientType=0, StartColorStr=#ffffff, EndColorStr=#DFF293);"><tr><td><table cellpadding="5" cellspacing="0" border="0" width="100%"><tr><td colspan="2" style="background-color:#303A07;"><font style="font-family: Arial, Helvetica, sans-serif; font-size: 12px; font-weight: normal; color: #ffffff; text-indent: 4px; text-decoration: none;">Alert:</font><img src="assets/images/transparent.gif" width="10"/></td></tr><tr><td colspan="2" height="5"/></tr><tr><td>';
	this.bodyDiv = '<div style="overflow-y:scroll; border-bottom: 1px solid #303A07; padding:5px; width:100%; height:150; scrollbar-base-color: #DFF293;scrollbar-track-color: #EFEFEF;	scrollbar-face-color: #DFF293;	scrollbar-highlight-color: #EFEFEF;	scrollbar-3dlight-color: #E3E8F9;scrollbar-darkshadow-color: #EFEFEF;scrollbar-shadow-color: #7E8FE2;scrollbar-arrow-color: #ffffff;">';
	this.bodyTable = '<table cellpadding="0" cellspacing="0" border="0" width="100%"><tr><td colspan="2" height="10"/></tr>';
	this.footer = '<tr><td colspan="2" height="20"/></tr></table></div></td></tr><tr><td colspan="2" align="center"><font style="font-family: Arial, Helvetica, sans-serif;	font-size: 9px; font-weight: normal; color: #303A07;	text-decoration:none;">To close this window, click on the link below!</font></td></tr><tr><td colspan="2" height="10"/></tr>';
	this.footerClose = '<tr><td colspan="2" align="center"><a id="AlertHide" href="javascript:void();" tabindex="1" onclick="document.getElementById(\'' + this.popup.id + '\').style.visibility = \'hidden\';"><font style="font-family: Arial, Helvetica, sans-serif; font-size: 14px; font-weight: normal; color: #303A07;	text-decoration: underline;">Close</font></a></td></tr></table></td></tr></table></div>';

	// ---------------------------------------------------------------------------------------------------
	// addMessage(): Adds an error entry to the SOARPopup Error Messagebox
	//
	// pErrorText: Error Text (can be HTML)
	// ---------------------------------------------------------------------------------------------------
	this.addMessage = function(pErrorText) {
		this.messages += '<tr><td colspan="2"><font style="font-family: Arial, Helvetica, sans-serif; '
			+ 'font-size: 14px; font-weight: normal; color: #303A07;text-decoration: none;">'
			+ '<font style="color:#303A07">'
			+ pErrorText
			+ '</font></font></td></tr>';
		this.numMessages += 1;
	}

	// ---------------------------------------------------------------------------------------------------
	// addConditionalMessage():	Adds an error entry to the Alert Error Messagebox, IF pCondition is true
	//							and set the style attribute of the flag to visible
	//
	// pCondition: Condition which is checked
	// pErrorText: Error Text (can be HTML)
	// pFlag: Flag item is set to visible if condition is true (optional!)
	// ---------------------------------------------------------------------------------------------------
	this.addConditionalMessage = function(pCondition,pErrorText,pFlagID) {
		if(pCondition)
		{
			if(pFlagID!=null) document.getElementById(pFlagID).style.visibility = 'visible';
			this.addMessage(pErrorText);
		}
		else
		{
			if(pFlagID!=null) document.getElementById(pFlagID).style.visibility = 'hidden';
		}
	}	

	// ---------------------------------------------------------------------------------------------------
	// size(): resizes the alert box
	// ---------------------------------------------------------------------------------------------------
	//	default height and width
	var vHeight = 300;
	var vWidth = 250;
	this.size = function(w,h)
	{
		if(w != null) vWidth = w;
		if(h != null) vHeight = h;
	}
	
	// ---------------------------------------------------------------------------------------------------
	// show(): shows the message box
	// x: X-Coordinate of popup window (default centered = 300)
	// y: Y-Coordinate of popup window (default centered = 300)
	// ---------------------------------------------------------------------------------------------------
	
	this.show = function(x,y) {
		
		htmlString = this.header + this.bodyDiv + this.bodyTable + this.messages + this.footer
			+ this.footerClose;
		
		if (x == null)x = 300;
		if (y == null)y = 300;
		
		this.popup.innerHTML = htmlString; 
		this.popup.style.left = x;
		this.popup.style.top = y;
		this.popup.style.width = vWidth;
		this.popup.style.height = vHeight;
		this.popup.style.visibility = 'visible';
	}
	
}


var openShow = 0;
function openShowroom(src,h,w)
{
	if(openShow)
	{
		if(!openShow.closed) 
		{
			openShow.close();
		}
	}
	openShow = window.open(src,'openShow','menubar=0,scrollbars=1,toolbar=0,locationbar=0,resizable=1,personalbar=0,statusbar=0,width='+ w +',height=' + h + ',left=50,top=50')
	//openShow.focus();
}
var openStyle = 0;
function openStyles(src,h,w)
{
	if(openStyle)
	{
		if(!openStyle.closed) 
		{
			openStyle.close();
		}
	}
	openStyle = window.open(src,'openStyle','menubar=0,scrollbars=1,toolbar=0,locationbar=0,resizable=0,personalbar=0,statusbar=0,width='+ w +',height=' + h + ',left=50,top=50')
	//openStyle.focus();
}

var openWin = 0;
function openURL(url,w,h)
{
	openWin = window.open(url,'openWin','menubar=0,scrollbars=1,toolbar=0,locationbar=0,resizable=1,personalbar=0,statusbar=0,width='+ w +',height=' + h + ',left=0,top=0')
	//openWin.focus();
}

function openWindow()
{
	var url,w,h,args = openWindow.arguments;
	if(args.length == 0) return;
	url = args[0];
	if(args.length > 1) 
	{
		w = args[1];
		h = args[2];
	}
	else
	{
		//set width and height
		window.moveTo(0,0);
		if (document.all){
  			h = screen.availHeight-50;
  			w = screen.availWidth-50;
		}
		else if (document.layers||document.getElementById){
  			if (top.window.outerHeight<screen.availHeight||top.window.outerWidth<screen.availWidth){
    			h = window.outerHeight-20;
    			w = window.outerWidth-20;
  			}
		}
	}
	openURL(url,w,h);
}

function SetInActive()
{
	var sID = null;
	var bSelected = false;
	var colInput = document.all.tags('Input');
	for(var i = 0; i < colInput.length; i++)
	{
		if(colInput[i].id.substring(0,6) == 'ItemID' && colInput[i].checked)
		{
			sID = colInput[i].id.substring(6,colInput[i].id.length);
			bSelected = true;
		}
	}
	
	if(bSelected)
	{
		var xmlDoc = new ActiveXObject("Msxml2.DOMDocument");
		xmlDoc.async = false;
		xmlDoc.loadXML('<Request><RequestID>Inactive</RequestID><ID>'+ sID +'</ID></Request>');
		
		SaveXML(xmlDoc);
	}
	else
	{
		alert('Select an item before \nperforming this action!');
	}
}

function DeleteItem()
{
	var sID = null;
	var bSelected = false;
	var colInput = document.all.tags('Input');
	for(var i = 0; i < colInput.length; i++)
	{
		if(colInput[i].id.substring(0,6) == 'ItemID' && colInput[i].checked)
		{
			sID = colInput[i].id.substring(6,colInput[i].id.length);
			bSelected = true;
		}
	}
	
	if(bSelected)
	{
		var xmlDoc = new ActiveXObject("Msxml2.DOMDocument");
		xmlDoc.async = false;
		xmlDoc.loadXML('<Request><RequestID>Delete</RequestID><ID>'+ sID +'</ID></Request>');
		
		SaveXML(xmlDoc);
	}
	else
	{
		alert('Select an item before \nperforming this action!');
	}
}

function SetPriority()
{
	var sID = null;
	var bSelected = false;
	var colInput = document.all.tags('Input');
	for(var i = 0; i < colInput.length; i++)
	{
		if(colInput[i].id.substring(0,6) == 'ItemID' && colInput[i].checked)
		{
			sID = colInput[i].id.substring(6,colInput[i].id.length);
			bSelected = true;
		}
	}
	
	if(bSelected)
	{
		var xmlDoc = new ActiveXObject("Msxml2.DOMDocument");
		xmlDoc.async = false;
		xmlDoc.loadXML('<Request><RequestID>Priority</RequestID><Priority>'+ document.getElementById('Priority').value +'</Priority><ID>'+ sID +'</ID></Request>');
	
		SaveXML(xmlDoc);
	}
	else
	{
		alert('Select an item before \nperforming this action!');
	}

}

function SaveXML(CommandContext){
	//Send client CommandContext
	//alert(CommandContext.xml); return;
	
	var xmlHTTP = new ActiveXObject("Microsoft.XMLHTTP");
	xmlHTTP.open("POST","adminsave.asp", false);
	xmlHTTP.send(CommandContext);
	

	//Transform on the client
		var vResponseContext = xmlHTTP.ResponseText;
		var newDocument = document.open("text/html","replace");
		newDocument.write(vResponseContext);
		newDocument.close();
}

//The following function is for elements called "Hidden"
function HiddenDisplay(id)
{
	if(document.getElementById(id).style.display == 'block')
	{
		document.getElementById(id).style.display = 'none';
		document.getElementById('Handle'+id).src = 'assets/images/hidden_down.gif';
	}
	else
	{
		//Close all other link windows (hidden displays) first
		
		var divElements = document.getElementsByTagName("div");
		for(var i = 0; i < divElements.length; i++)
		{
			if(divElements[i].id.substring(0,10) == "LinkWindow")
			{
				divElements[i].style.display = 'none';
				document.getElementById('Handle'+divElements[i].id).src = 'assets/images/hidden_down.gif';
			}
		}
		
		document.getElementById(id).style.display = 'block';
		document.getElementById('Handle'+id).src = 'assets/images/hidden_up.gif';
	}
}

function XMLobj()
{
		//Multi Browser Support
		var xmlobj =false;

		// JScript gives us Conditional compilation, we can cope with old IE versions.
		// and security blocked creation of the objects.
		try 
		{
			//Newer IE
			xmlobj = new ActiveXObject("Msxml2.DOMDocument");
		} 
		catch (e) 
		{
			try 
			{
				//Older IE
				xmlobj = new ActiveXObject("Microsoft.DOMDocument");
			} 
			catch (ee) 
			{
				xmlobj = false;
			}
		}
		//Other Browsers
	
		if (!xmlobj) 
		{
			var moz = (typeof document.implementation != 'undefined') && (typeof document.implementation.createDocument != 'undefined');
			if(moz)
			{
				xmlobj = document.implementation.createDocument("", "doc", null);
			}
			else
				xmlobj = null;
		}

		
		return xmlobj;
}

function XMLHttpObj()
{
	//Multi Browser Support
		var xmlhttp=false;

		// JScript gives us Conditional compilation, we can cope with old IE versions.
		// and security blocked creation of the objects.
		try 
		{
			//Newer IE
			xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) 
		{
			try 
			{
				//Older IE
				xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
			} 
			catch (ee) 
			{
				xmlhttp = false;
			}
		}
		//Other Browsers
		if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
			xmlhttp = new XMLHttpRequest();
		}
		
		return xmlhttp;
}

function PrintPage()
{
	window.print();
}


function DateToday(){
	var iDate = new Date();
	var iMonth;
	var iDay;
	var iYear;
	iMonth = iDate.getMonth() + 1; 
	if (iMonth <= 9){iMonth = '0' + iMonth};
	iDay = iDate.getDate() + 1;
	if (iDay <= 9){iDay = '0' + iDay};
	iYear = iDate.getFullYear();
	iToday = iMonth + '/' + iDay + '/' + iYear;
	return iToday;
}
